Chromium Code Reviews| 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 const char kWapiFileIdPrefix[] = "file:"; | |
| 18 const char kWapiFolderIdPrefix[] = "folder:"; | |
| 19 } | |
| 20 | |
| 21 std::string AddWapiFilePrefix(const std::string& resource_id) { | |
| 22 if (resource_id.empty() || | |
| 23 StartsWithASCII(resource_id, kWapiFileIdPrefix, true) || | |
| 24 StartsWithASCII(resource_id, kWapiFolderIdPrefix, true)) | |
|
kinuko
2013/05/28 15:09:20
Is this ok? Does this mean we get 'folder:xxxx' r
nhiroki
2013/05/29 04:10:18
Yes, I mean it. Do you prefer to return empty stri
kinuko
2013/05/29 04:44:36
When does this happen? If your answer to my first
nhiroki
2013/05/29 06:31:03
For example:
a) metadata.type() == TYPE_FILE, but
kinuko
2013/05/29 06:41:13
Thanks for the details.
If it never happens or it'
nhiroki
2013/05/29 07:49:16
Sorry, empty resource id is valid here since to-be
| |
| 25 return resource_id; | |
| 26 return kWapiFileIdPrefix + resource_id; | |
| 27 } | |
| 28 | |
| 29 std::string AddWapiFolderPrefix(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 kWapiFolderIdPrefix + resource_id; | |
| 35 } | |
| 36 | |
| 37 std::string AddWapiIdPrefix(const std::string& resource_id, | |
| 38 DriveMetadata_ResourceType type) { | |
| 39 switch (type) { | |
| 40 case DriveMetadata_ResourceType_RESOURCE_TYPE_FILE: | |
| 41 return AddWapiFilePrefix(resource_id); | |
| 42 case DriveMetadata_ResourceType_RESOURCE_TYPE_FOLDER: | |
| 43 return AddWapiFolderPrefix(resource_id); | |
| 44 default: | |
| 45 NOTREACHED(); | |
| 46 return resource_id; | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 std::string RemoveWapiIdPrefix(const std::string& resource_id) { | |
| 51 if (StartsWithASCII(resource_id, kWapiFileIdPrefix, true)) | |
| 52 return std::string(resource_id.begin() + arraysize(kWapiFileIdPrefix) - 1, | |
| 53 resource_id.end()); | |
| 54 if (StartsWithASCII(resource_id, kWapiFolderIdPrefix, true)) | |
| 55 return std::string(resource_id.begin() + arraysize(kWapiFolderIdPrefix) - 1, | |
| 56 resource_id.end()); | |
|
kinuko
2013/05/28 15:09:20
nit: please put { } if body spans multiple line (f
nhiroki
2013/05/29 04:10:18
Done.
| |
| 57 return resource_id; | |
| 58 } | |
| 59 | |
| 60 SyncStatusCode MigrateDatabaseFromV1ToV2(leveldb::DB* db) { | |
| 61 // Strips prefix of WAPI resource ID. | |
| 62 // (i.e. "file:xxxx" => "xxxx", "folder:yyyy" => "yyyy") | |
| 63 // | |
| 64 // Version 2 database format (changed keys/fields are marked with '*'): | |
| 65 // key: "VERSION" | |
| 66 // * value: 2 | |
| 67 // | |
| 68 // key: "CHANGE_STAMP" | |
| 69 // value: <Largest Changestamp> | |
| 70 // | |
| 71 // key: "SYNC_ROOT_DIR" | |
| 72 // * value: <Resource ID of the sync root directory> (striped) | |
| 73 // | |
| 74 // key: "METADATA: " + <Origin and URL> | |
| 75 // * value: <Serialized DriveMetadata> (striped) | |
| 76 // | |
| 77 // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin> | |
| 78 // * value: <Resource ID of the drive directory for the origin> (striped) | |
| 79 // | |
| 80 // key: "DISABLED_ORIGIN: " + <URL string of a disabled origin> | |
| 81 // * value: <Resource ID of the drive directory for the origin> (striped) | |
| 82 | |
| 83 const char kDatabaseVersionKey[] = "VERSION"; | |
| 84 const char kSyncRootDirectoryKey[] = "SYNC_ROOT_DIR"; | |
| 85 const char kDriveMetadataKeyPrefix[] = "METADATA: "; | |
| 86 const char kDriveIncrementalSyncOriginKeyPrefix[] = "ISYNC_ORIGIN: "; | |
| 87 const char kDriveDisabledOriginKeyPrefix[] = "DISABLED_ORIGIN: "; | |
| 88 | |
| 89 leveldb::WriteBatch write_batch; | |
| 90 write_batch.Put(kDatabaseVersionKey, "2"); | |
| 91 | |
| 92 // Strip resource id for the sync root directory. | |
| 93 std::string sync_root_resource_id; | |
| 94 leveldb::Status status = db->Get( | |
| 95 leveldb::ReadOptions(), kSyncRootDirectoryKey, &sync_root_resource_id); | |
| 96 write_batch.Put(kSyncRootDirectoryKey, | |
| 97 RemoveWapiIdPrefix(sync_root_resource_id)); | |
| 98 | |
| 99 // Strip resource ids in the drive metadata. | |
| 100 scoped_ptr<leveldb::Iterator> itr(db->NewIterator(leveldb::ReadOptions())); | |
| 101 for (itr->Seek(kDriveMetadataKeyPrefix); itr->Valid(); itr->Next()) { | |
| 102 std::string key = itr->key().ToString(); | |
| 103 if (!StartsWithASCII(key, kDriveMetadataKeyPrefix, true)) | |
| 104 break; | |
| 105 | |
| 106 DriveMetadata metadata; | |
| 107 bool success = metadata.ParseFromString(itr->value().ToString()); | |
| 108 DCHECK(success); | |
| 109 | |
| 110 metadata.set_resource_id(RemoveWapiIdPrefix(metadata.resource_id())); | |
| 111 std::string metadata_string; | |
| 112 metadata.SerializeToString(&metadata_string); | |
| 113 | |
| 114 write_batch.Put(key, metadata_string); | |
| 115 } | |
| 116 | |
| 117 // Strip resource ids of the incremental sync origins. | |
| 118 for (itr->Seek(kDriveIncrementalSyncOriginKeyPrefix); | |
| 119 itr->Valid(); itr->Next()) { | |
| 120 std::string key = itr->key().ToString(); | |
| 121 if (!StartsWithASCII(key, kDriveIncrementalSyncOriginKeyPrefix, true)) | |
| 122 break; | |
| 123 | |
| 124 GURL origin(std::string( | |
| 125 key.begin() + arraysize(kDriveIncrementalSyncOriginKeyPrefix) - 1, | |
| 126 key.end())); | |
| 127 DCHECK(origin.is_valid()); | |
| 128 | |
| 129 write_batch.Put(kDriveIncrementalSyncOriginKeyPrefix + origin.spec(), | |
| 130 RemoveWapiIdPrefix(itr->value().ToString())); | |
| 131 } | |
| 132 | |
| 133 // Strip resource ids of the disabled sync origins. | |
| 134 for (itr->Seek(kDriveDisabledOriginKeyPrefix); | |
| 135 itr->Valid(); itr->Next()) { | |
| 136 std::string key = itr->key().ToString(); | |
| 137 if (!StartsWithASCII(key, kDriveDisabledOriginKeyPrefix, true)) | |
| 138 break; | |
| 139 | |
| 140 GURL origin(std::string( | |
| 141 key.begin() + arraysize(kDriveDisabledOriginKeyPrefix) - 1, | |
| 142 key.end())); | |
| 143 DCHECK(origin.is_valid()); | |
| 144 | |
| 145 write_batch.Put(kDriveDisabledOriginKeyPrefix + origin.spec(), | |
| 146 RemoveWapiIdPrefix(itr->value().ToString())); | |
| 147 } | |
| 148 | |
| 149 return LevelDBStatusToSyncStatusCode( | |
| 150 db->Write(leveldb::WriteOptions(), &write_batch)); | |
| 151 } | |
| 152 | |
| 153 } // namespace drive | |
| 154 } // namespace sync_file_system | |
| OLD | NEW |