1/**
2 * Copyright 2021 Alibaba, Inc. and its affiliates. 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 * \author Haichao.chc
17 * \date Oct 2020
18 * \brief SimpleForwardIndexer is simple implementation of ForwardIndexer.
19 */
20
21#pragma once
22
23#include <ailego/parallel/lock.h>
24#include <aitheta2/index_closet.h>
25#include "common/macro_define.h"
26#include "common/types.h"
27#include "forward_indexer.h"
28#include "../snapshot.h"
29#include "../typedef.h"
30
31namespace proxima {
32namespace be {
33namespace index {
34
35class SimpleForwardIndexer;
36using SimpleForwardIndexerPtr = std::shared_ptr<SimpleForwardIndexer>;
37
38/*
39 * SimpleForwardIndexer implement ForwardIndexer with proxima simple forward
40 * module.
41 */
42class SimpleForwardIndexer : public ForwardIndexer {
43 public:
44 PROXIMA_DISALLOW_COPY_AND_ASSIGN(SimpleForwardIndexer);
45
46 //! Constructor
47 SimpleForwardIndexer(const std::string &coll_name,
48 const std::string &coll_path, SegmentID seg_id) {
49 this->set_collection_name(coll_name);
50 this->set_collection_path(coll_path);
51 this->set_segment_id(seg_id);
52 }
53
54 //! Destructor
55 ~SimpleForwardIndexer();
56
57 public:
58 //! Open persist storage
59 int open(const ReadOptions &read_options) override;
60
61 //! Close persist storage
62 int close() override;
63
64 //! Flush memory to persist storage
65 int flush() override;
66
67 //! Dump forward index
68 int dump(IndexDumperPtr dumper) override;
69
70 public:
71 //! Insert forward data
72 int insert(const ForwardData &fwd_data, idx_t *doc_id) override;
73
74#if 0
75 //! Update forward data
76 int update(idx_t doc_id, const ForwardData &forward_data) override;
77#endif
78
79 //! Remove forward data
80 int remove(idx_t doc_id) override;
81
82 //! Seek forward data by doc id
83 int seek(idx_t doc_id, ForwardData *fwd_data) override;
84
85 public:
86 //! Return index path
87 std::string index_file_path() const override {
88 if (snapshot_) {
89 return snapshot_->file_path();
90 } else {
91 return "";
92 }
93 }
94
95 //! Return doc count
96 size_t doc_count() const override {
97 if (proxima_forward_) {
98 return proxima_forward_->count();
99 } else {
100 return 0U;
101 }
102 }
103
104 private:
105 int open_proxima_forward();
106
107 private:
108 SnapshotPtr snapshot_{};
109 IndexClosetPtr proxima_forward_{};
110
111 bool opened_{false};
112};
113
114
115} // end namespace index
116} // namespace be
117} // end namespace proxima
118