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