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 Jan 2020
18 * \brief Interface of AiTheta Index Storage
19 */
20
21#ifndef __AITHETA2_INDEX_STORAGE_H__
22#define __AITHETA2_INDEX_STORAGE_H__
23
24#include "index_module.h"
25#include "index_params.h"
26
27namespace aitheta2 {
28
29/*! Index Storage
30 */
31class IndexStorage : public IndexModule {
32 public:
33 //! Index Storage Pointer
34 typedef std::shared_ptr<IndexStorage> Pointer;
35
36 /*! Index Storage Segment
37 */
38 struct Segment {
39 //! Index Storage Pointer
40 typedef std::shared_ptr<Segment> Pointer;
41
42 //! Destructor
43 virtual ~Segment(void) {}
44
45 //! Retrieve size of data
46 virtual size_t data_size(void) const = 0;
47
48 //! Retrieve crc of data
49 virtual uint32_t data_crc(void) const = 0;
50
51 //! Retrieve size of padding
52 virtual size_t padding_size(void) const = 0;
53
54 //! Retrieve capacity of segment
55 virtual size_t capacity(void) const = 0;
56
57 //! Fetch data from segment (with own buffer)
58 virtual size_t fetch(size_t offset, void *buf, size_t len) const = 0;
59
60 //! Read data from segment
61 virtual size_t read(size_t offset, const void **data, size_t len) = 0;
62
63 //! Write data into the storage with offset
64 virtual size_t write(size_t offset, const void *data, size_t len) = 0;
65
66 //! Resize size of data
67 virtual size_t resize(size_t size) = 0;
68
69 //! Update crc of data
70 virtual void update_data_crc(uint32_t crc) = 0;
71
72 //! Clone the segment
73 virtual Pointer clone(void) = 0;
74 };
75
76 //! Destructor
77 virtual ~IndexStorage(void) {}
78
79 //! Initialize storage
80 virtual int init(const IndexParams &params) = 0;
81
82 //! Cleanup storage
83 virtual int cleanup(void) = 0;
84
85 //! Open storage
86 virtual int open(const std::string &path, bool create) = 0;
87
88 //! Flush storage
89 virtual int flush(void) = 0;
90
91 //! Close storage
92 virtual int close(void) = 0;
93
94 //! Append a segment into storage
95 virtual int append(const std::string &id, size_t size) = 0;
96
97 //! Refresh meta information (checksum, update time, etc.)
98 virtual void refresh(uint64_t check_point) = 0;
99
100 //! Retrieve check point of storage
101 virtual uint64_t check_point(void) const = 0;
102
103 //! Retrieve a segment by id
104 virtual Segment::Pointer get(const std::string &id) = 0;
105
106 //! Test if it a segment exists
107 virtual bool has(const std::string &id) const = 0;
108
109 //! Retrieve magic number of index
110 virtual uint32_t magic(void) const = 0;
111};
112
113} // namespace aitheta2
114
115#endif // __AITHETA2_INDEX_STORAGE_H__
116