[8956d4]: / unimol / data / lmdb_dataset.py

Download this file

50 lines (41 with data), 1.4 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
# Copyright (c) DP Technology.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import lmdb
import os
import pickle
from functools import lru_cache
import logging
logger = logging.getLogger(__name__)
class LMDBDataset:
def __init__(self, db_path):
self.db_path = db_path
assert os.path.isfile(self.db_path), "{} not found".format(self.db_path)
env = self.connect_db(self.db_path)
with env.begin() as txn:
self._keys = list(txn.cursor().iternext(values=False))
def connect_db(self, lmdb_path, save_to_self=False):
env = lmdb.open(
lmdb_path,
subdir=False,
readonly=True,
lock=False,
readahead=False,
meminit=False,
max_readers=256,
)
if not save_to_self:
return env
else:
self.env = env
def __len__(self):
return len(self._keys)
@lru_cache(maxsize=16)
def __getitem__(self, idx):
if not hasattr(self, "env"):
self.connect_db(self.db_path, save_to_self=True)
#datapoint_pickled = self.env.begin().get(f"{idx}".encode("ascii"))
#print(idx)
datapoint_pickled = self.env.begin().get(f"{idx}".encode("ascii"))
data = pickle.loads(datapoint_pickled)
return data