-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBinaryRecordFile.py
executable file
·307 lines (262 loc) · 8.98 KB
/
BinaryRecordFile.py
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
#!/usr/bin/env python3
# Copyright (c) 2008-11 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. It is provided for educational
# purposes and is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
"""
>>> import shutil
>>> import sys
>>> S = struct.Struct("<15s")
>>> fileA = os.path.join(tempfile.gettempdir(), "fileA.dat")
>>> fileB = os.path.join(tempfile.gettempdir(), "fileB.dat")
>>> for name in (fileA, fileB):
... try:
... os.remove(name)
... except EnvironmentError:
... pass
>>> brf = BinaryRecordFile(fileA, S.size)
>>> for i, text in enumerate(("Alpha", "Bravo", "Charlie", "Delta",
... "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliet",
... "Kilo", "Lima", "Mike", "November", "Oscar", "Papa",
... "Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor",
... "Whisky", "X-Ray", "Yankee", "Zulu")):
... brf[i] = S.pack(text.encode("utf8"))
>>> assert len(brf) == 26
>>> brf[len(brf) + 2] = S.pack(b"Extra at the end")
>>> assert len(brf) == 29
>>> shutil.copy(fileA, fileB)
>>> del brf[12]
>>> assert len(brf) == 29
>>> brf.compact()
>>> assert len(brf) == 26
>>> brf.close()
>>> if ((os.path.getsize(fileA) + 3 + (3 * S.size)) !=
... os.path.getsize(fileB)):
... print("FAIL#1: expected file sizes are wrong")
... sys.exit()
>>> shutil.copy(fileB, fileA)
>>> if os.path.getsize(fileA) != os.path.getsize(fileB):
... print("FAIL#2: expected file sizes differ")
... sys.exit()
>>> for name in (fileA, fileB):
... try:
... os.remove(name)
... except EnvironmentError:
... pass
>>> filename = os.path.join(tempfile.gettempdir(), "test.dat")
>>> if os.path.exists(filename): os.remove(filename)
>>> S = struct.Struct("<8s")
>>> test = BinaryRecordFile(filename, S.size)
>>> test[0] = S.pack(b"Alpha")
>>> test[1] = S.pack(b"Bravo")
>>> test[2] = S.pack(b"Charlie")
>>> test[3] = S.pack(b"Delta")
>>> test[4] = S.pack(b"Echo")
>>> test.inplace_compact() # No blank or deleted
>>> test.close()
>>> os.path.getsize(filename)
45
>>> test = BinaryRecordFile(filename, S.size)
>>> len(test)
5
>>> for index in range(len(test)):
... del test[index]
>>> test.inplace_compact() # All blank or deleted
>>> test.close()
>>> os.path.getsize(filename)
0
>>> test = BinaryRecordFile(filename, S.size)
>>> test[0] = S.pack(b"Alpha")
>>> test[1] = S.pack(b"Bravo")
>>> test[2] = S.pack(b"Charlie")
>>> test[3] = S.pack(b"Delta")
>>> test[4] = S.pack(b"Echo")
>>> del test[2]
>>> del test[4]
>>> del test[3]
>>> test.inplace_compact() # Blank or deleted at the end
>>> test.close()
>>> os.path.getsize(filename)
18
>>> test = BinaryRecordFile(filename, S.size)
>>> test[0] = S.pack(b"Alpha")
>>> test[1] = S.pack(b"Bravo")
>>> test[2] = S.pack(b"Charlie")
>>> test[3] = S.pack(b"Delta")
>>> test[4] = S.pack(b"Echo")
>>> del test[0]
>>> del test[2]
>>> del test[3]
>>> test.inplace_compact() # Blank or deleted interspersed
>>> test.close()
>>> os.path.getsize(filename)
18
>>> os.remove(filename)
"""
import os
import struct
import tempfile
_DELETED = b"\x01"
_OKAY = b"\x02"
class BinaryRecordFile:
def __init__(self, filename, record_size, auto_flush=True):
"""A random access binary file that behaves rather like a list
with each item a bytes or bytesarray object of record_size.
"""
self.__record_size = record_size + 1
mode = "w+b" if not os.path.exists(filename) else "r+b"
self.__fh = open(filename, mode)
self.auto_flush = auto_flush
@property
def record_size(self):
"The size of each item"
return self.__record_size - 1
@property
def name(self):
"The name of the file"
return self.__fh.name
def flush(self):
"""Flush writes to disk
Done automatically if auto_flush is True
"""
self.__fh.flush()
def close(self):
self.__fh.close()
def __setitem__(self, index, record):
"""Sets the item at position index to be the given record
The index position can be beyond the current end of the file.
"""
assert isinstance(record, (bytes, bytearray)), \
"binary data required"
assert len(record) == self.record_size, (
"record must be exactly {0} bytes".format(
self.record_size))
self.__fh.seek(index * self.__record_size)
self.__fh.write(_OKAY)
self.__fh.write(record)
if self.auto_flush:
self.__fh.flush()
def __getitem__(self, index):
"""Returns the item at the given index position
If there is no item at the given position, raises an
IndexError exception.
If the item at the given position has been deleted returns
None.
"""
self.__seek_to_index(index)
state = self.__fh.read(1)
if state != _OKAY:
return None
return self.__fh.read(self.record_size)
def __seek_to_index(self, index):
if self.auto_flush:
self.__fh.flush()
self.__fh.seek(0, os.SEEK_END)
end = self.__fh.tell()
offset = index * self.__record_size
if offset >= end:
raise IndexError("no record at index position {0}".format(
index))
self.__fh.seek(offset)
def __delitem__(self, index):
"""Deletes the item at the given index position.
See undelete()
"""
self.__seek_to_index(index)
state = self.__fh.read(1)
if state != _OKAY:
return
self.__fh.seek(index * self.__record_size)
self.__fh.write(_DELETED)
if self.auto_flush:
self.__fh.flush()
def undelete(self, index):
"""Undeletes the item at the given index position.
If an item is deleted it can be undeleted---providing compact()
(or inplace_compact()) has not been called.
"""
self.__seek_to_index(index)
state = self.__fh.read(1)
if state == _DELETED:
self.__fh.seek(index * self.__record_size)
self.__fh.write(_OKAY)
if self.auto_flush:
self.__fh.flush()
return True
return False
def __len__(self):
"""The number number of record positions.
This is the maximum number of records there could be at
present. The true number may be less because some records
might be deleted. After calling compact() (or
inplace_compact()), this returns the true number.
"""
if self.auto_flush:
self.__fh.flush()
self.__fh.seek(0, os.SEEK_END)
end = self.__fh.tell()
return end // self.__record_size
def compact(self, keep_backup=False):
"""Eliminates blank and deleted records"""
compactfile = self.__fh.name + ".$$$"
backupfile = self.__fh.name + ".bak"
self.__fh.flush()
self.__fh.seek(0)
fh = open(compactfile, "wb")
while True:
data = self.__fh.read(self.__record_size)
if not data:
break
if data[:1] == _OKAY:
fh.write(data)
fh.close()
self.__fh.close()
os.rename(self.__fh.name, backupfile)
os.rename(compactfile, self.__fh.name)
if not keep_backup:
os.remove(backupfile)
self.__fh = open(self.__fh.name, "r+b")
def inplace_compact(self):
"""Eliminates blank and deleted records in-place preserving the
original order
"""
index = 0
length = len(self)
while index < length:
self.__seek_to_index(index)
state = self.__fh.read(1)
if state != _OKAY:
for next in range(index + 1, length):
self.__seek_to_index(next)
state = self.__fh.read(1)
if state == _OKAY:
self[index] = self[next]
del self[next]
break
else:
break
index += 1
self.__seek_to_index(0)
state = self.__fh.read(1)
if state != _OKAY:
self.__fh.truncate(0)
else:
limit = None
for index in range(len(self) - 1, 0, -1):
self.__seek_to_index(index)
state = self.__fh.read(1)
if state != _OKAY:
limit = index
else:
break
if limit is not None:
self.__fh.truncate(limit * self.__record_size)
self.__fh.flush()
if __name__ == "__main__":
import doctest
doctest.testmod()