Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(311)

Unified Diff: sync/syncable/directory_backing_store.cc

Issue 10989063: Changed DB to store node positions as Ordinals. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fixed errors and corrected migration logic. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sync/syncable/directory_backing_store.cc
diff --git a/sync/syncable/directory_backing_store.cc b/sync/syncable/directory_backing_store.cc
index 9b8e0dba855edc22128e0cb4aab84d96ec485da5..13b28494de5cd558cfa9bd24dcfd4ff757e119ad 100644
--- a/sync/syncable/directory_backing_store.cc
+++ b/sync/syncable/directory_backing_store.cc
@@ -27,6 +27,8 @@
#include "sync/syncable/syncable-inl.h"
#include "sync/syncable/syncable_columns.h"
#include "sync/util/time.h"
+#include "sync/internal_api/public/base/ordinal.h"
+#include "sync/internal_api/public/base/node_ordinal.h"
using std::string;
@@ -39,7 +41,7 @@ static const string::size_type kUpdateStatementBufferSize = 2048;
// Increment this version whenever updating DB tables.
extern const int32 kCurrentDBVersion; // Global visibility for our unittest.
-const int32 kCurrentDBVersion = 80;
+const int32 kCurrentDBVersion = 81;
// Iterate over the fields of |entry| and bind each to |statement| for
// updating. Returns the number of args bound.
@@ -48,6 +50,14 @@ void BindFields(const EntryKernel& entry,
int index = 0;
int i = 0;
for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) {
+ // Store SERVER_POSITION_IN_PARENT as a blob for eventual shift
+ // to using ordinals instead of int64's
rlarocque 2012/09/29 01:13:42 nit: put period after comment. Also, you might wa
vishwath 2012/10/01 20:39:12 Done.
+ if(i == SERVER_POSITION_IN_PARENT) {
rlarocque 2012/09/29 01:13:42 nit: space between if and parenthesis.
vishwath 2012/10/01 20:39:12 Done.
+ std::string ordinal = Int64ToNodeOrdinal(
+ entry.ref(SERVER_POSITION_IN_PARENT)).ToInternalValue();
+ statement->BindBlob(index++, ordinal.data(), ordinal.size());
+ continue;
rlarocque 2012/09/29 01:13:42 I'd prefer an 'else' branch to 'continue'.
vishwath 2012/10/01 20:39:12 Done.
+ }
statement->BindInt64(index++, entry.ref(static_cast<Int64Field>(i)));
}
for ( ; i < TIME_FIELDS_END; ++i) {
@@ -78,6 +88,17 @@ EntryKernel* UnpackEntry(sql::Statement* statement) {
DCHECK_EQ(statement->ColumnCount(), static_cast<int>(FIELD_COUNT));
int i = 0;
for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) {
+ // Store SERVER_POSITION_IN_PARENT as a string for eventual shift
+ // to using ordinals instead of int64's
+ if(i == SERVER_POSITION_IN_PARENT &&
+ statement->ColumnType(i) == sql::COLUMN_TYPE_BLOB) {
rlarocque 2012/09/29 01:13:42 We shouldn't get here unless the database is migra
+ std::string ordinal_blob;
+ DCHECK(statement->ColumnBlobAsString(i, &ordinal_blob));
+ int64 server_position = NodeOrdinalToInt64(
+ NodeOrdinal(ordinal_blob));
+ kernel->put(static_cast<Int64Field>(i), server_position);
rlarocque 2012/09/29 01:13:42 nit: I'd prefer that the first argument to put be
vishwath 2012/10/01 20:39:12 Done.
+ continue;
rlarocque 2012/09/29 01:13:42 I'd prefer an else branch here.
vishwath 2012/10/01 20:39:12 Done.
+ }
kernel->put(static_cast<Int64Field>(i), statement->ColumnInt64(i));
}
for ( ; i < TIME_FIELDS_END; ++i) {
@@ -323,6 +344,13 @@ bool DirectoryBackingStore::InitializeTables() {
version_on_disk = 80;
}
+ // Version 81 changes the server_position_in_parent_field from an int64
+ // to a string.
+ if (version_on_disk == 80) {
+ if (MigrateVersion80To81())
+ version_on_disk = 81;
+ }
+
// 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.
@@ -968,6 +996,7 @@ bool DirectoryBackingStore::MigrateVersion78To79() {
SetVersion(79);
return true;
}
+
bool DirectoryBackingStore::MigrateVersion79To80() {
if (!db_->Execute(
"ALTER TABLE share_info ADD COLUMN bag_of_chips BLOB"))
@@ -982,6 +1011,75 @@ bool DirectoryBackingStore::MigrateVersion79To80() {
return true;
}
+bool DirectoryBackingStore::MigrateVersion80To81() {
+ // Version 81 changes server_position_in_parent from a potentially lossy
+ // bigint to an unbounded fidelity ordinal (stored as a blob)
rlarocque 2012/09/29 01:13:42 I wouldn't call it a bigint, that term could be ov
vishwath 2012/10/01 20:39:12 Done.
+ // We make the current metas table temporary, create a new table where
+ // server_position_in_parent is of the right type, and transfer values
+ // after converting from int64's to Ordinals.
+ SafeDropTable("temp_metas");
rlarocque 2012/09/29 01:13:42 Is there a transaction open at this point? If not
vishwath 2012/10/01 20:39:12 I think all the Migrate*() functions rely on the c
+ if(!db_->Execute(
+ "ALTER TABLE metas RENAME TO temp_metas")) {
+ return false;
+ }
+ if(!CreateMetasTable(false))
+ return false;
+
+ std::string old_info_string = "SELECT ";
rlarocque 2012/09/29 01:13:42 I was hoping we could perform the migration withou
vishwath 2012/10/01 20:39:12 That'a actually the way I'd prefer to do it as wel
+ AppendColumnList(&old_info_string);
+ old_info_string.append(" FROM temp_metas");
+
+ std::string new_info_string = "INSERT INTO metas (";
+ AppendColumnList(&new_info_string);
+ new_info_string.append(") VALUES (");
+ for(int i = BEGIN_FIELDS; i < BEGIN_FIELDS + FIELD_COUNT - 1; ++i) {
rlarocque 2012/09/29 01:13:42 I'd really like to avoid this sort of thing, thoug
vishwath 2012/10/01 20:39:12 I see the inherent vulnerabilities in misusing thi
+ new_info_string.append("?, ");
+ }
+ new_info_string.append("?)");
+
+ sql::Statement old_info(db_->GetUniqueStatement(
+ old_info_string.c_str()));
+ sql::Statement new_info(db_->GetUniqueStatement(
+ new_info_string.c_str()));
+
+ while(old_info.Step()) {
+ int64 int_ordinal = old_info.ColumnInt(3);
+ std::string new_ordinal =
+ Int64ToNodeOrdinal(int_ordinal).ToInternalValue();
+ //Loop and bind each new column based on the old column type
+ for(int i = 0; i < FIELD_COUNT; ++i) {
+ if (i == SERVER_POSITION_IN_PARENT) {
+ new_info.BindBlob(i, new_ordinal.data(), new_ordinal.length());
+ continue;
+ }
+ switch(old_info.ColumnType(i)) {
+ case sql::COLUMN_TYPE_INTEGER:
+ new_info.BindInt64(i, old_info.ColumnInt64(i));
+ break;
+ case sql::COLUMN_TYPE_FLOAT:
+ new_info.BindDouble(i, old_info.ColumnDouble(i));
+ break;
+ case sql::COLUMN_TYPE_TEXT:
+ new_info.BindString(i, old_info.ColumnString(i));
+ break;
+ case sql::COLUMN_TYPE_BLOB:
+ new_info.BindBlob(i,
+ old_info.ColumnBlob(i),
+ old_info.ColumnByteLength(i));
+ break;
+ case sql::COLUMN_TYPE_NULL:
+ new_info.BindNull(i);
+ }
+ }
+
+ if(!new_info.Run())
+ return false;
+ new_info.Reset(true);
+ }
+ SetVersion(81);
+ return true;
+}
+
bool DirectoryBackingStore::CreateTables() {
DVLOG(1) << "First run, creating tables";
// Create two little tables share_version and share_info

Powered by Google App Engine
This is Rietveld 408576698