annotate src/lib/Db.luan @ 71:2206c20e91d2

add Db.restore_from_log
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 06 Mar 2025 20:43:54 -0700
parents 7b6691bd65c3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local new_error = Luan.new_error or error()
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
4 local ipairs = Luan.ipairs or error()
58
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
5 local Table = require "luan:Table.luan"
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
6 local sort = Table.sort or error()
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
7 local concat = Table.concat or error()
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Lucene = require "luan:lucene/Lucene.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Io = require "luan:Io.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local uri = Io.uri or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Http = require "luan:http/Http.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local Thread = require "luan:Thread.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local Time = require "luan:Time.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local Logging = require "luan:logging/Logging.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local logger = Logging.logger "Db"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local dir = uri("site:/private/local/lucene")
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 local Db = Lucene.index( dir, {
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 log_dir = uri("site:/private/local/lucene_log")
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 name = "lucene"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 version = 1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 } )
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 Db.indexed_fields.user_email = Lucene.type.lowercase
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
28 Db.indexed_fields.chat_user_ids = Lucene.type.long
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
29 Db.indexed_fields.chat_updated = Lucene.type.long
58
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
30 Db.indexed_fields.chat_key = Lucene.type.string
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
31
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
32 Db.indexed_fields.post_chat_id = Lucene.type.long
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
33 Db.indexed_fields.post_date = Lucene.type.long
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
34
57
c420f39eb474 chatuser_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
35 Db.indexed_fields.chatuser_key = Lucene.type.string
55
d21ae4920aac minor fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
36 Db.indexed_fields.chatuser_chat_id = Lucene.type.long
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
37
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 function Db.not_in_transaction()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 logger.error(new_error("not in transaction"))
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41
71
2206c20e91d2 add Db.restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
42 Db.restore_from_log()
2206c20e91d2 add Db.restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 58
diff changeset
43
58
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
44 -- copied from Chat
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
45 local function get_chat_key(user_ids)
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
46 sort(user_ids)
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
47 return concat(user_ids,"~")
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
48 end
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
49
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 Db.update{
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
51 [1] = function()
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
52 local docs = Db.search("type:post",1,1000000)
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
53 for _, doc in ipairs(docs) do
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
54 doc.post_date = doc.post_date or doc.date
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
55 Db.save(doc)
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
56 end
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
57 end
55
d21ae4920aac minor fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
58 [2] = function()
d21ae4920aac minor fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
59 Db.delete("type:chatuser")
d21ae4920aac minor fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
60 end
57
c420f39eb474 chatuser_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
61 [3] = function()
c420f39eb474 chatuser_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
62 Db.delete("type:chatuser")
c420f39eb474 chatuser_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
63 end
58
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
64 [4] = function()
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
65 local docs = Db.search("type:chat",1,1000000)
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
66 for _, doc in ipairs(docs) do
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
67 doc.chat_key = get_chat_key(doc.chat_user_ids)
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
68 Db.save(doc)
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
69 end
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
70 end
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 }
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 if Http.is_serving then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 Thread.schedule( Db.check, { delay=0, repeating_delay=Time.period{hours=1} } )
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 return Db