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_SESSION_GENERIC_STORAGE_MANAGER_H_ 31 #define MOZC_SESSION_GENERIC_STORAGE_MANAGER_H_ 32 33 #include <memory> 34 35 #include "base/port.h" 36 #include "protocol/commands.pb.h" 37 38 namespace mozc { 39 40 class GenericStorageInterface; 41 42 // For unit test. 43 class GenericLruStorageProxy; 44 45 namespace storage { 46 class LRUStorage; 47 } // namespace storage 48 49 // Override and set the subclass's instance to 50 // GenericStorageManager for unit test. 51 class GenericStorageManagerInterface { 52 public: GenericStorageManagerInterface()53 GenericStorageManagerInterface() {} ~GenericStorageManagerInterface()54 virtual ~GenericStorageManagerInterface() {} 55 virtual GenericStorageInterface *GetStorage( 56 commands::GenericStorageEntry::StorageType storage_type) = 0; 57 }; 58 59 // Manages generic storages. 60 class GenericStorageManagerFactory { 61 public: 62 // Returns corresponding storage's instance. 63 // If no instance is available, NULL is returned. 64 static GenericStorageInterface *GetStorage( 65 commands::GenericStorageEntry::StorageType storage_type); 66 // For unit test. 67 static void SetGenericStorageManager(GenericStorageManagerInterface *manager); 68 69 private: 70 DISALLOW_IMPLICIT_CONSTRUCTORS(GenericStorageManagerFactory); 71 }; 72 73 // Generic interface for storages. 74 // This class defines only the interfaces. 75 // Detailed behaviors depend on the subclass's 76 // backend. 77 class GenericStorageInterface { 78 public: GenericStorageInterface()79 GenericStorageInterface() {} ~GenericStorageInterface()80 virtual ~GenericStorageInterface() {} 81 82 // Inserts new entry. 83 // If something goes wrong, returns false. 84 // value should be terminated by '\0'. 85 virtual bool Insert(const string &key, const char *value) = 0; 86 // Looks up the value. 87 // If something goes wrong, returns NULL. 88 virtual const char *Lookup(const string &key) = 0; 89 // Lists all the values. 90 // If something goes wrong, returns false. 91 virtual bool GetAllValues(std::vector<string> *values) = 0; 92 // Clears all the entries. 93 virtual bool Clear() = 0; 94 }; 95 96 // Storage class of which backend is LRUStorage. 97 class GenericLruStorage : public GenericStorageInterface { 98 public: 99 GenericLruStorage( 100 const char *file_name, size_t value_size, size_t size, uint32 seed); 101 virtual ~GenericLruStorage(); 102 103 // If the storage has |key|, this method overwrites 104 // the old value. 105 // If the entiry's size is over GetSize(), 106 // the oldest value is disposed. 107 virtual bool Insert(const string &key, const char *value); 108 109 virtual const char *Lookup(const string &key); 110 111 // The order is new to old. 112 virtual bool GetAllValues(std::vector<string> *values); 113 114 virtual bool Clear(); 115 116 protected: 117 // Opens the storage if not opened yet. 118 // If something goes wrong, returns false. 119 bool EnsureStorage(); 120 121 private: 122 friend class GenericLruStorageProxy; 123 std::unique_ptr<mozc::storage::LRUStorage> lru_storage_; 124 const string file_name_; 125 const size_t value_size_; 126 const size_t size_; 127 const uint32 seed_; 128 // Temporary buffer to insert a value into this storage. 129 std::unique_ptr<char[]> value_buffer_; 130 131 DISALLOW_COPY_AND_ASSIGN(GenericLruStorage); 132 }; 133 134 } // namespace mozc 135 136 #endif // MOZC_SESSION_GENERIC_STORAGE_MANAGER_H_ 137