-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTSTable.lua
More file actions
324 lines (288 loc) · 12.2 KB
/
TSTable.lua
File metadata and controls
324 lines (288 loc) · 12.2 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
local M = {}
local TSPacker = require("TSPacker")
local RingBuffer = require("RingBuffer")
local TSTable = {}
TSTable.__index = TSTable
local function getFileSize(filePath)
local file, err = io.open(filePath, "rb")
if err then error("Read File Error : " .. err) end
if not file then error(filePath .. " is nil") end
local size = file:seek("end")
file:close()
return size or 0
end
local function getStartTime(filePath, schema)
local file = io.open(filePath, "rb")
if not file then error("Failed to open data file for getting startTime.") end
file:seek("set", 0)
local firstRecordBinary = file:read(schema.recordSize)
file:close()
local record = TSPacker.unpackRecord(schema, firstRecordBinary)
return record[1]
end
local function alignToInterval(ts, interval)
return math.floor(ts / interval) * interval
end
function M.new(schema, filePath, readOnly)
TSPacker.calculateSchema(schema)
local self = {
schema = schema,
filePath = filePath,
fileSize = 0,
startTime = 0,
endTime = 0,
interval = schema.columns[1].interval,
readOnly = readOnly,
}
::retry::
local recordSize = self.schema.recordSize
self.fileSize = getFileSize(self.filePath)
if self.fileSize >= recordSize then
self.startTime = getStartTime(self.filePath, self.schema)
end
local numFullRecords = math.floor(self.fileSize / recordSize)
if not readOnly then
if self.fileSize == 0 then
-- nothing
elseif self.fileSize > 0 and self.fileSize < recordSize then
error("Invalid Data File : " .. self.filePath)
else
if self.fileSize % recordSize > 0 then
local file = io.open(self.filePath, "r+b")
if not file then error(string.format("Failed to open data file for fixing : %s", self.filePath)) end
file:seek("set", numFullRecords * recordSize)
local nextRecordTime = self.startTime + numFullRecords * self.interval
file:write(TSPacker.createZeroRecordBin(self.schema, nextRecordTime))
file:flush()
file:close()
goto retry
end
end
end
if numFullRecords > 0 then
self.endTime = self.startTime + (numFullRecords - 1) * self.interval
end
return setmetatable(self, TSTable)
end
function TSTable:getStat()
return {
dataFile = self.filePath,
startTime = self.startTime,
endTime = self.endTime,
interval = self.interval,
fileSize = self.fileSize,
recordSize = self.schema.recordSize,
estimatedRows = math.floor(self.fileSize / self.schema.recordSize),
}
end
function TSTable:queryRange(queryStart, queryEnd, filterZero)
if self.fileSize == 0 then return {} end
local recordSize = self.schema.recordSize
local maxRecordsInBatch = math.floor(1048576 / recordSize)
queryStart = alignToInterval(queryStart, self.interval)
queryEnd = alignToInterval(queryEnd, self.interval)
local actualStart = math.max(queryStart, self.startTime)
local actualEnd = math.min(queryEnd, self.endTime)
if actualStart > actualEnd then return {} end
local startIndex = math.floor((actualStart - self.startTime) / self.interval)
local endIndex = math.floor((actualEnd - self.startTime) / self.interval)
local numRecordsRemaining = endIndex - startIndex + 1
local zeroRecordDataBin = TSPacker.getZeroRecordBinDataPart(self.schema)
local file = io.open(self.filePath, "rb")
if not file then return {} end
file:seek("set", startIndex * recordSize)
local records = {}
local count = 0
while numRecordsRemaining > 0 do
local recordsToRead = math.min(numRecordsRemaining, maxRecordsInBatch)
local batchSize = recordsToRead * recordSize
local bulkBinary = file:read(batchSize)
if not bulkBinary or #bulkBinary == 0 then
break
end
local currentOffset = 1
local actualRecordsInBatch = math.floor(#bulkBinary / recordSize)
for i = 1, actualRecordsInBatch do
local binaryRecord = bulkBinary:sub(currentOffset, currentOffset + recordSize - 1)
if not filterZero or not TSPacker.isZeroRecord(binaryRecord, zeroRecordDataBin) then
local record = TSPacker.unpackRecord(self.schema, binaryRecord)
count = count + 1
records[count] = record
end
currentOffset = currentOffset + recordSize
end
numRecordsRemaining = numRecordsRemaining - actualRecordsInBatch
end
file:close()
return records
end
function TSTable:queryAggTumbling(queryStart, queryEnd, aggInterval, aggs)
if self.fileSize == 0 then return {} end
local recordSize = self.schema.recordSize
local maxRecordsInBatch = math.floor(1048576 / recordSize)
queryStart = alignToInterval(queryStart, self.interval)
queryEnd = alignToInterval(queryEnd, self.interval)
local actualStart = math.max(queryStart, self.startTime)
local actualEnd = math.min(queryEnd, self.endTime)
if actualStart > actualEnd then return {} end
local startIndex = math.floor((actualStart - self.startTime) / self.interval)
local endIndex = math.floor((actualEnd - self.startTime) / self.interval)
local numRecordsRemaining = endIndex - startIndex + 1
local zeroRecordDataBin = TSPacker.getZeroRecordBinDataPart(self.schema)
local file = io.open(self.filePath, "rb")
if not file then return {} end
file:seek("set", startIndex * recordSize)
local records = {}
local count = 0
local aggRecord
local lastAggTime
local currentAggTime
while numRecordsRemaining > 0 do
local recordsToRead = math.min(numRecordsRemaining, maxRecordsInBatch)
local batchSize = recordsToRead * recordSize
local bulkBinary = file:read(batchSize)
if not bulkBinary or #bulkBinary == 0 then
break
end
local currentOffset = 1
local actualRecordsInBatch = math.floor(#bulkBinary / recordSize)
for i = 1, actualRecordsInBatch do
local binaryRecord = bulkBinary:sub(currentOffset, currentOffset + recordSize - 1)
if not TSPacker.isZeroRecord(binaryRecord, zeroRecordDataBin) then
local record = TSPacker.unpackRecord(self.schema, binaryRecord)
currentAggTime = alignToInterval(record[1], aggInterval)
if currentAggTime ~= lastAggTime then
count = count + 1
aggRecord = { currentAggTime }
records[count] = aggRecord
lastAggTime = currentAggTime
end
for j, aggItem in ipairs(aggs) do
aggRecord[j + 1] = aggItem.aggFunction(aggRecord[j + 1], record[aggItem.columnId])
end
end
currentOffset = currentOffset + recordSize
end
numRecordsRemaining = numRecordsRemaining - actualRecordsInBatch
end
file:close()
return records
end
function TSTable:queryAggSliding(queryStart, queryEnd, slidingSize, aggs)
if self.fileSize == 0 then return {} end
local recordSize = self.schema.recordSize
local maxRecordsInBatch = math.floor(1048576 / recordSize)
queryStart = alignToInterval(queryStart, self.interval)
queryEnd = alignToInterval(queryEnd, self.interval)
local actualStart = math.max(queryStart, self.startTime)
local actualEnd = math.min(queryEnd, self.endTime)
if actualStart > actualEnd then return {} end
local startIndex = math.floor((actualStart - self.startTime) / self.interval)
local endIndex = math.floor((actualEnd - self.startTime) / self.interval)
local numRecordsRemaining = endIndex - startIndex + 1
local zeroRecordDataBin = TSPacker.getZeroRecordBinDataPart(self.schema)
local file = io.open(self.filePath, "rb")
if not file then return {} end
file:seek("set", startIndex * recordSize)
local records = {}
local count = 0
local aggRecord
local columnArray = {}
local ringbuffer = RingBuffer.new(slidingSize)
while numRecordsRemaining > 0 do
local recordsToRead = math.min(numRecordsRemaining, maxRecordsInBatch)
local batchSize = recordsToRead * recordSize
local bulkBinary = file:read(batchSize)
if not bulkBinary or #bulkBinary == 0 then
break
end
local currentOffset = 1
local actualRecordsInBatch = math.floor(#bulkBinary / recordSize)
for i = 1, actualRecordsInBatch do
local binaryRecord = bulkBinary:sub(currentOffset, currentOffset + recordSize - 1)
if not filterZero or not TSPacker.isZeroRecord(binaryRecord, zeroRecordDataBin) then
local record = TSPacker.unpackRecord(self.schema, binaryRecord)
ringbuffer:add(record)
if ringbuffer:isFull() then
count = count + 1
aggRecord = { record[1] }
records[count] = aggRecord
for j, aggItem in ipairs(aggs) do
for g = 1, ringbuffer:size() do
columnArray[g] = ringbuffer:get(g)[aggItem.columnId]
end
aggRecord[j + 1] = aggItem.aggFunction(columnArray)
end
end
end
currentOffset = currentOffset + recordSize
end
numRecordsRemaining = numRecordsRemaining - actualRecordsInBatch
end
file:close()
return records
end
function TSTable:writeRecords(recordsArray)
if self.readOnly or #recordsArray == 0 then return 0 end
local lastRecordTime = self.endTime
local firstWriteRecordTime = nil
local packedBatch = {}
local packedBatchSize = 0
for _, record in ipairs(recordsArray) do
local recordTime = alignToInterval(record[1], self.interval)
if recordTime < lastRecordTime then
-- 历史数据
goto continue
elseif recordTime == lastRecordTime then
-- 更新的情况 保留最后一条
if not firstWriteRecordTime then
firstWriteRecordTime = recordTime
packedBatchSize = packedBatchSize + 1
end
packedBatch[packedBatchSize] = TSPacker.packRecord(self.schema, record)
else
-- 补gap
if lastRecordTime > 0 and recordTime > lastRecordTime + self.interval then
local gapCount = math.floor((recordTime - lastRecordTime) / self.interval) - 1
for i = 1, gapCount do
packedBatchSize = packedBatchSize + 1
lastRecordTime = lastRecordTime + self.interval
packedBatch[packedBatchSize] = TSPacker.createZeroRecordBin(self.schema, lastRecordTime)
if(not firstWriteRecordTime) then
firstWriteRecordTime = lastRecordTime
end
end
end
-- 正常
packedBatchSize = packedBatchSize + 1
packedBatch[packedBatchSize] = TSPacker.packRecord(self.schema, record)
lastRecordTime = recordTime
if not firstWriteRecordTime then
firstWriteRecordTime = recordTime
end
end
::continue::
end
if packedBatchSize > 0 then
local file = io.open(self.filePath, "r+b")
if not file then error(string.format("Failed to open data file for writing: %s", self.filePath)) end
local fileSizeIncr = 0
if firstWriteRecordTime == self.endTime then
file:seek("set", self.fileSize - self.schema.recordSize)
fileSizeIncr = (packedBatchSize - 1) * self.schema.recordSize
elseif firstWriteRecordTime > self.endTime then
file:seek("set", self.fileSize)
fileSizeIncr = packedBatchSize * self.schema.recordSize
end
file:write(table.concat(packedBatch))
file:flush()
file:close()
if self.startTime == 0 then
self.startTime = firstWriteRecordTime
end
self.endTime = lastRecordTime
self.fileSize = self.fileSize + fileSizeIncr
end
return packedBatchSize
end
return M