Commit dd78f342 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)
(cherry picked from commit ec03019c)

Conflicts:

	bbuild/Jamroot
	src/console.c
parent ad8c7d6a
......@@ -91,6 +91,13 @@ if ! $(luasec)
}
use-project luasec : $(luasec)/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)
{
......@@ -129,14 +136,14 @@ project luaopenbus
<target-os>windows:<define>_CRT_SECURE_NO_WARNINGS
<toolset>msvc-12.0:<cxxflags>/FS
<debug-symbols>on
<threading>multi
: default-build
<variant>release
<link>static
;
make luaopenbus.c
:
$(root)/lua/openbus/assistant.lua
: $(root)/lua/openbus/assistant.lua
$(root)/lua/openbus/assistant2.lua
$(root)/lua/openbus/core/Access.lua
$(root)/lua/openbus/core/idl/makeaux.lua
......@@ -150,7 +157,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
......@@ -228,36 +237,45 @@ make $(root)/lua/openbus/core/legacy/parsed.lua
<location>$(here)
;
local common-requirements =
<library>/lua//lua
<library>/luuid//luuid
<library>/lce//lce
<library>/luafilesystem//lfs
<library>/luavararg//luavararg
<library>/luastruct//luastruct
<library>/luasocket//luasocket
<library>/luasec//luasec
<library>/loop//loop
<library>/loop//luatuple
<library>/loop//luacothread
<library>/oil//oil
<library>/oil//luaidl
<library>/luascs//luascs
<library>/sqlite//sqlite3
<library>/luasqlite3//lsqlite3
<dependency>/lce//lce
<dependency>/loop//loop
<dependency>/loop//luacothread
<dependency>/loop//luatuple
<dependency>/oil//oil
<dependency>/oil//luaidl
<dependency>/luascs//luascs
<dependency>/luasocket//luasocket
<dependency>/luasec//luasec
<dependency>/luastruct//luastruct
<dependency>/luavararg//luavararg
<dependency>/sqlite//sqlite3
<dependency>/luasqlite3//lsqlite3
;
lib luaopenbus
: luaopenbus.c
$(root)/src/openbuslua.c
$(root)/src/lthreadlib.c
$(root)/src/lecholib.c
/lua//lua
/luuid//luuid
/lce//lce
/luafilesystem//lfs
/luavararg//luavararg
/luastruct//luastruct
/luasocket//luasocket
/loop//loop
/loop//luatuple
/loop//luacothread
/oil//oil
/oil//luaidl
/luascs//luascs
/luasec//luasec
: <dependency>/lce//lce
<dependency>/luasec//luasec
<dependency>/loop//loop
<dependency>/loop//luacothread
<dependency>/loop//luatuple
<dependency>/oil//oil
<dependency>/oil//luaidl
<dependency>/luascs//luascs
<dependency>/luasocket//luasocket
<dependency>/luastruct//luastruct
<dependency>/luavararg//luavararg
: $(common-requirements)
<dependency>luaopenbus.c
<include>$(root)/src
<include>$(here)
......@@ -272,29 +290,9 @@ exe busconsole
: $(root)/src/launcher.c
$(root)/src/consolelibs.c
luaconsole.c
/lua//lua
/luuid//luuid
/lce//lce
/luafilesystem//lfs
/luavararg//luavararg
/luastruct//luastruct
/luasocket//luasocket
/loop//loop
/loop//luatuple
/loop//luacothread
/oil//oil
/oil//luaidl
/luascs//luascs
/luaopenbus//luaopenbus
: <dependency>/loop//loop
<dependency>/loop//luatuple
<dependency>/loop//luacothread
<dependency>/oil//oil
<dependency>/oil//luaidl
<dependency>/lce//lce
<dependency>/luascs//luascs
: $(common-requirements)
<library>/luaopenbus//luaopenbus
<dependency>/luaopenbus//luaopenbus
<dependency>luaconsole.c
<define>OPENBUS_PROGNAME=\\\""busconsole\\\""
;
explicit busconsole ;
......
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.
......
This diff is collapsed.
......@@ -33,6 +33,7 @@
#include <oil.h>
#include <luascs.h>
#include <luaopenbus.h>
#include <lsqlite3.h>
......@@ -205,6 +206,7 @@ LUALIB_API int openbuslua_init (lua_State *L, int interactive, int debugmode) {
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