summaryrefslogtreecommitdiff
path: root/script/lazy-cacher.lua
blob: d6b29ba9d39e2225700948cac6a05c2172db34b9 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
local fs          = require 'bee.filesystem'
local linkedTable = require 'linked-table'

local setmt = setmetatable
local pairs = pairs
local iopen = io.open
local mmax  = math.max

_ENV = nil

---@class lazy-cacher
---@field _opening linked-table
---@field _openingMap table<string, file*>
---@field _dir string
local mt = {}
mt.__index = mt
mt.type = 'lazy-cacher'

mt.maxOpendFiles = 50
mt.maxFileSize   = 100 * 1024 * 1024 -- 100MB
mt.openingFiles  = {}

mt.errorHandler = function (err) end

---@param fileID string
function mt:_closeFile(fileID)
    self._opening:pop(fileID)
    self._openingMap[fileID]:close()
    self._openingMap[fileID] = nil
end

---@param fileID string
---@return file*?
---@return string? errorMessage
function mt:_getFile(fileID)
    if self._openingMap[fileID] then
        self._opening:pop(fileID)
        self._opening:pushTail(fileID)
        return self._openingMap[fileID]
    end
    local fullPath = self._dir .. '/' .. fileID
    local file, err = iopen(fullPath, 'a+b')
    if not file then
        return nil, err
    end
    self._opening:pushTail(fileID)
    self._openingMap[fileID] = file
    if self._opening:getSize() > self.maxOpendFiles then
        local oldest = self._opening:getHead()
        self:_closeFile(oldest)
    end
    return file
end

---@param fileID string
---@return fun(id: integer, code: string): boolean
---@return fun(id: integer): string?
function mt:writterAndReader(fileID)
    local maxFileSize = self.maxFileSize
    local map = {}
    ---@param file file*
    local function resize(file)
        local codes = {}
        for id, data in pairs(map) do
            local offset = data // 1000000
            local len    = data %  1000000
            local suc, err = file:seek('set', offset)
            if not suc then
                self.errorHandler(err)
                return
            end
            local code = file:read(len)
            codes[id] = code
        end

        self:_closeFile(fileID)
        local fullPath = self._dir .. '/' .. fileID
        local file, err = iopen(fullPath, 'wb')
        if not file then
            self.errorHandler(err)
            return
        end

        local offset = 0
        for id, code in pairs(codes) do
            file:write(code)
            map[id] = offset * 1000000 + #code
            offset = offset + #code
        end
        file:close()
    end
    ---@param id integer
    ---@param code string
    ---@return boolean
    local function writter(id, code)
        if not code then
            map[id] = nil
            return true
        end
        if #code > 1000000 then
            return false
        end
        local file, err = self:_getFile(fileID)
        if not file then
            self.errorHandler(err)
            return false
        end
        local offset, err = file:seek('end')
        if not offset then
            self.errorHandler(err)
            return false
        end
        if offset > maxFileSize then
            resize(file)
            file, err = self:_getFile(fileID)
            if not file then
                self.errorHandler(err)
                return false
            end
            offset, err = file:seek('end')
            if not offset then
                self.errorHandler(err)
                return false
            end
            maxFileSize = mmax(maxFileSize, (offset + #code) * 2)
        end
        local suc, err = file:write(code)
        if not suc then
            self.errorHandler(err)
            return false
        end
        map[id] = offset * 1000000 + #code
        return true
    end
    ---@param id integer
    ---@return string?
    local function reader(id)
        if not map[id] then
            return nil
        end
        local file, err = self:_getFile(fileID)
        if not file then
            self.errorHandler(err)
            return nil
        end
        local offset = map[id] // 1000000
        local len    = map[id] %  1000000
        local suc, err = file:seek('set', offset)
        if not suc then
            self.errorHandler(err)
            return nil
        end
        local code = file:read(len)
        return code
    end
    return writter, reader
end

---@param dir string
---@param errorHandle? fun(string)
---@return lazy-cacher?
return function (dir, errorHandle)
    fs.create_directories(fs.path(dir))
    local self = setmt({
        _dir         = dir,
        _opening     = linkedTable(),
        _openingMap  = {},
        errorHandler = errorHandle,
    }, mt)
    return self
end