/
dystopia.go
307 lines (258 loc) · 9.97 KB
/
dystopia.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
* Copyright 2009 Patrick Crosby, XB Labs LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dystopia
// this comment coming up isn't really a comment...tells C package what to include
// include "tdwrapper.h"
// #include <dystopia.h>
// #include "tdwrapper.h"
import "C"
import (
"fmt";
"os";
// "strconv";
"unsafe";
"container/vector";
)
type Connection struct {
Dystopia *_C_TCIDB;
}
type Query struct {
Dystopia unsafe.Pointer;
}
func (connection *Connection) ErrorCode() int {
// return int(C.xtcidb_ecode(connection.Dystopia));
return 0;
}
func (connection *Connection) ErrorMessage() string {
return C.GoString(C.tcidberrmsg(C.tcidbecode(connection.Dystopia)))
}
func (connection *Connection) ErrorDisplay() {
fmt.Printf("TD Error: " + connection.ErrorMessage() + "\n")
}
func OpenWriterFlag() int { return int(C.x_idbowriter()) }
func OpenReaderFlag() int { return int(C.x_idboreader()) }
func OpenCreateFlag() int { return int(C.x_idbocreate()) }
func OpenNoLockFlag() int { return int(C.x_idbonolock()) }
func OpenNoBlockFlag() int { return int(C.x_idbolocknoblock()) }
/*
func Connect(host string, port int) (connection *Connection, err os.Error) {
connection = new(Connection);
connection.Tyrant = C.xtcidb_new();
open := C.xtcidb_open(connection.Tyrant, C.CString("localhost"), 1978);
if open == 0 {
fmt.Printf("couldn't open database\n");
connection.ErrorDisplay();
return nil, os.NewError(connection.ErrorMessage());
}
fmt.Printf("connected to database localhost:1978\n");
return connection, nil;
}
*/
func Open(path string, ocode int) (connection *Connection, err os.Error) {
fmt.Printf("opening database: %s (%d)\n", path, ocode)
connection = new(Connection);
connection.Dystopia = C.tcidbnew();
open := C.tcidbopen(connection.Dystopia, C.CString(path), _C_int(ocode));
if open == false {
fmt.Printf("couldn't open database (%d) %s:\n", ocode, path);
connection.ErrorDisplay();
return nil, os.NewError(connection.ErrorMessage());
}
return connection, nil;
}
func OpenReadOnly(path string) (connection *Connection, err os.Error) {
return Open(path, OpenReaderFlag() | OpenNoBlockFlag());
//return Open(path, OpenReaderFlag() | OpenNoLockFlag());
// return Open(path, OpenReaderFlag());
}
func OpenAndCreate(path string) (connection *Connection, err os.Error) {
return Open(path, OpenWriterFlag() | OpenCreateFlag());
}
func (connection *Connection) Close() os.Error {
ok := C.tcidbclose(connection.Dystopia);
if ok == false {
return os.NewError(connection.ErrorMessage())
}
C.tcidbdel(connection.Dystopia);
return nil;
}
// If record exists with this key, it is overwritten
func (connection *Connection) Put(id int, text string) (err os.Error) {
fmt.Printf("storing %v => %v\n", id, text);
if C.tcidbput(connection.Dystopia, _C_int64_t(id), C.CString(text)) == false {
return os.NewError(connection.ErrorMessage())
}
return nil;
}
func (connection *Connection) Search(query string) (*vector.IntVector, os.Error) {
var count _C_int;
resp := C.tcidbsearch(connection.Dystopia, C.CString(query), C.x_substring(), &count);
fmt.Printf("searched for %v, num results = %d, resp = %v\n", query, count, resp);
var result vector.IntVector;
for i := 0; i < int(count); i++ {
result.Push(int(C.x_get_result_item(resp, _C_int(i))));
}
// return &result, nil;
return &result, nil;
}
func (connection *Connection) SearchPrefix(query string) (*vector.IntVector, os.Error) {
var count _C_int;
resp := C.tcidbsearch(connection.Dystopia, C.CString(query), C.x_prefix(), &count);
fmt.Printf("searched for %v, num results = %d, resp = %v\n", query, count, resp);
var result vector.IntVector;
for i := 0; i < int(count); i++ {
result.Push(int(C.x_get_result_item(resp, _C_int(i))));
}
// return &result, nil;
return &result, nil;
}
func (connection *Connection) Fetch(id int) (string) {
result := C.tcidbget(connection.Dystopia, _C_int64_t(id));
return C.GoString(result);
}
func (connection *Connection) SearchAndFetch(query string) (*vector.StringVector, os.Error) {
ids, err := connection.Search(query)
if err != nil {
return nil, err;
}
var result vector.StringVector;
for _, id := range *ids {
result.Push(connection.Fetch(id));
}
return &result, nil;
}
/*
// If record exists with this key, nothing happens
func (connection *Connection) Create(primaryKey string, columns ColumnMap) (exists bool, err os.Error) {
fmt.Printf("Create[%s]\n", primaryKey);
cols := C.xtc_mapnew();
defer C.xtc_mapdel(cols);
for name, value := range columns {
C.xtc_mapput(cols, C.CString(name), C.CString(value))
}
if C.xtcidb_tblputkeep(connection.Tyrant, C.CString(primaryKey), cols) == 0 {
if connection.ErrorCode() != ErrCodeKeep() {
return exists, os.NewError(connection.ErrorMessage())
}
exists = true
}
return exists, nil;
}
*/
/*
func (connection *Connection) Get(primaryKey string) *ColumnMap {
cols := C.xtcidb_tblget(connection.Tyrant, C.CString(primaryKey));
if cols == nil {
return nil
}
result := make(ColumnMap);
result["PKEY"] = primaryKey;
C.xtc_mapiterinit(cols);
name := C.xtc_mapiternext2(cols);
for name != nil {
result[C.GoString(name)] = C.GoString(C.xtc_mapget2(cols, name));
name = C.xtc_mapiternext2(cols);
}
return &result;
}
func (connection *Connection) MakeQuery() (query *Query) {
query = new(Query);
query.Tyrant = C.xtcidb_qrynew(connection.Tyrant);
return query;
}
func StringEqual() int { return int(C.x_streq()) }
func StringIncluded() int { return int(C.x_strinc()) }
func StringBeginsWith() int { return int(C.x_strbw()) }
func NumLessThan() int { return int(C.x_numlt()) }
func OrderStrAsc() int { return int(C.x_strasc()) }
func OrderStrDesc() int { return int(C.x_strdesc()) }
func OrderNumAsc() int { return int(C.x_numasc()) }
func OrderNumDesc() int { return int(C.x_numdesc()) }
func ErrCodeKeep() int { return int(C.x_errcode_keep()) }
func (query *Query) AddCondition(column_name string, op int, expression string) {
fmt.Printf("adding condition on column '%s', operation = %d, expression = '%s'\n",
column_name, op, expression);
C.xtcidb_qryaddcond(query.Tyrant, C.CString(column_name), _C_int(op), C.CString(expression));
}
func (query *Query) SetLimit(limit int) {
C.xtcidb_qrysetlimit(query.Tyrant, _C_int(limit), 0)
}
func (query *Query) SetLimitOffset(limit int, offset int) {
C.xtcidb_qrysetlimit(query.Tyrant, _C_int(limit), _C_int(offset))
}
func (query *Query) SetOrder(columnName string, order int) {
C.xtcidb_qrysetorder(query.Tyrant, C.CString(columnName), _C_int(order));
}
type ColumnMap map[string]string
type Row struct {
Data ColumnMap;
}
type SearchResult struct {
Rows []Row;
Count int;
}
// XXX: just return a Vector of map[string] string...
func (connection *Connection) Execute(query *Query) SearchResult {
list := C.xtcidb_qrysearch(query.Tyrant);
list_size := int(C.xtc_listnum(list));
fmt.Printf("list size: %d\n", list_size);
rows := make([]Row, list_size);
for i := 0; i < list_size; i++ {
pk := C.xtc_listval(list, _C_int(i));
cols := C.xtcidb_tblget(connection.Tyrant, pk);
if cols != nil {
var row Row;
row.Data = make(ColumnMap);
row.Data["PKEY"] = C.GoString(pk);
C.xtc_mapiterinit(cols);
name := C.xtc_mapiternext2(cols);
for name != nil {
row.Data[C.GoString(name)] = C.GoString(C.xtc_mapget2(cols, name));
name = C.xtc_mapiternext2(cols);
}
rows[i] = row;
}
}
var result SearchResult;
result.Rows = rows;
return result;
}
func (query *Query) Count() int {
result := C.xtcidb_qrysearchcount(query.Tyrant);
return int(result);
}
// Removes all records that match the query
func (query *Query) Remove() os.Error {
result := C.xtcidb_qrysearchout(query.Tyrant);
if result == 0 {
return os.ErrorString("Error removing records")
}
return nil
}
func (cmap *ColumnMap) GetInt64(key string) (int64, os.Error) {
frow := *cmap;
s, ok := frow[key];
if !ok {
//return 0, os.NewError("'%s' not found in column map", key);
return 0, os.NewError("key not found in column map");
}
result, err := strconv.Atoi64(s);
if err != nil {
return 0, os.NewError("error converting column to int");
}
return result, nil;
}
*/