Mercurial Hosting > luan
comparison lucene/src/luan/modules/lucene/Versioning.luan @ 547:0be287ab0309
add lucene/Versioning and simplify Lucene fn names
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 15 Jun 2015 01:22:58 -0600 |
parents | |
children | f4dfe9c64c25 |
comparison
equal
deleted
inserted
replaced
546:eaef1005ab87 | 547:0be287ab0309 |
---|---|
1 local Luan = require "luan:Luan" | |
2 local error = Luan.error | |
3 local pairs = Luan.pairs or error() | |
4 local Number = require "luan:Number" | |
5 local integer = Number.integer or error() | |
6 local long = Number.long or error() | |
7 local String = require "luan:String" | |
8 local matches = String.matches or error() | |
9 local sub = String.sub or error() | |
10 local string_to_number = String.string_to_number or error() | |
11 local Table = require "luan:Table" | |
12 local clone = Table.clone or error() | |
13 local Lucene = require "luan:lucene/Lucene" | |
14 require "luan:logging/init" | |
15 local Logging = require "luan:logging/Logging" | |
16 | |
17 local logger = Logging.logger "lucene versioning" | |
18 | |
19 | |
20 local M = {} | |
21 | |
22 function M.update(db,steps,version) | |
23 local doc = db.get_document"type:version" or { type="version", version=integer(0) } | |
24 while doc.version < version do | |
25 doc.version = integer(doc.version + 1) | |
26 logger.error("step "..doc.version) | |
27 db.update_in_transaction( function() | |
28 local step = steps[doc.version] | |
29 step and step(db) | |
30 db.save(doc) | |
31 end ) | |
32 end | |
33 end | |
34 | |
35 | |
36 -- hack to deal with latest changes | |
37 function M.a_big_step(db) | |
38 db.indexed_fields["id index"] = Lucene.type.string | |
39 db.advanced_search( Lucene.literal"id index" .. ":*", function(_,doc_fn) | |
40 local doc = doc_fn() | |
41 for field, value in pairs(clone(doc)) do | |
42 if matches(field,".* index") then | |
43 local new_field = sub(field,1,-7) | |
44 db.indexed_fields[new_field] or error("field '"..new_field.."' not indexed") | |
45 doc[new_field] = value | |
46 doc[field] = nil | |
47 end | |
48 end | |
49 doc.id = long(string_to_number(doc.id)) | |
50 db.save(doc) | |
51 end ) | |
52 db.indexed_fields["type index"] = Lucene.type.string | |
53 db.delete( Lucene.literal"type index" .. ":*" ) | |
54 end | |
55 | |
56 return M |