Jean-Tiare Le Bigot avatar Jean-Tiare Le Bigot committed cd635ee

add an (untested) table schema persisted layer

Comments (0)

Files changed (2)

ddbmock/database/db.py

 
 from .table import Table
 from .item import ItemSize
+from .storage import Store
 from collections import defaultdict
 from ddbmock import config
 from ddbmock.utils import push_write_throughput, push_read_throughput
 
 class DynamoDB(object):
     _shared_data = {
-        'data': {}
+        'data': {},
+        'store': None,
     }
 
     def __init__(self):
         cls = type(self)
         self.__dict__ = cls._shared_data
 
+        # At first instanciation, attempt to reload the database schema
+        if self.store is None:
+            self.store = Store('~*schema*~')
+            for table in self.store:
+                self.data[table.name] = table
+
     def hard_reset(self):
         for table in self.data.values():
             table.store.truncate() # FIXME: should be moved in table
         self.data.clear()
+        self.store.truncate()
 
     def list_tables(self):
         return self.data.keys()
             raise LimitExceededException("Table limit reached. You can have more than {} tables simultaneously".format(config.MAX_TABLES))
 
         self.data[name] = Table.from_dict(data)
+        self.store[name, None] = self.data[name]
         return self.data[name]
 
     def _internal_delete_table(self, name):
         if name in self.data:
             self.data[name].store.truncate()  # FIXME: should be moved in table
             del self.data[name]
+            del self.store[name, None]
 
     def delete_table(self, name):
         if name not in self.data:

ddbmock/database/table.py

             ret[u'KeySchema'][u'RangeKeyElement'] = self.range_key.to_dict()
 
         return ret
+
+    # these 2 functions helps to persist table schema (If store supports it)
+    # please note that only the schema is persisted, not the state
+    def __getstate__(self):
+        return (
+            self.name,
+            self.rt,
+            self.wt,
+            self.hash_key,
+            self.range_key,
+            'ACTIVE',
+        )
+
+    def __setstate__(self, state):
+        self.__init__(*state)  # quick and dirty (tm), but it does the job
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.