VecDB/Collection.pm

filter: Statementstmt 21.0% 24 / 114 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 38.4% 5 / 13 Podpod 85.7% 6 / 7 totaltotal 18.2% 35 / 192 CC 28 SCAR 37.3
CC
28
Cov
14%
add
90.0
delete
56.0
search
42.0
CRAP 41.8
↑ Summary VecDB/Role/Embedding.pm »
1 package SQLite::VecDB::Collection;
2 # ABSTRACT: A named vector collection in SQLite::VecDB
3 7 our $VERSION = '0.001';
4  
5 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use JSON::MaybeXS qw( encode_json decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use DBI qw( :sql_types );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use SQLite::VecDB::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 has name => (
12 is => 'ro',
13 isa => 'Str',
14 required => 1,
15 );
16  
17  
18 7 has dbh => (
19 is => 'ro',
20 required => 1,
21 );
22  
23 7 has dimensions => (
24 is => 'ro',
25 isa => 'Int',
26 required => 1,
27 );
28  
29 7 has distance_metric => (
30 is => 'ro',
31 isa => 'Str',
32 default => 'cosine',
33 );
34  
35 7 has embedding => (
36 is => 'ro',
37 predicate => 'has_embedding',
38 );
39  
40 7 has _initialized => (
41 is => 'rw',
42 isa => 'Bool',
43 default => 0,
44 init_arg => undef,
45 );
46  
47 0 ▶sub BUILD {
Subroutine BUILD: not called CC=2 SCAR=17.9
Pod undocumented
48 0 my ( $self ) = @_;
49 0 $self->_ensure_tables;
50 0 ▶ if ($self->has_embedding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_embedding) 0 0 0
51 0 require SQLite::VecDB::Role::Embedding;
52 0 SQLite::VecDB::Role::Embedding->meta->apply($self);
53 }
54 }
55  
56 0 ▶sub _ensure_tables {
Subroutine _ensure_tables: not called CC=2 SCAR=17.9
57 0 my ( $self ) = @_;
58 0 ▶ return if $self->_initialized;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_initialized 0 0 0
59  
60 0 my $name = $self->name;
61 0 my $dims = $self->dimensions;
62 0 my $metric = $self->distance_metric;
63 0 my $dbh = $self->dbh;
64  
65 0 $dbh->do(q{
66 INSERT OR IGNORE INTO _vecdb_collections (name, dimensions, distance_metric)
67 VALUES (?, ?, ?)
68 }, {}, $name, $dims, $metric);
69  
70 0 $dbh->do(qq{
71 CREATE VIRTUAL TABLE IF NOT EXISTS "vec_${name}" USING vec0(
72 embedding float[$dims] distance_metric=$metric
73 )
74 });
75  
76 0 $dbh->do(qq{
77 CREATE TABLE IF NOT EXISTS "meta_${name}" (
78 rowid INTEGER PRIMARY KEY,
79 id TEXT UNIQUE NOT NULL,
80 metadata TEXT,
81 content TEXT,
82 created_at REAL DEFAULT (julianday('now'))
83 )
84 });
85  
86 0 $self->_initialized(1);
87 }
88  
89 0 ▶sub add {
Subroutine add: not called CC=9 SCAR=45.0
Pod documented
90 0 my ( $self, %args ) = @_;
91 0 ▶ my $id = $args{id} // croak "id is required";
Condition0%0/2
$args{"id"} // croak("id is required")
l!l
00
MC/DC0%0/1
$args{"id"} // croak("id is required")
$args{"id"}
Truth table0%0/2
A
A= $args{"id"}
Aresult
11
00
92 0 ▶ my $vector = $args{vector} // croak "vector is required";
Condition0%0/2
$args{"vector"} // croak("vector is required")
l!l
00
MC/DC0%0/1
$args{"vector"} // croak("vector is required")
$args{"vector"}
Truth table0%0/2
A
A= $args{"vector"}
Aresult
11
00
93 0 my $metadata = $args{metadata};
94 0 my $content = $args{content};
95  
96 0 ▶ croak "vector must have " . $self->dimensions . " dimensions"
Branch: 1 branch
BranchTrueFalseTotal
unless @$vector == $self->dimensions 0 0 0
97 unless @$vector == $self->dimensions;
98  
99 0 my $name = $self->name;
100 0 my $dbh = $self->dbh;
101 0 ▶ my $json_meta = $metadata ? encode_json($metadata) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$metadata ? : 0 0 0
102 0 my $vec_json = encode_json($vector);
103  
104 0 my $need_txn = $dbh->{AutoCommit};
105 0 ▶ $dbh->begin_work if $need_txn;
Branch: 1 branch
BranchTrueFalseTotal
if $need_txn 0 0 0
106 0 eval {
107 0 $dbh->do(
108 qq{INSERT INTO "meta_${name}" (id, metadata, content) VALUES (?, ?, ?)},
109 {}, $id, $json_meta, $content,
110 );
111 0 my $rowid = $dbh->last_insert_id("", "", "", "");
112 0 my $vec_sth = $dbh->prepare(
113 qq{INSERT INTO "vec_${name}" (rowid, embedding) VALUES (?, ?)}
114 );
115 0 $vec_sth->bind_param(1, $rowid, SQL_INTEGER);
116 0 $vec_sth->bind_param(2, $vec_json);
117 0 $vec_sth->execute;
118 0 ▶ $dbh->commit if $need_txn;
Branch: 1 branch
BranchTrueFalseTotal
if $need_txn 0 0 0
119 };
120 0 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
121 0 ▶ eval { $dbh->rollback } if $need_txn;
Branch: 1 branch
BranchTrueFalseTotal
if $need_txn 0 0 0
122 0 croak "Failed to add vector: $err";
123 }
124 0 return $id;
125 }
126  
127  
128 0 ▶sub search {
Subroutine search: not called CC=6 SCAR=37.4
Pod documented
129 0 my ( $self, %args ) = @_;
130 0 ▶ my $vector = $args{vector} // croak "vector is required";
Condition0%0/2
$args{"vector"} // croak("vector is required")
l!l
00
MC/DC0%0/1
$args{"vector"} // croak("vector is required")
$args{"vector"}
Truth table0%0/2
A
A= $args{"vector"}
Aresult
11
00
131 0 ▶ my $limit = $args{limit} // 10;
Condition0%0/2
$args{"limit"} // 10
l!l
00
MC/DC0%0/1
$args{"limit"} // 10
$args{"limit"}
Truth table0%0/2
A
A= $args{"limit"}
Aresult
11
00
132  
133 0 my $name = $self->name;
134 0 my $vec_json = encode_json($vector);
135  
136 0 my $sth = $self->dbh->prepare(qq{
137 SELECT m.id, m.metadata, m.content, v.distance
138 FROM "vec_${name}" v
139 JOIN "meta_${name}" m ON m.rowid = v.rowid
140 WHERE v.embedding MATCH ?
141 AND k = ?
142 ORDER BY v.distance
143 });
144 0 $sth->execute($vec_json, $limit);
145  
146 0 my @results;
147 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
148 push @results, SQLite::VecDB::Result->new(
149 id => $row->{id},
150 distance => $row->{distance},
151 metadata => $row->{metadata} ? decode_json($row->{metadata}) : {},
152 0 ▶ ($row->{content} ? (content => $row->{content}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
$row->{'metadata'} ? : 0 0 0
$row->{'content'} ? : 0 0 0
153 );
154 }
155 0 return @results;
156 }
157  
158  
159 0 ▶sub get {
Subroutine get: not called CC=5 SCAR=34.0
Pod documented
160 0 my ( $self, %args ) = @_;
161 0 ▶ my $id = $args{id} // croak "id is required";
Condition0%0/2
$args{"id"} // croak("id is required")
l!l
00
MC/DC0%0/1
$args{"id"} // croak("id is required")
$args{"id"}
Truth table0%0/2
A
A= $args{"id"}
Aresult
11
00
162 0 my $name = $self->name;
163  
164 0 my $row = $self->dbh->selectrow_hashref(
165 qq{SELECT id, metadata, content FROM "meta_${name}" WHERE id = ?},
166 {}, $id,
167 );
168 0 ▶ return unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 0 0
169  
170 return SQLite::VecDB::Result->new(
171 id => $row->{id},
172 metadata => $row->{metadata} ? decode_json($row->{metadata}) : {},
173 0 ▶ ($row->{content} ? (content => $row->{content}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
$row->{'metadata'} ? : 0 0 0
$row->{'content'} ? : 0 0 0
174 );
175 }
176  
177  
178 0 ▶sub delete {
Subroutine delete: not called CC=7 SCAR=40.3
Pod documented
179 0 my ( $self, %args ) = @_;
180 0 ▶ my $id = $args{id} // croak "id is required";
Condition0%0/2
$args{"id"} // croak("id is required")
l!l
00
MC/DC0%0/1
$args{"id"} // croak("id is required")
$args{"id"}
Truth table0%0/2
A
A= $args{"id"}
Aresult
11
00
181 0 my $name = $self->name;
182 0 my $dbh = $self->dbh;
183  
184 0 my ($rowid) = $dbh->selectrow_array(
185 qq{SELECT rowid FROM "meta_${name}" WHERE id = ?},
186 {}, $id,
187 );
188 0 ▶ return 0 unless defined $rowid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rowid 0 0 0
189  
190 0 my $need_txn = $dbh->{AutoCommit};
191 0 ▶ $dbh->begin_work if $need_txn;
Branch: 1 branch
BranchTrueFalseTotal
if $need_txn 0 0 0
192 0 eval {
193 0 $dbh->do(qq{DELETE FROM "vec_${name}" WHERE rowid = ?}, {}, $rowid);
194 0 $dbh->do(qq{DELETE FROM "meta_${name}" WHERE rowid = ?}, {}, $rowid);
195 0 ▶ $dbh->commit if $need_txn;
Branch: 1 branch
BranchTrueFalseTotal
if $need_txn 0 0 0
196 };
197 0 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
198 0 ▶ eval { $dbh->rollback } if $need_txn;
Branch: 1 branch
BranchTrueFalseTotal
if $need_txn 0 0 0
199 0 croak "Failed to delete vector: $err";
200 }
201 0 return 1;
202 }
203  
204  
205 0 ▶sub count {
Subroutine count: not called CC=1 SCAR=6.9
Pod documented
206 0 my ( $self ) = @_;
207 0 my $name = $self->name;
208 0 my ($count) = $self->dbh->selectrow_array(
209 qq{SELECT COUNT(*) FROM "meta_${name}"},
210 );
211 0 return $count;
212 }
213  
214  
215 0 ▶sub add_batch {
Subroutine add_batch: not called CC=3 SCAR=24.8
Pod documented
216 0 my ( $self, @items ) = @_;
217 0 my $dbh = $self->dbh;
218 0 $dbh->begin_work;
219 0 eval {
220 0 for my $item (@items) {
221 0 $self->add(%$item);
222 }
223 0 $dbh->commit;
224 };
225 0 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
226 0 eval { $dbh->rollback };
227 0 croak "Batch add failed: $err";
228 }
229 0 return scalar @items;
230 }
231  
232  
233 7 __PACKAGE__->meta->make_immutable;
234  
235 7 1;
236  
237 __END__
↑ Summary VecDB/Role/Embedding.pm »