-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
269 lines (223 loc) · 6.32 KB
/
main.cpp
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
#include "Swordfish.h"
#include <memory>
using std::make_shared;
using std::shared_ptr;
using oltp::Connection;
using oltp::DB;
void testTable(DB &db);
void testVector(DB &db);
void testMatrix(DB &db);
void testSet(DB &db);
void testDictionary(DB &db);
int main()
{
// MUST inintialize runtime first
DolphinDBLib::initializeRuntime();
// create (or open) a database
oltp::DBOption option;
shared_ptr<DB> db = make_shared<DB>("test_db", option);
testTable(*db);
testVector(*db);
testMatrix(*db);
testSet(*db);
testDictionary(*db);
DolphinDBLib::finalizeRuntime();
return 0;
}
void testTable(DB &db)
{
Connection conn(db);
// create a table
std::string colName1 = "id";
std::string colName2 = "val";
std::vector<std::string> colNames = {colName1, colName2};
std::vector<DATA_TYPE> colTypes = {DT_INT, DT_DOUBLE};
TableSP tb1 = Util::createTable(colNames, colTypes, 0, 0);
// insert rows
std::string errMsg;
ConstantSP id = Util::createConstant(DT_INT);
ConstantSP val = Util::createConstant(DT_DOUBLE);
for (auto i = 0; i < 10; ++i)
{
id->setInt(i);
val->setDouble(i * 1.1);
std::vector<ConstantSP> rowValues = {id, val};
INDEX rowIdx = i;
tb1->append(rowValues, rowIdx, errMsg);
}
if (errMsg.empty())
{
std::cout << tb1->getString() << std::endl;
}
else
{
std::cout << "insert rows failed! " << errMsg << std::endl;
}
// delete row
errMsg.clear();
ConstantSP rowIdx1 = Util::createConstant(DT_INT);
rowIdx1->setInt(0);
VectorSP rowIdxVec = Util::createVector(DT_INT, 0, 1);
rowIdxVec->append(rowIdx1);
tb1->remove(rowIdxVec, errMsg);
if (errMsg.empty())
{
std::cout << tb1->getString() << std::endl;
}
else
{
std::cout << "delete row " << rowIdx1->getInt() << " failed! " << errMsg << std::endl;
}
// select row
INDEX rowIdx2 = 3;
ConstantSP row = tb1->getRow(rowIdx2);
std::cout << "row index: " << rowIdx2;
std::cout << " id: " << row->getMember(colName1)->getInt();
std::cout << " val: " << row->getMember(colName2)->getDouble() << std::endl;
// update row
errMsg.clear();
id->setInt(111);
val->setDouble(111.111);
std::vector<ConstantSP> rowValues = {id, val};
ConstantSP rowIdx3 = Util::createConstant(DT_INT);
rowIdx3->setInt(0);
tb1->update(rowValues, rowIdx3, colNames, errMsg);
if (errMsg.empty())
{
std::cout << tb1->getString() << std::endl;
}
else
{
std::cout << "update row failed! " << errMsg << std::endl;
}
// receive a table from database
TableSP tb2 = conn.execute("table(1 2 3 as id, 11 22 33 as val)");
std::cout << tb2->getString() << std::endl;
return;
}
void testVector(DB &db)
{
Connection conn(db);
// create a vector
VectorSP v1 = Util::createVector(DT_DOUBLE, 0);
// add elements
std::vector<double> newData = {1.1, 2.2, 3.3};
v1->appendDouble(newData.data(), newData.size());
// read elements' value
for (int i = 0; i < v1->size(); ++i)
{
std::cout << v1->getDouble(i) << std::endl;
}
// remove element
ConstantSP idx = Util::createConstant(DT_INT);
idx->setInt(0);
VectorSP idxVec = Util::createVector(DT_INT, 0, 1);
idxVec->append(idx);
v1->remove(idxVec);
std::cout << v1->getString() << std::endl;
// receive a vector from database
VectorSP v2 = conn.execute("1..10");
std::cout << v2->getString() << std::endl;
return;
}
void testMatrix(DB &db)
{
Connection conn(db);
// create a matrix
int *rawData = new int[12]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
VectorSP m1 = Util::createMatrix(DT_INT, 4, 3, 12, 0, rawData);
// set labels
VectorSP rowLabelVec = Util::createVector(DT_STRING, 0, 3);
VectorSP colLabelVec = Util::createVector(DT_STRING, 0, 4);
std::vector<std::string> rowLabels = {"row1", "row2", "row3"};
std::vector<std::string> colLabels = {"col1", "col2", "col3", "col4"};
rowLabelVec->appendString(rowLabels.data(), rowLabels.size());
colLabelVec->appendString(colLabels.data(), colLabels.size());
m1->setRowLabel(rowLabelVec);
m1->setColumnLabel(colLabelVec);
std::cout << m1->getString() << std::endl;
// read elements' value
for (int i = 0; i < m1->columns(); ++i)
{
auto row = m1->getColumn(i);
for (int j = 0; j < row->size(); ++j)
{
std::cout << row->getInt(j) << std::endl;
}
}
// receive a matrix from database
VectorSP m2 = conn.execute("1..6$2:3");
std::cout << m2->getString() << std::endl;
return;
}
void testSet(DB &db)
{
Connection conn(db);
// create a set
SetSP s1 = Util::createSet(DT_INT, nullptr, 3);
// add new element
ConstantSP val = Util::createConstant(DT_INT);
val->setInt(1);
s1->append(val);
val->setInt(2);
s1->append(val);
std::cout << s1->getString() << std::endl;
// check if set contains element
ConstantSP res = Util::createConstant(DT_BOOL);
s1->contain(val, res);
if (res->getBool())
{
std::cout << "s1 contains " << val->getInt() << std::endl;
}
// remove element
s1->remove(val);
s1->contain(val, res);
if (!res->getBool())
{
std::cout << "s1 does not contain " << val->getInt() << std::endl;
}
// clear the set
s1->clear();
// receive a set from database
SetSP s2 = conn.execute("set(1 3 4 5 5 6 6 7)");
std::cout << s2->getString() << std::endl;
return;
}
void testDictionary(DB &db)
{
Connection conn(db);
// create a dictionary
DictionarySP d1 = Util::createDictionary(DT_INT, nullptr, DT_DOUBLE, nullptr);
// add new key-value pair
ConstantSP k = Util::createConstant(DT_INT);
k->setInt(1);
ConstantSP v = Util::createConstant(DT_DOUBLE);
v->setDouble(1.1);
d1->set(k, v);
k->setInt(2);
v->setDouble(2.2);
d1->set(k, v);
std::cout << d1->getString() << std::endl;
// check key exists
ConstantSP res = Util::createConstant(DT_BOOL);
d1->contain(k, res);
if (res->getBool())
{
std::cout << "d1 contains " << k->getDouble() << std::endl;
}
// read value of specific key
std::cout << "key " << k->getInt() << " 's value is" << d1->getMember(k)->getDouble() << std::endl;
// remove key-value pair
d1->remove(k);
d1->contain(k, res);
if (!res->getBool())
{
std::cout << "d1 does not contain " << k->getDouble() << std::endl;
}
// clear the dictionary
d1->clear();
// receive a dictionary from database
DictionarySP d2 = conn.execute("dict(1 2 3, 10 20 30)");
std::cout << d2->getString() << std::endl;
return;
}