-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjectStore.lua
More file actions
42 lines (35 loc) · 991 Bytes
/
objectStore.lua
File metadata and controls
42 lines (35 loc) · 991 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
local serializer = require("serializer")
local filesystem = require("component").filesystem
local ensuredDirExists = false
local objectstore = {
baseDir = "/usr/objectstore"
}
local function ensureDirExists()
if not ensuredDirExists then
filesystem.makeDirectory(objectstore.baseDir)
ensuredDirExists = true
end
end
function objectstore.saveObject(name, obj)
if filesystem == nil then
return false
end
ensureDirExists()
return serializer.serializeToFile(objectstore.baseDir .. "/" .. name, obj)
end
function objectstore.loadObject(name)
if filesystem == nil then
return nil
end
ensureDirExists()
local status, lines = pcall(function() return io.lines(objectstore.baseDir .. "/" .. name) end)
if not status then return false end
return serializer.deserializeLines(lines)
end
function objectstore.deleteObject(name)
if filesystem == nil then
return nil
end
filesystem.remove(objectstore.baseDir .. "/" .. name)
end
return objectstore