Mercurial Hosting > luan
comparison src/luan/modules/lucene/Lucene.luan @ 1369:709f7498a363
change Lucene.index() and add Lucene.recover()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 19 Jun 2019 00:26:10 -0600 |
parents | 5225cd6ed478 |
children | 5c3702f60200 |
comparison
equal
deleted
inserted
replaced
1368:5225cd6ed478 | 1369:709f7498a363 |
---|---|
35 } | 35 } |
36 | 36 |
37 Lucene.literal = SaneQueryParser.literal | 37 Lucene.literal = SaneQueryParser.literal |
38 | 38 |
39 function Lucene.index(index_dir,default_type,default_fields) | 39 function Lucene.index(index_dir,default_type,default_fields) |
40 type(index_dir)=="table" or error "index_dir must be table" | |
41 index_dir.to_uri_string and matches(index_dir.to_uri_string(),"^file:") or error "must be file" | |
40 local index = {} | 42 local index = {} |
41 index.dir = index_dir | 43 index.dir = index_dir |
42 local java_index, closer = LuceneIndex.getLuceneIndex(index_dir,default_type,default_fields) | 44 local java_index, closer = LuceneIndex.getLuceneIndex(index_dir.java.file,default_type,default_fields) |
43 index.java = java_index | 45 index.java = java_index |
44 index.closer = closer or error() | 46 index.closer = closer or error() |
45 | 47 |
46 index.indexed_fields = {} | 48 index.indexed_fields = {} |
47 local mt = {} | 49 local mt = {} |
143 end | 145 end |
144 end ) | 146 end ) |
145 end | 147 end |
146 | 148 |
147 function index.schedule_backups_to(backup_dir,repeating_delay) | 149 function index.schedule_backups_to(backup_dir,repeating_delay) |
148 local lucene_dir = uri("file:"..index.dir) | 150 local lucene_dir = index.dir |
149 local function backup() | 151 local function backup() |
150 if backup_dir.last_modified() < lucene_dir.last_modified() then | 152 if backup_dir.last_modified() < lucene_dir.last_modified() then |
151 run_for_backup(function() | 153 run_for_backup(function() |
152 index.backup_to(backup_dir) | 154 index.backup_to(backup_dir) |
153 logger.info "backup" | 155 logger.info "backup" |
176 end | 178 end |
177 index.zip = Boot.no_security(index.zip) | 179 index.zip = Boot.no_security(index.zip) |
178 | 180 |
179 function index.restore(zip_file) | 181 function index.restore(zip_file) |
180 java_index.run_in_lock( function() | 182 java_index.run_in_lock( function() |
181 local lucene_dir = uri("file:"..index.dir) | 183 local lucene_dir = index.dir |
182 local before_restore = lucene_dir.parent().child("before_restore.zip") | 184 local before_restore = lucene_dir.parent().child("before_restore.zip") |
183 index.zip(before_restore) | 185 index.zip(before_restore) |
184 java_index.doClose() | 186 java_index.doClose() |
185 lucene_dir.delete() | 187 lucene_dir.delete() |
186 Io.uri("os:unzip "..zip_file.canonical().to_string(),{dir=lucene_dir.parent()}).read_text() | 188 Io.uri("os:unzip "..zip_file.canonical().to_string(),{dir=lucene_dir.parent()}).read_text() |
195 | 197 |
196 if Rpc.functions.lucene_backup == nil then | 198 if Rpc.functions.lucene_backup == nil then |
197 | 199 |
198 function Rpc.functions.lucene_backup(password) | 200 function Rpc.functions.lucene_backup(password) |
199 Io.password == password or error "wrong password" | 201 Io.password == password or error "wrong password" |
200 local zip_file = uri("file:"..index.dir).parent().child("backup.zip") | 202 local zip_file = index.dir.parent().child("backup.zip") |
201 index.zip(zip_file) | 203 index.zip(zip_file) |
202 return zip_file | 204 return zip_file |
203 end | 205 end |
204 | 206 |
205 function Rpc.functions.lucene_restore(password,zip_file) | 207 function Rpc.functions.lucene_restore(password,zip_file) |
206 Io.password == password or error "wrong password" | 208 Io.password == password or error "wrong password" |
207 local backup_zip = uri("file:"..index.dir).parent().child("backup.zip") | 209 local backup_zip = index.dir.parent().child("backup.zip") |
208 backup_zip.write(zip_file) | 210 backup_zip.write(zip_file) |
209 index.restore(backup_zip) | 211 index.restore(backup_zip) |
210 end | 212 end |
211 | 213 |
212 else | 214 else |
215 end | 217 end |
216 | 218 |
217 return index | 219 return index |
218 end | 220 end |
219 | 221 |
222 function Lucene.recover(index_dir,restore_dir) | |
223 if not index_dir.exists() and restore_dir.exists() then | |
224 index_dir.mkdir() | |
225 for _, child in ipairs(restore_dir.children()) do | |
226 index_dir.child(child.name()).link_to(child) | |
227 end | |
228 logger.error "recovered" | |
229 end | |
230 end | |
231 | |
220 return Lucene | 232 return Lucene |