OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/sync_file_system/drive/metadata_db_migration_util.h" | |
6 | |
7 #include "base/files/file_path.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/string_util.h" | |
10 #include "googleurl/src/gurl.h" | |
11 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | |
12 | |
13 namespace sync_file_system { | |
14 namespace drive { | |
15 | |
16 namespace { | |
17 | |
18 const char kWapiFileIdPrefix[] = "file:"; | |
19 const char kWapiFolderIdPrefix[] = "folder:"; | |
20 | |
21 std::string RemovePrefix(const std::string& str, const std::string& prefix) { | |
22 if (StartsWithASCII(str, prefix, true)) | |
23 return std::string(str.begin() + prefix.size(), str.end()); | |
24 return str; | |
25 } | |
26 | |
27 } // namespace | |
28 | |
29 std::string AddWapiFilePrefix(const std::string& resource_id) { | |
30 if (resource_id.empty() || | |
31 StartsWithASCII(resource_id, kWapiFileIdPrefix, true) || | |
32 StartsWithASCII(resource_id, kWapiFolderIdPrefix, true)) | |
33 return resource_id; | |
34 return kWapiFileIdPrefix + resource_id; | |
35 } | |
36 | |
37 std::string AddWapiFolderPrefix(const std::string& resource_id) { | |
38 if (resource_id.empty() || | |
39 StartsWithASCII(resource_id, kWapiFileIdPrefix, true) || | |
40 StartsWithASCII(resource_id, kWapiFolderIdPrefix, true)) | |
41 return resource_id; | |
42 return kWapiFolderIdPrefix + resource_id; | |
43 } | |
44 | |
45 std::string AddWapiIdPrefix(const std::string& resource_id, | |
46 DriveMetadata_ResourceType type) { | |
47 switch (type) { | |
48 case DriveMetadata_ResourceType_RESOURCE_TYPE_FILE: | |
49 return AddWapiFilePrefix(resource_id); | |
50 case DriveMetadata_ResourceType_RESOURCE_TYPE_FOLDER: | |
51 return AddWapiFolderPrefix(resource_id); | |
52 default: | |
tzik
2013/05/29 04:30:24
Could you drop "default:" and move this case out o
nhiroki
2013/05/29 06:31:03
Done.
| |
53 NOTREACHED(); | |
54 return resource_id; | |
55 } | |
56 } | |
57 | |
58 std::string RemoveWapiIdPrefix(const std::string& resource_id) { | |
59 if (StartsWithASCII(resource_id, kWapiFileIdPrefix, true)) | |
60 return RemovePrefix(resource_id, kWapiFileIdPrefix); | |
61 if (StartsWithASCII(resource_id, kWapiFolderIdPrefix, true)) | |
62 return RemovePrefix(resource_id, kWapiFolderIdPrefix); | |
63 return resource_id; | |
64 } | |
65 | |
66 SyncStatusCode MigrateDatabaseFromV1ToV2(leveldb::DB* db) { | |
67 // Strips prefix of WAPI resource ID. | |
68 // (i.e. "file:xxxx" => "xxxx", "folder:yyyy" => "yyyy") | |
69 // | |
70 // Version 2 database format (changed keys/fields are marked with '*'): | |
71 // key: "VERSION" | |
72 // * value: 2 | |
73 // | |
74 // key: "CHANGE_STAMP" | |
75 // value: <Largest Changestamp> | |
76 // | |
77 // key: "SYNC_ROOT_DIR" | |
78 // * value: <Resource ID of the sync root directory> (striped) | |
79 // | |
80 // key: "METADATA: " + <Origin and URL> | |
81 // * value: <Serialized DriveMetadata> (striped) | |
82 // | |
83 // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin> | |
84 // * value: <Resource ID of the drive directory for the origin> (striped) | |
85 // | |
86 // key: "DISABLED_ORIGIN: " + <URL string of a disabled origin> | |
87 // * value: <Resource ID of the drive directory for the origin> (striped) | |
88 | |
89 const char kDatabaseVersionKey[] = "VERSION"; | |
90 const char kSyncRootDirectoryKey[] = "SYNC_ROOT_DIR"; | |
91 const char kDriveMetadataKeyPrefix[] = "METADATA: "; | |
92 const char kDriveIncrementalSyncOriginKeyPrefix[] = "ISYNC_ORIGIN: "; | |
93 const char kDriveDisabledOriginKeyPrefix[] = "DISABLED_ORIGIN: "; | |
94 | |
95 leveldb::WriteBatch write_batch; | |
96 write_batch.Put(kDatabaseVersionKey, "2"); | |
97 | |
98 // Strip resource id for the sync root directory. | |
99 std::string sync_root_resource_id; | |
100 leveldb::Status status = db->Get( | |
101 leveldb::ReadOptions(), kSyncRootDirectoryKey, &sync_root_resource_id); | |
102 write_batch.Put(kSyncRootDirectoryKey, | |
103 RemoveWapiIdPrefix(sync_root_resource_id)); | |
kinuko
2013/05/29 04:44:36
I wonder if writing this in a single for could be
nhiroki
2013/05/29 06:31:03
Done.
| |
104 | |
105 // Strip resource ids in the drive metadata. | |
106 scoped_ptr<leveldb::Iterator> itr(db->NewIterator(leveldb::ReadOptions())); | |
107 for (itr->Seek(kDriveMetadataKeyPrefix); itr->Valid(); itr->Next()) { | |
108 std::string key = itr->key().ToString(); | |
109 if (!StartsWithASCII(key, kDriveMetadataKeyPrefix, true)) | |
110 break; | |
111 | |
112 DriveMetadata metadata; | |
113 bool success = metadata.ParseFromString(itr->value().ToString()); | |
114 DCHECK(success); | |
115 | |
116 metadata.set_resource_id(RemoveWapiIdPrefix(metadata.resource_id())); | |
117 std::string metadata_string; | |
118 metadata.SerializeToString(&metadata_string); | |
119 | |
120 write_batch.Put(key, metadata_string); | |
121 } | |
122 | |
123 // Strip resource ids of the incremental sync origins. | |
124 for (itr->Seek(kDriveIncrementalSyncOriginKeyPrefix); | |
125 itr->Valid(); itr->Next()) { | |
126 std::string key = itr->key().ToString(); | |
127 if (!StartsWithASCII(key, kDriveIncrementalSyncOriginKeyPrefix, true)) | |
128 break; | |
129 write_batch.Put(key, RemoveWapiIdPrefix(itr->value().ToString())); | |
130 } | |
131 | |
132 // Strip resource ids of the disabled sync origins. | |
133 for (itr->Seek(kDriveDisabledOriginKeyPrefix); itr->Valid(); itr->Next()) { | |
134 std::string key = itr->key().ToString(); | |
135 if (!StartsWithASCII(key, kDriveDisabledOriginKeyPrefix, true)) | |
136 break; | |
137 write_batch.Put(key, RemoveWapiIdPrefix(itr->value().ToString())); | |
138 } | |
139 | |
140 return LevelDBStatusToSyncStatusCode( | |
141 db->Write(leveldb::WriteOptions(), &write_batch)); | |
142 } | |
143 | |
144 } // namespace drive | |
145 } // namespace sync_file_system | |
OLD | NEW |