-
Notifications
You must be signed in to change notification settings - Fork 114
/
database.go
181 lines (164 loc) · 5.2 KB
/
database.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
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
package main
import (
"context"
"fmt"
"log"
"github.com/milvus-io/milvus-sdk-go/v2/client"
"github.com/milvus-io/milvus-sdk-go/v2/entity"
)
func main() {
fmt.Println("using default database...")
ctx := context.Background()
cfg := client.Config{
Address: "localhost:19530",
Username: "root",
Password: "Milvus",
}
clientDefault := mustConnect(ctx, cfg)
defer clientDefault.Close()
createCollection(ctx, clientDefault, "col1")
if err := clientDefault.CreateDatabase(ctx, "db1", client.WithDatabaseProperty("key1", "value1")); err != nil {
log.Fatalf("create db1 failed, %+v", err)
}
dbs, err := clientDefault.ListDatabases(ctx)
if err != nil {
log.Fatalf("list database failed: %+v", err)
}
fmt.Println("db:", dbs)
dbInfo, err := clientDefault.DescribeDatabase(ctx, "db1")
if err != nil {
log.Fatalf("describe default db failed")
}
fmt.Println("db props", dbInfo.Properties)
clientDefault.AlterDatabase(ctx, "db1", entity.DatabaseReplica(3), entity.DatabaseResourceGroups([]string{"rg1", "rg2", "rg3"}))
dbInfo, err = clientDefault.DescribeDatabase(ctx, "db1")
if err != nil {
log.Fatalf("describe default db failed")
}
fmt.Println("db props", dbInfo.Properties)
fmt.Println("using db1...")
cfg.DBName = ""
cfg.Address = "localhost:19530/db1"
clientDB1 := mustConnect(ctx, cfg)
defer clientDB1.Close()
createCollection(ctx, clientDB1, "col1")
createCollection(ctx, clientDB1, "col2")
fmt.Println("create collection col1 in default database...")
collections, err := clientDefault.ListCollections(ctx)
if err != nil {
log.Fatalf("list on default database failed: %+v", err)
}
if len(collections) != 1 {
log.Fatalf("unexpected count of default database, %d", len(collections))
}
if collections[0].Name != "col1" {
log.Fatalf("unexpected db name of default database, %s", collections[0].Name)
}
fmt.Println("create collection col1, col2 in db1...")
collections, err = clientDB1.ListCollections(ctx)
if err != nil {
log.Fatalf("list on db1 failed: %+v", err)
}
fmt.Println("collections:", collections)
fmt.Println("create db2...")
if err := clientDefault.CreateDatabase(ctx, "db2"); err != nil {
log.Fatalf("create db2 failed, %+v", err)
}
dbs, err = clientDefault.ListDatabases(ctx)
if err != nil {
log.Fatalf("list database failed: %+v", err)
}
fmt.Println("db:", dbs)
fmt.Println("connect to db2 with existing client...")
clientDefault.UsingDatabase(ctx, "db2")
fmt.Println("create and drop collection on db2...")
createCollection(ctx, clientDefault, "col1")
if err := clientDefault.DropCollection(ctx, "col1"); err != nil {
log.Fatalf("drop col1 at db2 failed, %+v", err)
}
err = clientDefault.UsingDatabase(ctx, "")
if err != nil {
log.Fatalf("using database failed: %+v", err)
}
fmt.Println("drop db2: drop empty database should be always success...")
if err := clientDefault.DropDatabase(ctx, "db2"); err != nil {
log.Fatalf("drop db2 failed, %+v", err)
}
dbs, err = clientDefault.ListDatabases(ctx)
if err != nil {
log.Fatalf("list database failed: %+v", err)
}
fmt.Println("db:", dbs)
fmt.Println("drop db1: drop non-empty database should be fail...")
if err := clientDB1.DropDatabase(ctx, "db1"); err == nil {
log.Fatalf("drop an non empty db success")
}
fmt.Println("drop db1: drop all collection before drop db...")
if err := clientDB1.DropCollection(ctx, "col1"); err != nil {
log.Fatalf("drop col1 at db1 failed, %+v", err)
}
if err := clientDB1.DropCollection(ctx, "col2"); err != nil {
log.Fatalf("drop col2 at db1 failed, %+v", err)
}
if err := clientDB1.DropDatabase(ctx, "db1"); err != nil {
log.Fatalf("drop db1 failed, %+v", err)
}
fmt.Println("Drop collection on default database...")
if err := clientDefault.DropCollection(ctx, "col1"); err != nil {
log.Fatalf("drop col1 at default db failed, %+v", err)
}
fmt.Println("drop default: drop default database should be fail...")
if err := clientDefault.DropDatabase(ctx, ""); err == nil {
log.Fatalf("drop an default db success")
}
dbs, err = clientDefault.ListDatabases(ctx)
if err != nil {
log.Fatalf("list database failed: %+v", err)
}
fmt.Println("db:", dbs)
}
func mustConnect(ctx context.Context, cfg client.Config) client.Client {
c, err := client.NewClient(ctx, cfg)
if err != nil {
log.Fatalf("connect to database failed, %+v", err)
}
return c
}
func createCollection(ctx context.Context, c client.Client, collectionName string) {
ok, err := c.HasCollection(ctx, collectionName)
if err != nil {
log.Fatalf("%v", err)
}
if ok {
c.DropCollection(ctx, collectionName)
}
schema := &entity.Schema{
CollectionName: collectionName,
Description: "database demo",
AutoID: false,
Fields: []*entity.Field{
{
Name: "id",
DataType: entity.FieldTypeInt64,
PrimaryKey: true,
AutoID: false,
},
{
Name: "data",
DataType: entity.FieldTypeDouble,
PrimaryKey: false,
AutoID: false,
},
{
Name: "embeddings",
DataType: entity.FieldTypeFloatVector,
TypeParams: map[string]string{
entity.TypeParamDim: fmt.Sprintf("%d", 128),
},
},
},
}
if err := c.CreateCollection(ctx, schema, entity.DefaultShardNumber); err != nil {
log.Fatalf("create collection failed, err: %v", err)
}
}