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 struct CreateResult { 34 // The create result passed up from the PortableFileBackedProtoLog::Create. 35 // Contains the document log. 36 PortableFileBackedProtoLog<DocumentWrapper>::CreateResult log_create_result; 37 38 // Whether the caller needs to also regenerate/generate any derived files 39 // based off of the initialized document log. 40 bool regen_derived_files; 41 }; 42 43 // Creates the document log in the base_dir. Will create one if it doesn't 44 // already exist. 45 // 46 // This also handles any potential migrations from old document log versions. 47 // At the end of this call, the most up-to-date log will be returned and will 48 // be usable. 49 // 50 // Returns: 51 // CreateResult on success. 52 // INTERNAL on any I/O error. 53 static libtextclassifier3::StatusOr<DocumentLogCreator::CreateResult> Create( 54 const Filesystem* filesystem, const std::string& base_dir); 55 56 // Returns the filename of the document log, without any directory prefixes. 57 // Used mainly for testing purposes. 58 static std::string GetDocumentLogFilename(); 59 60 private: 61 // Handles migrating a v0 document log (not portable) to a v1 document log 62 // (portable). This will initialize the log in the beginning, and close it 63 // when migration is done. Callers will need to reinitialize the log on their 64 // own. 65 // 66 // Returns: 67 // OK on success. 68 // INVALID_ARGUMENT if some invalid option was passed to the document log. 69 // INTERNAL on I/O error. 70 static libtextclassifier3::Status MigrateFromV0ToV1( 71 const Filesystem* filesystem, const std::string& base_dir); 72 }; 73 74 } // namespace lib 75 } // namespace icing 76 77 #endif // ICING_STORE_DOCUMENT_LOG_CREATOR_H_ 78