xref: /aosp_15_r20/external/icing/icing/legacy/index/icing-storage.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
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: [email protected] (Scott Banachowski)
16 //         [email protected] (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 #include "icing/util/crc32.h"
27 
28 namespace icing {
29 namespace lib {
30 
31 // Abstract base class for interface.
32 class IIcingStorage {
33  public:
34   // Any resource that is not removed in the Close() function should
35   // be removed in the child's destructor.
36   virtual ~IIcingStorage() = default;
37 
38   // This is called to upgrade to a new version.
39   // Returns true if the data store can be upgraded successfully.
40   virtual bool UpgradeTo(int new_version) = 0;
41 
42   // This must be called before the object is usable.
43   // Returns true if the storage is in a usable state.
44   virtual bool Init() = 0;
45 
46   // Attempts to init the given IIcingStorage. On failure, clears the underlying
47   // data and tries again. Returns false of the second init is also a failure.
InitWithRetry(IIcingStorage * file_in)48   static bool InitWithRetry(IIcingStorage* file_in) {
49     if (file_in->Init()) {
50       return true;
51     }
52     return file_in->Remove() && file_in->Init();
53   }
54 
55   // Closes all files and system resources.
56   // Init() must be called before the object is used again.
57   virtual void Close() = 0;
58 
59   // Closes all system resources, then removes the backing file.
60   // Init() is required before the object is used again.
61   // Returns true on success.
62   virtual bool Remove() = 0;
63 
64   // Syncs any unwritten data to disk.
65   virtual bool Sync() = 0;
66 
67   // Gets the total amount of disk usage for the object (i.e. the sum of the
68   // bytes of all underlying files).
69   // Note: reported values are estimated via the number of blocks the file takes
70   // up on disk. Sparse files are reported as their physical disk usage, as
71   // opposed to the logical size when read.
72   // Returns kBadFileSize on error.
73   virtual uint64_t GetDiskUsage() const = 0;
74 
75   // Updates any checksums that this storage maintains.
76   // By default, does nothing.
UpdateCrc()77   virtual Crc32 UpdateCrc() { return Crc32(); }
78 
79   virtual void GetDebugInfo(int verbosity, std::string* out) const = 0;
80 
81  protected:
82   IIcingStorage() = default;
83 
84  private:
85   // Document stores are non-copyable.
86   IIcingStorage(const IIcingStorage&);
87   IIcingStorage& operator=(const IIcingStorage&);
88 };
89 
90 }  // namespace lib
91 }  // namespace icing
92 
93 #endif  // ICING_LEGACY_INDEX_ICING_STORAGE_H_
94