# 2026-03-31 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # # This file contains tests for vec1. Specifically, it tests that the # index does not grow indefinitely if there are lots of inserts/deletes # and updates on the table. # source [file join [file dirname [info script]] vec1_common.tcl] set testprefix vec1growth vec1_load db foreach {tn nMeta} { 1 0 2 1 3 2 } { set lMeta [list] set cols "" set exprs "" for {set ii 0} {$ii<$nMeta} {incr ii} { lappend lMeta c$ii append cols ", c$ii" append exprs ", i+$ii" } do_execsql_test 1.$tn.0 " DROP TABLE IF EXISTS v1; CREATE VIRTUAL TABLE v1 USING vec1(vec $cols); " do_execsql_test 1.1 " WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1_000 ) INSERT INTO v1(rowid, vec $cols) SELECT i, random_vector(32) $exprs FROM s; " do_execsql_test 1.2 { INSERT INTO v1(cmd, arg) VALUES('rebuild', ( SELECT vec1_train(vec, '{nbucket:16, codesize:0}') FROM v1 )); } do_execsql_test 1.2.ic { PRAGMA integrity_check } {ok} # Get the original number of bytes of index data. # set nOrig [db one { SELECT sum( length(val) ) FROM v1_idx }] set nMax [expr {($nOrig * 6)/5}] proc randint {min max} { expr { int(abs(rand()) * (1+$max-$min)) } } for {set iIter 0} {$iIter<20} {incr iIter} { do_test 1.$tn.3.$iIter { for {set ii 1} {$ii <= 1000} {incr ii} { set r [randint 1 1000] execsql { UPDATE v1 SET vec=random_vector(32) WHERE rowid=$r } } set nByte [db one { SELECT sum( length(val) ) FROM v1_idx }] expr $nByte<=$nMax } {1} do_execsql_test 1.$tn.3.$iIter.ic.(sz=$nByte,max=$nMax) { PRAGMA integrity_check } {ok} } } reset_db vec1_load db do_execsql_test 2.0 { CREATE VIRTUAL TABLE vec USING vec1(a, b); INSERT INTO vec(cmd, arg) VALUES('rebuild', '{index:"flat"}'); BEGIN; } do_test 2.1 { for {set ii 1} {$ii<=128} {incr ii} { execsql { INSERT INTO vec(a, b) VALUES( random_vector(16), NULL ) } } } {} do_test 2.1 { for {set ii 128} {$ii>1} {incr ii -1} { execsql { DELETE FROM vec WHERE rowid=$ii } } } {} do_execsql_test 2.2 { COMMIT; PRAGMA integrity_check; } {ok} do_execsql_test 2.3 { SELECT count(*) FROM vec; } 1 finish_test