-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffsdb_test.go
115 lines (97 loc) · 2 KB
/
ffsdb_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
package ffsdb
import (
"testing"
"time"
)
func TestFfsdb(t *testing.T) {
// create a new database at path 'test.db'
fdb, err := NewFfsdb("test.db", 256, true)
if err != nil {
t.Fatal(err)
}
defer fdb.Close()
// example float64 slice to save in the database
foo := make([]float64, 256)
start := time.Now()
for i := 0; i < 100e3; i++ {
// add new entry to the database
err := fdb.Add(foo)
if err != nil {
t.Fatal(err)
}
}
t.Log("added 100000 in time:", time.Since(start))
// read the datebase from the beginning
fdb.Rewind()
start = time.Now()
i := 0
// read all entries from the database
ok := true
for ok {
foo, ok = fdb.ReadNext()
i++
}
t.Log("read", i-1, "in time:", time.Since(start))
}
func TestSeek(t *testing.T) {
// create a new database at path 'test.db'
fdb, err := NewFfsdb("test.db", 2, true)
if err != nil {
t.Fatal(err)
}
defer fdb.Close()
// example float64 slice to save in the database
foo := make([]float64, 2)
start := time.Now()
for i := 0; i < 10; i++ {
// add new entry to the database
foo[0] = float64(i)
err := fdb.Add(foo)
if err != nil {
t.Fatal(err)
}
}
t.Log("added 10 in time:", time.Since(start))
// test if we rewind if we still write at the end of the file
fdb.Rewind()
start = time.Now()
for i := 10; i < 20; i++ {
// add new entry to the database
foo[0] = float64(i)
err := fdb.Add(foo)
if err != nil {
t.Fatal(err)
}
}
t.Log("added 10 in time:", time.Since(start))
fdb.Rewind()
start = time.Now()
i := 0
// read all entries from the database
ok := true
for ok {
foo, ok = fdb.ReadNext()
if ok {
t.Log(i, "-", foo)
i++
}
}
t.Log("read", i, "in time:", time.Since(start))
// read at a specific id
bar, err := fdb.ReadId(10)
if err != nil {
t.Fatal(err)
}
t.Log("id 10:", bar)
// update an id
foo = []float64{100., 100.}
err = fdb.Update(10, foo)
if err != nil {
t.Fatal(err)
}
bar, err = fdb.ReadId(10)
if err != nil {
t.Fatal(err)
}
t.Log("id 10 after update:", bar)
}