forked from microsoft/MSVBASE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhnswindex_builder.cpp
144 lines (126 loc) · 4.19 KB
/
hnswindex_builder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "hnswindex_builder.hpp"
#include "util.hpp"
extern "C"
{
#include <access/tableam.h>
#include <storage/block.h>
#include <storage/bufmgr.h>
#include <utils/array.h>
#include <utils/elog.h>
#include <utils/fmgrprotos.h>
#include <utils/lsyscache.h>
#include <utils/numeric.h>
#include <utils/relcache.h>
}
HNSWIndexBuilder::HNSWIndexBuilder(Relation p_heap,
Relation p_index,
IndexInfo *p_indexInfo)
{
m_heap = p_heap;
m_index = p_index;
m_indexInfo = p_indexInfo;
m_numDimension = 0;
}
void HNSWIndexBuilder::ConstructInternalBuilder(DistanceMethod distance_method)
{
ComputeElements();
switch (distance_method)
{
case DistanceMethod::L2:
distance = std::make_shared<hnswlib::L2Space>(m_numDimension);
break;
case DistanceMethod::InnerProduct:
distance = std::make_shared<hnswlib::InnerProductSpace>(m_numDimension);
break;
default:
exit(1);
}
vector_index = std::make_shared<hnswlib::HierarchicalNSW<float>>(
distance.get(), m_indtuples * 10);
}
void HNSWComputeElementsCallback(Relation index,
ItemPointer tid,
Datum *values,
bool *isnull,
bool tupleIsAlive,
void *state)
{
HNSWIndexBuilder &self = *reinterpret_cast<HNSWIndexBuilder *>(state);
Datum value = values[0];
if (*isnull)
{
return;
}
// retrieve array and perform some checks
auto array = convert_array_to_vector(value);
[[unlikely]] if (self.m_numDimension == 0)
{
self.m_numDimension = array.size();
}
if (static_cast<size_t>(self.m_numDimension) != array.size())
{
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("inconsistent array length, expected %d, found %ld",
self.m_numDimension,
array.size())));
}
self.m_indtuples += 1;
}
void HNSWIndexBuilder::ComputeElements()
{
m_reltuples = table_index_build_scan(m_heap,
m_index,
m_indexInfo,
false,
true,
HNSWComputeElementsCallback,
reinterpret_cast<void *>(this),
NULL);
}
void HNSWLoadTupleCallback(Relation index,
ItemPointer tid,
Datum *values,
bool *isnull,
bool tupleIsAlive,
void *state)
{
HNSWIndexBuilder &self = *reinterpret_cast<HNSWIndexBuilder *>(state);
Datum value = values[0];
if (*isnull)
{
return;
}
// retrieve array and perform some checks
auto array = convert_array_to_vector(value);
if (static_cast<size_t>(self.m_numDimension) != array.size())
{
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("inconsistent array length, expected %d, found %ld",
self.m_numDimension,
array.size())));
}
std::int32_t blockId = ItemPointerGetBlockNumberNoCheck(tid);
std::int32_t offset = ItemPointerGetOffsetNumberNoCheck(tid);
std::uint64_t number = blockId;
number = (number << 32) + offset;
self.vector_index->addPoint((char *)array.data(), number);
}
void HNSWIndexBuilder::LoadTuples()
{
table_index_build_scan(m_heap,
m_index,
m_indexInfo,
false,
true,
HNSWLoadTupleCallback,
reinterpret_cast<void *>(this),
NULL);
}
void HNSWIndexBuilder::SaveIndex(const std::string &p_path)
{
vector_index->saveIndex(p_path);
}