| 16 | |
| 17 | |
| 18 | class LMDBDataset(Dataset): |
| 19 | def __init__(self, path, process_fn=None): |
| 20 | import lmdb |
| 21 | |
| 22 | self.path = path |
| 23 | self.env = lmdb.open( |
| 24 | path, |
| 25 | max_readers=32, |
| 26 | readonly=True, |
| 27 | lock=False, |
| 28 | readahead=False, |
| 29 | meminit=False, |
| 30 | ) |
| 31 | self.process_fn = process_fn |
| 32 | if not self.env: |
| 33 | raise IOError("Cannot open lmdb dataset", path) |
| 34 | |
| 35 | with self.env.begin(write=False) as txn: |
| 36 | self.length = int(txn.get("length".encode("utf-8")).decode("utf-8")) |
| 37 | |
| 38 | def __len__(self): |
| 39 | return self.length |
| 40 | |
| 41 | def __getitem__(self, idx): |
| 42 | # print(f"Get {self.path}: {idx}") |
| 43 | with self.env.begin(write=False) as txn: |
| 44 | key = str(idx).encode("utf-8") |
| 45 | # row = pickle.loads(txn.get(key)) |
| 46 | try: |
| 47 | row = pickle.loads(txn.get(key)) |
| 48 | except TypeError: |
| 49 | raise IndexError("Index out of range") |
| 50 | if self.process_fn: |
| 51 | return self.process_fn(row) |
| 52 | else: |
| 53 | return row |
| 54 | |
| 55 | |
| 56 | class PadDataset(Dataset): |
no outgoing calls
no test coverage detected