Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / sync / syncable / directory_backing_store.cc
index 0dd22a7..ec28a53 100644 (file)
@@ -35,7 +35,7 @@ namespace syncable {
 static const string::size_type kUpdateStatementBufferSize = 2048;
 
 // Increment this version whenever updating DB tables.
-const int32 kCurrentDBVersion = 86;
+const int32 kCurrentDBVersion = 88;
 
 // Iterate over the fields of |entry| and bind each to |statement| for
 // updating.  Returns the number of args bound.
@@ -70,6 +70,11 @@ void BindFields(const EntryKernel& entry,
     entry.ref(static_cast<UniquePositionField>(i)).SerializeToString(&temp);
     statement->BindBlob(index++, temp.data(), temp.length());
   }
+  for (; i < ATTACHMENT_METADATA_FIELDS_END; ++i) {
+    std::string temp;
+    entry.ref(static_cast<AttachmentMetadataField>(i)).SerializeToString(&temp);
+    statement->BindBlob(index++, temp.data(), temp.length());
+  }
 }
 
 // The caller owns the returned EntryKernel*.  Assumes the statement currently
@@ -114,6 +119,10 @@ scoped_ptr<EntryKernel> UnpackEntry(sql::Statement* statement) {
     kernel->mutable_ref(static_cast<UniquePositionField>(i)) =
         UniquePosition::FromProto(proto);
   }
+  for (; i < ATTACHMENT_METADATA_FIELDS_END; ++i) {
+    kernel->mutable_ref(static_cast<AttachmentMetadataField>(i)).ParseFromArray(
+        statement->ColumnBlob(i), statement->ColumnByteLength(i));
+  }
   return kernel.Pass();
 }
 
@@ -156,6 +165,8 @@ DirectoryBackingStore::DirectoryBackingStore(const string& dir_name)
     dir_name_(dir_name),
     needs_column_refresh_(false) {
   db_->set_histogram_tag("SyncDirectory");
+  db_->set_page_size(4096);
+  db_->set_cache_size(32);
 }
 
 DirectoryBackingStore::DirectoryBackingStore(const string& dir_name,
@@ -256,8 +267,11 @@ bool DirectoryBackingStore::SaveChanges(
     sql::Statement s2(db_->GetCachedStatement(
             SQL_FROM_HERE,
             "INSERT OR REPLACE "
-            "INTO models (model_id, progress_marker, transaction_version) "
-            "VALUES (?, ?, ?)"));
+            "INTO models (model_id, "
+                         "progress_marker, "
+                         "transaction_version, "
+                         "context) "
+            "VALUES (?, ?, ?, ?)"));
 
     ModelTypeSet protocol_types = ProtocolTypes();
     for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good();
@@ -270,6 +284,9 @@ bool DirectoryBackingStore::SaveChanges(
       s2.BindBlob(0, model_id.data(), model_id.length());
       s2.BindBlob(1, progress_marker.data(), progress_marker.length());
       s2.BindInt64(2, info.transaction_version[type]);
+      string context;
+      info.datatype_context[type].SerializeToString(&context);
+      s2.BindBlob(3, context.data(), context.length());
       if (!s2.Run())
         return false;
       DCHECK_EQ(db_->GetLastChangeCount(), 1);
@@ -404,6 +421,18 @@ bool DirectoryBackingStore::InitializeTables() {
       version_on_disk = 86;
   }
 
+  // Version 87 migration adds a collection of attachment ids per sync entry.
+  if (version_on_disk == 86) {
+    if (MigrateVersion86To87())
+      version_on_disk = 87;
+  }
+
+  // Version 88 migration adds datatype contexts to the models table.
+  if (version_on_disk == 87) {
+    if (MigrateVersion87To88())
+      version_on_disk = 88;
+  }
+
   // If one of the migrations requested it, drop columns that aren't current.
   // It's only safe to do this after migrating all the way to the current
   // version.
@@ -558,7 +587,7 @@ bool DirectoryBackingStore::LoadInfo(Directory::KernelLoadInfo* info) {
     sql::Statement s(
         db_->GetUniqueStatement(
             "SELECT model_id, progress_marker, "
-            "transaction_version FROM models"));
+            "transaction_version, context FROM models"));
 
     while (s.Step()) {
       ModelType type = ModelIdToModelTypeEnum(s.ColumnBlob(0),
@@ -567,6 +596,8 @@ bool DirectoryBackingStore::LoadInfo(Directory::KernelLoadInfo* info) {
         info->kernel_info.download_progress[type].ParseFromArray(
             s.ColumnBlob(1), s.ColumnByteLength(1));
         info->kernel_info.transaction_version[type] = s.ColumnInt64(2);
+        info->kernel_info.datatype_context[type].ParseFromArray(
+            s.ColumnBlob(3), s.ColumnByteLength(3));
       }
     }
     if (!s.Succeeded())
@@ -1126,7 +1157,7 @@ bool DirectoryBackingStore::MigrateVersion84To85() {
   // Version 85 removes the initial_sync_ended flag.
   if (!db_->Execute("ALTER TABLE models RENAME TO temp_models"))
     return false;
-  if (!CreateModelsTable())
+  if (!CreateV81ModelsTable())
     return false;
   if (!db_->Execute("INSERT INTO models SELECT "
                     "model_id, progress_marker, transaction_version "
@@ -1264,6 +1295,27 @@ bool DirectoryBackingStore::MigrateVersion85To86() {
   return true;
 }
 
+bool DirectoryBackingStore::MigrateVersion86To87() {
+  // Version 87 adds AttachmentMetadata proto.
+  if (!db_->Execute(
+          "ALTER TABLE metas ADD COLUMN "
+          "attachment_metadata BLOB")) {
+    return false;
+  }
+  SetVersion(87);
+  needs_column_refresh_ = true;
+  return true;
+}
+
+bool DirectoryBackingStore::MigrateVersion87To88() {
+  // Version 88 adds the datatype context to the models table.
+  if (!db_->Execute("ALTER TABLE models ADD COLUMN context blob"))
+    return false;
+
+  SetVersion(88);
+  return true;
+}
+
 bool DirectoryBackingStore::CreateTables() {
   DVLOG(1) << "First run, creating tables";
   // Create two little tables share_version and share_info
@@ -1380,8 +1432,20 @@ bool DirectoryBackingStore::CreateV75ModelsTable() {
       "initial_sync_ended BOOLEAN default 0)");
 }
 
+bool DirectoryBackingStore::CreateV81ModelsTable() {
+  // This is an old schema for the Models table, used from versions 81 to 87.
+  return db_->Execute(
+      "CREATE TABLE models ("
+      "model_id BLOB primary key, "
+      "progress_marker BLOB, "
+      // Gets set if the syncer ever gets updates from the
+      // server and the server returns 0.  Lets us detect the
+      // end of the initial sync.
+      "transaction_version BIGINT default 0)");
+}
+
 bool DirectoryBackingStore::CreateModelsTable() {
-  // This is the current schema for the Models table, from version 81
+  // This is the current schema for the Models table, from version 88
   // onward.  If you change the schema, you'll probably want to double-check
   // the use of this function in the v84-v85 migration.
   return db_->Execute(
@@ -1391,7 +1455,8 @@ bool DirectoryBackingStore::CreateModelsTable() {
       // Gets set if the syncer ever gets updates from the
       // server and the server returns 0.  Lets us detect the
       // end of the initial sync.
-      "transaction_version BIGINT default 0)");
+      "transaction_version BIGINT default 0,"
+      "context BLOB)");
 }
 
 bool DirectoryBackingStore::CreateShareInfoTable(bool is_temporary) {