Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
MariaDB
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
MariaDB
Commits
cac1ec19
Commit
cac1ec19
authored
Sep 26, 2024
by
Sergei Golubchik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rename MHNSW_Context->MHNSW_Share
parent
fe9092a8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
35 deletions
+35
-35
sql/vector_mhnsw.cc
sql/vector_mhnsw.cc
+35
-35
No files found.
sql/vector_mhnsw.cc
View file @
cac1ec19
...
...
@@ -63,7 +63,7 @@ enum Graph_table_indices {
IDX_TREF
,
IDX_LAYER
};
class
MHNSW_
Context
;
class
MHNSW_
Share
;
class
FVectorNode
;
/*
...
...
@@ -216,7 +216,7 @@ struct FVector
It's mainly used to store all neighbors of a given node on a given layer.
An array is fixed size, 2*M for the zero layer, M for other layers
see MHNSW_
Context
::max_neighbors().
see MHNSW_
Share
::max_neighbors().
Number of neighbors is zero-padded to multiples of 8 (for SIMD Bloom filter).
...
...
@@ -263,7 +263,7 @@ struct Neighborhood: public Sql_alloc
class
FVectorNode
{
private:
MHNSW_
Context
*
ctx
;
MHNSW_
Share
*
ctx
;
const
FVector
*
make_vec
(
const
void
*
v
);
int
alloc_neighborhood
(
uint8_t
layer
);
...
...
@@ -273,8 +273,8 @@ class FVectorNode
uint8_t
max_layer
;
bool
stored
:
1
,
deleted
:
1
;
FVectorNode
(
MHNSW_
Context
*
ctx_
,
const
void
*
gref_
);
FVectorNode
(
MHNSW_
Context
*
ctx_
,
const
void
*
tref_
,
uint8_t
layer
,
FVectorNode
(
MHNSW_
Share
*
ctx_
,
const
void
*
gref_
);
FVectorNode
(
MHNSW_
Share
*
ctx_
,
const
void
*
tref_
,
uint8_t
layer
,
const
void
*
vec_
);
float
distance_to
(
const
FVector
*
other
)
const
;
int
load
(
TABLE
*
graph
);
...
...
@@ -294,7 +294,7 @@ class FVectorNode
Shared algorithm context. The graph.
Stored in TABLE_SHARE and on TABLE_SHARE::mem_root.
Stores the complete graph in MHNSW_
Context
::root,
Stores the complete graph in MHNSW_
Share
::root,
The mapping gref->FVectorNode is in the node_cache.
Both root and node_cache are protected by a cache_lock, but it's
needed when loading nodes and is not used when the whole graph is in memory.
...
...
@@ -307,7 +307,7 @@ class FVectorNode
MyISAM automatically gets exclusive write access because of the TL_WRITE,
but InnoDB has to use a dedicated ctx->commit_lock for that
*/
class
MHNSW_
Context
:
public
Sql_alloc
class
MHNSW_
Share
:
public
Sql_alloc
{
std
::
atomic
<
uint
>
refcnt
{
0
};
mysql_mutex_t
cache_lock
;
...
...
@@ -340,7 +340,7 @@ class MHNSW_Context : public Sql_alloc
const
uint
M
;
metric_type
metric
;
MHNSW_
Context
(
TABLE
*
t
)
MHNSW_
Share
(
TABLE
*
t
)
:
tref_len
(
t
->
file
->
ref_length
),
gref_len
(
t
->
hlindex
->
file
->
ref_length
),
M
(
static_cast
<
uint
>
(
t
->
s
->
key_info
[
t
->
s
->
keys
].
option_struct
->
M
)),
...
...
@@ -353,7 +353,7 @@ class MHNSW_Context : public Sql_alloc
init_alloc_root
(
PSI_INSTRUMENT_MEM
,
&
root
,
1024
*
1024
,
0
,
MYF
(
0
));
}
virtual
~
MHNSW_
Context
()
virtual
~
MHNSW_
Share
()
{
free_root
(
&
root
,
MYF
(
0
));
mysql_rwlock_destroy
(
&
commit_lock
);
...
...
@@ -387,13 +387,13 @@ class MHNSW_Context : public Sql_alloc
vec_len
=
len
/
sizeof
(
float
);
}
static
int
acquire
(
MHNSW_
Context
**
ctx
,
TABLE
*
table
,
bool
for_update
);
static
MHNSW_
Context
*
get_from_share
(
TABLE_SHARE
*
share
,
TABLE
*
table
);
static
int
acquire
(
MHNSW_
Share
**
ctx
,
TABLE
*
table
,
bool
for_update
);
static
MHNSW_
Share
*
get_from_share
(
TABLE_SHARE
*
share
,
TABLE
*
table
);
virtual
void
reset
(
TABLE_SHARE
*
share
)
{
share
->
lock_share
();
if
(
static_cast
<
MHNSW_
Context
*>
(
share
->
hlindex
->
hlindex_data
)
==
this
)
if
(
static_cast
<
MHNSW_
Share
*>
(
share
->
hlindex
->
hlindex_data
)
==
this
)
{
share
->
hlindex
->
hlindex_data
=
nullptr
;
--
refcnt
;
...
...
@@ -413,7 +413,7 @@ class MHNSW_Context : public Sql_alloc
if
(
root_size
(
&
root
)
>
mhnsw_cache_size
)
reset
(
share
);
if
(
--
refcnt
==
0
)
this
->~
MHNSW_
Context
();
// XXX reuse
this
->~
MHNSW_
Share
();
// XXX reuse
}
FVectorNode
*
get_node
(
const
void
*
gref
)
...
...
@@ -475,14 +475,14 @@ class MHNSW_Context : public Sql_alloc
one instance of trx per TABLE_SHARE and allocated on the
thd->transaction->mem_root
*/
class
MHNSW_Trx
:
public
MHNSW_
Context
class
MHNSW_Trx
:
public
MHNSW_
Share
{
public:
TABLE_SHARE
*
table_share
;
bool
list_of_nodes_is_lost
=
false
;
MHNSW_Trx
*
next
=
nullptr
;
MHNSW_Trx
(
TABLE
*
table
)
:
MHNSW_
Context
(
table
),
table_share
(
table
->
s
)
{}
MHNSW_Trx
(
TABLE
*
table
)
:
MHNSW_
Share
(
table
),
table_share
(
table
->
s
)
{}
void
reset
(
TABLE_SHARE
*
)
override
{
node_cache
.
clear
();
...
...
@@ -552,7 +552,7 @@ int MHNSW_Trx::do_commit(THD *thd, bool)
trx
;
trx
=
trx_next
)
{
trx_next
=
trx
->
next
;
auto
ctx
=
MHNSW_
Context
::
get_from_share
(
trx
->
table_share
,
nullptr
);
auto
ctx
=
MHNSW_
Share
::
get_from_share
(
trx
->
table_share
,
nullptr
);
if
(
ctx
)
{
mysql_rwlock_wrlock
(
&
ctx
->
commit_lock
);
...
...
@@ -601,13 +601,13 @@ MHNSW_Trx *MHNSW_Trx::get_from_thd(TABLE *table, bool for_update)
return
trx
;
}
MHNSW_
Context
*
MHNSW_Context
::
get_from_share
(
TABLE_SHARE
*
share
,
TABLE
*
table
)
MHNSW_
Share
*
MHNSW_Share
::
get_from_share
(
TABLE_SHARE
*
share
,
TABLE
*
table
)
{
share
->
lock_share
();
auto
ctx
=
static_cast
<
MHNSW_
Context
*>
(
share
->
hlindex
->
hlindex_data
);
auto
ctx
=
static_cast
<
MHNSW_
Share
*>
(
share
->
hlindex
->
hlindex_data
);
if
(
!
ctx
&&
table
)
{
ctx
=
new
(
&
share
->
hlindex
->
mem_root
)
MHNSW_
Context
(
table
);
ctx
=
new
(
&
share
->
hlindex
->
mem_root
)
MHNSW_
Share
(
table
);
if
(
!
ctx
)
return
nullptr
;
share
->
hlindex
->
hlindex_data
=
ctx
;
ctx
->
refcnt
++
;
...
...
@@ -618,13 +618,13 @@ MHNSW_Context *MHNSW_Context::get_from_share(TABLE_SHARE *share, TABLE *table)
return
ctx
;
}
int
MHNSW_
Context
::
acquire
(
MHNSW_Context
**
ctx
,
TABLE
*
table
,
bool
for_update
)
int
MHNSW_
Share
::
acquire
(
MHNSW_Share
**
ctx
,
TABLE
*
table
,
bool
for_update
)
{
TABLE
*
graph
=
table
->
hlindex
;
if
(
!
(
*
ctx
=
MHNSW_Trx
::
get_from_thd
(
table
,
for_update
)))
{
*
ctx
=
MHNSW_
Context
::
get_from_share
(
table
->
s
,
table
);
*
ctx
=
MHNSW_
Share
::
get_from_share
(
table
->
s
,
table
);
if
(
table
->
file
->
has_transactions
())
mysql_rwlock_rdlock
(
&
(
*
ctx
)
->
commit_lock
);
}
...
...
@@ -652,13 +652,13 @@ const FVector *FVectorNode::make_vec(const void *v)
return
FVector
::
create
(
ctx
->
metric
,
tref
()
+
tref_len
(),
v
,
ctx
->
byte_len
);
}
FVectorNode
::
FVectorNode
(
MHNSW_
Context
*
ctx_
,
const
void
*
gref_
)
FVectorNode
::
FVectorNode
(
MHNSW_
Share
*
ctx_
,
const
void
*
gref_
)
:
ctx
(
ctx_
),
stored
(
true
),
deleted
(
false
)
{
memcpy
(
gref
(),
gref_
,
gref_len
());
}
FVectorNode
::
FVectorNode
(
MHNSW_
Context
*
ctx_
,
const
void
*
tref_
,
uint8_t
layer
,
FVectorNode
::
FVectorNode
(
MHNSW_
Share
*
ctx_
,
const
void
*
tref_
,
uint8_t
layer
,
const
void
*
vec_
)
:
ctx
(
ctx_
),
stored
(
false
),
deleted
(
false
)
{
...
...
@@ -832,7 +832,7 @@ class VisitedSet
one extra candidate is specified separately to avoid appending it to
the Neighborhood candidates, which might be already at its max size.
*/
static
int
select_neighbors
(
MHNSW_
Context
*
ctx
,
TABLE
*
graph
,
size_t
layer
,
static
int
select_neighbors
(
MHNSW_
Share
*
ctx
,
TABLE
*
graph
,
size_t
layer
,
FVectorNode
&
target
,
const
Neighborhood
&
candidates
,
FVectorNode
*
extra_candidate
,
size_t
max_neighbor_connections
)
...
...
@@ -935,7 +935,7 @@ int FVectorNode::save(TABLE *graph)
return
err
;
}
static
int
update_second_degree_neighbors
(
MHNSW_
Context
*
ctx
,
TABLE
*
graph
,
static
int
update_second_degree_neighbors
(
MHNSW_
Share
*
ctx
,
TABLE
*
graph
,
size_t
layer
,
FVectorNode
*
node
)
{
const
uint
max_neighbors
=
ctx
->
max_neighbors
(
layer
);
...
...
@@ -968,7 +968,7 @@ static inline float generous_furthest(const Queue<Visited> &q, float maxd, float
return
d
*
(
1
+
(
g
-
1
)
/
2
*
(
1
-
sigmoid
));
}
static
int
search_layer
(
MHNSW_
Context
*
ctx
,
TABLE
*
graph
,
const
FVector
*
target
,
static
int
search_layer
(
MHNSW_
Share
*
ctx
,
TABLE
*
graph
,
const
FVector
*
target
,
Neighborhood
*
start_nodes
,
uint
result_size
,
size_t
layer
,
Neighborhood
*
result
,
bool
construction
)
{
...
...
@@ -1096,7 +1096,7 @@ int mhnsw_insert(TABLE *table, KEY *keyinfo)
MY_BITMAP
*
old_map
=
dbug_tmp_use_all_columns
(
table
,
&
table
->
read_set
);
Field
*
vec_field
=
keyinfo
->
key_part
->
field
;
String
buf
,
*
res
=
vec_field
->
val_str
(
&
buf
);
MHNSW_
Context
*
ctx
;
MHNSW_
Share
*
ctx
;
/* metadata are checked on open */
DBUG_ASSERT
(
graph
);
...
...
@@ -1116,7 +1116,7 @@ int mhnsw_insert(TABLE *table, KEY *keyinfo)
table
->
file
->
position
(
table
->
record
[
0
]);
int
err
=
MHNSW_
Context
::
acquire
(
&
ctx
,
table
,
true
);
int
err
=
MHNSW_
Share
::
acquire
(
&
ctx
,
table
,
true
);
SCOPE_EXIT
([
ctx
,
table
](){
ctx
->
release
(
table
);
});
if
(
err
)
{
...
...
@@ -1205,12 +1205,12 @@ int mhnsw_first(TABLE *table, KEY *keyinfo, Item *dist, ulonglong limit)
DBUG_ASSERT
(
fun
);
String
buf
,
*
res
=
fun
->
get_const_arg
()
->
val_str
(
&
buf
);
MHNSW_
Context
*
ctx
;
MHNSW_
Share
*
ctx
;
if
(
int
err
=
table
->
file
->
ha_rnd_init
(
0
))
return
err
;
int
err
=
MHNSW_
Context
::
acquire
(
&
ctx
,
table
,
false
);
int
err
=
MHNSW_
Share
::
acquire
(
&
ctx
,
table
,
false
);
SCOPE_EXIT
([
ctx
,
table
](){
ctx
->
release
(
table
);
});
if
(
err
)
return
err
;
...
...
@@ -1292,7 +1292,7 @@ void mhnsw_free(TABLE_SHARE *share)
if
(
!
graph_share
->
hlindex_data
)
return
;
static_cast
<
MHNSW_
Context
*>
(
graph_share
->
hlindex_data
)
->~
MHNSW_Context
();
static_cast
<
MHNSW_
Share
*>
(
graph_share
->
hlindex_data
)
->~
MHNSW_Share
();
graph_share
->
hlindex_data
=
0
;
}
...
...
@@ -1300,8 +1300,8 @@ int mhnsw_invalidate(TABLE *table, const uchar *rec, KEY *keyinfo)
{
TABLE
*
graph
=
table
->
hlindex
;
handler
*
h
=
table
->
file
;
MHNSW_
Context
*
ctx
;
bool
use_ctx
=
!
MHNSW_
Context
::
acquire
(
&
ctx
,
table
,
true
);
MHNSW_
Share
*
ctx
;
bool
use_ctx
=
!
MHNSW_
Share
::
acquire
(
&
ctx
,
table
,
true
);
SCOPE_EXIT
([
ctx
,
table
](){
ctx
->
release
(
table
);
});
/* metadata are checked on open */
...
...
@@ -1351,8 +1351,8 @@ int mhnsw_delete_all(TABLE *table, KEY *keyinfo, bool truncate)
:
graph
->
file
->
delete_all_rows
())
return
err
;
MHNSW_
Context
*
ctx
;
if
(
!
MHNSW_
Context
::
acquire
(
&
ctx
,
table
,
true
))
MHNSW_
Share
*
ctx
;
if
(
!
MHNSW_
Share
::
acquire
(
&
ctx
,
table
,
true
))
{
ctx
->
reset
(
table
->
s
);
ctx
->
release
(
table
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment