view src/lib/Db.luan @ 58:7b6691bd65c3

chat_key
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 04 Mar 2025 07:38:43 -0700
parents c420f39eb474
children 2206c20e91d2
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local new_error = Luan.new_error or error()
local ipairs = Luan.ipairs or error()
local Table = require "luan:Table.luan"
local sort = Table.sort or error()
local concat = Table.concat or error()
local Lucene = require "luan:lucene/Lucene.luan"
local Io = require "luan:Io.luan"
local uri = Io.uri or error()
local Http = require "luan:http/Http.luan"
local Thread = require "luan:Thread.luan"
local Time = require "luan:Time.luan"
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "Db"


local dir = uri("site:/private/local/lucene")

local Db = Lucene.index( dir, {
	log_dir = uri("site:/private/local/lucene_log")
	name = "lucene"
	version = 1
} )

Db.indexed_fields.user_email = Lucene.type.lowercase

Db.indexed_fields.chat_user_ids = Lucene.type.long
Db.indexed_fields.chat_updated = Lucene.type.long
Db.indexed_fields.chat_key = Lucene.type.string

Db.indexed_fields.post_chat_id = Lucene.type.long
Db.indexed_fields.post_date = Lucene.type.long

Db.indexed_fields.chatuser_key = Lucene.type.string
Db.indexed_fields.chatuser_chat_id = Lucene.type.long

function Db.not_in_transaction()
	logger.error(new_error("not in transaction"))
end

-- copied from Chat
local function get_chat_key(user_ids)
	sort(user_ids)
	return concat(user_ids,"~")
end

Db.update{
	[1] = function()
		local docs = Db.search("type:post",1,1000000)
		for _, doc in ipairs(docs) do
			doc.post_date = doc.post_date or doc.date
			Db.save(doc)
		end
	end
	[2] = function()
		Db.delete("type:chatuser")
	end
	[3] = function()
		Db.delete("type:chatuser")
	end
	[4] = function()
		local docs = Db.search("type:chat",1,1000000)
		for _, doc in ipairs(docs) do
			doc.chat_key = get_chat_key(doc.chat_user_ids)
			Db.save(doc)
		end
	end
}

if Http.is_serving then
	Thread.schedule( Db.check, { delay=0, repeating_delay=Time.period{hours=1} } )
end

return Db