-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTSDatabase.lua
More file actions
47 lines (38 loc) · 1.22 KB
/
TSDatabase.lua
File metadata and controls
47 lines (38 loc) · 1.22 KB
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
43
44
45
46
47
local M = {}
local TSTable = require("TSTable")
local Schema = require("Schema")
local TSDatabase = {}
TSDatabase.__index = TSDatabase
local function getFilePath(dataPath, tableName)
return dataPath .. tableName .. ".bin"
end
function M.new(dataPath, tableName, readOnly)
local self = {
dataPath = dataPath,
tables = {},
readOnly = readOnly,
}
for tblName, schemaDef in pairs(Schema) do
if not tableName or tableName == tblName then
local filePath = getFilePath(self.dataPath, tblName)
self.tables[tblName] = TSTable.new(schemaDef, filePath, self.readOnly)
end
end
if tableName and not self.tables[tableName] then
error(string.format("Table '%s' not defined in schema module.", tableName))
end
return setmetatable(self, TSDatabase)
end
function TSDatabase:getTable(tableName)
local tsTable = self.tables[tableName]
if not tsTable then error("Table '" .. tableName .. "' not loaded or defined in schema.") end
return tsTable
end
function TSDatabase:scanTablesStat()
local result = {}
for tableName, tsTable in pairs(self.tables) do
result[tableName] = tsTable:getStat()
end
return result
end
return M