view src/luan/modules/lucene/Versioning.luan @ 1153:1f4da56abd4f

change http cookies interface
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 04 Feb 2018 20:01:23 -0700
parents bae2d0c2576c
children 7f4a12fb7716
line wrap: on
line source

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 Versioning = {}

function Versioning.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 Versioning.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 Versioning