~jameinel/u1db/get-all-docs

« back to all changes in this revision

Viewing changes to u1db/backends/sqlite_backend.py

  • Committer: Samuele Pedroni (Canonical Services Ltd.)
  • Date: 2011-10-28 09:35:51 UTC
  • mfrom: (92.1.2 naming-replica)
  • Revision ID: samuele.pedroni@canonical.com-20111028093551-p5s8oew9mgk5nm9m
merge machine_id -> replica_uid and related

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
    def __init__(self, sqlite_file):
30
30
        """Create a new sqlite file."""
31
31
        self._db_handle = dbapi2.connect(sqlite_file)
32
 
        self._real_machine_id = None
 
32
        self._real_replica_uid = None
33
33
        self._ensure_schema()
34
34
 
35
35
    def get_sync_target(self):
107
107
            c.execute("CREATE INDEX document_fields_field_value_doc_idx"
108
108
                      " ON document_fields(field_name, value, doc_id)")
109
109
            c.execute("CREATE TABLE sync_log ("
110
 
                      " machine_id TEXT PRIMARY KEY,"
 
110
                      " replica_uid TEXT PRIMARY KEY,"
111
111
                      " known_generation INTEGER)")
112
112
            c.execute("CREATE TABLE conflicts ("
113
113
                      " doc_id TEXT,"
136
136
    def _extra_schema_init(self, c):
137
137
        """Add any extra fields, etc to the basic table definitions."""
138
138
 
139
 
    def _set_machine_id(self, machine_id):
140
 
        """Force the machine_id to be set."""
 
139
    def _set_replica_uid(self, replica_uid):
 
140
        """Force the replica_uid to be set."""
141
141
        with self._db_handle:
142
142
            c = self._db_handle.cursor()
143
 
            c.execute("INSERT INTO u1db_config VALUES ('machine_id', ?)",
144
 
                      (machine_id,))
145
 
        self._real_machine_id = machine_id
 
143
            c.execute("INSERT INTO u1db_config VALUES ('replica_uid', ?)",
 
144
                      (replica_uid,))
 
145
        self._real_replica_uid = replica_uid
146
146
 
147
 
    def _get_machine_id(self):
148
 
        if self._real_machine_id is not None:
149
 
            return self._real_machine_id
 
147
    def _get_replica_uid(self):
 
148
        if self._real_replica_uid is not None:
 
149
            return self._real_replica_uid
150
150
        c = self._db_handle.cursor()
151
 
        c.execute("SELECT value FROM u1db_config WHERE name = 'machine_id'")
 
151
        c.execute("SELECT value FROM u1db_config WHERE name = 'replica_uid'")
152
152
        val = c.fetchone()
153
153
        if val is None:
154
154
            return None
155
 
        self._real_machine_id = val[0]
156
 
        return self._real_machine_id
 
155
        self._real_replica_uid = val[0]
 
156
        return self._real_replica_uid
157
157
 
158
 
    _machine_id = property(_get_machine_id)
 
158
    _replica_uid = property(_get_replica_uid)
159
159
 
160
160
    def _get_generation(self):
161
161
        c = self._db_handle.cursor()
295
295
            this_doc_rev, this_doc = self._get_doc(doc_id)
296
296
        return [(this_doc_rev, this_doc)] + conflict_docs
297
297
 
298
 
    def get_sync_generation(self, other_db_id):
 
298
    def get_sync_generation(self, other_replica_uid):
299
299
        c = self._db_handle.cursor()
300
 
        c.execute("SELECT known_generation FROM sync_log WHERE machine_id = ?",
301
 
                  (other_db_id,))
 
300
        c.execute("SELECT known_generation FROM sync_log WHERE replica_uid = ?",
 
301
                  (other_replica_uid,))
302
302
        val = c.fetchone()
303
303
        if val is None:
304
304
            other_gen = 0
306
306
            other_gen = val[0]
307
307
        return other_gen
308
308
 
309
 
    def set_sync_generation(self, other_db_id, other_generation):
 
309
    def set_sync_generation(self, other_replica_uid, other_generation):
310
310
        with self._db_handle:
311
311
            c = self._db_handle.cursor()
312
312
            my_gen = self._get_generation()
313
313
            c.execute("INSERT OR REPLACE INTO sync_log VALUES (?, ?)",
314
 
                      (other_db_id, other_generation))
 
314
                      (other_replica_uid, other_generation))
315
315
 
316
316
    def _compare_and_insert_doc(self, doc_id, doc_rev, doc):
317
317
        with self._db_handle:
455
455
 
456
456
class SQLiteSyncTarget(CommonSyncTarget):
457
457
 
458
 
    def get_sync_info(self, other_machine_id):
459
 
        other_gen = self._db.get_sync_generation(other_machine_id)
 
458
    def get_sync_info(self, other_replica_uid):
 
459
        other_gen = self._db.get_sync_generation(other_replica_uid)
460
460
        my_gen = self._db._get_generation()
461
 
        return self._db._machine_id, my_gen, other_gen
 
461
        return self._db._replica_uid, my_gen, other_gen
462
462
 
463
 
    def record_sync_info(self, other_machine_id, other_machine_generation):
464
 
        self._db.set_sync_generation(other_machine_id,
465
 
                                     other_machine_generation)
 
463
    def record_sync_info(self, other_replica_uid, other_replica_generation):
 
464
        self._db.set_sync_generation(other_replica_uid,
 
465
                                     other_replica_generation)
466
466
 
467
467
 
468
468
class SQLiteExpandedDatabase(SQLiteDatabase):