This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexamples_test.go
357 lines (288 loc) · 7.2 KB
/
examples_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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package gotinydb
import (
"context"
"fmt"
"io"
"log"
"os"
"time"
"github.com/blevesearch/bleve"
)
type (
user struct {
ID int
Email string
LastLogin time.Time
}
)
var (
testTime time.Time
dbPath string
dbKey [32]byte
exampleDB *DB
exampleCollection *Collection
)
func init() {
testTime.UnmarshalText([]byte("2018-11-05T12:20:44.588809926+01:00"))
dbPath = os.TempDir() + "/example"
dbKey = [32]byte{}
os.RemoveAll(dbPath)
os.RemoveAll(os.TempDir() + "/package_example")
os.RemoveAll("path_to_database_directory")
var err error
// Open or create the database at the given path and with the given encryption key
exampleDB, err = Open(dbPath, dbKey)
if err != nil {
log.Fatal(err)
}
// Open a collection
exampleCollection, err = exampleDB.Use("users")
if err != nil {
log.Fatal(err)
}
userDocumentMapping := bleve.NewDocumentMapping()
exampleCollection.SetBleveIndex("index X", userDocumentMapping)
doc := &struct {
Name string
}{
"I'm the example document",
}
exampleCollection.Put("index X document", doc)
var writer Writer
writer, err = exampleDB.GetFileStore().GetFileWriter("read file", "txt")
if err != nil {
log.Fatal(err)
}
defer writer.Close()
writer.Write([]byte("JbBSFroiVAtjQy6bR3xXgrPY2GFvOPRqvWxfHUmAFAksELPTpV0lmPvwjMwdqq5i"))
}
func Example() {
// Open or create the database at the given path and with the given encryption key
db, err := Open(os.TempDir()+"/package_example", dbKey)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Open a collection
var c *Collection
c, err = db.Use("users")
if err != nil {
log.Fatal(err)
}
// Build the index mapping (take a look at bleve)
// This is a static mapping document to index only specified fields
userDocumentMapping := bleve.NewDocumentStaticMapping()
// Build the field checker
emailFieldMapping := bleve.NewTextFieldMapping()
// Add a text filed to Email property
userDocumentMapping.AddFieldMappingsAt("Email", emailFieldMapping)
// In this case it indexes only the field "Email"
// Save the bleve indexexes
err = c.SetBleveIndex("email", userDocumentMapping)
if err != nil {
if err != ErrNameAllreadyExists {
log.Fatal(err)
}
}
// Example user
record := &user{
316,
"jonas-90@tlaloc.com",
testTime,
}
// Save it in DB
if err = c.Put("id", record); err != nil {
log.Fatal(err)
}
// Build the query
query := bleve.NewQueryStringQuery(record.Email)
// Add the query to the search
var searchResult *SearchResult
searchResult, err = c.Search("email", query)
if err != nil {
log.Fatal(err)
}
// Convert the reccored into a struct using JSON internally
retrievedRecord := new(user)
id, respErr := searchResult.Next(retrievedRecord)
if respErr != nil {
log.Fatal(respErr)
}
// Display the result
fmt.Println(id)
fmt.Println(retrievedRecord.ID, retrievedRecord.Email, retrievedRecord.LastLogin.Format(time.Stamp))
// Output: id
// 316 jonas-90@tlaloc.com Nov 5 12:20:44
}
func ExampleOpen() {
// Open or create the database at the given path and with the given encryption key
db, err := Open("path_to_database_directory", dbKey)
if err != nil {
log.Fatal(err)
}
// Remumber to close the database
err = db.Close()
fmt.Println(err)
// Output: <nil>
}
func ExampleDB_Use() {
// Open a collection
col, err := exampleDB.Use("collection name")
if err != nil {
log.Fatal(err)
}
fmt.Println(col.Name())
// Output: collection name
}
func ExampleCollection_Put() {
record := &struct{}{}
err := exampleCollection.Put("id", record)
fmt.Println(err)
// Output: <nil>
}
func ExampleCollection_Get() {
record := &struct {
Name string
}{
"testing name",
}
exampleCollection.Put("id", record)
retrievedRecord := &struct {
Name string
}{}
recordAsBytes, _ := exampleCollection.Get("id", retrievedRecord)
fmt.Println(string(recordAsBytes))
fmt.Println(retrievedRecord)
// Output: {"Name":"testing name"}
// &{testing name}
}
func ExampleWriter() {
writer, err := exampleDB.GetFileStore().GetFileWriter("file example", "test.txt")
if err != nil {
log.Fatal(err)
}
defer writer.Close()
n := 0
writtenBytes := 0
n, err = writer.Write([]byte("this is a text file"))
if err != nil {
log.Fatal(err)
}
writtenBytes += n
n, err = writer.Write([]byte("\n"))
if err != nil {
log.Fatal(err)
}
writtenBytes += n
n, err = writer.Write([]byte("and then the second is written"))
if err != nil {
log.Fatal(err)
}
writtenBytes += n
fmt.Println("writtenBytes", writtenBytes)
readBuff := make([]byte, 1000)
writer.Seek(0, io.SeekStart)
n, err = writer.Read(readBuff)
if err != nil && err != io.EOF {
log.Fatal(err)
}
fmt.Println(n, string(readBuff[:n]))
// Output: writtenBytes 50
// 50 this is a text file
// and then the second is written
}
func ExampleReader() {
reader, err := exampleDB.GetFileStore().GetFileReader("read file")
if err != nil {
log.Fatal(err)
}
defer reader.Close()
readBuffer := make([]byte, 100)
n := 0
n, err = reader.ReadAt(readBuffer, 25)
if err != nil && err != io.EOF {
log.Fatal(err)
}
readBuffer = readBuffer[:n]
fmt.Println(n, string(readBuffer))
// Output: 39 GFvOPRqvWxfHUmAFAksELPTpV0lmPvwjMwdqq5i
}
func ExampleCollection_SetBleveIndex() {
// Build the index mapping (take a look at bleve)
// This is a static mapping document to index only specified fields
userDocumentMapping := bleve.NewDocumentStaticMapping()
// Build the field checker
emailFieldMapping := bleve.NewTextFieldMapping()
// Add a text filed to Email property
userDocumentMapping.AddFieldMappingsAt("Email", emailFieldMapping)
err := exampleCollection.SetBleveIndex("your index name", userDocumentMapping)
fmt.Println(err)
// Output: <nil>
}
func ExampleCollection_Search() {
query := bleve.NewMatchQuery("example")
response, err := exampleCollection.Search("index X", query)
if err != nil {
log.Fatal(err)
}
dest := &struct{ Name string }{}
var resp *Response
resp, err = response.NextResponse(dest)
if err != nil {
log.Fatal(err)
}
fmt.Println(dest)
fmt.Println(resp.ID)
fmt.Println(string(resp.Content))
fmt.Println(resp.DocumentMatch.Score)
// Output: &{I'm the example document}
// index X document
// {"Name":"I'm the example document"}
// 0.7071067690849304
}
func ExampleBatch() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
u1 := &user{
2435,
"user1@dom.com",
testTime,
}
batch, err := exampleCollection.NewBatch(ctx)
batch.Put("second write", u1)
if err != nil {
log.Fatal(err)
}
u2 := &user{
56548,
"user2@dom.com",
testTime,
}
batch.Put("second write", u2)
err = batch.Write()
if err != nil {
log.Fatal(err)
}
fmt.Println(err)
// Output: <nil>
}
func ExampleCollectionIterator() {
iter := exampleCollection.GetIterator()
defer iter.Close()
for ; iter.Valid(); iter.Next() {
fmt.Println(string(iter.GetBytes()))
}
// Output: {"Name":"testing name"}
// {"Name":"I'm the example document"}
// {"ID":56548,"Email":"user2@dom.com","LastLogin":"2018-11-05T12:20:44.588809926+01:00"}
}
func ExampleFileIterator() {
iter := exampleDB.GetFileStore().GetFileIterator()
defer iter.Close()
for ; iter.Valid(); iter.Next() {
meta := iter.GetMeta()
fmt.Println(meta.ID, meta.Name)
}
// Output: read file txt
// file example test.txt
}