kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/common/index_reader.h (about) 1 /* 2 * Copyright 2018 The Kythe Authors. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef KYTHE_CXX_COMMON_INDEX_READER_H_ 18 #define KYTHE_CXX_COMMON_INDEX_READER_H_ 19 20 #include <functional> 21 #include <string> 22 23 #include "absl/status/status.h" 24 #include "absl/status/statusor.h" 25 #include "absl/strings/string_view.h" 26 #include "kythe/proto/analysis.pb.h" 27 28 namespace kythe { 29 30 /// \brief Simple interface for reading IndexedCompilations and files 31 /// from an underlying data store. 32 class IndexReaderInterface { 33 public: 34 /// \brief Callback invoked for each available unit digest. 35 using ScanCallback = std::function<bool(absl::string_view)>; 36 37 IndexReaderInterface() = default; 38 // IndexReaderInterface is neither copyable nor movable. 39 IndexReaderInterface(const IndexReaderInterface&) = delete; 40 IndexReaderInterface& operator=(const IndexReaderInterface&) = delete; 41 virtual ~IndexReaderInterface() = default; 42 43 /// \brief Invokes `scan` for each IndexedCompilation unit digest or until it 44 /// returns false. 45 virtual absl::Status Scan(const ScanCallback& scan) = 0; 46 47 /// \brief Reads and returns requested IndexCompilation. 48 /// Returns kNotFound if the digest isn't present. 49 virtual absl::StatusOr<kythe::proto::IndexedCompilation> ReadUnit( 50 absl::string_view digest) = 0; 51 52 /// \brief Reads and returns the requested file data. 53 /// Returns kNotFound if the digest isn't present. 54 virtual absl::StatusOr<std::string> ReadFile(absl::string_view digest) = 0; 55 }; 56 57 /// \brief Pimpl wrapper around IndexReaderInterface. 58 class IndexReader { 59 public: 60 using ScanCallback = IndexReaderInterface::ScanCallback; 61 62 /// \brief Constructs an IndexReader from the provided implementation. 63 explicit IndexReader(std::unique_ptr<IndexReaderInterface> impl) 64 : impl_(std::move(impl)) {} 65 66 // IndexReader is move-only. 67 IndexReader(IndexReader&&) = default; 68 IndexReader& operator=(IndexReader&&) = default; 69 70 /// \brief Invokes `scan` for each IndexedCompilation unit digest or until it 71 /// returns false. 72 absl::Status Scan(const ScanCallback& scan) { return impl_->Scan(scan); } 73 74 /// \brief Reads and returns requested IndexCompilation. 75 /// Returns kNotFound if the digest isn't present. 76 absl::StatusOr<kythe::proto::IndexedCompilation> ReadUnit( 77 absl::string_view digest) { 78 return impl_->ReadUnit(digest); 79 } 80 81 /// \brief Reads and returns the requested file data. 82 /// Returns kNotFound if the digest isn't present. 83 absl::StatusOr<std::string> ReadFile(absl::string_view digest) { 84 return impl_->ReadFile(digest); 85 } 86 87 private: 88 std::unique_ptr<IndexReaderInterface> impl_; 89 }; 90 91 } // namespace kythe 92 93 #endif // KYTHE_CXX_COMMON_INDEX_READER_H_