1 // Copyright (C) 2021 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 #ifndef ICING_STORE_DOCUMENT_LOG_CREATOR_H_ 16 #define ICING_STORE_DOCUMENT_LOG_CREATOR_H_ 17 18 #include <string> 19 20 #include "icing/text_classifier/lib3/utils/base/status.h" 21 #include "icing/text_classifier/lib3/utils/base/statusor.h" 22 #include "icing/file/filesystem.h" 23 #include "icing/file/portable-file-backed-proto-log.h" 24 #include "icing/proto/document_wrapper.pb.h" 25 26 namespace icing { 27 namespace lib { 28 29 // Handles creation of the document log and any underlying migrations that may 30 // be necessary. 31 class DocumentLogCreator { 32 public: 33 // Version 0 refers to FileBackedProtoLog 34 // Version 1 refers to PortableFileBackedProtoLog with kFileFormatVersion = 0 35 static constexpr int32_t kCurrentVersion = 1; 36 struct CreateResult { 37 // The create result passed up from the PortableFileBackedProtoLog::Create. 38 // Contains the document log. 39 PortableFileBackedProtoLog<DocumentWrapper>::CreateResult log_create_result; 40 41 // The version number of the pre-existing document log file. 42 // If there is no document log file, it will be set to kCurrentVersion. 43 int preexisting_file_version; 44 45 // Whether the created file is new. 46 bool new_file; 47 }; 48 49 // Creates the document log in the base_dir. Will create one if it doesn't 50 // already exist. 51 // 52 // This also handles any potential migrations from old document log versions. 53 // At the end of this call, the most up-to-date log will be returned and will 54 // be usable. 55 // 56 // Returns: 57 // CreateResult on success. 58 // INTERNAL on any I/O error. 59 static libtextclassifier3::StatusOr<DocumentLogCreator::CreateResult> Create( 60 const Filesystem* filesystem, const std::string& base_dir, 61 int32_t compression_level); 62 63 // Returns the filename of the document log, without any directory prefixes. 64 // Used mainly for testing purposes. 65 static std::string GetDocumentLogFilename(); 66 67 private: 68 // Handles migrating a v0 document log (not portable) to a v1 document log 69 // (portable). This will initialize the log in the beginning, and close it 70 // when migration is done. Callers will need to reinitialize the log on their 71 // own. 72 // 73 // Returns: 74 // OK on success. 75 // INVALID_ARGUMENT if some invalid option was passed to the document log. 76 // INTERNAL on I/O error. 77 static libtextclassifier3::Status MigrateFromV0ToV1( 78 const Filesystem* filesystem, const std::string& base_dir, 79 int32_t compression_level); 80 }; 81 82 } // namespace lib 83 } // namespace icing 84 85 #endif // ICING_STORE_DOCUMENT_LOG_CREATOR_H_ 86