-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathwait_for_index.go
More file actions
99 lines (82 loc) · 2.8 KB
/
Copy pathwait_for_index.go
File metadata and controls
99 lines (82 loc) · 2.8 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
// WaitForIndex example.
//
// Demonstrates how to block until an index finishes building before
// issuing the first query against it. Useful for boot sequences that
// want predictable latency on the very first search.
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/apache/arrow/go/v17/arrow"
"github.com/apache/arrow/go/v17/arrow/array"
"github.com/apache/arrow/go/v17/arrow/memory"
"github.com/lancedb/lancedb-go/pkg/contracts"
"github.com/lancedb/lancedb-go/pkg/lancedb"
)
const embeddingDim = 64
func main() {
fmt.Println("🚀 LanceDB Go SDK - WaitForIndex Example")
fmt.Println("=========================================")
ctx := context.Background()
tempDir, err := os.MkdirTemp("", "lancedb_wait_for_index_example_")
if err != nil {
log.Fatalf("temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
conn, err := lancedb.Connect(ctx, tempDir, nil)
if err != nil {
log.Fatalf("connect: %v", err)
}
defer conn.Close()
arrowSchema := arrow.NewSchema([]arrow.Field{
{Name: "id", Type: arrow.PrimitiveTypes.Int32, Nullable: false},
{Name: "embedding", Type: arrow.FixedSizeListOf(embeddingDim, arrow.PrimitiveTypes.Float32), Nullable: false},
}, nil)
schema, err := lancedb.NewSchema(arrowSchema)
if err != nil {
log.Fatalf("schema: %v", err)
}
table, err := conn.CreateTable(ctx, "docs", schema)
if err != nil {
log.Fatalf("create table: %v", err)
}
defer table.Close()
const n = 300
pool := memory.NewGoAllocator()
idB := array.NewInt32Builder(pool)
embB := array.NewFixedSizeListBuilder(pool, embeddingDim, arrow.PrimitiveTypes.Float32)
embValB := embB.ValueBuilder().(*array.Float32Builder)
for i := 0; i < n; i++ {
idB.Append(int32(i))
embB.Append(true)
for j := 0; j < embeddingDim; j++ {
embValB.Append(float32(i)*0.01 + float32(j)*0.001)
}
}
rec := array.NewRecord(arrowSchema, []arrow.Array{idB.NewArray(), embB.NewArray()}, n)
defer rec.Release()
if err := table.Add(ctx, rec, nil); err != nil {
log.Fatalf("add: %v", err)
}
fmt.Println("\n▶ CreateIndex (returns before the index is fully built)")
if err := table.CreateIndexWithName(ctx, []string{"embedding"}, contracts.IndexTypeIvfPq, "emb_idx"); err != nil {
log.Fatalf("create index: %v", err)
}
fmt.Println("▶ WaitForIndex timeout=30s")
start := time.Now()
if err := table.WaitForIndex(ctx, []string{"emb_idx"}, 30*time.Second); err != nil {
log.Fatalf("wait: %v", err)
}
fmt.Printf(" done after %s\n", time.Since(start).Round(time.Millisecond))
stats, err := table.IndexStats(ctx, "emb_idx")
if err != nil {
log.Fatalf("stats: %v", err)
}
fmt.Printf(" indexed=%d unindexed=%d\n", stats.NumIndexedRows, stats.NumUnindexedRows)
fmt.Println("\n✅ WaitForIndex example complete")
}