VectorSource.cpp 2.9 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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.

S
starlord 已提交
18
#include "db/insert/VectorSource.h"
S
starlord 已提交
19
#include "db/engine/EngineFactory.h"
S
starlord 已提交
20
#include "db/engine/ExecutionEngine.h"
Z
zhiru 已提交
21
#include "metrics/Metrics.h"
S
starlord 已提交
22
#include "utils/Log.h"
Z
zhiru 已提交
23 24 25 26

namespace milvus {
namespace engine {

S
starlord 已提交
27 28
VectorSource::VectorSource(const size_t& n, const float* vectors)
    : n_(n), vectors_(vectors), id_generator_(std::make_shared<SimpleIDGenerator>()) {
Z
zhiru 已提交
29 30 31
    current_num_vectors_added = 0;
}

S
starlord 已提交
32
Status
S
starlord 已提交
33 34
VectorSource::Add(const ExecutionEnginePtr& execution_engine, const meta::TableFileSchema& table_file_schema,
                  const size_t& num_vectors_to_add, size_t& num_vectors_added, IDNumbers& vector_ids) {
Y
Yu Kun 已提交
35
    server::CollectAddMetrics metrics(n_, table_file_schema.dimension_);
Z
zhiru 已提交
36

S
starlord 已提交
37 38
    num_vectors_added =
        current_num_vectors_added + num_vectors_to_add <= n_ ? num_vectors_to_add : n_ - current_num_vectors_added;
Z
zhiru 已提交
39
    IDNumbers vector_ids_to_add;
Y
Yu Kun 已提交
40 41 42 43 44
    if (vector_ids.empty()) {
        id_generator_->GetNextIDNumbers(num_vectors_added, vector_ids_to_add);
    } else {
        vector_ids_to_add.resize(num_vectors_added);
        for (int pos = current_num_vectors_added; pos < current_num_vectors_added + num_vectors_added; pos++) {
S
starlord 已提交
45
            vector_ids_to_add[pos - current_num_vectors_added] = vector_ids[pos];
Y
Yu Kun 已提交
46 47
        }
    }
Z
zhiru 已提交
48 49 50
    Status status = execution_engine->AddWithIds(num_vectors_added,
                                                 vectors_ + current_num_vectors_added * table_file_schema.dimension_,
                                                 vector_ids_to_add.data());
Z
zhiru 已提交
51 52
    if (status.ok()) {
        current_num_vectors_added += num_vectors_added;
S
starlord 已提交
53
        vector_ids_.insert(vector_ids_.end(), std::make_move_iterator(vector_ids_to_add.begin()),
Z
zhiru 已提交
54
                           std::make_move_iterator(vector_ids_to_add.end()));
Z
update  
zhiru 已提交
55
    } else {
Z
zhiru 已提交
56 57 58 59 60 61
        ENGINE_LOG_ERROR << "VectorSource::Add failed: " + status.ToString();
    }

    return status;
}

S
starlord 已提交
62 63
size_t
VectorSource::GetNumVectorsAdded() {
Z
zhiru 已提交
64 65 66
    return current_num_vectors_added;
}

S
starlord 已提交
67 68
bool
VectorSource::AllAdded() {
Z
zhiru 已提交
69 70 71
    return (current_num_vectors_added == n_);
}

S
starlord 已提交
72 73
IDNumbers
VectorSource::GetVectorIds() {
Z
zhiru 已提交
74 75 76
    return vector_ids_;
}

S
starlord 已提交
77 78
}  // namespace engine
}  // namespace milvus