mirror of
https://gitee.com/milvus-io/milvus.git
synced 2026-01-07 19:31:51 +08:00
This commit adds sparse float vector support to segcore with the following: 1. data type enum declarations 2. Adds corresponding data structures for handling sparse float vectors in various scenarios, including: * FieldData as a bridge between the binlog and the in memory data structures * mmap::Column as the in memory representation of a sparse float vector column of a sealed segment; * ConcurrentVector as the in memory representation of a sparse float vector of a growing segment which supports inserts. 3. Adds logic in payload reader/writer to serialize/deserialize from/to binlog 4. Adds the ability to allow the index node to build sparse float vector index 5. Adds the ability to allow the query node to build growing index for growing segment and temp index for sealed segment without index built This commit also includes some code cleanness, comment improvement, and some unit tests for sparse vector. https://github.com/milvus-io/milvus/issues/29419 Signed-off-by: Buqian Zheng <zhengbuqian@gmail.com>
152 lines
3.6 KiB
C++
152 lines
3.6 KiB
C++
// Licensed to the LF AI & Data foundation 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.
|
|
|
|
#pragma once
|
|
|
|
#include <cassert>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <type_traits>
|
|
|
|
#include "Types.h"
|
|
#include "VectorTrait.h"
|
|
|
|
namespace milvus {
|
|
// type erasure to work around virtual restriction
|
|
class SpanBase {
|
|
public:
|
|
explicit SpanBase(const void* data,
|
|
int64_t row_count,
|
|
int64_t element_sizeof)
|
|
: data_(data), row_count_(row_count), element_sizeof_(element_sizeof) {
|
|
}
|
|
|
|
int64_t
|
|
row_count() const {
|
|
return row_count_;
|
|
}
|
|
|
|
int64_t
|
|
element_sizeof() const {
|
|
return element_sizeof_;
|
|
}
|
|
|
|
const void*
|
|
data() const {
|
|
return data_;
|
|
}
|
|
|
|
private:
|
|
const void* data_;
|
|
int64_t row_count_;
|
|
int64_t element_sizeof_;
|
|
};
|
|
|
|
template <typename T, typename Enable = void>
|
|
class Span;
|
|
|
|
// TODO: refine Span to support T=FloatVector
|
|
template <typename T>
|
|
class Span<T,
|
|
typename std::enable_if_t<IsSparse<T> || IsScalar<T> ||
|
|
std::is_same_v<T, PkType>>> {
|
|
public:
|
|
using embedded_type = T;
|
|
explicit Span(const T* data, int64_t row_count)
|
|
: data_(data), row_count_(row_count) {
|
|
}
|
|
|
|
explicit Span(std::string_view data) {
|
|
Span(data.data(), data.size());
|
|
}
|
|
|
|
operator SpanBase() const {
|
|
return SpanBase(data_, row_count_, sizeof(T));
|
|
}
|
|
|
|
explicit Span(const SpanBase& base)
|
|
: Span(reinterpret_cast<const T*>(base.data()), base.row_count()) {
|
|
assert(base.element_sizeof() == sizeof(T));
|
|
}
|
|
|
|
int64_t
|
|
element_sizeof() const {
|
|
return sizeof(T);
|
|
}
|
|
|
|
const T*
|
|
data() const {
|
|
return data_;
|
|
}
|
|
|
|
const T&
|
|
operator[](int64_t offset) const {
|
|
return data_[offset];
|
|
}
|
|
|
|
int64_t
|
|
row_count() const {
|
|
return row_count_;
|
|
}
|
|
|
|
private:
|
|
const T* data_;
|
|
const int64_t row_count_;
|
|
};
|
|
|
|
template <typename VectorType>
|
|
class Span<
|
|
VectorType,
|
|
typename std::enable_if_t<std::is_base_of_v<VectorTrait, VectorType>>> {
|
|
public:
|
|
using embedded_type = typename VectorType::embedded_type;
|
|
|
|
Span(const embedded_type* data, int64_t row_count, int64_t element_sizeof)
|
|
: row_count_(row_count), data_(data), element_sizeof_(element_sizeof) {
|
|
}
|
|
|
|
explicit Span(const SpanBase& base)
|
|
: data_(reinterpret_cast<const embedded_type*>(base.data())),
|
|
row_count_(base.row_count()),
|
|
element_sizeof_(base.element_sizeof()) {
|
|
}
|
|
|
|
operator SpanBase() const {
|
|
return SpanBase(data_, row_count_, element_sizeof_);
|
|
}
|
|
|
|
int64_t
|
|
element_sizeof() const {
|
|
return element_sizeof_;
|
|
}
|
|
|
|
const embedded_type*
|
|
data() const {
|
|
return data_;
|
|
}
|
|
|
|
int64_t
|
|
row_count() const {
|
|
return row_count_;
|
|
}
|
|
|
|
private:
|
|
const embedded_type* data_;
|
|
const int64_t row_count_;
|
|
const int64_t element_sizeof_;
|
|
};
|
|
} // namespace milvus
|