-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
demo.go
71 lines (61 loc) · 1.38 KB
/
demo.go
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
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/asg017/sqlite-vss/bindings/go"
_ "github.com/mattn/go-sqlite3"
)
// #cgo linux,amd64 LDFLAGS: -Wl,-undefined,dynamic_lookup -lstdc++
// #cgo darwin,amd64 LDFLAGS: -Wl,-undefined,dynamic_lookup -lomp
// #cgo darwin,arm64 LDFLAGS: -Wl,-undefined,dynamic_lookup -lomp
import "C"
func main() {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()
var version, vector string
err = db.QueryRow("SELECT vss_version(), vector_to_json(?)", []byte{0x00, 0x00, 0x28, 0x42}).Scan(&version, &vector)
if err != nil {
log.Fatal(err)
}
fmt.Printf("version=%s vector=%s\n", version, vector)
_, err = db.Exec(`
CREATE VIRTUAL TABLE vss_demo USING vss0(a(2));
INSERT INTO vss_demo(rowid, a)
VALUES
(1, '[1.0, 2.0]'),
(2, '[2.0, 2.0]'),
(3, '[3.0, 2.0]')
`)
if err != nil {
log.Fatal(err)
}
rows, err := db.Query(`
SELECT
rowid,
distance
FROM vss_demo
WHERE vss_search(a, '[1.0, 2.0]')
LIMIT 3
`)
if err != nil {
log.Fatal(err)
}
for rows.Next() {
var rowid int64
var distance float32
err = rows.Scan(&rowid, &distance)
if err != nil {
log.Fatal(err)
}
fmt.Printf("rowid=%d, distance=%f\n", rowid, distance)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
fmt.Println("✅ demo.go ran successfully. \n");
}