1 // Copyright (C) 2019 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Author: sbanacho@google.com (Scott Banachowski) 16 // vmarko@google.com (Vladimir Marko) 17 // 18 // Interface class for disk-backed storage. 19 20 #ifndef ICING_LEGACY_INDEX_ICING_STORAGE_H_ 21 #define ICING_LEGACY_INDEX_ICING_STORAGE_H_ 22 23 #include <cstdint> 24 #include <string> 25 26 namespace icing { 27 namespace lib { 28 29 // Abstract base class for interface. 30 class IIcingStorage { 31 public: 32 // Any resource that is not removed in the Close() function should 33 // be removed in the child's destructor. 34 virtual ~IIcingStorage() = default; 35 36 // This is called to upgrade to a new version. 37 // Returns true if the data store can be upgraded successfully. 38 virtual bool UpgradeTo(int new_version) = 0; 39 40 // This must be called before the object is usable. 41 // Returns true if the storage is in a usable state. 42 virtual bool Init() = 0; 43 44 // Attempts to init the given IIcingStorage. On failure, clears the underlying 45 // data and tries again. Returns false of the second init is also a failure. InitWithRetry(IIcingStorage * file_in)46 static bool InitWithRetry(IIcingStorage* file_in) { 47 if (file_in->Init()) { 48 return true; 49 } 50 return file_in->Remove() && file_in->Init(); 51 } 52 53 // Closes all files and system resources. 54 // Init() must be called before the object is used again. 55 virtual void Close() = 0; 56 57 // Closes all system resources, then removes the backing file. 58 // Init() is required before the object is used again. 59 // Returns true on success. 60 virtual bool Remove() = 0; 61 62 // Syncs any unwritten data to disk. 63 virtual bool Sync() = 0; 64 65 // Gets the total amount of disk usage for the object (i.e. the sum of the 66 // bytes of all underlying files). 67 // Note: reported values are estimated via the number of blocks the file takes 68 // up on disk. Sparse files are reported as their physical disk usage, as 69 // opposed to the logical size when read. 70 // Returns kBadFileSize on error. 71 virtual uint64_t GetDiskUsage() const = 0; 72 73 // Optional handler for when our process is entering a vulnerable 74 // state (highly likely to get killed). Default implementation does 75 // nothing. OnSleep()76 virtual void OnSleep() {} 77 78 virtual void GetDebugInfo(int verbosity, std::string* out) const = 0; 79 80 protected: 81 IIcingStorage() = default; 82 83 private: 84 // Document stores are non-copyable. 85 IIcingStorage(const IIcingStorage&); 86 IIcingStorage& operator=(const IIcingStorage&); 87 }; 88 89 } // namespace lib 90 } // namespace icing 91 92 #endif // ICING_LEGACY_INDEX_ICING_STORAGE_H_ 93