SegmentInterface.cpp 8.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// 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

#include "segcore/SegmentInterface.h"
F
FluorineDog 已提交
13
#include "query/generated/ExecPlanNodeVisitor.h"
14 15 16 17
namespace milvus::segcore {
class Naive;

void
18
SegmentInternalInterface::FillTargetEntry(const query::Plan* plan, SearchResult& results) const {
F
FluorineDog 已提交
19
    std::shared_lock lck(mutex_);
20 21 22
    AssertInfo(plan, "empty plan");
    auto size = results.result_distances_.size();
    Assert(results.internal_seg_offsets_.size() == size);
23
    // Assert(results.result_offsets_.size() == size);
24 25
    Assert(results.row_data_.size() == 0);

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    // std::vector<int64_t> row_ids(size);
    std::vector<int64_t> element_sizeofs;
    std::vector<aligned_vector<char>> blobs;

    // fill row_ids
    {
        aligned_vector<char> blob(size * sizeof(int64_t));
        if (plan->schema_.get_is_auto_id()) {
            bulk_subscript(SystemFieldType::RowId, results.internal_seg_offsets_.data(), size, blob.data());
        } else {
            auto key_offset_opt = get_schema().get_primary_key_offset();
            Assert(key_offset_opt.has_value());
            auto key_offset = key_offset_opt.value();
            Assert(get_schema()[key_offset].get_data_type() == DataType::INT64);
            bulk_subscript(key_offset, results.internal_seg_offsets_.data(), size, blob.data());
        }
        blobs.emplace_back(std::move(blob));
        element_sizeofs.push_back(sizeof(int64_t));
    }

    // fill other entries
    for (auto field_offset : plan->target_entries_) {
        auto& field_meta = get_schema()[field_offset];
        auto element_sizeof = field_meta.get_sizeof();
        aligned_vector<char> blob(size * element_sizeof);
        bulk_subscript(field_offset, results.internal_seg_offsets_.data(), size, blob.data());
        blobs.emplace_back(std::move(blob));
        element_sizeofs.push_back(element_sizeof);
54 55
    }

56 57
    auto target_sizeof = std::accumulate(element_sizeofs.begin(), element_sizeofs.end(), 0);

58
    for (int64_t i = 0; i < size; ++i) {
59 60 61 62 63 64 65 66 67 68 69 70
        int64_t element_offset = 0;
        std::vector<char> target(target_sizeof);
        for (int loc = 0; loc < blobs.size(); ++loc) {
            auto element_sizeof = element_sizeofs[loc];
            auto blob_ptr = blobs[loc].data();
            auto src = blob_ptr + element_sizeof * i;
            auto dst = target.data() + element_offset;
            memcpy(dst, src, element_sizeof);
            element_offset += element_sizeof;
        }
        assert(element_offset == target_sizeof);
        results.row_data_.emplace_back(std::move(target));
71 72
    }
}
F
FluorineDog 已提交
73

74
SearchResult
F
FluorineDog 已提交
75
SegmentInternalInterface::Search(const query::Plan* plan,
76 77
                                 const query::PlaceholderGroup& placeholder_group,
                                 Timestamp timestamp) const {
F
FluorineDog 已提交
78
    std::shared_lock lck(mutex_);
C
cai.zhang 已提交
79
    check_search(plan);
80
    query::ExecPlanNodeVisitor visitor(*this, timestamp, placeholder_group);
F
FluorineDog 已提交
81
    auto results = visitor.get_moved_result(*plan->plan_node_);
82
    results.segment_ = (void*)this;
F
FluorineDog 已提交
83 84 85
    return results;
}

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
// Note: this is temporary solution.
// modify bulk script implement to make process more clear
static std::unique_ptr<ScalarArray>
CreateScalarArrayFrom(const void* data_raw, int64_t count, DataType data_type) {
    auto scalar_array = std::make_unique<ScalarArray>();
    switch (data_type) {
        case DataType::BOOL: {
            auto data = reinterpret_cast<const double*>(data_raw);
            auto obj = scalar_array->mutable_bool_data();
            obj->mutable_data()->Add(data, data + count);
            break;
        }
        case DataType::INT8: {
            auto data = reinterpret_cast<const int8_t*>(data_raw);
            auto obj = scalar_array->mutable_int_data();
            obj->mutable_data()->Add(data, data + count);
            break;
        }
        case DataType::INT16: {
            auto data = reinterpret_cast<const int16_t*>(data_raw);
            auto obj = scalar_array->mutable_int_data();
            obj->mutable_data()->Add(data, data + count);
            break;
        }
        case DataType::INT32: {
            auto data = reinterpret_cast<const int16_t*>(data_raw);
            auto obj = scalar_array->mutable_int_data();
            obj->mutable_data()->Add(data, data + count);
            break;
        }
        case DataType::INT64: {
            auto data = reinterpret_cast<const int64_t*>(data_raw);
            auto obj = scalar_array->mutable_long_data();
            obj->mutable_data()->Add(data, data + count);
            break;
        }
        case DataType::FLOAT: {
            auto data = reinterpret_cast<const float*>(data_raw);
            auto obj = scalar_array->mutable_float_data();
            obj->mutable_data()->Add(data, data + count);
            break;
        }
        case DataType::DOUBLE: {
            auto data = reinterpret_cast<const double*>(data_raw);
            auto obj = scalar_array->mutable_double_data();
            obj->mutable_data()->Add(data, data + count);
            break;
        }
        default: {
            PanicInfo("unsupported datatype");
        }
    }
    return scalar_array;
}

static std::unique_ptr<DataArray>
CreateDataArrayFrom(const void* data_raw, int64_t count, const FieldMeta& field_meta) {
    auto data_type = field_meta.get_data_type();
    auto data_array = std::make_unique<DataArray>();
145
    data_array->set_field_id(field_meta.get_id().get());
146
    data_array->set_type(milvus::proto::schema::DataType(field_meta.get_data_type()));
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

    if (!datatype_is_vector(data_type)) {
        auto scalar_array = CreateScalarArrayFrom(data_raw, count, data_type);
        data_array->set_allocated_scalars(scalar_array.release());
    } else {
        auto vector_array = data_array->mutable_vectors();
        auto dim = field_meta.get_dim();
        vector_array->set_dim(dim);
        switch (data_type) {
            case DataType::VECTOR_FLOAT: {
                auto length = count * dim;
                auto data = reinterpret_cast<const float*>(data_raw);
                auto obj = vector_array->mutable_float_vector();
                obj->mutable_data()->Add(data, data + length);
                break;
            }
            case DataType::VECTOR_BINARY: {
                Assert(dim % 8 == 0);
                auto num_bytes = count * dim / 8;
                auto data = reinterpret_cast<const char*>(data_raw);
                auto obj = vector_array->mutable_binary_vector();
                obj->assign(data, num_bytes);
                break;
            }
            default: {
172
                PanicInfo("unsupported datatype");
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
            }
        }
    }
    return data_array;
}

std::unique_ptr<DataArray>
SegmentInternalInterface::BulkSubScript(FieldOffset field_offset, const SegOffset* seg_offsets, int64_t count) const {
    if (field_offset.get() >= 0) {
        auto& field_meta = get_schema()[field_offset];
        aligned_vector<char> data(field_meta.get_sizeof() * count);
        bulk_subscript(field_offset, (const int64_t*)seg_offsets, count, data.data());
        return CreateDataArrayFrom(data.data(), count, field_meta);
    } else {
        Assert(field_offset.get() == -1);
        aligned_vector<char> data(sizeof(int64_t) * count);
        bulk_subscript(SystemFieldType::RowId, (const int64_t*)seg_offsets, count, data.data());
        return CreateDataArrayFrom(data.data(), count, FieldMeta::RowIdMeta);
    }
}

194
std::unique_ptr<proto::segcore::RetrieveResults>
195 196 197
SegmentInternalInterface::GetEntityById(const std::vector<FieldOffset>& field_offsets,
                                        const IdArray& id_array,
                                        Timestamp timestamp) const {
198
    auto results = std::make_unique<proto::segcore::RetrieveResults>();
199

200
    auto [ids_, seg_offsets] = search_ids(id_array, timestamp);
F
FluorineDog 已提交
201 202 203 204 205 206 207

    // std::string dbg_log;
    // dbg_log += "id_array:" + id_array.DebugString() + "\n";
    // dbg_log += "ids:" + ids_->DebugString() + "\n";
    // dbg_log += "segment_info:" + this->debug();
    // std::cout << dbg_log << std::endl;

208 209
    results->set_allocated_ids(ids_.release());

210 211 212 213
    for (auto& seg_offset : seg_offsets) {
        results->add_offset(seg_offset.get());
    }

214 215 216 217 218 219 220
    auto fields_data = results->mutable_fields_data();
    for (auto field_offset : field_offsets) {
        auto col = BulkSubScript(field_offset, seg_offsets.data(), seg_offsets.size());
        fields_data->AddAllocated(col.release());
    }
    return results;
}
221
}  // namespace milvus::segcore