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 Provides ability for management of persist segments
19 */
20
21#pragma once
22
23#include "common/macro_define.h"
24#include "persist_segment.h"
25#include "../concurrent_hash_map.h"
26
27namespace proxima {
28namespace be {
29namespace index {
30
31class PersistSegmentManager;
32using PersistSegmentManagerPtr = std::shared_ptr<PersistSegmentManager>;
33
34/*
35 * PersistSegmentManager is mainly for managing persist segments.
36 */
37class PersistSegmentManager {
38 public:
39 //! Constructor
40 PersistSegmentManager(const std::string &coll_name,
41 const std::string &coll_path)
42 : collection_name_(coll_name), collection_path_(coll_path) {}
43
44 //! Destructor
45 ~PersistSegmentManager();
46
47 //! Create an instance
48 static PersistSegmentManagerPtr Create(const std::string &collection_name,
49 const std::string &collection_path);
50
51 public:
52 //! Add persist segment
53 void add_segment(PersistSegmentPtr persist_segment);
54
55 //! Get a specific segment
56 const PersistSegmentPtr &get_segment(SegmentID segment_id);
57
58 //! Get latest put-in segment
59 const PersistSegmentPtr &get_latest_segment();
60
61 //! Upload all segments
62 int unload_segments();
63
64 //! Return whether has some segment
65 bool has_segment(SegmentID segment_id) {
66 return segments_.has(segment_id);
67 }
68
69 //! Return loaded persist segment count
70 size_t segment_count() {
71 return segments_.size();
72 }
73
74 private:
75 std::string collection_name_{};
76 std::string collection_path_{};
77
78 ConcurrentHashMap<SegmentID, PersistSegmentPtr> segments_{};
79};
80
81
82} // end namespace index
83} // namespace be
84} // end namespace proxima
85