comparison 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
comparison
equal deleted inserted replaced
57:c420f39eb474 58:7b6691bd65c3
1 local Luan = require "luan:Luan.luan" 1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error 2 local error = Luan.error
3 local new_error = Luan.new_error or error() 3 local new_error = Luan.new_error or error()
4 local ipairs = Luan.ipairs or error() 4 local ipairs = Luan.ipairs or error()
5 local Table = require "luan:Table.luan"
6 local sort = Table.sort or error()
7 local concat = Table.concat or error()
5 local Lucene = require "luan:lucene/Lucene.luan" 8 local Lucene = require "luan:lucene/Lucene.luan"
6 local Io = require "luan:Io.luan" 9 local Io = require "luan:Io.luan"
7 local uri = Io.uri or error() 10 local uri = Io.uri or error()
8 local Http = require "luan:http/Http.luan" 11 local Http = require "luan:http/Http.luan"
9 local Thread = require "luan:Thread.luan" 12 local Thread = require "luan:Thread.luan"
22 25
23 Db.indexed_fields.user_email = Lucene.type.lowercase 26 Db.indexed_fields.user_email = Lucene.type.lowercase
24 27
25 Db.indexed_fields.chat_user_ids = Lucene.type.long 28 Db.indexed_fields.chat_user_ids = Lucene.type.long
26 Db.indexed_fields.chat_updated = Lucene.type.long 29 Db.indexed_fields.chat_updated = Lucene.type.long
30 Db.indexed_fields.chat_key = Lucene.type.string
27 31
28 Db.indexed_fields.post_chat_id = Lucene.type.long 32 Db.indexed_fields.post_chat_id = Lucene.type.long
29 Db.indexed_fields.post_date = Lucene.type.long 33 Db.indexed_fields.post_date = Lucene.type.long
30 34
31 Db.indexed_fields.chatuser_key = Lucene.type.string 35 Db.indexed_fields.chatuser_key = Lucene.type.string
32 Db.indexed_fields.chatuser_chat_id = Lucene.type.long 36 Db.indexed_fields.chatuser_chat_id = Lucene.type.long
33 37
34 function Db.not_in_transaction() 38 function Db.not_in_transaction()
35 logger.error(new_error("not in transaction")) 39 logger.error(new_error("not in transaction"))
40 end
41
42 -- copied from Chat
43 local function get_chat_key(user_ids)
44 sort(user_ids)
45 return concat(user_ids,"~")
36 end 46 end
37 47
38 Db.update{ 48 Db.update{
39 [1] = function() 49 [1] = function()
40 local docs = Db.search("type:post",1,1000000) 50 local docs = Db.search("type:post",1,1000000)
47 Db.delete("type:chatuser") 57 Db.delete("type:chatuser")
48 end 58 end
49 [3] = function() 59 [3] = function()
50 Db.delete("type:chatuser") 60 Db.delete("type:chatuser")
51 end 61 end
62 [4] = function()
63 local docs = Db.search("type:chat",1,1000000)
64 for _, doc in ipairs(docs) do
65 doc.chat_key = get_chat_key(doc.chat_user_ids)
66 Db.save(doc)
67 end
68 end
52 } 69 }
53 70
54 if Http.is_serving then 71 if Http.is_serving then
55 Thread.schedule( Db.check, { delay=0, repeating_delay=Time.period{hours=1} } ) 72 Thread.schedule( Db.check, { delay=0, repeating_delay=Time.period{hours=1} } )
56 end 73 end