OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/syncable/directory_backing_store.h" | 5 #include "chrome/browser/sync/syncable/directory_backing_store.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 | 8 |
9 #if defined(OS_MACOSX) | 9 #if defined(OS_MACOSX) |
10 #include <CoreFoundation/CoreFoundation.h> | 10 #include <CoreFoundation/CoreFoundation.h> |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 << " corrupt. Deleting and recreating."; | 243 << " corrupt. Deleting and recreating."; |
244 file_util::Delete(backing_filepath_, false); | 244 file_util::Delete(backing_filepath_, false); |
245 return OpenAndConfigureHandleHelper(&load_dbhandle_); | 245 return OpenAndConfigureHandleHelper(&load_dbhandle_); |
246 } | 246 } |
247 | 247 |
248 void DirectoryBackingStore::EndLoad() { | 248 void DirectoryBackingStore::EndLoad() { |
249 sqlite3_close(load_dbhandle_); | 249 sqlite3_close(load_dbhandle_); |
250 load_dbhandle_ = NULL; // No longer used. | 250 load_dbhandle_ = NULL; // No longer used. |
251 } | 251 } |
252 | 252 |
| 253 void DirectoryBackingStore::EndSave() { |
| 254 sqlite3_close(save_dbhandle_); |
| 255 save_dbhandle_ = NULL; |
| 256 } |
| 257 |
| 258 bool DirectoryBackingStore::DeleteEntries(const MetahandleSet& handles) { |
| 259 if (handles.empty()) |
| 260 return true; |
| 261 |
| 262 sqlite3* dbhandle = LazyGetSaveHandle(); |
| 263 |
| 264 string query = "DELETE FROM metas WHERE metahandle IN ("; |
| 265 for (MetahandleSet::const_iterator it = handles.begin(); it != handles.end(); |
| 266 ++it) { |
| 267 if (it != handles.begin()) |
| 268 query.append(","); |
| 269 query.append(Int64ToString(*it)); |
| 270 } |
| 271 query.append(")"); |
| 272 SQLStatement statement; |
| 273 int result = statement.prepare(dbhandle, query.data(), query.size()); |
| 274 if (SQLITE_OK == result) |
| 275 result = statement.step(); |
| 276 |
| 277 return SQLITE_DONE == result; |
| 278 } |
| 279 |
253 bool DirectoryBackingStore::SaveChanges( | 280 bool DirectoryBackingStore::SaveChanges( |
254 const Directory::SaveChangesSnapshot& snapshot) { | 281 const Directory::SaveChangesSnapshot& snapshot) { |
255 sqlite3* dbhandle = LazyGetSaveHandle(); | 282 sqlite3* dbhandle = LazyGetSaveHandle(); |
256 | 283 |
257 // SQLTransaction::BeginExclusive causes a disk write to occur. This is not | 284 // SQLTransaction::BeginExclusive causes a disk write to occur. This is not |
258 // something that should happen every 10 seconds when this function runs, so | 285 // something that should happen every 10 seconds when this function runs, so |
259 // just stop here if there's nothing to save. | 286 // just stop here if there's nothing to save. |
260 bool save_info = | 287 bool save_info = |
261 (Directory::KERNEL_SHARE_INFO_DIRTY == snapshot.kernel_info_status); | 288 (Directory::KERNEL_SHARE_INFO_DIRTY == snapshot.kernel_info_status); |
262 if (snapshot.dirty_metas.size() < 1 && !save_info) | 289 if (snapshot.dirty_metas.size() < 1 && !save_info) |
263 return true; | 290 return true; |
264 | 291 |
265 SQLTransaction transaction(dbhandle); | 292 SQLTransaction transaction(dbhandle); |
266 if (SQLITE_OK != transaction.BeginExclusive()) | 293 if (SQLITE_OK != transaction.BeginExclusive()) |
267 return false; | 294 return false; |
268 | 295 |
269 for (OriginalEntries::const_iterator i = snapshot.dirty_metas.begin(); | 296 for (OriginalEntries::const_iterator i = snapshot.dirty_metas.begin(); |
270 i != snapshot.dirty_metas.end(); ++i) { | 297 i != snapshot.dirty_metas.end(); ++i) { |
271 DCHECK(i->is_dirty()); | 298 DCHECK(i->is_dirty()); |
272 if (!SaveEntryToDB(*i)) | 299 if (!SaveEntryToDB(*i)) |
273 return false; | 300 return false; |
274 } | 301 } |
275 | 302 |
| 303 if (!DeleteEntries(snapshot.metahandles_to_purge)) |
| 304 return false; |
| 305 |
276 if (save_info) { | 306 if (save_info) { |
277 const Directory::PersistedKernelInfo& info = snapshot.kernel_info; | 307 const Directory::PersistedKernelInfo& info = snapshot.kernel_info; |
278 SQLStatement update; | 308 SQLStatement update; |
279 update.prepare(dbhandle, "UPDATE share_info " | 309 update.prepare(dbhandle, "UPDATE share_info " |
280 "SET store_birthday = ?, " | 310 "SET store_birthday = ?, " |
281 "next_id = ?"); | 311 "next_id = ?"); |
282 update.bind_string(0, info.store_birthday); | 312 update.bind_string(0, info.store_birthday); |
283 update.bind_int64(1, info.next_id); | 313 update.bind_int64(1, info.next_id); |
284 | 314 |
285 if (!(SQLITE_DONE == update.step() && | 315 if (!(SQLITE_DONE == update.step() && |
(...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
910 "name TEXT, " | 940 "name TEXT, " |
911 "store_birthday TEXT, " | 941 "store_birthday TEXT, " |
912 "db_create_version TEXT, " | 942 "db_create_version TEXT, " |
913 "db_create_time INT, " | 943 "db_create_time INT, " |
914 "next_id INT default -2, " | 944 "next_id INT default -2, " |
915 "cache_guid TEXT)"); | 945 "cache_guid TEXT)"); |
916 return ExecQuery(load_dbhandle_, query.c_str()); | 946 return ExecQuery(load_dbhandle_, query.c_str()); |
917 } | 947 } |
918 | 948 |
919 } // namespace syncable | 949 } // namespace syncable |
OLD | NEW |