this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

rename blockstore methods

+34 -34
+27 -27
blockstore.py
··· 20 20 """ 21 21 22 22 @abstractmethod 23 - def put(self, key: bytes, value: bytes) -> None: 23 + def put_block(self, key: bytes, value: bytes) -> None: 24 24 pass 25 25 26 26 @abstractmethod 27 - def get(self, key: bytes) -> bytes: 27 + def get_block(self, key: bytes) -> bytes: 28 28 pass 29 29 30 30 @abstractmethod 31 - def delete(self, key: bytes) -> None: 31 + def delete_block(self, key: bytes) -> None: 32 32 pass 33 33 34 34 ··· 41 41 """ 42 42 self._state = dict() if state is None else state 43 43 44 - def put(self, key: bytes, value: bytes) -> None: 44 + def put_block(self, key: bytes, value: bytes) -> None: 45 45 existing_value = self._state.get(key) 46 46 if existing_value: 47 47 if existing_value == value: ··· 49 49 raise ValueError("block values are immutable") 50 50 self._state[key] = value 51 51 52 - def get(self, key: bytes) -> bytes: 52 + def get_block(self, key: bytes) -> bytes: 53 53 value = self._state.get(key) 54 54 if value is None: 55 55 raise KeyError("no block matches this key") 56 56 return value 57 57 58 - def delete(self, key: bytes) -> None: 58 + def delete_block(self, key: bytes) -> None: 59 59 if key in self._state: 60 60 del self._state[key] 61 61 ··· 75 75 ) WITHOUT ROWID; 76 76 """) 77 77 78 - def put(self, key: bytes, value: bytes) -> None: 78 + def put_block(self, key: bytes, value: bytes) -> None: 79 79 # XXX: this will fail silently if the key already exists but with a different value 80 80 # (that should never happen but it'd be nice to have guard rails) 81 81 self._cur.execute(f"INSERT OR IGNORE INTO {self.table} (block_key, block_val) VALUES (?, ?)", (key, value)) 82 82 83 - def get(self, key: bytes) -> bytes: 83 + def get_block(self, key: bytes) -> bytes: 84 84 row = self._cur.execute(f"SELECT block_val FROM {self.table} WHERE block_key=?", (key,)).fetchone() 85 85 if row is None: 86 86 raise KeyError("no block matches this key") 87 87 return row[0] 88 88 89 - def delete(self, key: bytes) -> None: 89 + def delete_block(self, key: bytes) -> None: 90 90 self._cur.execute(f"DELETE FROM {self.table} WHERE block_key=?", (key,)) 91 91 92 92 ··· 100 100 self.upper = upper 101 101 self.lower = lower 102 102 103 - def put(self, key: bytes, value: bytes) -> None: 104 - self.upper.put(key, value) 103 + def put_block(self, key: bytes, value: bytes) -> None: 104 + self.upper.put_block(key, value) 105 105 106 - def get(self, key: bytes) -> bytes: 106 + def get_block(self, key: bytes) -> bytes: 107 107 try: 108 - return self.upper.get(key) 108 + return self.upper.get_block(key) 109 109 except KeyError: 110 - return self.lower.get(key) 110 + return self.lower.get_block(key) 111 111 112 - def delete(self, key: bytes) -> None: 113 - self.upper.delete(key) 112 + def delete_block(self, key: bytes) -> None: 113 + self.upper.delete_block(key) 114 114 115 115 116 116 ··· 118 118 import os 119 119 120 120 bs = MemoryBlockStore() 121 - bs.put(b"hello", b"world") 121 + bs.put_block(b"hello", b"world") 122 122 123 - bs.put(b"hello", b"world") # putting twice is a nop 123 + bs.put_block(b"hello", b"world") # putting twice is a nop 124 124 125 125 try: 126 - bs.put(b"hello", b"foobar") 126 + bs.put_block(b"hello", b"foobar") 127 127 assert(False) # should be unreachable 128 128 except ValueError: 129 129 pass 130 130 131 - print("hello ->", bs.get(b"hello")) 131 + print("hello ->", bs.get_block(b"hello")) 132 132 133 - bs.delete(b"nothing") # nop 133 + bs.delete_block(b"nothing") # nop 134 134 135 - bs.delete(b"hello") 135 + bs.delete_block(b"hello") 136 136 137 137 try: 138 - bs.get(b"hello") 138 + bs.get_block(b"hello") 139 139 assert(False) # should be unreachable 140 140 except KeyError: 141 141 pass ··· 144 144 145 145 with sqlite3.connect(TEST_DB) as db: 146 146 bs = SqliteBlockStore(db) 147 - bs.put(b"hello", b"sqlite world") 147 + bs.put_block(b"hello", b"sqlite world") 148 148 149 149 with sqlite3.connect(TEST_DB) as db: 150 150 bs = SqliteBlockStore(db) 151 - print("hello ->", bs.get(b"hello")) 152 - bs.delete(b"hello") 151 + print("hello ->", bs.get_block(b"hello")) 152 + bs.delete_block(b"hello") 153 153 154 154 try: 155 155 with sqlite3.connect(TEST_DB) as db: 156 156 bs = SqliteBlockStore(db) 157 - print("hello ->", bs.get(b"hello")) 157 + print("hello ->", bs.get_block(b"hello")) 158 158 assert(False) # should be unreachable 159 159 except KeyError: 160 160 pass
+4 -4
carfile.py
··· 47 47 self.block_offsets[cid] = (start + CID_LENGTH, length - CID_LENGTH) 48 48 file.seek(start + length) 49 49 50 - def put(self, key: bytes, value: bytes) -> None: 50 + def put_block(self, key: bytes, value: bytes) -> None: 51 51 raise NotImplementedError("ReadOnlyCARBlockStore does not support put()") 52 52 53 - def get(self, key: bytes) -> bytes: 53 + def get_block(self, key: bytes) -> bytes: 54 54 offset, length = self.block_offsets[key] 55 55 self.file.seek(offset) 56 56 value = self.file.read(length) ··· 58 58 raise EOFError() 59 59 return value 60 60 61 - def delete(self, key: bytes) -> None: 61 + def delete_block(self, key: bytes) -> None: 62 62 raise NotImplementedError("ReadOnlyCARBlockStore does not support delete()") 63 63 64 64 65 65 if __name__ == "__main__": 66 66 f = open("/home/david/programming/python/bskyclient/retr0id.car", "rb") 67 67 bs = ReadOnlyCARBlockStore(f) 68 - commit_obj = dag_cbor.decode(bs.get(bytes(bs.car_roots[0]))) 68 + commit_obj = dag_cbor.decode(bs.get_block(bytes(bs.car_roots[0]))) 69 69 print(commit_obj) 70 70 mst_root: CID = commit_obj["data"] 71 71
+3 -3
mst.py
··· 186 186 if cid is None: 187 187 return self.put_node(MSTNode.empty_root()) 188 188 189 - res = MSTNode.deserialise(self.bs.get(bytes(cid))) 189 + res = MSTNode.deserialise(self.bs.get_block(bytes(cid))) 190 190 self.cache[cid] = res 191 191 return res 192 192 193 193 # TODO: also put in cache 194 194 def put_node(self, node: MSTNode) -> MSTNode: 195 195 self.cache[node.cid] = node 196 - self.bs.put(bytes(node.cid), node.serialised) 196 + self.bs.put_block(bytes(node.cid), node.serialised) 197 197 return node # this is convenient 198 198 199 199 # MST pretty-printing ··· 617 617 from carfile import ReadOnlyCARBlockStore 618 618 f = open("/home/david/programming/python/bskyclient/retr0id.car", "rb") 619 619 bs = OverlayBlockStore(MemoryBlockStore(), ReadOnlyCARBlockStore(f)) 620 - commit_obj = dag_cbor.decode(bs.get(bytes(bs.lower.car_roots[0]))) 620 + commit_obj = dag_cbor.decode(bs.get_block(bytes(bs.lower.car_roots[0]))) 621 621 mst_root: CID = commit_obj["data"] 622 622 ns = NodeStore(bs) 623 623 wrangler = NodeWrangler(ns)