Mercurial Hosting > luan
diff src/luan/modules/lucene/Versioning.luan @ 775:1a68fc55a80c
simplify dir structure
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 26 Aug 2016 14:36:40 -0600 |
parents | lucene/src/luan/modules/lucene/Versioning.luan@ca169567ce07 |
children | bae2d0c2576c |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/lucene/Versioning.luan Fri Aug 26 14:36:40 2016 -0600 @@ -0,0 +1,56 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local pairs = Luan.pairs or error() +local Number = require "luan:Number.luan" +local integer = Number.integer or error() +local long = Number.long or error() +local String = require "luan:String.luan" +local matches = String.matches or error() +local sub = String.sub or error() +local string_to_number = String.to_number or error() +local Table = require "luan:Table.luan" +local copy = Table.copy or error() +local Lucene = require "luan:lucene/Lucene.luan" +require "luan:logging/init.luan" +local Logging = require "luan:logging/Logging.luan" + +local logger = Logging.logger "lucene versioning" + + +local M = {} + +function M.update(db,steps,version) + local doc = db.get_document"type:version" or { type="version", version=integer(0) } + while doc.version < version do + doc.version = integer(doc.version + 1) + logger.error("step "..doc.version) + db.update_in_transaction( function() + local step = steps[doc.version] + step and step(db) + db.save(doc) + end ) + end +end + + +-- hack to deal with latest changes +function M.a_big_step(db) + db.indexed_fields["id index"] = Lucene.type.string + db.advanced_search( Lucene.literal"id index" .. ":*", function(_,doc_fn) + local doc = doc_fn() + for field, value in pairs(copy(doc)) do + if matches(field," index$") then + local new_field = sub(field,1,-7) + db.indexed_fields[new_field] or error("field '"..new_field.."' not indexed") + doc[new_field] = value + doc[field] = nil + end + end + doc.id = long(string_to_number(doc.id)) + db.save(doc) + end ) + db.indexed_fields["type index"] = Lucene.type.string + db.delete( Lucene.literal"type index" .. ":*" ) +end + +return M