-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathstorage_configuration.go
More file actions
279 lines (236 loc) · 7.29 KB
/
storage_configuration.go
File metadata and controls
279 lines (236 loc) · 7.29 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
// Storage Configuration Example
//
// This example demonstrates storage configuration capabilities with LanceDB
// using the Go SDK. It covers:
// - Local file system storage
// - AWS S3 storage configuration
// - MinIO object storage for local development
// - Cloudflare R2 configuration
// - Google Cloud Storage configuration
// - Azure Blob Storage configuration
package main
import (
"context"
"fmt"
"log"
"math"
"math/rand"
"os"
"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 (
VectorDim = 128
SampleRecords = 100
)
func main() {
fmt.Println("LanceDB Go SDK - Storage Configuration Example")
fmt.Println("================================================")
ctx := context.Background()
// Local storage
fmt.Println("\nStep 1: Local storage...")
if err := demonstrateLocalStorage(ctx); err != nil {
log.Printf("Local storage demo failed: %v", err)
}
// Cloud storage configurations (display only - require real credentials)
fmt.Println("\nStep 2: Cloud storage configurations...")
showCloudConfigurations()
fmt.Println("\nStorage configuration examples completed!")
}
func demonstrateLocalStorage(ctx context.Context) error {
// Basic local storage (no options needed)
tempDir, err := os.MkdirTemp("", "lancedb_local_")
if err != nil {
return fmt.Errorf("failed to create temp directory: %w", err)
}
defer os.RemoveAll(tempDir)
conn, err := lancedb.Connect(ctx, tempDir, nil)
if err != nil {
return fmt.Errorf("failed to connect: %w", err)
}
defer conn.Close()
fmt.Printf(" Local storage at: %s\n", tempDir)
// Test basic operations
if err := testStorageConfiguration(ctx, "Local", conn); err != nil {
return fmt.Errorf("local storage test failed: %w", err)
}
return nil
}
func showCloudConfigurations() {
// AWS S3
fmt.Println(" AWS S3:")
s3Opts := &ConnectionOptions{
StorageOptions: map[string]string{
StorageAccessKeyID: "AKIA...",
StorageSecretAccessKey: "wJalr...",
StorageRegion: "us-east-1",
},
}
printConfig("S3", s3Opts.StorageOptions)
// MinIO
fmt.Println("\n MinIO (S3-compatible):")
minioOpts := &ConnectionOptions{
StorageOptions: map[string]string{
StorageAccessKeyID: "minioadmin",
StorageSecretAccessKey: "minioadmin",
StorageEndpoint: "http://localhost:9000",
StorageRegion: "us-east-1",
StorageAllowHTTP: "true",
StorageVirtualHostedStyleRequest: "false",
},
}
printConfig("MinIO", minioOpts.StorageOptions)
// Cloudflare R2
fmt.Println("\n Cloudflare R2:")
r2Opts := &ConnectionOptions{
StorageOptions: map[string]string{
StorageAccessKeyID: "...",
StorageSecretAccessKey: "...",
StorageAWSEndpoint: "https://ACCOUNT_ID.r2.cloudflarestorage.com",
StorageRegion: "auto",
},
}
printConfig("R2", r2Opts.StorageOptions)
fmt.Println(" Note: R2 requires StorageAWSEndpoint (not StorageEndpoint)")
// Google Cloud Storage
fmt.Println("\n Google Cloud Storage:")
gcsOpts := &ConnectionOptions{
StorageOptions: map[string]string{
StorageGCSServiceAccount: "/path/to/service-account.json",
},
}
printConfig("GCS", gcsOpts.StorageOptions)
// Azure Blob Storage
fmt.Println("\n Azure Blob Storage:")
azureOpts := &ConnectionOptions{
StorageOptions: map[string]string{
StorageAzureAccountName: "myaccount",
StorageAzureAccessKey: "...",
},
}
printConfig("Azure", azureOpts.StorageOptions)
}
func printConfig(name string, opts map[string]string) {
fmt.Printf(" %s configuration (%d keys):\n", name, len(opts))
for k, v := range opts {
// Mask sensitive values
display := v
if len(v) > 12 {
display = v[:8] + "..."
}
fmt.Printf(" %s = %s\n", k, display)
}
}
// Helper functions
func testStorageConfiguration(ctx context.Context, name string, conn IConnection) error {
table, schema, err := createTestTable(ctx, conn, fmt.Sprintf("test_%s", name))
if err != nil {
return err
}
defer table.Close()
testData := generateTestData(SampleRecords)
if err := insertTestData(table, schema, testData); err != nil {
return err
}
queryVector := generateRandomVector(VectorDim)
results, err := table.VectorSearch(context.Background(), "vector", queryVector, 5)
if err != nil {
return err
}
fmt.Printf(" %s: %d records inserted, %d results from query\n",
name, len(testData), len(results))
return nil
}
func createTestTable(ctx context.Context, conn IConnection, tableName string) (ITable, *arrow.Schema, error) {
fields := []arrow.Field{
{Name: "id", Type: arrow.PrimitiveTypes.Int32, Nullable: false},
{Name: "name", Type: arrow.BinaryTypes.String, Nullable: false},
{Name: "vector", Type: arrow.FixedSizeListOf(VectorDim, arrow.PrimitiveTypes.Float32), Nullable: false},
}
arrowSchema := arrow.NewSchema(fields, nil)
schema, err := lancedb.NewSchema(arrowSchema)
if err != nil {
return nil, nil, err
}
table, err := conn.CreateTable(ctx, tableName, schema)
if err != nil {
return nil, nil, err
}
return table, arrowSchema, nil
}
func generateTestData(count int) []struct {
ID int32
Name string
Vector []float32
} {
records := make([]struct {
ID int32
Name string
Vector []float32
}, count)
for i := 0; i < count; i++ {
records[i].ID = int32(i + 1)
records[i].Name = fmt.Sprintf("Record %d", i+1)
records[i].Vector = generateRandomVector(VectorDim)
}
return records
}
func generateRandomVector(dimensions int) []float32 {
vector := make([]float32, dimensions)
for i := 0; i < dimensions; i++ {
vector[i] = rand.Float32()*2 - 1
}
var norm float32
for _, v := range vector {
norm += v * v
}
norm = float32(math.Sqrt(float64(norm)))
if norm > 0 {
for i := range vector {
vector[i] /= norm
}
}
return vector
}
func insertTestData(table ITable, schema *arrow.Schema, data []struct {
ID int32
Name string
Vector []float32
}) error {
pool := memory.NewGoAllocator()
ids := make([]int32, len(data))
names := make([]string, len(data))
allVectors := make([]float32, len(data)*VectorDim)
for i, record := range data {
ids[i] = record.ID
names[i] = record.Name
copy(allVectors[i*VectorDim:(i+1)*VectorDim], record.Vector)
}
idBuilder := array.NewInt32Builder(pool)
idBuilder.AppendValues(ids, nil)
idArray := idBuilder.NewArray()
defer idArray.Release()
nameBuilder := array.NewStringBuilder(pool)
nameBuilder.AppendValues(names, nil)
nameArray := nameBuilder.NewArray()
defer nameArray.Release()
vectorBuilder := array.NewFloat32Builder(pool)
vectorBuilder.AppendValues(allVectors, nil)
vectorFloat32Array := vectorBuilder.NewArray()
defer vectorFloat32Array.Release()
vectorListType := arrow.FixedSizeListOf(VectorDim, arrow.PrimitiveTypes.Float32)
vectorArray := array.NewFixedSizeListData(
array.NewData(vectorListType, len(data), []*memory.Buffer{nil},
[]arrow.ArrayData{vectorFloat32Array.Data()}, 0, 0),
)
defer vectorArray.Release()
columns := []arrow.Array{idArray, nameArray, vectorArray}
record := array.NewRecord(schema, columns, int64(len(data)))
defer record.Release()
return table.Add(context.Background(), record, nil)
}