github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/sawtooth_validator/database/database.py (about) 1 # Copyright 2016 Intel Corporation 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ------------------------------------------------------------------------------ 15 from abc import ABCMeta 16 from abc import abstractmethod 17 18 19 class Database(metaclass=ABCMeta): 20 """The Database interface. This class is intended to be inherited by 21 specific database implementations. 22 """ 23 24 def __init__(self): 25 """Constructor for the Database class. 26 """ 27 pass 28 29 def __getitem__(self, key): 30 return self.get(key) 31 32 def __setitem__(self, key, value): 33 return self.put(key, value) 34 35 def __delitem__(self, key): 36 self.delete(key) 37 38 @abstractmethod 39 def __len__(self): 40 raise NotImplementedError() 41 42 def __contains__(self, key): 43 return self.contains_key(key) 44 45 @abstractmethod 46 def contains_key(self, key, index=None): 47 raise NotImplementedError() 48 49 @abstractmethod 50 def count(self, index=None): 51 """Retrieve the count of entries in the main database or the index.""" 52 raise NotImplementedError() 53 54 def get(self, key, index=None): 55 """Retrieves a value associated with a key from the database 56 57 Args: 58 key (str): The key to retrieve 59 """ 60 records = self.get_multi([key], index=index) 61 62 try: 63 return records[0][1] # return the value from the key/value tuple 64 except IndexError: 65 return None 66 67 @abstractmethod 68 def get_multi(self, keys, index=None): 69 """ 70 Retrieves the values for the given keys, if they exists. 71 72 This returns an iterable of key-value pairs for the keys retrieved. 73 Any key not found will not be in the resulting list. Optionally, an 74 index name can be provided and the values found via the index will be 75 returned. In this case the key int the returned tuple will be the 76 provided key. 77 78 Args: 79 keys (:iterable:str:): an iterable of keys 80 index (:str:): an optional index name; defaults to `None` 81 82 Returns: 83 list: a list of key-value pairs of the items found in the db 84 """ 85 raise NotImplementedError() 86 87 @abstractmethod 88 def cursor(self, index=None): 89 """Creates a cursor on the database. 90 91 Creates a cursor on the database, which traverses the keys in their 92 natural order. It may be associated with an index, where it traverses 93 the specified index's keys in their natural order. 94 95 Args: 96 index (str; optional) - an optional index; defaults to `None` 97 98 Returns: 99 (:obj:`Cursor`) - a Cursor instance 100 """ 101 raise NotImplementedError() 102 103 def set(self, key, value): 104 """Sets a value associated with a key in the database. 105 106 Alias for `put`. 107 108 Args: 109 key (str): The key to set. 110 value (str): The value to associate with the key. 111 """ 112 self.put(key, value) 113 114 def put(self, key, value): 115 """Sets a value associated with a key in the database 116 117 Args: 118 key (str): The key to set. 119 value (str): The value to associate with the key. 120 """ 121 return self.update([(key, value)], []) 122 123 def put_multi(self, items): 124 """Puts all the (key, value) pairs in the iterable `items` into the 125 database. 126 127 Args: 128 items: (:iterable:`tuple`): an iterable of key/value pairs 129 """ 130 self.update(items, []) 131 132 def delete(self, key): 133 """Removes a key:value from the database 134 135 Args: 136 key (str): The key to remove. 137 """ 138 self.update([], [key]) 139 140 def delete_multi(self, keys): 141 """Removes the given keys from the database 142 143 Args: 144 keys (str): The key to remove. 145 """ 146 self.update([], keys) 147 148 @abstractmethod 149 def update(self, puts, deletes): 150 """Applies the given puts and deletes atomically. 151 152 Args: 153 puts (:iterable:`tuple`): an iterable of key/value pairs to insert 154 deletes (:iterable:str:) an iterable of keys to delete 155 """ 156 raise NotImplementedError() 157 158 @abstractmethod 159 def close(self): 160 """Closes the connection to the database 161 """ 162 raise NotImplementedError() 163 164 @abstractmethod 165 def keys(self, index=None): 166 """Returns a list of keys in the database 167 """ 168 raise NotImplementedError() 169 170 171 class Cursor(metaclass=ABCMeta): 172 """A cursor for items in the database. 173 174 A cursor can be used to traverse items in the database in an efficient 175 manner. Depending on underlying database implementation, the items may 176 be consistent within the context of a database transaction. 177 """ 178 179 def __enter__(self): 180 """Context Manager: Enter""" 181 self.open() 182 return self 183 184 def __exit__(self, *args): 185 """Context Manager: Exit""" 186 self.close() 187 188 def open(self): 189 """ 190 Opens the cursor. 191 """ 192 pass 193 194 def close(self): 195 """ 196 Closes the cursor, terminating any state. 197 """ 198 pass 199 200 @abstractmethod 201 def iter(self): 202 """Returns a forward iterator of the items 203 204 The iterator starting from the seek key, or `first` if no seek key has 205 been set. 206 """ 207 raise NotImplementedError() 208 209 @abstractmethod 210 def iter_rev(self): 211 """Returns a reverse iterator of the items 212 213 The iterator starting from the seek key, or `last` if no seek key has 214 been set. 215 """ 216 raise NotImplementedError() 217 218 @abstractmethod 219 def first(self): 220 """Sets the position to the first key, based on natural key order 221 """ 222 raise NotImplementedError() 223 224 @abstractmethod 225 def last(self): 226 """Sets the position to the last key, based on natural key order 227 """ 228 raise NotImplementedError() 229 230 @abstractmethod 231 def seek(self, key): 232 """Sets the position to the given key 233 """ 234 raise NotImplementedError() 235 236 @abstractmethod 237 def key(self): 238 """Returns the current key at the set position, or None if it has not 239 been positioned. 240 """ 241 raise NotImplementedError() 242 243 @abstractmethod 244 def value(self): 245 """Returns the current value at the set position, or None if it has not 246 been positioned. 247 """ 248 raise NotImplementedError()