kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/common/index_writer.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_WRITER_H_
    18  #define KYTHE_CXX_COMMON_INDEX_WRITER_H_
    19  
    20  #include "absl/status/status.h"
    21  #include "absl/status/statusor.h"
    22  #include "absl/strings/string_view.h"
    23  #include "kythe/proto/analysis.pb.h"
    24  
    25  namespace kythe {
    26  
    27  /// \brief Simple interface for writing IndexedCompilations and files
    28  /// to an underlying data store.
    29  class IndexWriterInterface {
    30   public:
    31    IndexWriterInterface() = default;
    32    // IndexWriterInterface is neither copyable nor movable.
    33    IndexWriterInterface(const IndexWriterInterface&) = delete;
    34    IndexWriterInterface& operator=(const IndexWriterInterface&) = delete;
    35    virtual ~IndexWriterInterface() = default;
    36  
    37    /// \brief Write the `IndexedCompilation` to the index. Returns the
    38    /// hex-encoded SHA256 digest of the unit's contents.
    39    virtual absl::StatusOr<std::string> WriteUnit(
    40        const kythe::proto::IndexedCompilation& unit) = 0;
    41  
    42    /// \brief Write the file data to the index. Returns the hex-encoded
    43    /// SHA256 digest of the file's contents.
    44    virtual absl::StatusOr<std::string> WriteFile(absl::string_view content) = 0;
    45  
    46    /// \brief Flush and finalize any outstanding writes.
    47    virtual absl::Status Close() = 0;
    48  };
    49  
    50  /// \brief Pimpl wrapper around IndexWriterInterface.
    51  class IndexWriter final {
    52   public:
    53    /// \brief Constructs an IndexWriter from the provided implementation.
    54    explicit IndexWriter(std::unique_ptr<IndexWriterInterface> impl)
    55        : impl_(std::move(impl)) {}
    56  
    57    // IndexWriter is move-only.
    58    IndexWriter(IndexWriter&&) = default;
    59    IndexWriter& operator=(IndexWriter&&) = default;
    60  
    61    /// \brief Write the `IndexedCompilation` to the index.
    62    absl::StatusOr<std::string> WriteUnit(
    63        const kythe::proto::IndexedCompilation& unit) {
    64      return impl_->WriteUnit(unit);
    65    }
    66  
    67    /// \brief Write the file data to the index.
    68    absl::StatusOr<std::string> WriteFile(absl::string_view content) {
    69      return impl_->WriteFile(content);
    70    }
    71  
    72    /// \brief Flush and finalize any outstanding writes.
    73    absl::Status Close() { return impl_->Close(); }
    74  
    75   private:
    76    std::unique_ptr<IndexWriterInterface> impl_;
    77  };
    78  
    79  }  // namespace kythe
    80  
    81  #endif  // KYTHE_CXX_COMMON_INDEX_WRITER_H_