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 Implementation of persist segment manager
19 */
20
21#include "persist_segment_manager.h"
22#include "common/error_code.h"
23#include "common/logger.h"
24#include "typedef.h"
25
26namespace proxima {
27namespace be {
28namespace index {
29
30PersistSegmentManagerPtr PersistSegmentManager::Create(
31 const std::string &collection_name, const std::string &collection_path) {
32 PersistSegmentManagerPtr new_mgr =
33 std::make_shared<PersistSegmentManager>(collection_name, collection_path);
34 return new_mgr;
35}
36
37PersistSegmentManager::~PersistSegmentManager() {
38 if (segments_.size() > 0) {
39 unload_segments();
40 }
41}
42
43void PersistSegmentManager::add_segment(PersistSegmentPtr persist_segment) {
44 segments_.emplace(persist_segment->segment_id(), persist_segment);
45}
46
47const PersistSegmentPtr &PersistSegmentManager::get_segment(
48 SegmentID segment_id) {
49 return segments_.get(segment_id);
50}
51
52const PersistSegmentPtr &PersistSegmentManager::get_latest_segment() {
53 SegmentID max_segment_id = 0U;
54 for (auto &it : segments_) {
55 if (it.first > max_segment_id) {
56 max_segment_id = it.first;
57 }
58 }
59 return segments_.get(max_segment_id);
60}
61
62int PersistSegmentManager::unload_segments() {
63 for (auto iter = segments_.begin(); iter != segments_.end(); iter++) {
64 iter->second->unload();
65 }
66 segments_.clear();
67 return 0;
68}
69
70
71} // end namespace index
72} // namespace be
73} // end namespace proxima
74