Commit ec03019c authored by Ricardo Cosme's avatar Ricardo Cosme

[OPENBUS-2970] Migração da base de dados do barramento para SQLite3

+ Suporte ao SQLite3 através do LuaSQLite3 (módulo database.lua)
 - Suporte ao formato anterior (módulo database_legacy.lua)
 - Módulo responsável por converter um banco no formato anterior
   em uma base SQLite (módulo database_converter.lua)
parent 2ef870ff
......@@ -84,6 +84,13 @@ if ! $(luascs)
}
use-project luascs : $(luascs)/bbuild ;
local luasqlite3 = [ os.environ LUASQLITE3 ] ;
if ! $(luasqlite3)
{
luasqlite3 = "$(deps)/luasqlite3" ;
}
use-project luasqlite3 : $(luasqlite3)/bbuild ;
scs-idl = [ os.environ SCS_IDL ] ;
if ! $(scs-idl)
{
......@@ -141,7 +148,9 @@ make luaopenbus.c
$(root)/lua/openbus/idl.lua
$(root)/lua/openbus/util/argcheck.lua
$(root)/lua/openbus/util/autotable.lua
$(root)/lua/openbus/util/database_converter.lua
$(root)/lua/openbus/util/database.lua
$(root)/lua/openbus/util/database_legacy.lua
$(root)/lua/openbus/util/except.lua
$(root)/lua/openbus/util/logger.lua
$(root)/lua/openbus/util/messages.lua
......@@ -211,6 +220,8 @@ local common-requirements =
<library>/oil//oil
<library>/oil//luaidl
<library>/luascs//luascs
<library>/sqlite//sqlite3
<library>/luasqlite3//lsqlite3
<dependency>/lce//lce
<dependency>/loop//loop
<dependency>/loop//luacothread
......@@ -221,6 +232,8 @@ local common-requirements =
<dependency>/luasocket//luasocket
<dependency>/luastruct//luastruct
<dependency>/luavararg//luavararg
<dependency>/sqlite//sqlite3
<dependency>/luasqlite3//lsqlite3
;
lib luaopenbus
......
This diff is collapsed.
local db = require "openbus.util.database"
local dblegacy = require "openbus.util.database_legacy"
local module = {}
local function bool2int(val)
return (val and 1) or 0
end
function module.convert(dblegacy, db)
db.conn:exec("BEGIN;")
local certificateDB = dblegacy:gettable("Certificates")
for entity in certificateDB:ientries() do
local certificate = certificateDB:getentry(entity)
assert(db:pexec("addCertificate", certificate, entity))
end
local autosets = dblegacy:gettable("AutoSetttings")
local busid = autosets:getentry("BusId")
if busid then
assert(db:pexec("addSettings", "BusId", busid))
end
local loginsDB = dblegacy:gettable("Logins")
for id, data in loginsDB:ientries() do
assert(db:pexec("addLogin", id, data.entity, data.encodedkey,
bool2int(data.allowLegacyDelegate)))
end
local loginobsDB = dblegacy:gettable("LoginObservers")
for id, data in loginobsDB:ientries() do
assert(db:pexec("addLoginObserver", id, data.ior, bool2int(data.legacy),
data.login))
for login in pairs(data.watched) do
assert(db:pexec("addWatchedLogin", id, login))
end
end
local interfaceDB = dblegacy:gettable("Interfaces")
for _, ifaceId in interfaceDB:ientries() do
assert(db:pexec("addInterface", ifaceId))
end
local categoryDB = dblegacy:gettable("Categories")
for id, name in categoryDB:ientries() do
assert(db:pexec("addCategory", id, name))
end
local entityDB = dblegacy:gettable("Entities")
for id, entry in entityDB:ientries() do
assert(db:pexec("addEntity", id, entry.name, entry.categoryId))
if entry.authorized then
for repid in pairs(entry.authorized) do
assert(db:pexec("addEntityInterface", id, repid))
end
end
end
local offerDB = dblegacy:gettable("Offers")
for id, entry in offerDB:ientries() do
assert(db:pexec("addOffer",
id,
entry.service_ref,
entry.entity,
entry.login,
entry.creation.timestamp,
entry.creation.day,
entry.creation.month,
entry.creation.year,
entry.creation.hour,
entry.creation.minute,
entry.creation.second,
entry.component.name,
tostring(entry.component.major_version),
tostring(entry.component.minor_version),
tostring(entry.component.patch_version),
entry.component.platform_spec
))
for _, property in ipairs(entry.properties) do
assert(db:pexec("addPropertyOffer", property.name, property.value, id))
end
for _, facet in ipairs(entry.facets) do
assert(db:pexec("addFacet", facet.name, facet.interface_name, id))
end
for observer_id, entry in pairs(entry.observers) do
assert(db:pexec("addOfferObserver", observer_id, entry.login,
entry.observer, id))
end
end
local offerRegObsDB = dblegacy:gettable("OfferRegistryObservers")
for id, entry in offerRegObsDB:ientries() do
assert(db:pexec("addOfferRegistryObserver", id, entry.login,
entry.observer))
for _, property in ipairs(entry.properties) do
assert(db:pexec("addPropertyOfferRegistryObserver",
property.name, property.value, id))
end
end
db.conn:exec("COMMIT;")
end
return module
local _G = require "_G"
local assert = _G.assert
local ipairs = _G.ipairs
local loadfile = _G.loadfile
local pairs = _G.pairs
local pcall = _G.pcall
local lfs = require "lfs"
local listdir = lfs.dir
local getattribute = lfs.attributes
local Viewer = require "loop.debug.Viewer"
local oo = require "openbus.util.oo"
local class = oo.class
local Serializer = Viewer{ nolabels = true }
local function loadfrom_cont(path, ok, ...)
if ok then return ... end
local errmsg = ...
return nil, "corrupted file '"..path.."' ("..errmsg..")"
end
local function loadfrom(path)
local result, errmsg = loadfile(path, "t", {})
if result == nil then
if errmsg:find("No such file or directory") then
return
end
return nil, "unable to load file '"..path.."' ("..errmsg..")"
end
return loadfrom_cont(path, pcall(result))
end
local Table = class()
function Table:__init()
function self.iterator(next)
local file = next()
while file ~= nil do
local path = self.path..file
if getattribute(path, "mode") == "file" then
return file:match("^(.+)%.lua$"), assert(loadfrom(path))
end
file = next()
end
end
end
function Table:getentry(key)
return loadfrom(self.path..key..".lua")
end
function Table:ientries()
return self.iterator, listdir(self.path)
end
local TableNamePat = "[^.][^/\\?*]*"
local DataBase = class()
function DataBase:__init()
local path = self.path
local schema = {
"AutoSetttings",
"Categories",
"Certificates",
"Entities",
"Interfaces",
"LoginObservers",
"Logins",
"OfferRegistryObservers",
"Offers"
}
local tables = {}
for _, name in ipairs(schema) do
local tablepath = path..name.."/"
if name:match(TableNamePat)
and getattribute(tablepath, "mode") == "directory" then
tables[name] = Table{ base = self, name = name, path = tablepath }
else
error('corrupted database')
end
end
self.tables = tables
end
function DataBase:gettable(name)
return self.tables[name]
end
function DataBase:itables()
return pairs(self.tables)
end
local module = {}
function module.open(path)
if getattribute(path, "mode") ~= "directory" then return nil end
local res, db = pcall(DataBase, { path = path.."/" })
if res then return db else return nil end
end
return module
......@@ -23,6 +23,7 @@ local log = Verbose{
{"debug"}, -- informações de depuração para usuários do serviço
{"access"}, -- informações sobre as chamadas interceptadas
{"multiplex"}, -- informações sobre logins utilizados nas chamadas
{"database"},
error = {
"unexpected", -- unexpected errors, usually indicates a bug.
......
......@@ -30,6 +30,7 @@
#include "oil.h"
#include "luascs.h"
#include "luaopenbus.h"
#include "lsqlite3.h"
#define OPENBUS_EXECUTOR "openbus.call"
......@@ -499,6 +500,7 @@ static int pmain (lua_State *L) {
lua_pushcfunction(L,luaopen_vararg);lua_setfield(L,-2,"vararg");
lua_pushcfunction(L,luaopen_struct);lua_setfield(L,-2,"struct");
lua_pushcfunction(L,luaopen_socket_core);lua_setfield(L,-2,"socket.core");
lua_pushcfunction(L,luaopen_lsqlite3);lua_setfield(L,-2,"lsqlite3");
lua_pop(L, 1); /* pop 'package.preload' table */
/* preload other C libraries */
luapreload_lce(L);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment