annotate src/lib/Db.luan @ 53:9298b04607ae

add unread
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 03 Mar 2025 19:39:30 -0700
parents f9e6a4cc4f7d
children d21ae4920aac
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()
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local Lucene = require "luan:lucene/Lucene.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local Io = require "luan:Io.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local uri = Io.uri or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Http = require "luan:http/Http.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Thread = require "luan:Thread.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Time = require "luan:Time.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Logging = require "luan:logging/Logging.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local logger = Logging.logger "Db"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local dir = uri("site:/private/local/lucene")
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local Db = Lucene.index( dir, {
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 log_dir = uri("site:/private/local/lucene_log")
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 name = "lucene"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 version = 1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 } )
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 Db.indexed_fields.user_email = Lucene.type.lowercase
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
25 Db.indexed_fields.chat_user_ids = Lucene.type.long
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
26 Db.indexed_fields.chat_updated = Lucene.type.long
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
27
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
28 Db.indexed_fields.post_chat_id = Lucene.type.long
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
29 Db.indexed_fields.post_date = Lucene.type.long
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
30
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
31 Db.indexed_fields.chatuser_id = Lucene.type.string
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
32
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 function Db.not_in_transaction()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 logger.error(new_error("not in transaction"))
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 Db.update{
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
38 [1] = function()
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
39 local docs = Db.search("type:post",1,1000000)
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
40 for _, doc in ipairs(docs) do
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
41 doc.post_date = doc.post_date or doc.date
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
42 Db.save(doc)
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
43 end
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
44 end
2
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 }
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 if Http.is_serving then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 Thread.schedule( Db.check, { delay=0, repeating_delay=Time.period{hours=1} } )
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 return Db