/
Program.cpp
208 lines (178 loc) · 7.02 KB
/
Program.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
/** -*- C++ -*-
* Copyright (C) 2010-2014 Thalmann Software & Consulting, http://www.softdev.ch
*
* This file is part of ht4n.
*
* ht4n is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or any later version.
*
* Hypertable is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "stdafx.h"
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace System::Globalization;
using namespace System::Text;
using namespace Hypertable;
int main(array<String^> ^args)
{
// Preamble
Console::WriteLine("Welcome to ht4n getting started C++/CLR.");
Console::WriteLine("For more information about ht4n, visit http://ht4n.softdev.ch/");
Console::WriteLine();
try {
// Compose the connection string - Provider={0};Uri={1}
String^ connectionString = String::Format(
CultureInfo::InvariantCulture,
"Provider={0};Uri={1}",
args->Length > 0 ? args[0] : "Hyper",
args->Length > 1 ? args[1] : "net.tcp://localhost");
// Connect to the database instance
Console::WriteLine("Connecting {0}", connectionString);
IContext^ context = nullptr;
IClient^ client = nullptr;
INamespace^ ns = nullptr;
ITable^ table = nullptr;
try {
context = Context::Create(connectionString);
client = context->CreateClient();
// Open or create namespace
Console::WriteLine("Open or create namespace 'tutorials'");
ns = client->OpenNamespace("tutorials", OpenDispositions::OpenAlways | OpenDispositions::CreateIntermediate);
// Define the table schema using xml
String^ TableSchema =
"<Schema>" +
"<AccessGroup name=\"default\">" +
"<ColumnFamily><Name>color</Name></ColumnFamily>" +
"<ColumnFamily><Name>energy</Name></ColumnFamily>" +
"<ColumnFamily><Name>protein</Name></ColumnFamily>" +
"<ColumnFamily><Name>vitamins</Name></ColumnFamily>" +
"</AccessGroup>" +
"</Schema>";
// Open or create table
Console::WriteLine("Open or create table 'fruits'");
table = ns->OpenTable("fruits", TableSchema, OpenDispositions::OpenAlways);
// Insert some fruits
ITableMutator^ mutator = nullptr;
try {
mutator = table->CreateMutator();
Console::WriteLine("Insert 'apple' into 'fruits'");
Key^ key = gcnew Key();
key->Row = "apple";
key->ColumnFamily = "color";
mutator->Set(key, Encoding::UTF8->GetBytes("red"));
key->ColumnFamily = "energy";
mutator->Set(key, BitConverter::GetBytes(207)); // [KJ]
key->ColumnFamily = "protein";
mutator->Set(key, BitConverter::GetBytes(0.4)); // [g]
key->ColumnFamily = "vitamins";
key->ColumnQualifier = "C";
mutator->Set(key, BitConverter::GetBytes(15.0)); // [mg]
key->ColumnQualifier = "B1";
mutator->Set(key, BitConverter::GetBytes(0.02)); // [mg]
Console::WriteLine("Insert 'banana' into 'fruits'");
key = gcnew Key();
key->Row = "banana";
key->ColumnFamily = "color";
mutator->Set(key, Encoding::UTF8->GetBytes("yellow"));
key->ColumnFamily = "energy";
mutator->Set(key, BitConverter::GetBytes(375)); // [KJ]
key->ColumnFamily = "protein";
mutator->Set(key, BitConverter::GetBytes(1.2)); // [g]
key->ColumnFamily = "vitamins";
key->ColumnQualifier = "C";
mutator->Set(key, BitConverter::GetBytes(10.0)); // [mg]
key->ColumnQualifier = "B1";
mutator->Set(key, BitConverter::GetBytes(0.04)); // [mg]
}
finally {
if (mutator != nullptr) {
delete mutator;
mutator = nullptr;
}
}
Console::WriteLine();
// Some query examples
Console::WriteLine("Select all cells from 'fruits'");
ITableScanner^ scanner = nullptr;
try {
scanner = table->CreateScanner();
for each (Cell^ cell in scanner) {
Console::WriteLine(cell);
}
}
finally {
if (scanner != nullptr) {
delete scanner;
scanner = nullptr;
}
}
Console::WriteLine();
Console::WriteLine("Select all 'apple' from 'fruits'");
ScanSpec^ scanSpec = gcnew ScanSpec("apple");
try {
scanner = table->CreateScanner(scanSpec);
Cell^ cell;
while (scanner->Next(cell)) {
Console::WriteLine(cell);
}
}
finally {
if (scanner != nullptr) {
delete scanner;
scanner = nullptr;
}
}
Console::WriteLine();
Console::WriteLine("Select all 'vitamins' for the 'banana'");
scanSpec = gcnew ScanSpec("banana");
scanSpec->AddColumn("vitamins");
try {
scanner = table->CreateScanner(scanSpec);
Cell^ cell = gcnew Cell();
while (scanner->Move(cell)) { // caution, re-use cell instance
Console::WriteLine("{0} {1} {2}mg", cell->Key->Row, cell->Key->Column, BitConverter::ToDouble(cell->Value, 0));
}
}
finally {
if (scanner != nullptr) {
delete scanner;
scanner = nullptr;
}
}
Console::WriteLine();
// Drop table
Console::WriteLine("Drop table 'fruits'");
ns->DropTable("fruits");
}
finally {
if (table != nullptr) {
delete table;
}
if (ns != nullptr) {
delete ns;
}
if (client != nullptr) {
delete client;
}
if (context != nullptr) {
delete context;
}
}
}
catch (Exception^ e) {
Console::WriteLine();
Console::WriteLine(e);
}
return 0;
}