Mercurial Hosting > chat
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 |
| rev | line source |
|---|---|
| 2 | 1 local Luan = require "luan:Luan.luan" |
| 2 local error = Luan.error | |
| 3 local new_error = Luan.new_error or error() | |
| 53 | 4 local ipairs = Luan.ipairs or error() |
| 58 | 5 local Table = require "luan:Table.luan" |
| 6 local sort = Table.sort or error() | |
| 7 local concat = Table.concat or error() | |
| 2 | 8 local Lucene = require "luan:lucene/Lucene.luan" |
| 9 local Io = require "luan:Io.luan" | |
| 10 local uri = Io.uri or error() | |
| 11 local Http = require "luan:http/Http.luan" | |
| 12 local Thread = require "luan:Thread.luan" | |
| 13 local Time = require "luan:Time.luan" | |
| 14 local Logging = require "luan:logging/Logging.luan" | |
| 15 local logger = Logging.logger "Db" | |
| 16 | |
| 17 | |
| 18 local dir = uri("site:/private/local/lucene") | |
| 19 | |
| 20 local Db = Lucene.index( dir, { | |
| 21 log_dir = uri("site:/private/local/lucene_log") | |
| 22 name = "lucene" | |
| 23 version = 1 | |
| 24 } ) | |
| 25 | |
| 26 Db.indexed_fields.user_email = Lucene.type.lowercase | |
| 27 | |
| 4 | 28 Db.indexed_fields.chat_user_ids = Lucene.type.long |
| 29 Db.indexed_fields.chat_updated = Lucene.type.long | |
| 58 | 30 Db.indexed_fields.chat_key = Lucene.type.string |
| 4 | 31 |
| 10 | 32 Db.indexed_fields.post_chat_id = Lucene.type.long |
| 53 | 33 Db.indexed_fields.post_date = Lucene.type.long |
| 34 | |
| 57 | 35 Db.indexed_fields.chatuser_key = Lucene.type.string |
| 55 | 36 Db.indexed_fields.chatuser_chat_id = Lucene.type.long |
| 10 | 37 |
| 2 | 38 function Db.not_in_transaction() |
| 39 logger.error(new_error("not in transaction")) | |
| 40 end | |
| 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 | 44 -- copied from Chat |
| 45 local function get_chat_key(user_ids) | |
| 46 sort(user_ids) | |
| 47 return concat(user_ids,"~") | |
| 48 end | |
| 49 | |
| 2 | 50 Db.update{ |
| 53 | 51 [1] = function() |
| 52 local docs = Db.search("type:post",1,1000000) | |
| 53 for _, doc in ipairs(docs) do | |
| 54 doc.post_date = doc.post_date or doc.date | |
| 55 Db.save(doc) | |
| 56 end | |
| 57 end | |
| 55 | 58 [2] = function() |
| 59 Db.delete("type:chatuser") | |
| 60 end | |
| 57 | 61 [3] = function() |
| 62 Db.delete("type:chatuser") | |
| 63 end | |
| 58 | 64 [4] = function() |
| 65 local docs = Db.search("type:chat",1,1000000) | |
| 66 for _, doc in ipairs(docs) do | |
| 67 doc.chat_key = get_chat_key(doc.chat_user_ids) | |
| 68 Db.save(doc) | |
| 69 end | |
| 70 end | |
| 2 | 71 } |
| 72 | |
| 73 if Http.is_serving then | |
| 74 Thread.schedule( Db.check, { delay=0, repeating_delay=Time.period{hours=1} } ) | |
| 75 end | |
| 76 | |
| 77 return Db |
