resource_metadata_storage.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_STORAGE_H_ 6#define CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_STORAGE_H_ 7 8#include <string> 9#include <vector> 10 11#include "base/basictypes.h" 12#include "base/callback_forward.h" 13#include "base/files/file_path.h" 14#include "base/memory/scoped_ptr.h" 15 16namespace leveldb { 17class DB; 18} 19 20namespace drive { 21 22class ResourceEntry; 23class ResourceMetadataHeader; 24 25typedef base::Callback<void(const ResourceEntry& entry)> IterateCallback; 26 27// Storage for ResourceMetadata which is responsible to manage entry info 28// and child-parent relationships between entries. 29class ResourceMetadataStorage { 30 public: 31 // This should be incremented when incompatibility change is made to DB 32 // format. 33 static const int kDBVersion = 5; 34 35 explicit ResourceMetadataStorage(const base::FilePath& directory_path); 36 virtual ~ResourceMetadataStorage(); 37 38 // Initializes this object. 39 bool Initialize(); 40 41 // Sets the largest changestamp. 42 bool SetLargestChangestamp(int64 largest_changestamp); 43 44 // Gets the largest changestamp. 45 int64 GetLargestChangestamp(); 46 47 // Puts the entry to this storage. 48 bool PutEntry(const ResourceEntry& entry); 49 50 // Returns an entry stored in this storage. 51 scoped_ptr<ResourceEntry> GetEntry(const std::string& resource_id); 52 53 // Removes an entry from this storage. 54 bool RemoveEntry(const std::string& resource_id); 55 56 // Iterates over entries stored in this storage. 57 void Iterate(const IterateCallback& callback); 58 59 // Returns resource ID of the parent's child. 60 std::string GetChild(const std::string& parent_resource_id, 61 const std::string& child_name); 62 63 // Returns resource IDs of the parent's children. 64 void GetChildren(const std::string& parent_resource_id, 65 std::vector<std::string>* children); 66 67 private: 68 friend class ResourceMetadataStorageTest; 69 70 // Returns a string to be used as a key for child entry. 71 static std::string GetChildEntryKey(const std::string& parent_resource_id, 72 const std::string& child_name); 73 74 // Puts header. 75 bool PutHeader(const ResourceMetadataHeader& header); 76 77 // Gets header. 78 scoped_ptr<ResourceMetadataHeader> GetHeader(); 79 80 // Checks validity of the data. 81 bool CheckValidity(); 82 83 // Path to the directory where the data is stored. 84 base::FilePath directory_path_; 85 86 // Entries stored in this storage. 87 scoped_ptr<leveldb::DB> resource_map_; 88 89 DISALLOW_COPY_AND_ASSIGN(ResourceMetadataStorage); 90}; 91 92} // namespace drive 93 94#endif // CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_STORAGE_H_ 95