VecDB.pm

filter: Statementstmt 79.5% 35 / 44 Branchbran 25.0% 2 / 8 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 87.5% 7 / 8 Podpod 100% 2 / 2 totaltotal 72.3% 47 / 65 CC 6 SCAR 11.0
CC
6
Cov
70%
_build_sqlite_vec_path
5.3
collection
3.7
collections
2.0
CRAP 3.0
« VecDB/Role/Embedding.pm ↑ Summary VecDB/Result.pm »
1 package SQLite::VecDB;
2 # ABSTRACT: SQLite as a vector database using sqlite-vec
3 7 our $VERSION = '0.001';
4  
5 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use SQLite::VecDB::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 has db_file => (
11 is => 'ro',
12 isa => 'Str',
13 required => 1,
14 );
15  
16  
17 7 has dimensions => (
18 is => 'ro',
19 isa => 'Int',
20 required => 1,
21 );
22  
23  
24 7 has distance_metric => (
25 is => 'ro',
26 isa => 'Str',
27 default => 'cosine',
28 );
29  
30  
31 7 has embedding => (
32 is => 'ro',
33 predicate => 'has_embedding',
34 );
35  
36  
37 7 has sqlite_vec_path => (
38 is => 'ro',
39 isa => 'Str',
40 lazy => 1,
41 builder => '_build_sqlite_vec_path',
42 );
43  
44  
45 7 has dbh => (
46 is => 'ro',
47 lazy => 1,
48 builder => '_build_dbh',
49 init_arg => undef,
50 );
51  
52 5 ▶sub _build_sqlite_vec_path {
Subroutine _build_sqlite_vec_path: called CC=4 SCAR=16.6
53 5 my ( $self ) = @_;
54 5 ▶ return $ENV{SQLITE_VEC_PATH} if $ENV{SQLITE_VEC_PATH};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SQLITE_VEC_PATH"} 0 5 5
55 5 eval { require Alien::sqlite_vec };
56 5 ▶ if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 5 5
57 0 my @libs = Alien::sqlite_vec->dynamic_libs;
58 0 ▶ return $libs[0] if @libs;
Branch: 1 branch
BranchTrueFalseTotal
if @libs 0 0 0
59 }
60 5 croak "Cannot find sqlite-vec extension. Set SQLITE_VEC_PATH or install Alien::sqlite_vec";
61 }
62  
63 5 ▶sub _build_dbh {
Subroutine _build_dbh: called CC=1 SCAR=0.5
64 5 my ( $self ) = @_;
65 5 my $dbh = DBI->connect("dbi:SQLite:dbname=" . $self->db_file, "", "", {
66 RaiseError => 1,
67 PrintError => 0,
68 sqlite_unicode => 1,
69 });
70 5 $dbh->sqlite_enable_load_extension(1);
71 5 my $sth = $dbh->prepare("SELECT load_extension(?)");
72 5 $sth->execute($self->sqlite_vec_path);
73 0 $sth->finish;
74 0 $dbh->do(q{
75 CREATE TABLE IF NOT EXISTS _vecdb_collections (
76 name TEXT PRIMARY KEY,
77 dimensions INTEGER NOT NULL,
78 distance_metric TEXT NOT NULL DEFAULT 'cosine',
79 created_at REAL DEFAULT (julianday('now'))
80 )
81 });
82 0 return $dbh;
83 }
84  
85 5 ▶sub collection {
Subroutine collection: called CC=3 SCAR=13.1
Pod documented
86 5 my ( $self, $name ) = @_;
87 5 ▶ $name //= '_default';
Condition50%1/2
$name //= "_default"
l!l
50
MC/DC0%0/1
$name //= "_default"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
88 5 ▶ return SQLite::VecDB::Collection->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->has_embedding ? : 0 0 0
89 name => $name,
90 dbh => $self->dbh,
91 dimensions => $self->dimensions,
92 distance_metric => $self->distance_metric,
93 ($self->has_embedding ? (embedding => $self->embedding) : ()),
94 );
95 }
96  
97  
98 0 ▶sub collections {
Subroutine collections: not called CC=1 SCAR=6.9
Pod documented
99 0 my ( $self ) = @_;
100 0 my $rows = $self->dbh->selectall_arrayref(
101 "SELECT name FROM _vecdb_collections ORDER BY name",
102 { Slice => {} },
103 );
104 0 return map { $_->{name} } @$rows;
105 }
106  
107  
108 7 __PACKAGE__->meta->make_immutable;
109  
110 7 1;
111  
112 __END__
« VecDB/Role/Embedding.pm ↑ Summary VecDB/Result.pm »