solvit json substitution for db
This commit is contained in:
+76
-10
@@ -1,17 +1,41 @@
|
|||||||
|
-- local db = require("solvitdb")
|
||||||
local db = {}
|
local db = {}
|
||||||
local json = require("json")
|
|
||||||
local fs = require("filesystem")
|
local fs = require("filesystem")
|
||||||
function db.init()
|
local json = require("json")
|
||||||
|
local dbpath = "/ag2/testdb.json"
|
||||||
|
function db.create()
|
||||||
|
local handle = fs.open(dbpath,"w")
|
||||||
|
handle:write("{}")
|
||||||
|
handle:close()
|
||||||
|
end
|
||||||
|
function db.readJSON()
|
||||||
|
local handle = fs.open(dbpath,"r")
|
||||||
|
local content = ""
|
||||||
|
while true do
|
||||||
|
local s = handle:read()
|
||||||
|
if not s then break end
|
||||||
|
content=content..s
|
||||||
|
end
|
||||||
|
handle:close()
|
||||||
|
return content
|
||||||
end
|
end
|
||||||
function db.get(pack)
|
function db.get(pack)
|
||||||
|
local dbc = json.decode(db.readJSON())
|
||||||
|
return dbc[pack]
|
||||||
end
|
end
|
||||||
function db.set(pack,info)
|
function db.set(pack,info)
|
||||||
|
local dbc = json.decode(db.readJSON())
|
||||||
|
dbc[pack]=info
|
||||||
|
local handle = fs.open(dbpath,"w")
|
||||||
|
handle:write(json.encode(dbc))
|
||||||
|
handle:close()
|
||||||
end
|
end
|
||||||
function db.remove(pack)
|
function db.remove(pack)
|
||||||
|
local dbc = json.decode(db.readJSON())
|
||||||
|
dbc[pack]=nil
|
||||||
|
local handle = fs.open(dbpath,"w")
|
||||||
|
handle:write(json.encode(dbc))
|
||||||
|
handle:close()
|
||||||
end
|
end
|
||||||
|
|
||||||
local avs = {}
|
local avs = {}
|
||||||
@@ -47,6 +71,36 @@ function avs.parse(pack)
|
|||||||
return {name,verstr}
|
return {name,verstr}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function avs.serializeSingle(ver)
|
||||||
|
local ver2 = table.copy(ver)
|
||||||
|
for i=1,3 do
|
||||||
|
if ver2[i]==-1 then
|
||||||
|
ver2[i]="*"
|
||||||
|
else
|
||||||
|
ver2[i]=tostring(ver2[i])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return ver2[1].."."..ver2[2].."."..ver2[3]
|
||||||
|
end
|
||||||
|
|
||||||
|
function avs.serializeVersion(ver)
|
||||||
|
out=""
|
||||||
|
for i=1,#ver do
|
||||||
|
out=out..avs.serializeSingle(ver[i])
|
||||||
|
if i~=#ver then
|
||||||
|
out=out.."-"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return out
|
||||||
|
end
|
||||||
|
|
||||||
|
function avs.serializePack(pack)
|
||||||
|
if #pack==1 then
|
||||||
|
return pack[1]
|
||||||
|
end
|
||||||
|
return pack[1].."="..avs.serializeVersion(pack[2])
|
||||||
|
end
|
||||||
|
|
||||||
function avs.singleGreater(ver1,ver2)
|
function avs.singleGreater(ver1,ver2)
|
||||||
for i=1,3 do
|
for i=1,3 do
|
||||||
if ver1[i]~=ver2[i] then
|
if ver1[i]~=ver2[i] then
|
||||||
@@ -90,7 +144,9 @@ function avs.compatibleRange(vers)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function startTransaction()
|
local function startTransaction()
|
||||||
-- TODO: load from database
|
if not fs.exists(dbpath) then
|
||||||
|
db.create()
|
||||||
|
end
|
||||||
|
|
||||||
local packInfo = {}
|
local packInfo = {}
|
||||||
local installPacks = {}
|
local installPacks = {}
|
||||||
@@ -99,14 +155,22 @@ local function startTransaction()
|
|||||||
table.insert(installPacks,avs.parse(name))
|
table.insert(installPacks,avs.parse(name))
|
||||||
end
|
end
|
||||||
function transaction.remove(name)
|
function transaction.remove(name)
|
||||||
-- TODO: add reverse dependencies to database
|
|
||||||
-- TODO: add reverse conflicts to database
|
|
||||||
-- TODO: implement removing packages
|
-- TODO: implement removing packages
|
||||||
end
|
end
|
||||||
function transaction.addInfo(name,info)
|
function transaction.addInfo(name,info)
|
||||||
|
packInfo[name]=info
|
||||||
end
|
end
|
||||||
function transaction.finalize()
|
function transaction.finalize()
|
||||||
|
local ins = table.copy(installPacks)
|
||||||
|
|
||||||
|
local foundDeps=false
|
||||||
|
local i=0
|
||||||
|
while i<=#ins do
|
||||||
|
-- TODO: continue here
|
||||||
|
end
|
||||||
|
|
||||||
|
return {install=ins}
|
||||||
|
|
||||||
-- return "true, {["install"] = {"dep1", "package1", "package2"}, ["remove"] = {"package3"}}" on success
|
-- return "true, {["install"] = {"dep1", "package1", "package2"}, ["remove"] = {"package3"}}" on success
|
||||||
-- return "false, {"dep1"}" when not enough data
|
-- return "false, {"dep1"}" when not enough data
|
||||||
-- return "false, "[verbose string]"" when conflict found
|
-- return "false, "[verbose string]"" when conflict found
|
||||||
@@ -123,9 +187,11 @@ local function startTransaction()
|
|||||||
end
|
end
|
||||||
function transaction.resolveConflict()
|
function transaction.resolveConflict()
|
||||||
-- :whymustisuffer:
|
-- :whymustisuffer:
|
||||||
|
-- TODO: be able to resolve conflicts
|
||||||
end
|
end
|
||||||
function transaction.store()
|
function transaction.store()
|
||||||
-- TODO: store to database
|
-- TODO: store to database
|
||||||
|
-- TODO: add reverse dependencies
|
||||||
end
|
end
|
||||||
return transaction
|
return transaction
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user