From 77a29a4cda84ee7d7d4859d5ed183810a3e81693 Mon Sep 17 00:00:00 2001 From: drebs Date: Sun, 17 Feb 2013 08:37:48 -0300 Subject: Simple refactor and fix. --- __init__.py | 9 +++++++-- backends/couch.py | 30 ++++++++---------------------- backends/objectstore.py | 26 +++++++++++++++++--------- 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/__init__.py b/__init__.py index 6329cf30..92c9feb5 100644 --- a/__init__.py +++ b/__init__.py @@ -14,6 +14,7 @@ import random import hmac from leap.soledad.backends import sqlcipher from leap.soledad.util import GPGWrapper +from leap.soledad.backends.leap_backend import LeapDocument class Soledad(object): @@ -68,8 +69,12 @@ class Soledad(object): # instantiate u1db # TODO: verify if secret for sqlcipher should be the same as the # one for symmetric encryption. - self._db = sqlcipher.open(self.LOCAL_DB_PATH, True, self._secret, - soledad=self) + self._db = sqlcipher.open( + self.LOCAL_DB_PATH, + self._secret, + create=True, + document_factory=LeapDocument, + soledad=self) def close(self): """ diff --git a/backends/couch.py b/backends/couch.py index 7c884aee..b7a77054 100644 --- a/backends/couch.py +++ b/backends/couch.py @@ -14,7 +14,10 @@ from u1db.errors import DatabaseDoesNotExist from couchdb.client import Server, Document as CouchDocument from couchdb.http import ResourceNotFound # leap -from leap.soledad.backends.objectstore import ObjectStore +from leap.soledad.backends.objectstore import ( + ObjectStoreDatabase, + ObjectStoreSyncTarget, +) from leap.soledad.backends.leap_backend import LeapDocument try: @@ -28,7 +31,7 @@ class InvalidURLError(Exception): pass -class CouchDatabase(ObjectStore): +class CouchDatabase(ObjectStoreDatabase): """A U1DB backend that uses Couch as its persistence layer.""" @classmethod @@ -168,7 +171,7 @@ class CouchDatabase(ObjectStore): autocreate=autocreate) #------------------------------------------------------------------------- - # methods from ObjectStore + # methods from ObjectStoreDatabase #------------------------------------------------------------------------- def _init_u1db_data(self): @@ -237,25 +240,8 @@ class CouchDatabase(ObjectStore): return dict -class CouchSyncTarget(LocalSyncTarget): - - def get_sync_info(self, source_replica_uid): - """Return information about known state.""" - source_gen, source_trans_id = self._db._get_replica_gen_and_trans_id( - source_replica_uid) - my_gen, my_trans_id = self._db._get_generation_info() - return ( - self._db._replica_uid, my_gen, my_trans_id, source_gen, - source_trans_id) - - def record_sync_info(self, source_replica_uid, source_replica_generation, - source_replica_transaction_id): - """Record tip information for another replica.""" - if self._trace_hook: - self._trace_hook('record_sync_info') - self._db._set_replica_gen_and_trans_id( - source_replica_uid, source_replica_generation, - source_replica_transaction_id) +class CouchSyncTarget(ObjectStoreSyncTarget): + pass class CouchServerState(ServerState): diff --git a/backends/objectstore.py b/backends/objectstore.py index 1ac03df4..7c5d1177 100644 --- a/backends/objectstore.py +++ b/backends/objectstore.py @@ -6,11 +6,14 @@ Right now, this is only used by CouchDatabase backend, but can also be extended to implement OpenStack or Amazon S3 storage, for example. """ -from u1db.backends.inmemory import InMemoryDatabase +from u1db.backends.inmemory import ( + InMemoryDatabase, + InMemorySyncTarget, +) from u1db import errors -class ObjectStore(InMemoryDatabase): +class ObjectStoreDatabase(InMemoryDatabase): """ A backend for storing u1db data in an object store. """ @@ -20,8 +23,9 @@ class ObjectStore(InMemoryDatabase): raise NotImplementedError(cls.open_database) def __init__(self, replica_uid=None, document_factory=None): - super(ObjectStore, self).__init__(replica_uid, - document_factory=document_factory) + super(ObjectStoreDatabase, self).__init__( + replica_uid, + document_factory=document_factory) # sync data in memory with data in object store if not self._get_doc(self.U1DB_DATA_DOC_ID): self._init_u1db_data() @@ -32,7 +36,7 @@ class ObjectStore(InMemoryDatabase): #------------------------------------------------------------------------- def _set_replica_uid(self, replica_uid): - super(ObjectStore, self)._set_replica_uid(replica_uid) + super(ObjectStoreDatabase, self)._set_replica_uid(replica_uid) self._store_u1db_data() def _put_doc(self, doc): @@ -45,7 +49,7 @@ class ObjectStore(InMemoryDatabase): raise NotImplementedError(self.get_all_docs) def delete_doc(self, doc): - """Mark a document as deleted.""" + """Mark a document as deleted.""" old_doc = self._get_doc(doc.doc_id, check_for_conflicts=True) if old_doc is None: raise errors.DocumentDoesNotExist @@ -71,17 +75,17 @@ class ObjectStore(InMemoryDatabase): def delete_index(self, index_name): """Remove a named index.""" - super(ObjectStore, self).delete_index(index_name) + super(ObjectStoreDatabase, self).delete_index(index_name) self._store_u1db_data() def _replace_conflicts(self, doc, conflicts): - super(ObjectStore, self)._replace_conflicts(doc, conflicts) + super(ObjectStoreDatabase, self)._replace_conflicts(doc, conflicts) self._store_u1db_data() def _do_set_replica_gen_and_trans_id(self, other_replica_uid, other_generation, other_transaction_id): - super(ObjectStore, self)._do_set_replica_gen_and_trans_id( + super(ObjectStoreDatabase, self)._do_set_replica_gen_and_trans_id( other_replica_uid, other_generation, other_transaction_id) @@ -125,3 +129,7 @@ class ObjectStore(InMemoryDatabase): Initialize u1db configuration data on backend storage. """ NotImplementedError(self._init_u1db_data) + + +class ObjectStoreSyncTarget(InMemorySyncTarget): + pass -- cgit v1.2.3