-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_simple_api_test.go
140 lines (128 loc) · 3.69 KB
/
example_simple_api_test.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
//////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2018-2019 YottaDB LLC and/or its subsidiaries. //
// All rights reserved. //
// //
// This source code contains the intellectual property //
// of its copyright holder(s), and is made available //
// under a license. If you do not know the terms of //
// the license, please stop and do not read further. //
// //
//////////////////////////////////////////////////////////////////
package yottadb_test
import (
"fmt"
"lang.yottadb.com/go/yottadb"
)
// Example demonstrating the most basic features of YottaDB using the simple API;
// setting a value, getting a value, iterating through values,
// and deleting a value.
//
// The SimpleAPI is somewhat more difficult to use than the EasyAPI, but is more
// performant. It is recommended to use the SimpleAPI if you are building a
// performance critical application.
func Example_simpleAPI() {
// Allocate a key to set our value equal too
var key1 yottadb.KeyT
var buff1, cur_sub yottadb.BufferT
var tptoken uint64
var err error
// The tptoken argument to many functions is either a value passed into the
// callback routine for TP, or yottadb.NOTTP if not in a transaction
tptoken = yottadb.NOTTP
// Set global node ["^hello", "world"] to "Go World"
key1.Alloc(64, 10, 64)
err = key1.Varnm.SetValStr(tptoken, nil, "^hello")
if err != nil {
panic(err)
}
err = key1.Subary.SetElemUsed(tptoken, nil, 1)
if err != nil {
panic(err)
}
err = key1.Subary.SetValStr(tptoken, nil, 0, "world")
if err != nil {
panic(err)
}
// Create a buffer which is used to specify the value we will be setting the global to
buff1.Alloc(64)
err = buff1.SetValStr(tptoken, nil, "Go world")
if err != nil {
panic(err)
}
// Set the value
err = key1.SetValST(tptoken, nil, &buff1)
if err != nil {
panic(err)
}
// Retrieve the value that was set
// We can reuse the KeyT we already made for setting the value; hence part
// of the performance gain
// For the sake of demonstration, we will first clear the buffer we used to set the
// value
buff1.Alloc(64)
val1, err := buff1.ValStr(tptoken, nil)
if err != nil {
panic(err)
}
if (val1) != "" {
panic("Buffer not empty when it should be!")
}
err = key1.ValST(tptoken, nil, &buff1)
if err != nil {
panic(err)
}
val1, err = buff1.ValStr(tptoken, nil)
if (val1) != "Go world" {
panic("Value not what was expected; did someone else set something?")
}
// Set a few more nodes so we can iterate through them
err = key1.Subary.SetValStr(tptoken, nil, 0, "shire")
if err != nil {
panic(err)
}
err = buff1.SetValStr(tptoken, nil, "Go Middle Earth")
if err != nil {
panic(err)
}
err = key1.SetValST(tptoken, nil, &buff1)
if err != nil {
panic(err)
}
err = key1.Subary.SetValStr(tptoken, nil, 0, "Winterfell")
if err != nil {
panic(err)
}
err = buff1.SetValStr(tptoken, nil, "Go Westeros")
if err != nil {
panic(err)
}
err = key1.SetValST(tptoken, nil, &buff1)
if err != nil {
panic(err)
}
// Allocate a BufferT for return values
cur_sub.Alloc(64)
// Start iterating through the list at the start by setting the last subscript
// to ""; stop when we get the error code meaning end
err = key1.Subary.SetValStr(tptoken, nil, 0, "")
for true {
err = key1.SubNextST(tptoken, nil, &cur_sub)
if err != nil {
error_code := yottadb.ErrorCode(err)
if error_code == yottadb.YDB_ERR_NODEEND {
break
} else {
panic(err)
}
}
val1, err = cur_sub.ValStr(tptoken, nil)
if err != nil {
panic(err)
}
fmt.Printf("%s ", val1)
// Move to that key by setting the next node in the key
key1.Subary.SetValStr(tptoken, nil, 0, val1)
}
/* Output: Winterfell shire world */
}