-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcursor_test.go
220 lines (180 loc) · 5.15 KB
/
cursor_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
package sophia
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
func TestCursor(t *testing.T) {
const (
keyPath = "key"
valuePath = "value"
recordsCount = 100
valueTemplate = "value%011v"
)
tmpDir, err := ioutil.TempDir("", "sophia_test")
require.Nil(t, err)
defer os.RemoveAll(tmpDir)
env, err := NewEnvironment()
require.Nil(t, err)
require.NotNil(t, env)
require.True(t, env.SetString(EnvironmentPath, tmpDir))
schema := &Schema{}
require.Nil(t, schema.AddKey("key", FieldTypeUInt64))
require.Nil(t, schema.AddValue("value", FieldTypeString))
db, err := env.NewDatabase(DatabaseConfig{
Name: "test_database",
Schema: schema,
})
require.Nil(t, err)
require.NotNil(t, db)
require.Nil(t, env.Open())
defer env.Close()
for i := 0; i < recordsCount; i++ {
doc := db.Document()
require.True(t, doc.SetInt(keyPath, int64(i)))
require.True(t, doc.SetString(valuePath, fmt.Sprintf(valueTemplate, i)))
require.Nil(t, db.Set(doc))
doc.Free()
}
t.Run("All records", func(t *testing.T) { testCursor(t, db, 0, recordsCount, valueTemplate) })
t.Run("Half records", func(t *testing.T) { testCursor(t, db, recordsCount/2, recordsCount, valueTemplate) })
t.Run("Quater records", func(t *testing.T) { testCursor(t, db, recordsCount/4, recordsCount, valueTemplate) })
t.Run("Use closed cursor error", func(t *testing.T) { testCursorError(t, db) })
t.Run("Reverse iterator", func(t *testing.T) { testReverseCursor(t, db, recordsCount, valueTemplate) })
}
func testCursorError(t *testing.T, db *Database) {
doc := db.Document()
require.False(t, doc.IsEmpty())
cursor, err := db.Cursor(doc)
require.Nil(t, err)
require.NotNil(t, cursor)
err = cursor.Close()
require.Nil(t, err)
require.Error(t, cursor.Close())
d := cursor.Next()
require.True(t, d.IsEmpty())
}
func testCursor(t *testing.T, db *Database, start, count int64, valueTemplate string) {
id := start
doc := db.Document()
require.False(t, doc.IsEmpty())
if start != 0 {
doc.SetInt("key", start)
}
cursor, err := db.Cursor(doc)
require.Nil(t, err)
require.NotNil(t, cursor)
defer func() {
require.Nil(t, cursor.Close())
}()
var (
size int
counter int64
)
for d := cursor.Next(); !d.IsEmpty(); d = cursor.Next() {
require.Equal(t, id, d.GetInt("key"))
require.Equal(t, fmt.Sprintf(valueTemplate, id), d.GetString("value", &size))
counter++
id++
}
require.Equal(t, count-start, counter)
}
func testReverseCursor(t *testing.T, db *Database, count int64, valueTemplate string) {
doc := db.Document()
require.False(t, doc.IsEmpty())
doc.Set(CursorOrder, LTE)
cursor, err := db.Cursor(doc)
require.Nil(t, err)
require.NotNil(t, cursor)
defer func() {
require.Nil(t, cursor.Close())
}()
var (
size int
id int64 = count - 1
)
for d := cursor.Next(); !d.IsEmpty(); d = cursor.Next() {
require.Equal(t, id, d.GetInt("key"))
require.Equal(t, fmt.Sprintf(valueTemplate, id), d.GetString("value", &size))
id--
}
}
func TestCursorPrefix(t *testing.T) {
const (
keyPath = "key"
valuePath = "value"
recordsCount = 100
valueTemplate = "value%011v"
)
tmpDir, err := ioutil.TempDir("", "sophia_test")
require.Nil(t, err)
defer os.RemoveAll(tmpDir)
env, err := NewEnvironment()
require.Nil(t, err)
require.NotNil(t, env)
require.True(t, env.SetString(EnvironmentPath, tmpDir))
schema := &Schema{}
require.Nil(t, schema.AddKey(keyPath, FieldTypeString))
require.Nil(t, schema.AddValue(valuePath, FieldTypeString))
db, err := env.NewDatabase(DatabaseConfig{
Name: "test_database",
Schema: schema,
})
require.Nil(t, err)
require.NotNil(t, db)
require.Nil(t, env.Open())
defer env.Close()
const base = 36
for i := int64(0); i < recordsCount; i++ {
doc := db.Document()
require.True(t, doc.SetString(keyPath, strconv.FormatInt(i, base)))
require.True(t, doc.SetString(valuePath, fmt.Sprintf(valueTemplate, i)))
require.Nil(t, db.Set(doc))
doc.Free()
}
// Calculate prefix for inserted rows
c := recordsCount
maxDigit := 1
for c > base {
c /= base
maxDigit *= base
}
expectedCount := recordsCount
for maxDigit != 1 {
c := expectedCount / maxDigit
expectedCount -= c * maxDigit
maxDigit /= base
}
prefix := recordsCount - expectedCount
prefixStr := strconv.FormatInt(int64(prefix), base)
prefixStr = prefixStr[:len(prefixStr)-1]
doc := db.Document()
require.False(t, doc.IsEmpty())
doc.Set(CursorPrefix, prefixStr)
cursor, err := db.Cursor(doc)
require.Nil(t, err)
require.NotNil(t, cursor)
defer func() {
require.Nil(t, cursor.Close())
}()
var (
size int
count int
)
// get row that match prefix
d := cursor.Next()
require.False(t, d.IsEmpty())
require.Equal(t, prefixStr, d.GetString(keyPath, &size))
require.Equal(t, fmt.Sprintf(valueTemplate, prefix/base), d.GetString(valuePath, &size))
// get rows that have additional symbol at the end
for d := cursor.Next(); !d.IsEmpty(); d = cursor.Next() {
id := prefix + count
require.Equal(t, strconv.FormatInt(int64(id), base), d.GetString(keyPath, &size))
require.Equal(t, fmt.Sprintf(valueTemplate, id), d.GetString(valuePath, &size))
count++
}
require.Equal(t, expectedCount, count)
}