SnapshotVisitor.h 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under the License.

#pragma once

#include "db/meta/FilesHolder.h"
#include "db/snapshot/Snapshot.h"

17
#include <map>
18
#include <memory>
19
#include <set>
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <string>

namespace milvus {
namespace engine {

class SnapshotVisitor {
 public:
    explicit SnapshotVisitor(snapshot::ScopedSnapshotT ss);
    explicit SnapshotVisitor(const std::string& collection_name);
    explicit SnapshotVisitor(snapshot::ID_TYPE collection_id);

    Status
    SegmentsToSearch(meta::FilesHolder& files_holder);

 protected:
    snapshot::ScopedSnapshotT ss_;
    Status status_;
};

39
class SegmentFieldElementVisitor {
40
 public:
41
    using Ptr = std::shared_ptr<SegmentFieldElementVisitor>;
42 43

    static Ptr
44
    Build(snapshot::ScopedSnapshotT ss, snapshot::ID_TYPE segment_id, snapshot::ID_TYPE field_element_id);
45

46
    SegmentFieldElementVisitor() = default;
47

48 49 50 51 52 53 54 55 56 57 58 59 60
    void
    SetFieldElement(snapshot::FieldElementPtr field_element) {
        field_element_ = field_element;
    }
    void
    SetFile(snapshot::SegmentFilePtr file) {
        file_ = file;
    }

    const snapshot::FieldElementPtr
    GetElement() const {
        return field_element_;
    }
61 62 63 64
    const snapshot::SegmentFilePtr
    GetFile() const {
        return file_;
    }
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

 protected:
    snapshot::FieldElementPtr field_element_;
    snapshot::SegmentFilePtr file_;
};

class SegmentFieldVisitor {
 public:
    using Ptr = std::shared_ptr<SegmentFieldVisitor>;
    using ElementT = typename SegmentFieldElementVisitor::Ptr;
    using ElementsMapT = std::map<snapshot::ID_TYPE, ElementT>;

    static Ptr
    Build(snapshot::ScopedSnapshotT ss, snapshot::ID_TYPE segment_id, snapshot::ID_TYPE field_id);

    SegmentFieldVisitor() = default;

    const ElementsMapT&
    GetElementVistors() const {
        return elements_map_;
    }
    const snapshot::FieldPtr&
87 88 89 90 91 92 93 94
    GetField() const {
        return field_;
    }

    void
    SetField(snapshot::FieldPtr field) {
        field_ = field;
    }
95

96
    void
97 98
    InsertElement(ElementT element) {
        elements_map_[element->GetElement()->GetID()] = element;
99 100 101
    }

 protected:
102
    ElementsMapT elements_map_;
103 104 105 106 107 108
    snapshot::FieldPtr field_;
};

class SegmentVisitor {
 public:
    using Ptr = std::shared_ptr<SegmentVisitor>;
109 110 111
    using FieldVisitorT = typename SegmentFieldVisitor::Ptr;
    using IdMapT = std::map<snapshot::ID_TYPE, FieldVisitorT>;
    using NameMapT = std::map<std::string, FieldVisitorT>;
112 113 114 115 116

    static Ptr
    Build(snapshot::ScopedSnapshotT ss, snapshot::ID_TYPE segment_id);
    SegmentVisitor() = default;

117 118 119
    const IdMapT&
    GetFieldVisitors() const {
        return id_map_;
120
    }
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

    FieldVisitorT
    GetFieldVisitor(snapshot::ID_TYPE field_id) const {
        auto it = id_map_.find(field_id);
        if (it == id_map_.end()) {
            return nullptr;
        }
        return it->second;
    }
    FieldVisitorT
    GetFieldVisitor(const std::string& field_name) const {
        auto it = name_map_.find(field_name);
        if (it == name_map_.end()) {
            return nullptr;
        }
        return it->second;
    }

139 140 141 142 143 144 145 146 147 148
    const snapshot::SegmentPtr&
    GetSegment() const {
        return segment_;
    }

    void
    SetSegment(snapshot::SegmentPtr segment) {
        segment_ = segment;
    }
    void
149 150 151
    InsertField(FieldVisitorT field_visitor) {
        id_map_[field_visitor->GetField()->GetID()] = field_visitor;
        name_map_[field_visitor->GetField()->GetName()] = field_visitor;
152 153
    }

154 155 156
    std::string
    ToString() const;

157 158
 protected:
    snapshot::SegmentPtr segment_;
159 160
    IdMapT id_map_;
    NameMapT name_map_;
161 162
};

163 164
}  // namespace engine
}  // namespace milvus