1 // Copyright 2010-2018, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30 #ifndef MOZC_STORAGE_LRU_STORAGE_H_ 31 #define MOZC_STORAGE_LRU_STORAGE_H_ 32 33 #include <map> 34 #include <memory> 35 #include <string> 36 #include <vector> 37 38 #include "base/port.h" 39 40 namespace mozc { 41 42 class Mmap; 43 44 namespace storage { 45 46 class LRUStorage { 47 public: 48 LRUStorage(); 49 ~LRUStorage(); 50 51 bool Open(const char *filename); 52 void Close(); 53 54 // Try to open exisiting database 55 // If the file is broken or cannot open, tries to recreate 56 // new file 57 bool OpenOrCreate(const char *filename, 58 size_t new_value_size, 59 size_t new_size, 60 uint32 new_seed); 61 62 // Lookup key 63 const char *Lookup(const string &key, 64 uint32 *last_access_time) const; 65 66 const char *Lookup(const string &key) const; 67 68 // Returns all values. 69 // The order is new to old (*values->begin() is the newest). 70 bool GetAllValues(std::vector<string> *values) const; 71 72 // clear all LRU cache; 73 // mapped file is also initialized 74 bool Clear(); 75 76 // Merge from other data 77 bool Merge(const char *filename); 78 bool Merge(const LRUStorage &storage); 79 80 // update timestamp 81 bool Touch(const string &key); 82 83 // Insert key and values 84 bool Insert(const string &key, 85 const char *value); 86 87 // if key is found, insert value, 88 // otherwise return 89 bool TryInsert(const string &key, 90 const char *value); 91 92 size_t value_size() const; 93 size_t size() const; 94 size_t used_size() const; 95 uint32 seed() const; 96 const string &filename() const; 97 98 // Write one entry at |i| th index. 99 // i must be 0 <= i < size. 100 // This data will not update the index of the storage. 101 void Write(size_t i, 102 uint64 fp, 103 const string &value, 104 uint32 last_access_time); 105 106 // Read one entry from |i| th index. 107 // i must be 0 <= i < size. 108 void Read(size_t i, 109 uint64 *fp, 110 string *value, 111 uint32 *last_access_time) const; 112 113 // Create Instance from file. Call Open internally 114 static LRUStorage *Create(const char *filename); 115 116 // Create Instance from file. Call OpenOrCreate internally 117 static LRUStorage *Create(const char *filename, 118 size_t value_size, 119 size_t size, 120 uint32 seed); 121 122 // Create an empty LRU db file 123 static bool CreateStorageFile(const char *filename, 124 size_t value_size, 125 size_t size, 126 uint32 seed); 127 private: 128 class LRUList; 129 class Node; 130 131 // load from memory buffer 132 bool Open(char *ptr, size_t ptr_size); 133 134 size_t value_size_; 135 size_t size_; 136 uint32 seed_; 137 char *last_item_; 138 char *begin_; 139 char *end_; 140 string filename_; 141 std::map<uint64, Node *> map_; 142 std::unique_ptr<LRUList> lru_list_; 143 std::unique_ptr<Mmap> mmap_; 144 145 DISALLOW_COPY_AND_ASSIGN(LRUStorage); 146 }; 147 148 } // namespace storage 149 } // namespace mozc 150 151 #endif // MOZC_STORAGE_LRU_STORAGE_H_ 152