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

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: 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 MigrateFromVersion0Database();
170 SyncStatusCode MigrateFromVersion1Database();
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 MigrateFromVersion0Database();
948 // fall-through
949 case 1:
950 MigrateFromVersion1Database();
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::MigrateFromVersion0Database() {
kinuko 2013/05/27 08:25:16 Can we rename this: MigrateFromVersion0To1Database
nhiroki 2013/05/27 09:13:03 Done.
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::MigrateFromVersion1Database() {
kinuko 2013/05/27 08:25:16 Can we rename this: MigrateFromVersion1To2Database
nhiroki 2013/05/27 09:13:03 Done.
1023 // Version 1 database format (changed keys/fields are marked with '*'):
1024 // key: "VERSION" (new)
tzik 2013/05/27 08:04:42 drop (new)?
nhiroki 2013/05/27 09:13:03 Done.
1025 // value: 1
1026 //
1027 // key: "CHANGE_STAMP"
1028 // value: <Largest Changestamp>
1029 //
1030 // key: "SYNC_ROOT_DIR"
1031 // value: <Resource ID of the sync root directory>
1032 //
1033 // key: "METADATA: " + <Origin and URL> (changed)
tzik 2013/05/27 08:04:42 drop (changed)?
nhiroki 2013/05/27 09:13:03 Done.
1034 // value: <Serialized DriveMetadata>
1035 //
1036 // key: "BSYNC_ORIGIN: " + <URL string of a batch sync origin>
1037 // value: <Resource ID of the drive directory for the origin>
1038 //
1039 // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin>
1040 // value: <Resource ID of the drive directory for the origin>
1041 //
1042 // key: "DISABLED_ORIGIN: " + <URL string of a disabled origin>
1043 // value: <Resource ID of the drive directory for the origin>
1044 //
kinuko 2013/05/27 08:25:16 I think we could drop comments for Version 1 here?
kinuko 2013/05/27 08:26:57 I mean, because we have the same text right above
nhiroki 2013/05/27 09:13:03 Done.
1045 // Version 2 database format (changed keys/fields are marked with '*'):
1046 // key: "VERSION"
1047 // * value: 2
1048 //
1049 // key: "CHANGE_STAMP"
1050 // value: <Largest Changestamp>
1051 //
1052 // key: "SYNC_ROOT_DIR"
1053 // value: <Resource ID of the sync root directory>
1054 //
1055 // key: "METADATA: " + <Origin and URL>
1056 // * value: <Serialized DriveMetadata> (Prefix of WAPI resource ID is striped)
1057 // (i.e. "file:xxxx" => "xxxx", "folder:yyyy" => "yyyy")
1058 //
1059 // key: "BSYNC_ORIGIN: " + <URL string of a batch sync origin>
1060 // value: <Resource ID of the drive directory for the origin>
1061 //
1062 // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin>
1063 // value: <Resource ID of the drive directory for the origin>
1064 //
1065 // key: "DISABLED_ORIGIN: " + <URL string of a disabled origin>
1066 // value: <Resource ID of the drive directory for the origin>
1067
1068 leveldb::WriteBatch write_batch;
1069 write_batch.Put(kDatabaseVersionKey, "2");
1070
1071 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions()));
1072 for (itr->Seek(kDriveMetadataKeyPrefix); itr->Valid(); itr->Next()) {
1073 std::string key = itr->key().ToString();
1074 if (!StartsWithASCII(key, kDriveMetadataKeyPrefix, true))
1075 break;
1076
1077 DriveMetadata metadata;
1078 bool success = metadata.ParseFromString(itr->value().ToString());
1079 DCHECK(success);
1080
1081 metadata.set_resource_id(RemoveWapiIdPrefix(metadata.resource_id()));
1082
1083 write_batch.Put(key, itr->value());
1084 }
1085 return LevelDBStatusToSyncStatusCode(
1086 db_->Write(leveldb::WriteOptions(), &write_batch));
1087 }
1088
971 SyncStatusCode DriveMetadataDB::SetLargestChangestamp( 1089 SyncStatusCode DriveMetadataDB::SetLargestChangestamp(
972 int64 largest_changestamp) { 1090 int64 largest_changestamp) {
973 DCHECK(CalledOnValidThread()); 1091 DCHECK(CalledOnValidThread());
974 DCHECK(db_.get()); 1092 DCHECK(db_.get());
975 1093
976 leveldb::Status status = db_->Put( 1094 leveldb::Status status = db_->Put(
977 leveldb::WriteOptions(), 1095 leveldb::WriteOptions(),
978 kChangeStampKey, base::Int64ToString(largest_changestamp)); 1096 kChangeStampKey, base::Int64ToString(largest_changestamp));
979 return LevelDBStatusToSyncStatusCode(status); 1097 return LevelDBStatusToSyncStatusCode(status);
980 } 1098 }
981 1099
982 SyncStatusCode DriveMetadataDB::SetSyncRootDirectory( 1100 SyncStatusCode DriveMetadataDB::SetSyncRootDirectory(
983 const std::string& resource_id) { 1101 const std::string& resource_id) {
984 DCHECK(CalledOnValidThread()); 1102 DCHECK(CalledOnValidThread());
985 DCHECK(db_.get()); 1103 DCHECK(db_.get());
986 1104
987 leveldb::Status status = db_->Put( 1105 leveldb::Status status = db_->Put(leveldb::WriteOptions(),
988 leveldb::WriteOptions(), kSyncRootDirectoryKey, resource_id); 1106 kSyncRootDirectoryKey,
1107 RemoveWapiIdPrefix(resource_id));
989 return LevelDBStatusToSyncStatusCode(status); 1108 return LevelDBStatusToSyncStatusCode(status);
990 } 1109 }
991 1110
992 SyncStatusCode DriveMetadataDB::SetOriginRootDirectory( 1111 SyncStatusCode DriveMetadataDB::SetOriginRootDirectory(
993 const GURL& origin, 1112 const GURL& origin,
994 OriginSyncType sync_type, 1113 OriginSyncType sync_type,
995 const std::string& resource_id) { 1114 const std::string& resource_id) {
996 DCHECK(CalledOnValidThread()); 1115 DCHECK(CalledOnValidThread());
997 DCHECK(db_.get()); 1116 DCHECK(db_.get());
998 1117
999 std::string key = CreateKeyForOriginRoot(origin, sync_type); 1118 std::string key = CreateKeyForOriginRoot(origin, sync_type);
1000 if (key.empty()) 1119 if (key.empty())
1001 return SYNC_DATABASE_ERROR_FAILED; 1120 return SYNC_DATABASE_ERROR_FAILED;
1002 1121
1003 leveldb::Status status = db_->Put(leveldb::WriteOptions(), key, resource_id); 1122 leveldb::Status status = db_->Put(
1123 leveldb::WriteOptions(), key, RemoveWapiIdPrefix(resource_id));
1004 return LevelDBStatusToSyncStatusCode(status); 1124 return LevelDBStatusToSyncStatusCode(status);
1005 } 1125 }
1006 1126
1007 SyncStatusCode DriveMetadataDB::GetSyncRootDirectory(std::string* resource_id) { 1127 SyncStatusCode DriveMetadataDB::GetSyncRootDirectory(std::string* resource_id) {
1008 DCHECK(CalledOnValidThread()); 1128 DCHECK(CalledOnValidThread());
1009 DCHECK(db_.get()); 1129 DCHECK(db_.get());
1010 1130
1011 leveldb::Status status = db_->Get( 1131 leveldb::Status status = db_->Get(
1012 leveldb::ReadOptions(), kSyncRootDirectoryKey, resource_id); 1132 leveldb::ReadOptions(), kSyncRootDirectoryKey, resource_id);
1133
1134 if (!IsDriveAPIEnabled() && status.ok())
1135 *resource_id = AddWapiFolderPrefix(*resource_id);
1136
1013 return LevelDBStatusToSyncStatusCode(status); 1137 return LevelDBStatusToSyncStatusCode(status);
1014 } 1138 }
1015 1139
1016 SyncStatusCode DriveMetadataDB::UpdateEntry(const FileSystemURL& url, 1140 SyncStatusCode DriveMetadataDB::UpdateEntry(const FileSystemURL& url,
1017 const DriveMetadata& metadata) { 1141 DriveMetadata metadata) {
1018 DCHECK(CalledOnValidThread()); 1142 DCHECK(CalledOnValidThread());
1019 DCHECK(db_.get()); 1143 DCHECK(db_.get());
1020 1144
1145 if (!IsDriveAPIEnabled())
1146 metadata.set_resource_id(RemoveWapiIdPrefix(metadata.resource_id()));
1147
1021 std::string metadata_key = FileSystemURLToMetadataKey(url); 1148 std::string metadata_key = FileSystemURLToMetadataKey(url);
1022 std::string value; 1149 std::string value;
1023 bool success = metadata.SerializeToString(&value); 1150 bool success = metadata.SerializeToString(&value);
1024 DCHECK(success); 1151 DCHECK(success);
1025 leveldb::Status status = db_->Put( 1152 leveldb::Status status = db_->Put(
1026 leveldb::WriteOptions(), metadata_key, value); 1153 leveldb::WriteOptions(), metadata_key, value);
1027 1154
1028 return LevelDBStatusToSyncStatusCode(status); 1155 return LevelDBStatusToSyncStatusCode(status);
1029 } 1156 }
1030 1157
1031 SyncStatusCode DriveMetadataDB::DeleteEntry(const FileSystemURL& url) { 1158 SyncStatusCode DriveMetadataDB::DeleteEntry(const FileSystemURL& url) {
1032 DCHECK(CalledOnValidThread()); 1159 DCHECK(CalledOnValidThread());
1033 DCHECK(db_.get()); 1160 DCHECK(db_.get());
1034 1161
1035 std::string metadata_key = FileSystemURLToMetadataKey(url); 1162 std::string metadata_key = FileSystemURLToMetadataKey(url);
1036 leveldb::Status status = db_->Delete( 1163 leveldb::Status status = db_->Delete(
1037 leveldb::WriteOptions(), metadata_key); 1164 leveldb::WriteOptions(), metadata_key);
1038 return LevelDBStatusToSyncStatusCode(status); 1165 return LevelDBStatusToSyncStatusCode(status);
1039 } 1166 }
1040 1167
1041 SyncStatusCode DriveMetadataDB::UpdateOriginAsBatchSync( 1168 SyncStatusCode DriveMetadataDB::UpdateOriginAsBatchSync(
1042 const GURL& origin, const std::string& resource_id) { 1169 const GURL& origin, const std::string& resource_id) {
1043 DCHECK(CalledOnValidThread()); 1170 DCHECK(CalledOnValidThread());
1044 DCHECK(db_.get()); 1171 DCHECK(db_.get());
1045 1172
1046 leveldb::Status status = db_->Put( 1173 leveldb::Status status = db_->Put(
1047 leveldb::WriteOptions(), 1174 leveldb::WriteOptions(),
1048 CreateKeyForOriginRoot(origin, BATCH_SYNC_ORIGIN), 1175 CreateKeyForOriginRoot(origin, BATCH_SYNC_ORIGIN),
1049 resource_id); 1176 RemoveWapiIdPrefix(resource_id));
1050 return LevelDBStatusToSyncStatusCode(status); 1177 return LevelDBStatusToSyncStatusCode(status);
1051 } 1178 }
1052 1179
1053 SyncStatusCode DriveMetadataDB::UpdateOriginAsIncrementalSync( 1180 SyncStatusCode DriveMetadataDB::UpdateOriginAsIncrementalSync(
1054 const GURL& origin, const std::string& resource_id) { 1181 const GURL& origin, const std::string& resource_id) {
1055 DCHECK(CalledOnValidThread()); 1182 DCHECK(CalledOnValidThread());
1056 DCHECK(db_.get()); 1183 DCHECK(db_.get());
1057 1184
1058 leveldb::WriteBatch batch; 1185 leveldb::WriteBatch batch;
1059 batch.Delete(CreateKeyForOriginRoot(origin, BATCH_SYNC_ORIGIN)); 1186 batch.Delete(CreateKeyForOriginRoot(origin, BATCH_SYNC_ORIGIN));
1060 batch.Delete(CreateKeyForOriginRoot(origin, DISABLED_ORIGIN)); 1187 batch.Delete(CreateKeyForOriginRoot(origin, DISABLED_ORIGIN));
1061 batch.Put(CreateKeyForOriginRoot(origin, INCREMENTAL_SYNC_ORIGIN), 1188 batch.Put(CreateKeyForOriginRoot(origin, INCREMENTAL_SYNC_ORIGIN),
1062 resource_id); 1189 RemoveWapiIdPrefix(resource_id));
1063 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); 1190 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch);
1064 1191
1065 return LevelDBStatusToSyncStatusCode(status); 1192 return LevelDBStatusToSyncStatusCode(status);
1066 } 1193 }
1067 1194
1068 SyncStatusCode DriveMetadataDB::EnableOrigin( 1195 SyncStatusCode DriveMetadataDB::EnableOrigin(
1069 const GURL& origin, const std::string& resource_id) { 1196 const GURL& origin, const std::string& resource_id) {
1070 DCHECK(CalledOnValidThread()); 1197 DCHECK(CalledOnValidThread());
1071 DCHECK(db_.get()); 1198 DCHECK(db_.get());
1072 1199
1073 leveldb::WriteBatch batch; 1200 leveldb::WriteBatch batch;
1074 batch.Delete(CreateKeyForOriginRoot(origin, INCREMENTAL_SYNC_ORIGIN)); 1201 batch.Delete(CreateKeyForOriginRoot(origin, INCREMENTAL_SYNC_ORIGIN));
1075 batch.Delete(CreateKeyForOriginRoot(origin, DISABLED_ORIGIN)); 1202 batch.Delete(CreateKeyForOriginRoot(origin, DISABLED_ORIGIN));
1076 batch.Put(CreateKeyForOriginRoot(origin, BATCH_SYNC_ORIGIN), resource_id); 1203 batch.Put(CreateKeyForOriginRoot(origin, BATCH_SYNC_ORIGIN),
1204 RemoveWapiIdPrefix(resource_id));
1077 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); 1205 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch);
1078 1206
1079 return LevelDBStatusToSyncStatusCode(status); 1207 return LevelDBStatusToSyncStatusCode(status);
1080 } 1208 }
1081 1209
1082 SyncStatusCode DriveMetadataDB::DisableOrigin( 1210 SyncStatusCode DriveMetadataDB::DisableOrigin(
1083 const GURL& origin_to_disable, const std::string& resource_id) { 1211 const GURL& origin_to_disable, const std::string& resource_id) {
1084 DCHECK(CalledOnValidThread()); 1212 DCHECK(CalledOnValidThread());
1085 DCHECK(db_.get()); 1213 DCHECK(db_.get());
1086 1214
1087 leveldb::WriteBatch batch; 1215 leveldb::WriteBatch batch;
1088 batch.Delete(CreateKeyForOriginRoot(origin_to_disable, BATCH_SYNC_ORIGIN)); 1216 batch.Delete(CreateKeyForOriginRoot(origin_to_disable, BATCH_SYNC_ORIGIN));
1089 batch.Delete(CreateKeyForOriginRoot(origin_to_disable, 1217 batch.Delete(CreateKeyForOriginRoot(origin_to_disable,
1090 INCREMENTAL_SYNC_ORIGIN)); 1218 INCREMENTAL_SYNC_ORIGIN));
1091 batch.Put(CreateKeyForOriginRoot(origin_to_disable, DISABLED_ORIGIN), 1219 batch.Put(CreateKeyForOriginRoot(origin_to_disable, DISABLED_ORIGIN),
1092 resource_id); 1220 RemoveWapiIdPrefix(resource_id));
1093 1221
1094 // Remove entries specified by |origin_to_disable|. 1222 // Remove entries specified by |origin_to_disable|.
1095 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); 1223 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions()));
1096 std::string metadata_key = kDriveMetadataKeyPrefix + origin_to_disable.spec(); 1224 std::string metadata_key = kDriveMetadataKeyPrefix + origin_to_disable.spec();
1097 for (itr->Seek(metadata_key); itr->Valid(); itr->Next()) { 1225 for (itr->Seek(metadata_key); itr->Valid(); itr->Next()) {
1098 std::string key = itr->key().ToString(); 1226 std::string key = itr->key().ToString();
1099 if (!StartsWithASCII(key, kDriveMetadataKeyPrefix, true)) 1227 if (!StartsWithASCII(key, kDriveMetadataKeyPrefix, true))
1100 break; 1228 break;
1101 GURL origin; 1229 GURL origin;
1102 base::FilePath path; 1230 base::FilePath path;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1149 // Get batch sync origins from the DB. 1277 // Get batch sync origins from the DB.
1150 for (itr->Seek(kDriveBatchSyncOriginKeyPrefix); 1278 for (itr->Seek(kDriveBatchSyncOriginKeyPrefix);
1151 itr->Valid(); itr->Next()) { 1279 itr->Valid(); itr->Next()) {
1152 std::string key = itr->key().ToString(); 1280 std::string key = itr->key().ToString();
1153 if (!StartsWithASCII(key, kDriveBatchSyncOriginKeyPrefix, true)) 1281 if (!StartsWithASCII(key, kDriveBatchSyncOriginKeyPrefix, true))
1154 break; 1282 break;
1155 GURL origin(std::string( 1283 GURL origin(std::string(
1156 key.begin() + arraysize(kDriveBatchSyncOriginKeyPrefix) - 1, 1284 key.begin() + arraysize(kDriveBatchSyncOriginKeyPrefix) - 1,
1157 key.end())); 1285 key.end()));
1158 DCHECK(origin.is_valid()); 1286 DCHECK(origin.is_valid());
1287
1288 std::string origin_resource_id =
1289 IsDriveAPIEnabled() ? itr->value().ToString()
1290 : AddWapiFolderPrefix(itr->value().ToString());
1291
1159 bool result = batch_sync_origins->insert( 1292 bool result = batch_sync_origins->insert(
1160 std::make_pair(origin, itr->value().ToString())).second; 1293 std::make_pair(origin, origin_resource_id)).second;
1161 DCHECK(result); 1294 DCHECK(result);
1162 } 1295 }
1163 1296
1164 // Get incremental sync origins from the DB. 1297 // Get incremental sync origins from the DB.
1165 for (itr->Seek(kDriveIncrementalSyncOriginKeyPrefix); 1298 for (itr->Seek(kDriveIncrementalSyncOriginKeyPrefix);
1166 itr->Valid(); itr->Next()) { 1299 itr->Valid(); itr->Next()) {
1167 std::string key = itr->key().ToString(); 1300 std::string key = itr->key().ToString();
1168 if (!StartsWithASCII(key, kDriveIncrementalSyncOriginKeyPrefix, true)) 1301 if (!StartsWithASCII(key, kDriveIncrementalSyncOriginKeyPrefix, true))
1169 break; 1302 break;
1170 GURL origin(std::string( 1303 GURL origin(std::string(
1171 key.begin() + arraysize(kDriveIncrementalSyncOriginKeyPrefix) - 1, 1304 key.begin() + arraysize(kDriveIncrementalSyncOriginKeyPrefix) - 1,
1172 key.end())); 1305 key.end()));
1173 DCHECK(origin.is_valid()); 1306 DCHECK(origin.is_valid());
1307
1308 std::string origin_resource_id =
1309 IsDriveAPIEnabled() ? itr->value().ToString()
1310 : AddWapiFolderPrefix(itr->value().ToString());
1311
1174 bool result = incremental_sync_origins->insert( 1312 bool result = incremental_sync_origins->insert(
1175 std::make_pair(origin, itr->value().ToString())).second; 1313 std::make_pair(origin, origin_resource_id)).second;
1176 DCHECK(result); 1314 DCHECK(result);
1177 } 1315 }
1178 1316
1179 // Get disabled origins from the DB. 1317 // Get disabled origins from the DB.
1180 for (itr->Seek(kDriveDisabledOriginKeyPrefix); 1318 for (itr->Seek(kDriveDisabledOriginKeyPrefix);
1181 itr->Valid(); itr->Next()) { 1319 itr->Valid(); itr->Next()) {
1182 std::string key = itr->key().ToString(); 1320 std::string key = itr->key().ToString();
1183 if (!StartsWithASCII(key, kDriveDisabledOriginKeyPrefix, true)) 1321 if (!StartsWithASCII(key, kDriveDisabledOriginKeyPrefix, true))
1184 break; 1322 break;
1185 GURL origin(std::string( 1323 GURL origin(std::string(
1186 key.begin() + arraysize(kDriveDisabledOriginKeyPrefix) - 1, 1324 key.begin() + arraysize(kDriveDisabledOriginKeyPrefix) - 1,
1187 key.end())); 1325 key.end()));
1188 DCHECK(origin.is_valid()); 1326 DCHECK(origin.is_valid());
1327
1328 std::string origin_resource_id =
1329 IsDriveAPIEnabled() ? itr->value().ToString()
1330 : AddWapiFolderPrefix(itr->value().ToString());
1331
1189 bool result = disabled_origins->insert( 1332 bool result = disabled_origins->insert(
1190 std::make_pair(origin, itr->value().ToString())).second; 1333 std::make_pair(origin, origin_resource_id)).second;
1191 DCHECK(result); 1334 DCHECK(result);
1192 } 1335 }
1193 1336
1194 return SYNC_STATUS_OK; 1337 return SYNC_STATUS_OK;
1195 } 1338 }
1196 1339
1197 } // namespace sync_file_system 1340 } // namespace sync_file_system
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698