-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmmap_index.py
More file actions
29 lines (23 loc) · 811 Bytes
/
mmap_index.py
File metadata and controls
29 lines (23 loc) · 811 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
import pickle
import array
import mmap
import json
class Qry:
def __init__(self, filepref):
with open(filepref+".meta", "rt") as f:
self.meta = json.load(f)
self.data = open(filepref+".data", "rb")
self.data_mmap = mmap.mmap(
self.data.fileno(), 0, access=mmap.ACCESS_READ)
self.index = array.array("L")
with open(filepref+".index", "rb") as f:
self.index.fromfile(f, self.meta["len"])
self.lengths = array.array("I")
with open(filepref+".lengths", "rb") as f:
self.lengths.fromfile(f, self.meta["len"])
def get(self, i):
idx = self.index[i]
length = self.lengths[i]
self.data_mmap.seek(idx)
data = self.data_mmap.read(length)
return pickle.loads(data)