1 // Copyright 2014 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 "sync/syncable/deferred_on_disk_directory_backing_store.h" 6 7 #include "base/logging.h" 8 #include "base/metrics/histogram.h" 9 #include "base/stl_util.h" 10 #include "sync/syncable/syncable-inl.h" 11 12 namespace syncer { 13 namespace syncable { 14 DeferredOnDiskDirectoryBackingStore(const std::string & dir_name,const base::FilePath & backing_filepath)15DeferredOnDiskDirectoryBackingStore::DeferredOnDiskDirectoryBackingStore( 16 const std::string& dir_name, const base::FilePath& backing_filepath) 17 : DirectoryBackingStore(dir_name), 18 backing_filepath_(backing_filepath), 19 db_is_on_disk_(false) { 20 } 21 ~DeferredOnDiskDirectoryBackingStore()22DeferredOnDiskDirectoryBackingStore::~DeferredOnDiskDirectoryBackingStore() {} 23 SaveChanges(const Directory::SaveChangesSnapshot & snapshot)24bool DeferredOnDiskDirectoryBackingStore::SaveChanges( 25 const Directory::SaveChangesSnapshot& snapshot) { 26 DCHECK(CalledOnValidThread()); 27 28 // Back out early if there is nothing to save. 29 if (snapshot.dirty_metas.empty() && snapshot.metahandles_to_purge.empty() && 30 snapshot.delete_journals.empty() && 31 snapshot.delete_journals_to_purge.empty()) { 32 return true; 33 } 34 35 if (!db_is_on_disk_) { 36 if (!base::DeleteFile(backing_filepath_, false)) 37 return false; 38 39 // Reopen DB on disk. 40 db_.reset(new sql::Connection); 41 db_->set_exclusive_locking(); 42 db_->set_page_size(4096); 43 if (!db_->Open(backing_filepath_) || !InitializeTables()) 44 return false; 45 46 db_is_on_disk_ = true; 47 } 48 49 return DirectoryBackingStore::SaveChanges(snapshot); 50 } 51 Load(Directory::MetahandlesMap * handles_map,JournalIndex * delete_journals,Directory::KernelLoadInfo * kernel_load_info)52DirOpenResult DeferredOnDiskDirectoryBackingStore::Load( 53 Directory::MetahandlesMap* handles_map, 54 JournalIndex* delete_journals, 55 Directory::KernelLoadInfo* kernel_load_info) { 56 // Open an in-memory database at first to create initial sync data needed by 57 // Directory. 58 CHECK(!db_->is_open()); 59 if (!db_->OpenInMemory()) 60 return FAILED_OPEN_DATABASE; 61 62 if (!InitializeTables()) 63 return FAILED_OPEN_DATABASE; 64 if (!LoadEntries(handles_map)) 65 return FAILED_DATABASE_CORRUPT; 66 if (!LoadInfo(kernel_load_info)) 67 return FAILED_DATABASE_CORRUPT; 68 69 return OPENED; 70 } 71 72 } // namespace syncable 73 } // namespace syncer 74