VectorSource.cpp 3.1 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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.


G
groot 已提交
19
#include "db/insert/VectorSource.h"
G
groot 已提交
20 21
#include "db/engine/ExecutionEngine.h"
#include "db/engine/EngineFactory.h"
G
groot 已提交
22
#include "utils/Log.h"
Z
zhiru 已提交
23
#include "metrics/Metrics.h"
Z
zhiru 已提交
24 25 26 27 28 29 30

namespace zilliz {
namespace milvus {
namespace engine {

VectorSource::VectorSource(const size_t &n,
                           const float *vectors) :
G
groot 已提交
31 32 33
    n_(n),
    vectors_(vectors),
    id_generator_(std::make_shared<SimpleIDGenerator>()) {
Z
zhiru 已提交
34 35 36
    current_num_vectors_added = 0;
}

G
groot 已提交
37 38 39 40 41 42
Status
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 已提交
43
    server::CollectAddMetrics metrics(n_, table_file_schema.dimension_);
Z
zhiru 已提交
44

Z
zhiru 已提交
45 46
    num_vectors_added = current_num_vectors_added + num_vectors_to_add <= n_ ?
                        num_vectors_to_add : n_ - current_num_vectors_added;
Z
zhiru 已提交
47
    IDNumbers vector_ids_to_add;
Y
Yu Kun 已提交
48 49 50 51 52
    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++) {
G
groot 已提交
53
            vector_ids_to_add[pos - current_num_vectors_added] = vector_ids[pos];
Y
Yu Kun 已提交
54 55
        }
    }
Z
zhiru 已提交
56 57 58
    Status status = execution_engine->AddWithIds(num_vectors_added,
                                                 vectors_ + current_num_vectors_added * table_file_schema.dimension_,
                                                 vector_ids_to_add.data());
Z
zhiru 已提交
59 60
    if (status.ok()) {
        current_num_vectors_added += num_vectors_added;
Z
zhiru 已提交
61 62 63
        vector_ids_.insert(vector_ids_.end(),
                           std::make_move_iterator(vector_ids_to_add.begin()),
                           std::make_move_iterator(vector_ids_to_add.end()));
Z
update  
zhiru 已提交
64
    } else {
Z
zhiru 已提交
65 66 67 68 69 70
        ENGINE_LOG_ERROR << "VectorSource::Add failed: " + status.ToString();
    }

    return status;
}

G
groot 已提交
71 72
size_t
VectorSource::GetNumVectorsAdded() {
Z
zhiru 已提交
73 74 75
    return current_num_vectors_added;
}

G
groot 已提交
76 77
bool
VectorSource::AllAdded() {
Z
zhiru 已提交
78 79 80
    return (current_num_vectors_added == n_);
}

G
groot 已提交
81 82
IDNumbers
VectorSource::GetVectorIds() {
Z
zhiru 已提交
83 84 85 86 87
    return vector_ids_;
}

} // namespace engine
} // namespace milvus
G
groot 已提交
88
} // namespace zilliz