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

Side by Side Diff: chrome/browser/sync_file_system/drive_metadata_store.cc

Issue 15808002: SyncFS: Convert WAPI ResourceID to DriveAPI FileID (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: migrate more fields and address comments Created 7 years, 7 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_store.h" 5 #include "chrome/browser/sync_file_system/drive_metadata_store.h"
6 6
7 #include <utility> 7 #include <utility>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/location.h" 13 #include "base/location.h"
14 #include "base/message_loop/message_loop_proxy.h" 14 #include "base/message_loop/message_loop_proxy.h"
15 #include "base/sequenced_task_runner.h" 15 #include "base/sequenced_task_runner.h"
16 #include "base/stl_util.h" 16 #include "base/stl_util.h"
17 #include "base/string_util.h" 17 #include "base/string_util.h"
18 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
19 #include "base/task_runner_util.h" 19 #include "base/task_runner_util.h"
20 #include "chrome/browser/sync_file_system/drive_file_sync_service.h" 20 #include "chrome/browser/sync_file_system/drive_file_sync_service.h"
21 #include "chrome/browser/sync_file_system/drive_file_sync_util.h"
21 #include "chrome/browser/sync_file_system/sync_file_system.pb.h" 22 #include "chrome/browser/sync_file_system/sync_file_system.pb.h"
22 #include "googleurl/src/gurl.h" 23 #include "googleurl/src/gurl.h"
23 #include "third_party/leveldatabase/src/include/leveldb/db.h" 24 #include "third_party/leveldatabase/src/include/leveldb/db.h"
24 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" 25 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
25 #include "webkit/fileapi/file_system_url.h" 26 #include "webkit/fileapi/file_system_url.h"
26 #include "webkit/fileapi/file_system_util.h" 27 #include "webkit/fileapi/file_system_util.h"
27 #include "webkit/fileapi/syncable/syncable_file_system_util.h" 28 #include "webkit/fileapi/syncable/syncable_file_system_util.h"
28 29
29 using fileapi::FileSystemURL; 30 using fileapi::FileSystemURL;
30 31
31 namespace sync_file_system { 32 namespace sync_file_system {
32 33
33 typedef DriveMetadataStore::ResourceIdByOrigin ResourceIdByOrigin; 34 typedef DriveMetadataStore::ResourceIdByOrigin ResourceIdByOrigin;
34 typedef DriveMetadataStore::OriginByResourceId OriginByResourceId; 35 typedef DriveMetadataStore::OriginByResourceId OriginByResourceId;
35 36
36 const base::FilePath::CharType DriveMetadataStore::kDatabaseName[] = 37 const base::FilePath::CharType DriveMetadataStore::kDatabaseName[] =
37 FILE_PATH_LITERAL("DriveMetadata"); 38 FILE_PATH_LITERAL("DriveMetadata");
38 39
39 namespace { 40 namespace {
40 41
41 const char* const kServiceName = DriveFileSyncService::kServiceName; 42 const char* const kServiceName = DriveFileSyncService::kServiceName;
42 const char kDatabaseVersionKey[] = "VERSION"; 43 const char kDatabaseVersionKey[] = "VERSION";
43 const int64 kCurrentDatabaseVersion = 1; 44 const int64 kCurrentDatabaseVersion = 2;
44 const char kChangeStampKey[] = "CHANGE_STAMP"; 45 const char kChangeStampKey[] = "CHANGE_STAMP";
45 const char kSyncRootDirectoryKey[] = "SYNC_ROOT_DIR"; 46 const char kSyncRootDirectoryKey[] = "SYNC_ROOT_DIR";
46 const char kDriveMetadataKeyPrefix[] = "METADATA: "; 47 const char kDriveMetadataKeyPrefix[] = "METADATA: ";
47 const char kMetadataKeySeparator = ' '; 48 const char kMetadataKeySeparator = ' ';
48 const char kDriveBatchSyncOriginKeyPrefix[] = "BSYNC_ORIGIN: "; 49 const char kDriveBatchSyncOriginKeyPrefix[] = "BSYNC_ORIGIN: ";
49 const char kDriveIncrementalSyncOriginKeyPrefix[] = "ISYNC_ORIGIN: "; 50 const char kDriveIncrementalSyncOriginKeyPrefix[] = "ISYNC_ORIGIN: ";
50 const char kDriveDisabledOriginKeyPrefix[] = "DISABLED_ORIGIN: "; 51 const char kDriveDisabledOriginKeyPrefix[] = "DISABLED_ORIGIN: ";
51 const size_t kDriveMetadataKeyPrefixLength = arraysize(kDriveMetadataKeyPrefix); 52 const size_t kDriveMetadataKeyPrefixLength = arraysize(kDriveMetadataKeyPrefix);
52 53
54 const char kWapiFileIdPrefix[] = "file:";
55 const char kWapiFolderIdPrefix[] = "folder:";
56
53 const base::FilePath::CharType kV0FormatPathPrefix[] = 57 const base::FilePath::CharType kV0FormatPathPrefix[] =
54 FILE_PATH_LITERAL("drive/"); 58 FILE_PATH_LITERAL("drive/");
55 59
56 bool ParseV0FormatFileSystemURLString(const GURL& url, 60 bool ParseV0FormatFileSystemURLString(const GURL& url,
57 GURL* origin, 61 GURL* origin,
58 base::FilePath* path) { 62 base::FilePath* path) {
59 fileapi::FileSystemType mount_type; 63 fileapi::FileSystemType mount_type;
60 base::FilePath virtual_path; 64 base::FilePath virtual_path;
61 65
62 if (!fileapi::FileSystemURL::ParseFileSystemSchemeURL( 66 if (!fileapi::FileSystemURL::ParseFileSystemSchemeURL(
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 ResourceIdByOrigin::iterator found = map->find(origin); 102 ResourceIdByOrigin::iterator found = map->find(origin);
99 if (found == map->end()) 103 if (found == map->end())
100 return false; 104 return false;
101 reverse_map->erase(found->second); 105 reverse_map->erase(found->second);
102 reverse_map->insert(std::make_pair(resource_id, origin)); 106 reverse_map->insert(std::make_pair(resource_id, origin));
103 107
104 found->second = resource_id; 108 found->second = resource_id;
105 return true; 109 return true;
106 } 110 }
107 111
112 std::string AddWapiFilePrefix(const std::string& resource_id) {
113 if (resource_id.empty() ||
114 StartsWithASCII(resource_id, kWapiFileIdPrefix, true))
115 return resource_id;
116 return kWapiFileIdPrefix + resource_id;
117 }
118
119 std::string AddWapiFolderPrefix(const std::string& resource_id) {
120 if (resource_id.empty() ||
121 StartsWithASCII(resource_id, kWapiFolderIdPrefix, true))
122 return resource_id;
123 return kWapiFolderIdPrefix + resource_id;
124 }
125
126 std::string AddWapiIdPrefix(const std::string& resource_id,
127 DriveMetadata_ResourceType type) {
128 switch (type) {
129 case DriveMetadata_ResourceType_RESOURCE_TYPE_FILE:
130 return AddWapiFilePrefix(resource_id);
131 case DriveMetadata_ResourceType_RESOURCE_TYPE_FOLDER:
132 return AddWapiFolderPrefix(resource_id);
133 default:
134 NOTREACHED();
135 return resource_id;
136 }
137 }
138
139 std::string RemoveWapiIdPrefix(const std::string& resource_id) {
140 if (StartsWithASCII(resource_id, kWapiFileIdPrefix, true))
141 return std::string(resource_id.begin() + arraysize(kWapiFileIdPrefix) - 1,
142 resource_id.end());
143 if (StartsWithASCII(resource_id, kWapiFolderIdPrefix, true))
144 return std::string(resource_id.begin() + arraysize(kWapiFolderIdPrefix) - 1,
145 resource_id.end());
146 return resource_id;
147 }
148
108 } // namespace 149 } // namespace
109 150
110 class DriveMetadataDB { 151 class DriveMetadataDB {
111 public: 152 public:
112 enum OriginSyncType { 153 enum OriginSyncType {
113 BATCH_SYNC_ORIGIN, 154 BATCH_SYNC_ORIGIN,
114 INCREMENTAL_SYNC_ORIGIN, 155 INCREMENTAL_SYNC_ORIGIN,
115 DISABLED_ORIGIN 156 DISABLED_ORIGIN
116 }; 157 };
117 158
118 typedef DriveMetadataStore::MetadataMap MetadataMap; 159 typedef DriveMetadataStore::MetadataMap MetadataMap;
119 160
120 DriveMetadataDB(const base::FilePath& base_dir, 161 DriveMetadataDB(const base::FilePath& base_dir,
121 base::SequencedTaskRunner* task_runner); 162 base::SequencedTaskRunner* task_runner);
122 ~DriveMetadataDB(); 163 ~DriveMetadataDB();
123 164
124 SyncStatusCode Initialize(bool* created); 165 SyncStatusCode Initialize(bool* created);
125 SyncStatusCode ReadContents(DriveMetadataDBContents* contents); 166 SyncStatusCode ReadContents(DriveMetadataDBContents* contents);
126 167
127 SyncStatusCode MigrateDatabaseIfNeeded(); 168 SyncStatusCode MigrateDatabaseIfNeeded();
128 SyncStatusCode MigrateFromVersion0Database(); 169 SyncStatusCode MigrateFromVersion0To1Database();
170 SyncStatusCode MigrateFromVersion1To2Database();
129 171
130 SyncStatusCode SetLargestChangestamp(int64 largest_changestamp); 172 SyncStatusCode SetLargestChangestamp(int64 largest_changestamp);
131 SyncStatusCode SetSyncRootDirectory(const std::string& resource_id); 173 SyncStatusCode SetSyncRootDirectory(const std::string& resource_id);
132 SyncStatusCode GetSyncRootDirectory(std::string* resource_id); 174 SyncStatusCode GetSyncRootDirectory(std::string* resource_id);
133 SyncStatusCode SetOriginRootDirectory(const GURL& origin, 175 SyncStatusCode SetOriginRootDirectory(const GURL& origin,
134 OriginSyncType sync_type, 176 OriginSyncType sync_type,
135 const std::string& resource_id); 177 const std::string& resource_id);
136 SyncStatusCode UpdateEntry(const FileSystemURL& url, 178 SyncStatusCode UpdateEntry(const FileSystemURL& url,
137 const DriveMetadata& metadata); 179 DriveMetadata metadata);
138 SyncStatusCode DeleteEntry(const FileSystemURL& url); 180 SyncStatusCode DeleteEntry(const FileSystemURL& url);
139 181
140 // TODO(calvinlo): consolidate these state transition functions for sync 182 // TODO(calvinlo): consolidate these state transition functions for sync
141 // origins like "UpdateOrigin(GURL, SyncStatusEnum)". And manage origins in 183 // origins like "UpdateOrigin(GURL, SyncStatusEnum)". And manage origins in
142 // just one map like "Map<SyncStatusEnum, ResourceIDMap>". 184 // just one map like "Map<SyncStatusEnum, ResourceIDMap>".
143 // http://crbug.com/211600 185 // http://crbug.com/211600
144 SyncStatusCode UpdateOriginAsBatchSync(const GURL& origin, 186 SyncStatusCode UpdateOriginAsBatchSync(const GURL& origin,
145 const std::string& resource_id); 187 const std::string& resource_id);
146 SyncStatusCode UpdateOriginAsIncrementalSync(const GURL& origin, 188 SyncStatusCode UpdateOriginAsIncrementalSync(const GURL& origin,
147 const std::string& resource_id); 189 const std::string& resource_id);
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 484
443 if (found->second.erase(url.path()) == 1) { 485 if (found->second.erase(url.path()) == 1) {
444 base::PostTaskAndReplyWithResult( 486 base::PostTaskAndReplyWithResult(
445 file_task_runner_, FROM_HERE, 487 file_task_runner_, FROM_HERE,
446 base::Bind(&DriveMetadataDB::DeleteEntry, 488 base::Bind(&DriveMetadataDB::DeleteEntry,
447 base::Unretained(db_.get()), url), 489 base::Unretained(db_.get()), url),
448 base::Bind(&DriveMetadataStore::UpdateDBStatusAndInvokeCallback, 490 base::Bind(&DriveMetadataStore::UpdateDBStatusAndInvokeCallback,
449 AsWeakPtr(), callback)); 491 AsWeakPtr(), callback));
450 return; 492 return;
451 } 493 }
494
452 base::MessageLoopProxy::current()->PostTask( 495 base::MessageLoopProxy::current()->PostTask(
453 FROM_HERE, 496 FROM_HERE,
454 base::Bind(callback, SYNC_DATABASE_ERROR_NOT_FOUND)); 497 base::Bind(callback, SYNC_DATABASE_ERROR_NOT_FOUND));
455 } 498 }
456 499
457 SyncStatusCode DriveMetadataStore::ReadEntry(const FileSystemURL& url, 500 SyncStatusCode DriveMetadataStore::ReadEntry(const FileSystemURL& url,
458 DriveMetadata* metadata) const { 501 DriveMetadata* metadata) const {
459 DCHECK(CalledOnValidThread()); 502 DCHECK(CalledOnValidThread());
460 DCHECK(metadata); 503 DCHECK(metadata);
461 504
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after
850 DCHECK(success); 893 DCHECK(success);
851 } else if (StartsWithASCII(key, kDriveMetadataKeyPrefix, true)) { 894 } else if (StartsWithASCII(key, kDriveMetadataKeyPrefix, true)) {
852 GURL origin; 895 GURL origin;
853 base::FilePath path; 896 base::FilePath path;
854 MetadataKeyToOriginAndPath(key, &origin, &path); 897 MetadataKeyToOriginAndPath(key, &origin, &path);
855 898
856 DriveMetadata metadata; 899 DriveMetadata metadata;
857 bool success = metadata.ParseFromString(itr->value().ToString()); 900 bool success = metadata.ParseFromString(itr->value().ToString());
858 DCHECK(success); 901 DCHECK(success);
859 902
903 if (!IsDriveAPIEnabled()) {
904 metadata.set_resource_id(
905 AddWapiIdPrefix(metadata.resource_id(), metadata.type()));
906 }
907
860 success = contents->metadata_map[origin].insert( 908 success = contents->metadata_map[origin].insert(
861 std::make_pair(path, metadata)).second; 909 std::make_pair(path, metadata)).second;
862 DCHECK(success); 910 DCHECK(success);
863 } 911 }
864 } 912 }
865 913
866 SyncStatusCode status = GetOrigins(&contents->batch_sync_origins, 914 SyncStatusCode status = GetOrigins(&contents->batch_sync_origins,
867 &contents->incremental_sync_origins, 915 &contents->incremental_sync_origins,
868 &contents->disabled_origins); 916 &contents->disabled_origins);
869 if (status != SYNC_STATUS_OK && 917 if (status != SYNC_STATUS_OK &&
(...skipping 17 matching lines...) Expand all
887 bool success = base::StringToInt64(itr->value().ToString(), 935 bool success = base::StringToInt64(itr->value().ToString(),
888 &database_version); 936 &database_version);
889 if (!success) 937 if (!success)
890 return SYNC_DATABASE_ERROR_FAILED; 938 return SYNC_DATABASE_ERROR_FAILED;
891 if (database_version > kCurrentDatabaseVersion) 939 if (database_version > kCurrentDatabaseVersion)
892 return SYNC_DATABASE_ERROR_FAILED; 940 return SYNC_DATABASE_ERROR_FAILED;
893 if (database_version == kCurrentDatabaseVersion) 941 if (database_version == kCurrentDatabaseVersion)
894 return SYNC_STATUS_OK; 942 return SYNC_STATUS_OK;
895 } 943 }
896 944
897 if (database_version == 0) { 945 switch (database_version) {
898 MigrateFromVersion0Database(); 946 case 0:
899 return SYNC_STATUS_OK; 947 MigrateFromVersion0To1Database();
948 // fall-through
949 case 1:
950 MigrateFromVersion1To2Database();
951 return SYNC_STATUS_OK;
900 } 952 }
901 return SYNC_DATABASE_ERROR_FAILED; 953 return SYNC_DATABASE_ERROR_FAILED;
902 } 954 }
903 955
904 SyncStatusCode DriveMetadataDB::MigrateFromVersion0Database() { 956 SyncStatusCode DriveMetadataDB::MigrateFromVersion0To1Database() {
905 // Version 0 database format: 957 // Version 0 database format:
906 // key: "CHANGE_STAMP" 958 // key: "CHANGE_STAMP"
907 // value: <Largest Changestamp> 959 // value: <Largest Changestamp>
908 // 960 //
909 // key: "SYNC_ROOT_DIR" 961 // key: "SYNC_ROOT_DIR"
910 // value: <Resource ID of the sync root directory> 962 // value: <Resource ID of the sync root directory>
911 // 963 //
912 // key: "METADATA: " + 964 // key: "METADATA: " +
913 // <FileSystemURL serialized by SerializeSyncableFileSystemURL> 965 // <FileSystemURL serialized by SerializeSyncableFileSystemURL>
914 // value: <Serialized DriveMetadata> 966 // value: <Serialized DriveMetadata>
(...skipping 20 matching lines...) Expand all
935 // key: "BSYNC_ORIGIN: " + <URL string of a batch sync origin> 987 // key: "BSYNC_ORIGIN: " + <URL string of a batch sync origin>
936 // value: <Resource ID of the drive directory for the origin> 988 // value: <Resource ID of the drive directory for the origin>
937 // 989 //
938 // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin> 990 // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin>
939 // value: <Resource ID of the drive directory for the origin> 991 // value: <Resource ID of the drive directory for the origin>
940 // 992 //
941 // key: "DISABLED_ORIGIN: " + <URL string of a disabled origin> 993 // key: "DISABLED_ORIGIN: " + <URL string of a disabled origin>
942 // value: <Resource ID of the drive directory for the origin> 994 // value: <Resource ID of the drive directory for the origin>
943 995
944 leveldb::WriteBatch write_batch; 996 leveldb::WriteBatch write_batch;
945 write_batch.Put(kDatabaseVersionKey, 997 write_batch.Put(kDatabaseVersionKey, "1");
946 base::Int64ToString(kCurrentDatabaseVersion));
947 998
948 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); 999 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions()));
949 for (itr->Seek(kDriveMetadataKeyPrefix); itr->Valid(); itr->Next()) { 1000 for (itr->Seek(kDriveMetadataKeyPrefix); itr->Valid(); itr->Next()) {
950 std::string key = itr->key().ToString(); 1001 std::string key = itr->key().ToString();
951 if (!StartsWithASCII(key, kDriveMetadataKeyPrefix, true)) 1002 if (!StartsWithASCII(key, kDriveMetadataKeyPrefix, true))
952 break; 1003 break;
953 std::string serialized_url( 1004 std::string serialized_url(
954 key.begin() + kDriveMetadataKeyPrefixLength - 1, key.end()); 1005 key.begin() + kDriveMetadataKeyPrefixLength - 1, key.end());
955 1006
956 GURL origin; 1007 GURL origin;
957 base::FilePath path; 1008 base::FilePath path;
958 bool success = ParseV0FormatFileSystemURLString( 1009 bool success = ParseV0FormatFileSystemURLString(
959 GURL(serialized_url), &origin, &path); 1010 GURL(serialized_url), &origin, &path);
960 DCHECK(success) << serialized_url; 1011 DCHECK(success) << serialized_url;
961 std::string new_key = kDriveMetadataKeyPrefix + origin.spec() + 1012 std::string new_key = kDriveMetadataKeyPrefix + origin.spec() +
962 kMetadataKeySeparator + path.AsUTF8Unsafe(); 1013 kMetadataKeySeparator + path.AsUTF8Unsafe();
963 1014
964 write_batch.Put(new_key, itr->value()); 1015 write_batch.Put(new_key, itr->value());
965 write_batch.Delete(key); 1016 write_batch.Delete(key);
966 } 1017 }
967 return LevelDBStatusToSyncStatusCode( 1018 return LevelDBStatusToSyncStatusCode(
968 db_->Write(leveldb::WriteOptions(), &write_batch)); 1019 db_->Write(leveldb::WriteOptions(), &write_batch));
969 } 1020 }
970 1021
1022 SyncStatusCode DriveMetadataDB::MigrateFromVersion1To2Database() {
1023 // Strips prefix of WAPI resource ID.
1024 // (i.e. "file:xxxx" => "xxxx", "folder:yyyy" => "yyyy")
1025 //
1026 // Version 2 database format (changed keys/fields are marked with '*'):
1027 // key: "VERSION"
1028 // * value: 2
1029 //
1030 // key: "CHANGE_STAMP"
1031 // value: <Largest Changestamp>
1032 //
1033 // key: "SYNC_ROOT_DIR"
1034 // * value: <Resource ID of the sync root directory> (striped)
1035 //
1036 // key: "METADATA: " + <Origin and URL>
1037 // * value: <Serialized DriveMetadata> (striped)
1038 //
1039 // key: "BSYNC_ORIGIN: " + <URL string of a batch sync origin>
1040 // * value: <Resource ID of the drive directory for the origin> (striped)
1041 //
1042 // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin>
1043 // * value: <Resource ID of the drive directory for the origin> (striped)
1044 //
1045 // key: "DISABLED_ORIGIN: " + <URL string of a disabled origin>
1046 // * value: <Resource ID of the drive directory for the origin> (striped)
1047
1048 leveldb::WriteBatch write_batch;
1049 write_batch.Put(kDatabaseVersionKey, "2");
1050
1051 // Strip resource id for the sync root directory.
1052 std::string sync_root_resource_id;
1053 leveldb::Status status = db_->Get(
1054 leveldb::ReadOptions(), kSyncRootDirectoryKey, &sync_root_resource_id);
1055 write_batch.Put(kSyncRootDirectoryKey,
1056 RemoveWapiIdPrefix(sync_root_resource_id));
1057
1058 // Strip resource ids in the drive metadata.
1059 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions()));
1060 for (itr->Seek(kDriveMetadataKeyPrefix); itr->Valid(); itr->Next()) {
1061 std::string key = itr->key().ToString();
1062 if (!StartsWithASCII(key, kDriveMetadataKeyPrefix, true))
1063 break;
1064
1065 DriveMetadata metadata;
1066 bool success = metadata.ParseFromString(itr->value().ToString());
1067 DCHECK(success);
1068
1069 metadata.set_resource_id(RemoveWapiIdPrefix(metadata.resource_id()));
1070
1071 write_batch.Put(key, itr->value());
1072 }
1073
1074 // Strip resource ids of the batch sync origins.
1075 for (itr->Seek(kDriveBatchSyncOriginKeyPrefix);
1076 itr->Valid(); itr->Next()) {
1077 std::string key = itr->key().ToString();
1078 if (!StartsWithASCII(key, kDriveBatchSyncOriginKeyPrefix, true))
1079 break;
1080
1081 GURL origin(std::string(
1082 key.begin() + arraysize(kDriveBatchSyncOriginKeyPrefix) - 1,
1083 key.end()));
1084 DCHECK(origin.is_valid());
1085
1086 write_batch.Put(CreateKeyForOriginRoot(origin, BATCH_SYNC_ORIGIN),
1087 RemoveWapiIdPrefix(itr->value().ToString()));
1088 }
1089
1090 // Strip resource ids of the incremental sync origins.
1091 for (itr->Seek(kDriveIncrementalSyncOriginKeyPrefix);
1092 itr->Valid(); itr->Next()) {
1093 std::string key = itr->key().ToString();
1094 if (!StartsWithASCII(key, kDriveIncrementalSyncOriginKeyPrefix, true))
1095 break;
1096
1097 GURL origin(std::string(
1098 key.begin() + arraysize(kDriveIncrementalSyncOriginKeyPrefix) - 1,
1099 key.end()));
1100 DCHECK(origin.is_valid());
1101
1102 write_batch.Put(CreateKeyForOriginRoot(origin, INCREMENTAL_SYNC_ORIGIN),
1103 RemoveWapiIdPrefix(itr->value().ToString()));
1104 }
1105
1106 // Strip resource ids of the disabled sync origins.
1107 for (itr->Seek(kDriveDisabledOriginKeyPrefix);
1108 itr->Valid(); itr->Next()) {
1109 std::string key = itr->key().ToString();
1110 if (!StartsWithASCII(key, kDriveDisabledOriginKeyPrefix, true))
1111 break;
1112
1113 GURL origin(std::string(
1114 key.begin() + arraysize(kDriveDisabledOriginKeyPrefix) - 1,
1115 key.end()));
1116 DCHECK(origin.is_valid());
1117
1118 write_batch.Put(CreateKeyForOriginRoot(origin, DISABLED_ORIGIN),
1119 RemoveWapiIdPrefix(itr->value().ToString()));
1120 }
1121
1122 return LevelDBStatusToSyncStatusCode(
1123 db_->Write(leveldb::WriteOptions(), &write_batch));
1124 }
1125
971 SyncStatusCode DriveMetadataDB::SetLargestChangestamp( 1126 SyncStatusCode DriveMetadataDB::SetLargestChangestamp(
972 int64 largest_changestamp) { 1127 int64 largest_changestamp) {
973 DCHECK(CalledOnValidThread()); 1128 DCHECK(CalledOnValidThread());
974 DCHECK(db_.get()); 1129 DCHECK(db_.get());
975 1130
976 leveldb::Status status = db_->Put( 1131 leveldb::Status status = db_->Put(
977 leveldb::WriteOptions(), 1132 leveldb::WriteOptions(),
978 kChangeStampKey, base::Int64ToString(largest_changestamp)); 1133 kChangeStampKey, base::Int64ToString(largest_changestamp));
979 return LevelDBStatusToSyncStatusCode(status); 1134 return LevelDBStatusToSyncStatusCode(status);
980 } 1135 }
981 1136
982 SyncStatusCode DriveMetadataDB::SetSyncRootDirectory( 1137 SyncStatusCode DriveMetadataDB::SetSyncRootDirectory(
983 const std::string& resource_id) { 1138 const std::string& resource_id) {
984 DCHECK(CalledOnValidThread()); 1139 DCHECK(CalledOnValidThread());
985 DCHECK(db_.get()); 1140 DCHECK(db_.get());
986 1141
987 leveldb::Status status = db_->Put( 1142 leveldb::Status status = db_->Put(leveldb::WriteOptions(),
988 leveldb::WriteOptions(), kSyncRootDirectoryKey, resource_id); 1143 kSyncRootDirectoryKey,
1144 RemoveWapiIdPrefix(resource_id));
989 return LevelDBStatusToSyncStatusCode(status); 1145 return LevelDBStatusToSyncStatusCode(status);
990 } 1146 }
991 1147
992 SyncStatusCode DriveMetadataDB::SetOriginRootDirectory( 1148 SyncStatusCode DriveMetadataDB::SetOriginRootDirectory(
993 const GURL& origin, 1149 const GURL& origin,
994 OriginSyncType sync_type, 1150 OriginSyncType sync_type,
995 const std::string& resource_id) { 1151 const std::string& resource_id) {
996 DCHECK(CalledOnValidThread()); 1152 DCHECK(CalledOnValidThread());
997 DCHECK(db_.get()); 1153 DCHECK(db_.get());
998 1154
999 std::string key = CreateKeyForOriginRoot(origin, sync_type); 1155 std::string key = CreateKeyForOriginRoot(origin, sync_type);
1000 if (key.empty()) 1156 if (key.empty())
1001 return SYNC_DATABASE_ERROR_FAILED; 1157 return SYNC_DATABASE_ERROR_FAILED;
1002 1158
1003 leveldb::Status status = db_->Put(leveldb::WriteOptions(), key, resource_id); 1159 leveldb::Status status = db_->Put(
1160 leveldb::WriteOptions(), key, RemoveWapiIdPrefix(resource_id));
1004 return LevelDBStatusToSyncStatusCode(status); 1161 return LevelDBStatusToSyncStatusCode(status);
1005 } 1162 }
1006 1163
1007 SyncStatusCode DriveMetadataDB::GetSyncRootDirectory(std::string* resource_id) { 1164 SyncStatusCode DriveMetadataDB::GetSyncRootDirectory(std::string* resource_id) {
1008 DCHECK(CalledOnValidThread()); 1165 DCHECK(CalledOnValidThread());
1009 DCHECK(db_.get()); 1166 DCHECK(db_.get());
1010 1167
1011 leveldb::Status status = db_->Get( 1168 leveldb::Status status = db_->Get(
1012 leveldb::ReadOptions(), kSyncRootDirectoryKey, resource_id); 1169 leveldb::ReadOptions(), kSyncRootDirectoryKey, resource_id);
1170
1171 if (!IsDriveAPIEnabled() && status.ok())
1172 *resource_id = AddWapiFolderPrefix(*resource_id);
1173
1013 return LevelDBStatusToSyncStatusCode(status); 1174 return LevelDBStatusToSyncStatusCode(status);
1014 } 1175 }
1015 1176
1016 SyncStatusCode DriveMetadataDB::UpdateEntry(const FileSystemURL& url, 1177 SyncStatusCode DriveMetadataDB::UpdateEntry(const FileSystemURL& url,
1017 const DriveMetadata& metadata) { 1178 DriveMetadata metadata) {
1018 DCHECK(CalledOnValidThread()); 1179 DCHECK(CalledOnValidThread());
1019 DCHECK(db_.get()); 1180 DCHECK(db_.get());
1020 1181
1182 if (!IsDriveAPIEnabled())
1183 metadata.set_resource_id(RemoveWapiIdPrefix(metadata.resource_id()));
1184
1021 std::string metadata_key = FileSystemURLToMetadataKey(url); 1185 std::string metadata_key = FileSystemURLToMetadataKey(url);
1022 std::string value; 1186 std::string value;
1023 bool success = metadata.SerializeToString(&value); 1187 bool success = metadata.SerializeToString(&value);
1024 DCHECK(success); 1188 DCHECK(success);
1025 leveldb::Status status = db_->Put( 1189 leveldb::Status status = db_->Put(
1026 leveldb::WriteOptions(), metadata_key, value); 1190 leveldb::WriteOptions(), metadata_key, value);
1027 1191
1028 return LevelDBStatusToSyncStatusCode(status); 1192 return LevelDBStatusToSyncStatusCode(status);
1029 } 1193 }
1030 1194
1031 SyncStatusCode DriveMetadataDB::DeleteEntry(const FileSystemURL& url) { 1195 SyncStatusCode DriveMetadataDB::DeleteEntry(const FileSystemURL& url) {
1032 DCHECK(CalledOnValidThread()); 1196 DCHECK(CalledOnValidThread());
1033 DCHECK(db_.get()); 1197 DCHECK(db_.get());
1034 1198
1035 std::string metadata_key = FileSystemURLToMetadataKey(url); 1199 std::string metadata_key = FileSystemURLToMetadataKey(url);
1036 leveldb::Status status = db_->Delete( 1200 leveldb::Status status = db_->Delete(
1037 leveldb::WriteOptions(), metadata_key); 1201 leveldb::WriteOptions(), metadata_key);
1038 return LevelDBStatusToSyncStatusCode(status); 1202 return LevelDBStatusToSyncStatusCode(status);
1039 } 1203 }
1040 1204
1041 SyncStatusCode DriveMetadataDB::UpdateOriginAsBatchSync( 1205 SyncStatusCode DriveMetadataDB::UpdateOriginAsBatchSync(
1042 const GURL& origin, const std::string& resource_id) { 1206 const GURL& origin, const std::string& resource_id) {
1043 DCHECK(CalledOnValidThread()); 1207 DCHECK(CalledOnValidThread());
1044 DCHECK(db_.get()); 1208 DCHECK(db_.get());
1045 1209
1046 leveldb::Status status = db_->Put( 1210 leveldb::Status status = db_->Put(
1047 leveldb::WriteOptions(), 1211 leveldb::WriteOptions(),
1048 CreateKeyForOriginRoot(origin, BATCH_SYNC_ORIGIN), 1212 CreateKeyForOriginRoot(origin, BATCH_SYNC_ORIGIN),
1049 resource_id); 1213 RemoveWapiIdPrefix(resource_id));
1050 return LevelDBStatusToSyncStatusCode(status); 1214 return LevelDBStatusToSyncStatusCode(status);
1051 } 1215 }
1052 1216
1053 SyncStatusCode DriveMetadataDB::UpdateOriginAsIncrementalSync( 1217 SyncStatusCode DriveMetadataDB::UpdateOriginAsIncrementalSync(
1054 const GURL& origin, const std::string& resource_id) { 1218 const GURL& origin, const std::string& resource_id) {
1055 DCHECK(CalledOnValidThread()); 1219 DCHECK(CalledOnValidThread());
1056 DCHECK(db_.get()); 1220 DCHECK(db_.get());
1057 1221
1058 leveldb::WriteBatch batch; 1222 leveldb::WriteBatch batch;
1059 batch.Delete(CreateKeyForOriginRoot(origin, BATCH_SYNC_ORIGIN)); 1223 batch.Delete(CreateKeyForOriginRoot(origin, BATCH_SYNC_ORIGIN));
1060 batch.Delete(CreateKeyForOriginRoot(origin, DISABLED_ORIGIN)); 1224 batch.Delete(CreateKeyForOriginRoot(origin, DISABLED_ORIGIN));
1061 batch.Put(CreateKeyForOriginRoot(origin, INCREMENTAL_SYNC_ORIGIN), 1225 batch.Put(CreateKeyForOriginRoot(origin, INCREMENTAL_SYNC_ORIGIN),
1062 resource_id); 1226 RemoveWapiIdPrefix(resource_id));
1063 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); 1227 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch);
1064 1228
1065 return LevelDBStatusToSyncStatusCode(status); 1229 return LevelDBStatusToSyncStatusCode(status);
1066 } 1230 }
1067 1231
1068 SyncStatusCode DriveMetadataDB::EnableOrigin( 1232 SyncStatusCode DriveMetadataDB::EnableOrigin(
1069 const GURL& origin, const std::string& resource_id) { 1233 const GURL& origin, const std::string& resource_id) {
1070 DCHECK(CalledOnValidThread()); 1234 DCHECK(CalledOnValidThread());
1071 DCHECK(db_.get()); 1235 DCHECK(db_.get());
1072 1236
1073 leveldb::WriteBatch batch; 1237 leveldb::WriteBatch batch;
1074 batch.Delete(CreateKeyForOriginRoot(origin, INCREMENTAL_SYNC_ORIGIN)); 1238 batch.Delete(CreateKeyForOriginRoot(origin, INCREMENTAL_SYNC_ORIGIN));
1075 batch.Delete(CreateKeyForOriginRoot(origin, DISABLED_ORIGIN)); 1239 batch.Delete(CreateKeyForOriginRoot(origin, DISABLED_ORIGIN));
1076 batch.Put(CreateKeyForOriginRoot(origin, BATCH_SYNC_ORIGIN), resource_id); 1240 batch.Put(CreateKeyForOriginRoot(origin, BATCH_SYNC_ORIGIN),
1241 RemoveWapiIdPrefix(resource_id));
1077 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); 1242 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch);
1078 1243
1079 return LevelDBStatusToSyncStatusCode(status); 1244 return LevelDBStatusToSyncStatusCode(status);
1080 } 1245 }
1081 1246
1082 SyncStatusCode DriveMetadataDB::DisableOrigin( 1247 SyncStatusCode DriveMetadataDB::DisableOrigin(
1083 const GURL& origin_to_disable, const std::string& resource_id) { 1248 const GURL& origin_to_disable, const std::string& resource_id) {
1084 DCHECK(CalledOnValidThread()); 1249 DCHECK(CalledOnValidThread());
1085 DCHECK(db_.get()); 1250 DCHECK(db_.get());
1086 1251
1087 leveldb::WriteBatch batch; 1252 leveldb::WriteBatch batch;
1088 batch.Delete(CreateKeyForOriginRoot(origin_to_disable, BATCH_SYNC_ORIGIN)); 1253 batch.Delete(CreateKeyForOriginRoot(origin_to_disable, BATCH_SYNC_ORIGIN));
1089 batch.Delete(CreateKeyForOriginRoot(origin_to_disable, 1254 batch.Delete(CreateKeyForOriginRoot(origin_to_disable,
1090 INCREMENTAL_SYNC_ORIGIN)); 1255 INCREMENTAL_SYNC_ORIGIN));
1091 batch.Put(CreateKeyForOriginRoot(origin_to_disable, DISABLED_ORIGIN), 1256 batch.Put(CreateKeyForOriginRoot(origin_to_disable, DISABLED_ORIGIN),
1092 resource_id); 1257 RemoveWapiIdPrefix(resource_id));
1093 1258
1094 // Remove entries specified by |origin_to_disable|. 1259 // Remove entries specified by |origin_to_disable|.
1095 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); 1260 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions()));
1096 std::string metadata_key = kDriveMetadataKeyPrefix + origin_to_disable.spec(); 1261 std::string metadata_key = kDriveMetadataKeyPrefix + origin_to_disable.spec();
1097 for (itr->Seek(metadata_key); itr->Valid(); itr->Next()) { 1262 for (itr->Seek(metadata_key); itr->Valid(); itr->Next()) {
1098 std::string key = itr->key().ToString(); 1263 std::string key = itr->key().ToString();
1099 if (!StartsWithASCII(key, kDriveMetadataKeyPrefix, true)) 1264 if (!StartsWithASCII(key, kDriveMetadataKeyPrefix, true))
1100 break; 1265 break;
1101 GURL origin; 1266 GURL origin;
1102 base::FilePath path; 1267 base::FilePath path;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1149 // Get batch sync origins from the DB. 1314 // Get batch sync origins from the DB.
1150 for (itr->Seek(kDriveBatchSyncOriginKeyPrefix); 1315 for (itr->Seek(kDriveBatchSyncOriginKeyPrefix);
1151 itr->Valid(); itr->Next()) { 1316 itr->Valid(); itr->Next()) {
1152 std::string key = itr->key().ToString(); 1317 std::string key = itr->key().ToString();
1153 if (!StartsWithASCII(key, kDriveBatchSyncOriginKeyPrefix, true)) 1318 if (!StartsWithASCII(key, kDriveBatchSyncOriginKeyPrefix, true))
1154 break; 1319 break;
1155 GURL origin(std::string( 1320 GURL origin(std::string(
1156 key.begin() + arraysize(kDriveBatchSyncOriginKeyPrefix) - 1, 1321 key.begin() + arraysize(kDriveBatchSyncOriginKeyPrefix) - 1,
1157 key.end())); 1322 key.end()));
1158 DCHECK(origin.is_valid()); 1323 DCHECK(origin.is_valid());
1324
1325 std::string origin_resource_id =
1326 IsDriveAPIEnabled() ? itr->value().ToString()
1327 : AddWapiFolderPrefix(itr->value().ToString());
1328
1159 bool result = batch_sync_origins->insert( 1329 bool result = batch_sync_origins->insert(
1160 std::make_pair(origin, itr->value().ToString())).second; 1330 std::make_pair(origin, origin_resource_id)).second;
1161 DCHECK(result); 1331 DCHECK(result);
1162 } 1332 }
1163 1333
1164 // Get incremental sync origins from the DB. 1334 // Get incremental sync origins from the DB.
1165 for (itr->Seek(kDriveIncrementalSyncOriginKeyPrefix); 1335 for (itr->Seek(kDriveIncrementalSyncOriginKeyPrefix);
1166 itr->Valid(); itr->Next()) { 1336 itr->Valid(); itr->Next()) {
1167 std::string key = itr->key().ToString(); 1337 std::string key = itr->key().ToString();
1168 if (!StartsWithASCII(key, kDriveIncrementalSyncOriginKeyPrefix, true)) 1338 if (!StartsWithASCII(key, kDriveIncrementalSyncOriginKeyPrefix, true))
1169 break; 1339 break;
1170 GURL origin(std::string( 1340 GURL origin(std::string(
1171 key.begin() + arraysize(kDriveIncrementalSyncOriginKeyPrefix) - 1, 1341 key.begin() + arraysize(kDriveIncrementalSyncOriginKeyPrefix) - 1,
1172 key.end())); 1342 key.end()));
1173 DCHECK(origin.is_valid()); 1343 DCHECK(origin.is_valid());
1344
1345 std::string origin_resource_id =
1346 IsDriveAPIEnabled() ? itr->value().ToString()
1347 : AddWapiFolderPrefix(itr->value().ToString());
1348
1174 bool result = incremental_sync_origins->insert( 1349 bool result = incremental_sync_origins->insert(
1175 std::make_pair(origin, itr->value().ToString())).second; 1350 std::make_pair(origin, origin_resource_id)).second;
1176 DCHECK(result); 1351 DCHECK(result);
1177 } 1352 }
1178 1353
1179 // Get disabled origins from the DB. 1354 // Get disabled origins from the DB.
1180 for (itr->Seek(kDriveDisabledOriginKeyPrefix); 1355 for (itr->Seek(kDriveDisabledOriginKeyPrefix);
1181 itr->Valid(); itr->Next()) { 1356 itr->Valid(); itr->Next()) {
1182 std::string key = itr->key().ToString(); 1357 std::string key = itr->key().ToString();
1183 if (!StartsWithASCII(key, kDriveDisabledOriginKeyPrefix, true)) 1358 if (!StartsWithASCII(key, kDriveDisabledOriginKeyPrefix, true))
1184 break; 1359 break;
1185 GURL origin(std::string( 1360 GURL origin(std::string(
1186 key.begin() + arraysize(kDriveDisabledOriginKeyPrefix) - 1, 1361 key.begin() + arraysize(kDriveDisabledOriginKeyPrefix) - 1,
1187 key.end())); 1362 key.end()));
1188 DCHECK(origin.is_valid()); 1363 DCHECK(origin.is_valid());
1364
1365 std::string origin_resource_id =
1366 IsDriveAPIEnabled() ? itr->value().ToString()
1367 : AddWapiFolderPrefix(itr->value().ToString());
1368
1189 bool result = disabled_origins->insert( 1369 bool result = disabled_origins->insert(
1190 std::make_pair(origin, itr->value().ToString())).second; 1370 std::make_pair(origin, origin_resource_id)).second;
1191 DCHECK(result); 1371 DCHECK(result);
1192 } 1372 }
1193 1373
1194 return SYNC_STATUS_OK; 1374 return SYNC_STATUS_OK;
1195 } 1375 }
1196 1376
1197 } // namespace sync_file_system 1377 } // namespace sync_file_system
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698