-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.js
146 lines (131 loc) · 3.01 KB
/
example.js
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
const grpc = require("@grpc/grpc-js");
const vald = require("vald-client-node");
const insert = vald.v1_vald.insert_grpc;
const search = vald.v1_vald.search_grpc;
const remove = vald.v1_vald.remove_grpc;
const payload = vald.v1_payload.payload;
const addr = "localhost:8081";
const DIM = process.env.DIM || 4;
const ID = "id_1";
// sleep
const sleep = (second) =>
new Promise((resolve) => {
console.log("Wait for ", second, "s");
setTimeout(resolve, second * 1000);
});
const main = async () => {
const vec = [];
for (let i = 0; i < DIM; i++) {
vec.push(0.1 * (i + 1));
}
// insert
const ivec = payload.Object_Vector.create({
id: ID,
vector: vec,
});
const icfg = payload.Insert_Config.create({
skip_strict_exist_check: false,
});
const ireq = payload.Insert_Request.create({
vector: ivec,
config: icfg,
});
const iclient = new insert.InsertClient(
addr,
grpc.credentials.createInsecure(),
);
const insertFunc = async (req) => {
return new Promise((resolve, reject) => {
iclient.insert(req, (err, resp) => {
if (err) {
reject(err);
} else {
resolve(resp);
}
});
});
};
console.log("Insert start");
await insertFunc(ireq)
.then((res) => {
console.log("insert res: ", res);
})
.catch((e) => {
console.log("insert err: ", e);
process.exit(1);
});
// Wait for createIndex completed
const second = 90;
await sleep(second);
// search
const scfg = payload.Search_Config.create({
num: 10,
min_num: 1,
radius: -1,
epsilon: 0.01,
timeout: 3000000000,
});
const sreq = payload.Search_Request.create({
vector: vec,
config: scfg,
});
const sclient = new search.SearchClient(
addr,
grpc.credentials.createInsecure(),
);
const searchFunc = async (req) => {
return new Promise((resolve, reject) => {
sclient.search(req, (err, resp) => {
if (err) {
reject(err);
} else {
resolve(resp);
}
});
});
};
console.log("Search start");
await searchFunc(sreq)
.then((res) => {
console.log("search res: ", res);
})
.catch((e) => {
console.log("err: ", e);
process.exit(1);
});
const rcfg = payload.Remove_Config.create({
skip_strict_exist_check: false,
});
const robjId = payload.Object_ID.create({
id: ID,
});
const rreq = payload.Remove_Request.create({
id: robjId,
config: rcfg,
});
const rclient = new remove.RemoveClient(
addr,
grpc.credentials.createInsecure(),
);
const removeFunc = async (req) => {
return new Promise((resolve, reject) => {
rclient.remove(req, (err, resp) => {
if (err) {
reject(err);
} else {
resolve(resp);
}
});
});
};
await removeFunc(rreq)
.then((res) => {
console.log("remove res: ", res);
})
.catch((e) => {
console.log("err: ", e);
process.exit(1);
});
process.exit(0);
};
main();