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

Side by Side Diff: chrome/browser/sync_file_system/drive/metadata_db_migration_util.cc

Issue 15951007: SyncFS: Factor out DriveMetadataDB migration functions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix win test Created 7 years, 6 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/sync_file_system/drive/metadata_db_migration_util.h" 5 #include "chrome/browser/sync_file_system/drive/metadata_db_migration_util.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "googleurl/src/gurl.h" 10 #include "googleurl/src/gurl.h"
11 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" 11 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
12 #include "webkit/browser/fileapi/file_system_url.h"
13 #include "webkit/common/fileapi/file_system_types.h"
12 14
13 namespace sync_file_system { 15 namespace sync_file_system {
14 namespace drive { 16 namespace drive {
15 17
16 namespace { 18 namespace {
17 19
20 const base::FilePath::CharType kV0FormatPathPrefix[] =
21 FILE_PATH_LITERAL("drive/");
18 const char kWapiFileIdPrefix[] = "file:"; 22 const char kWapiFileIdPrefix[] = "file:";
19 const char kWapiFolderIdPrefix[] = "folder:"; 23 const char kWapiFolderIdPrefix[] = "folder:";
20 24
21 std::string RemovePrefix(const std::string& str, const std::string& prefix) { 25 std::string RemovePrefix(const std::string& str, const std::string& prefix) {
22 if (StartsWithASCII(str, prefix, true)) 26 if (StartsWithASCII(str, prefix, true))
23 return std::string(str.begin() + prefix.size(), str.end()); 27 return std::string(str.begin() + prefix.size(), str.end());
24 return str; 28 return str;
25 } 29 }
26 30
27 } // namespace 31 } // namespace
28 32
33 bool ParseV0FormatFileSystemURL(const GURL& url,
34 GURL* origin,
35 base::FilePath* path) {
36 fileapi::FileSystemType mount_type;
37 base::FilePath virtual_path;
38
39 if (!fileapi::FileSystemURL::ParseFileSystemSchemeURL(
40 url, origin, &mount_type, &virtual_path) ||
41 mount_type != fileapi::kFileSystemTypeExternal) {
42 NOTREACHED() << "Failed to parse filesystem scheme URL";
43 return false;
44 }
45
46 base::FilePath::StringType prefix =
47 base::FilePath(kV0FormatPathPrefix).NormalizePathSeparators().value();
48 if (virtual_path.value().substr(0, prefix.size()) != prefix)
49 return false;
50
51 *path = base::FilePath(virtual_path.value().substr(prefix.size()));
52 return true;
53 }
54
29 std::string AddWapiFilePrefix(const std::string& resource_id) { 55 std::string AddWapiFilePrefix(const std::string& resource_id) {
30 DCHECK(!StartsWithASCII(resource_id, kWapiFileIdPrefix, true)); 56 DCHECK(!StartsWithASCII(resource_id, kWapiFileIdPrefix, true));
31 DCHECK(!StartsWithASCII(resource_id, kWapiFolderIdPrefix, true)); 57 DCHECK(!StartsWithASCII(resource_id, kWapiFolderIdPrefix, true));
32 58
33 if (resource_id.empty() || 59 if (resource_id.empty() ||
34 StartsWithASCII(resource_id, kWapiFileIdPrefix, true) || 60 StartsWithASCII(resource_id, kWapiFileIdPrefix, true) ||
35 StartsWithASCII(resource_id, kWapiFolderIdPrefix, true)) 61 StartsWithASCII(resource_id, kWapiFolderIdPrefix, true))
36 return resource_id; 62 return resource_id;
37 return kWapiFileIdPrefix + resource_id; 63 return kWapiFileIdPrefix + resource_id;
38 } 64 }
(...skipping 22 matching lines...) Expand all
61 } 87 }
62 88
63 std::string RemoveWapiIdPrefix(const std::string& resource_id) { 89 std::string RemoveWapiIdPrefix(const std::string& resource_id) {
64 if (StartsWithASCII(resource_id, kWapiFileIdPrefix, true)) 90 if (StartsWithASCII(resource_id, kWapiFileIdPrefix, true))
65 return RemovePrefix(resource_id, kWapiFileIdPrefix); 91 return RemovePrefix(resource_id, kWapiFileIdPrefix);
66 if (StartsWithASCII(resource_id, kWapiFolderIdPrefix, true)) 92 if (StartsWithASCII(resource_id, kWapiFolderIdPrefix, true))
67 return RemovePrefix(resource_id, kWapiFolderIdPrefix); 93 return RemovePrefix(resource_id, kWapiFolderIdPrefix);
68 return resource_id; 94 return resource_id;
69 } 95 }
70 96
97 SyncStatusCode MigrateDatabaseFromV0ToV1(leveldb::DB* db) {
98 // Version 0 database format:
99 // key: "CHANGE_STAMP"
100 // value: <Largest Changestamp>
101 //
102 // key: "SYNC_ROOT_DIR"
103 // value: <Resource ID of the sync root directory>
104 //
105 // key: "METADATA: " +
106 // <FileSystemURL serialized by SerializeSyncableFileSystemURL>
107 // value: <Serialized DriveMetadata>
108 //
109 // key: "BSYNC_ORIGIN: " + <URL string of a batch sync origin>
110 // value: <Resource ID of the drive directory for the origin>
111 //
112 // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin>
113 // value: <Resource ID of the drive directory for the origin>
114 //
115 // Version 1 database format (changed keys/fields are marked with '*'):
116 // * key: "VERSION" (new)
117 // * value: 1
118 //
119 // key: "CHANGE_STAMP"
120 // value: <Largest Changestamp>
121 //
122 // key: "SYNC_ROOT_DIR"
123 // value: <Resource ID of the sync root directory>
124 //
125 // * key: "METADATA: " + <Origin and URL> (changed)
126 // * value: <Serialized DriveMetadata>
127 //
128 // key: "BSYNC_ORIGIN: " + <URL string of a batch sync origin>
129 // value: <Resource ID of the drive directory for the origin>
130 //
131 // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin>
132 // value: <Resource ID of the drive directory for the origin>
133 //
134 // key: "DISABLED_ORIGIN: " + <URL string of a disabled origin>
135 // value: <Resource ID of the drive directory for the origin>
136
137 const char kDatabaseVersionKey[] = "VERSION";
138 const char kDriveMetadataKeyPrefix[] = "METADATA: ";
139 const char kMetadataKeySeparator = ' ';
140
141 leveldb::WriteBatch write_batch;
142 write_batch.Put(kDatabaseVersionKey, "1");
143
144 scoped_ptr<leveldb::Iterator> itr(db->NewIterator(leveldb::ReadOptions()));
145 for (itr->Seek(kDriveMetadataKeyPrefix); itr->Valid(); itr->Next()) {
146 std::string key = itr->key().ToString();
147 if (!StartsWithASCII(key, kDriveMetadataKeyPrefix, true))
148 break;
149 std::string serialized_url(RemovePrefix(key, kDriveMetadataKeyPrefix));
150
151 GURL origin;
152 base::FilePath path;
153 bool success = ParseV0FormatFileSystemURL(
154 GURL(serialized_url), &origin, &path);
155 DCHECK(success) << serialized_url;
156 std::string new_key = kDriveMetadataKeyPrefix + origin.spec() +
157 kMetadataKeySeparator + path.AsUTF8Unsafe();
158
159 write_batch.Put(new_key, itr->value());
160 write_batch.Delete(key);
161 }
162
163 return LevelDBStatusToSyncStatusCode(
164 db->Write(leveldb::WriteOptions(), &write_batch));
165 }
166
71 SyncStatusCode MigrateDatabaseFromV1ToV2(leveldb::DB* db) { 167 SyncStatusCode MigrateDatabaseFromV1ToV2(leveldb::DB* db) {
72 // Strips prefix of WAPI resource ID. 168 // Strips prefix of WAPI resource ID, and discards batch sync origins.
73 // (i.e. "file:xxxx" => "xxxx", "folder:yyyy" => "yyyy") 169 // (i.e. "file:xxxx" => "xxxx", "folder:yyyy" => "yyyy")
74 // 170 //
75 // Version 2 database format (changed keys/fields are marked with '*'): 171 // Version 2 database format (changed keys/fields are marked with '*'):
76 // key: "VERSION" 172 // key: "VERSION"
77 // * value: 2 173 // * value: 2
78 // 174 //
79 // key: "CHANGE_STAMP" 175 // key: "CHANGE_STAMP"
80 // value: <Largest Changestamp> 176 // value: <Largest Changestamp>
81 // 177 //
82 // key: "SYNC_ROOT_DIR" 178 // key: "SYNC_ROOT_DIR"
83 // * value: <Resource ID of the sync root directory> (striped) 179 // * value: <Resource ID of the sync root directory> (striped)
84 // 180 //
85 // key: "METADATA: " + <Origin and URL> 181 // key: "METADATA: " + <Origin and URL>
86 // * value: <Serialized DriveMetadata> (striped) 182 // * value: <Serialized DriveMetadata> (stripped)
183 //
184 // * key: "BSYNC_ORIGIN: " + <URL string of a batch sync origin> (deleted)
185 // * value: <Resource ID of the drive directory for the origin> (deleted)
87 // 186 //
88 // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin> 187 // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin>
89 // * value: <Resource ID of the drive directory for the origin> (striped) 188 // * value: <Resource ID of the drive directory for the origin> (stripped)
90 // 189 //
91 // key: "DISABLED_ORIGIN: " + <URL string of a disabled origin> 190 // key: "DISABLED_ORIGIN: " + <URL string of a disabled origin>
92 // * value: <Resource ID of the drive directory for the origin> (striped) 191 // * value: <Resource ID of the drive directory for the origin> (stripped)
93 192
94 const char kDatabaseVersionKey[] = "VERSION"; 193 const char kDatabaseVersionKey[] = "VERSION";
95 const char kSyncRootDirectoryKey[] = "SYNC_ROOT_DIR"; 194 const char kSyncRootDirectoryKey[] = "SYNC_ROOT_DIR";
96 const char kDriveMetadataKeyPrefix[] = "METADATA: "; 195 const char kDriveMetadataKeyPrefix[] = "METADATA: ";
196 const char kDriveBatchSyncOriginKeyPrefix[] = "BSYNC_ORIGIN: ";
97 const char kDriveIncrementalSyncOriginKeyPrefix[] = "ISYNC_ORIGIN: "; 197 const char kDriveIncrementalSyncOriginKeyPrefix[] = "ISYNC_ORIGIN: ";
98 const char kDriveDisabledOriginKeyPrefix[] = "DISABLED_ORIGIN: "; 198 const char kDriveDisabledOriginKeyPrefix[] = "DISABLED_ORIGIN: ";
99 199
100 leveldb::WriteBatch write_batch; 200 leveldb::WriteBatch write_batch;
101 write_batch.Put(kDatabaseVersionKey, "2"); 201 write_batch.Put(kDatabaseVersionKey, "2");
102 202
103 scoped_ptr<leveldb::Iterator> itr(db->NewIterator(leveldb::ReadOptions())); 203 scoped_ptr<leveldb::Iterator> itr(db->NewIterator(leveldb::ReadOptions()));
104 for (itr->SeekToFirst(); itr->Valid(); itr->Next()) { 204 for (itr->SeekToFirst(); itr->Valid(); itr->Next()) {
105 std::string key = itr->key().ToString(); 205 std::string key = itr->key().ToString();
106 206
(...skipping 10 matching lines...) Expand all
117 DCHECK(success); 217 DCHECK(success);
118 218
119 metadata.set_resource_id(RemoveWapiIdPrefix(metadata.resource_id())); 219 metadata.set_resource_id(RemoveWapiIdPrefix(metadata.resource_id()));
120 std::string metadata_string; 220 std::string metadata_string;
121 metadata.SerializeToString(&metadata_string); 221 metadata.SerializeToString(&metadata_string);
122 222
123 write_batch.Put(key, metadata_string); 223 write_batch.Put(key, metadata_string);
124 continue; 224 continue;
125 } 225 }
126 226
227 // Deprecate legacy batch sync origin entries that are no longer needed.
228 if (StartsWithASCII(key, kDriveBatchSyncOriginKeyPrefix, true)) {
229 write_batch.Delete(key);
230 continue;
231 }
232
127 // Strip resource ids of the incremental sync origins. 233 // Strip resource ids of the incremental sync origins.
128 if (StartsWithASCII(key, kDriveIncrementalSyncOriginKeyPrefix, true)) { 234 if (StartsWithASCII(key, kDriveIncrementalSyncOriginKeyPrefix, true)) {
129 write_batch.Put(key, RemoveWapiIdPrefix(itr->value().ToString())); 235 write_batch.Put(key, RemoveWapiIdPrefix(itr->value().ToString()));
130 continue; 236 continue;
131 } 237 }
132 238
133 // Strip resource ids of the disabled sync origins. 239 // Strip resource ids of the disabled sync origins.
134 if (StartsWithASCII(key, kDriveDisabledOriginKeyPrefix, true)) { 240 if (StartsWithASCII(key, kDriveDisabledOriginKeyPrefix, true)) {
135 write_batch.Put(key, RemoveWapiIdPrefix(itr->value().ToString())); 241 write_batch.Put(key, RemoveWapiIdPrefix(itr->value().ToString()));
136 continue; 242 continue;
137 } 243 }
138 } 244 }
139 245
140 return LevelDBStatusToSyncStatusCode( 246 return LevelDBStatusToSyncStatusCode(
141 db->Write(leveldb::WriteOptions(), &write_batch)); 247 db->Write(leveldb::WriteOptions(), &write_batch));
142 } 248 }
143 249
144 } // namespace drive 250 } // namespace drive
145 } // namespace sync_file_system 251 } // namespace sync_file_system
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698