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 Hechong.xyf
17 * \date Oct 2019
18 * \brief Interface of AiTheta Index Context
19 */
20
21#ifndef __AITHETA2_INDEX_CONTEXT_H__
22#define __AITHETA2_INDEX_CONTEXT_H__
23
24#include <memory>
25#include "index_document.h"
26#include "index_error.h"
27#include "index_filter.h"
28#include "index_params.h"
29
30namespace aitheta2 {
31
32/*! Index Context
33 */
34class IndexContext {
35 public:
36 //! Index Context Pointer
37 typedef std::unique_ptr<IndexContext> Pointer;
38
39 //! Destructor
40 virtual ~IndexContext(void) {}
41
42 //! Set topk of search result
43 virtual void set_topk(uint32_t topk) = 0;
44
45 //! Set mode of debug
46 virtual void set_debug_mode(bool /*enable*/) {}
47
48 //! Retrieve search result
49 virtual const IndexDocumentList &result(void) const = 0;
50
51 //! Retrieve search result with index
52 virtual const IndexDocumentList &result(size_t /*index*/) const {
53 return this->result();
54 }
55
56 //! Update the parameters of context
57 virtual int update(const IndexParams & /*params*/) {
58 return IndexError_NotImplemented;
59 }
60
61 //! Retrieve mode of debug
62 virtual bool debug_mode(void) const {
63 return false;
64 }
65
66 //! Retrieve debug information
67 virtual std::string debug_string(void) const {
68 return std::string();
69 }
70
71 //! Retrieve magic number
72 virtual uint32_t magic(void) const {
73 return 0;
74 }
75
76 //! Retrieve search filter
77 const IndexFilter &filter(void) const {
78 return filter_;
79 }
80
81 //! Set the filter of context
82 template <typename T>
83 void set_filter(T &&func) {
84 filter_.set(std::forward<T>(func));
85 }
86
87 //! Reset the filter of context
88 void reset_filter(void) {
89 filter_.reset();
90 }
91
92 //! Set threshold for RNN
93 void set_threshold(float val) {
94 threshold_ = val;
95 }
96
97 //! Retrieve value of threshold for RNN
98 float threshold(void) const {
99 return threshold_;
100 }
101
102 //! Generate a global magic number
103 static uint32_t GenerateMagic(void);
104
105 private:
106 //! Members
107 IndexFilter filter_{};
108 float threshold_{std::numeric_limits<float>::max()};
109};
110
111} // namespace aitheta2
112
113#endif // __AITHETA2_INDEX_CONTEXT_H__
114