-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_test.go
146 lines (129 loc) · 3.88 KB
/
database_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
package imdb_test
import (
"fmt"
"strings"
"sync/atomic"
"testing"
"github.com/hugjobk/imdb"
)
// Define a record struct.
type record struct {
Id int
Email string
Name string
Age int
}
// Utility function to print query results.
func printResults(recs []interface{}) string {
var arr []string
for i := range recs {
arr = append(arr, fmt.Sprintf("%+v", recs[i].(record)))
}
return fmt.Sprintf("[%s]", strings.Join(arr, ", "))
}
// Simple test for creating, indexing, insertion and query database.
func TestDatabase(t *testing.T) {
db := imdb.NewDatabase() // Create new database.
db.UniqueIndex("Id") // Create a unique index on field `Id`.
db.UniqueIndex("Email") // Create a unique index on field `Email`.
db.Index("Age") // Create a non-unique index on field `Age`.
db.Index("Age", "Name") // Create a non-unique index on fields `Age` and `Name`.
// Note: unique index is normally faster than non-unique index.
// If a query mathes both unique and non-unique index, it will choose unique index to query.
// Add first recrod.
if err := db.Add(record{1, "email1", "name1", 20}); err != nil {
t.Log(err)
}
// Add second record.
if err := db.Add(record{2, "email2", "name2", 20}); err != nil {
t.Log(err)
}
// Add third record.
if err := db.Add(record{3, "email3", "name3", 21}); err != nil {
t.Log(err)
}
// Add forth record.
if err := db.Add(record{4, "email4", "name4", 21}); err != nil {
t.Log(err)
}
// Add fifth record.
// This should return an error since a record with `Id` = 1 already exists (violate unique index on `Id`).
if err := db.Add(record{1, "email5", "name5", 20}); err != nil {
t.Log(err)
}
// Note: you can query by any field even if it is not indexed.
// However it can be slow because it has to scan the database to find matched records.
// You can use PrepareFilter to reuse a query multiple times for different query parameters.
// Build a query where `Id` = 3.
q1 := db.Query().Filter("Id", 3).Build()
// Build a query where `Email` = "email2".
q2 := db.Query().Filter("Email", "email2").Build()
// Build a query where `Age` = 21.
q3 := db.Query().Filter("Age", 21).Build()
// Build a query where `Name` = "name4" AND `Age` = 21.
q4 := db.Query().Filter("Name", "name4").Filter("Age", 21).Build()
// Print the queries and their results.
t.Logf("%s -> %s", q1, printResults(q1.Run()))
t.Logf("%s -> %s", q2, printResults(q2.Run()))
t.Logf("%s -> %s", q3, printResults(q3.Run()))
t.Logf("%s -> %s", q4, printResults(q4.Run()))
}
var db *imdb.Database
var DatabaseSize uint32
func init() {
// Create a database and indexes to speed up query speed.
db = imdb.NewDatabase()
db.UniqueIndex("Id")
db.UniqueIndex("Email")
db.Index("Name", "Age")
}
func BenchmarkAdd(b *testing.B) {
b.RunParallel(func(p *testing.PB) {
for p.Next() {
i := atomic.AddUint32(&DatabaseSize, 1)
id := int(i)
email := fmt.Sprintf("email%d", i)
name := fmt.Sprintf("name%d", i%10000)
age := int(i % 100)
db.Add(record{id, email, name, age})
}
})
}
func BenchmarkQueryById(b *testing.B) {
var c uint32
b.RunParallel(func(p *testing.PB) {
var id int
q := db.Query().PrepareFilter("Id", &id).Build()
for p.Next() {
i := atomic.AddUint32(&c, 1)
id = int(i % uint32(DatabaseSize))
q.Run()
}
})
}
func BenchmarkQueryByEmail(b *testing.B) {
var c uint32
b.RunParallel(func(p *testing.PB) {
var email string
q := db.Query().PrepareFilter("Email", &email).Build()
for p.Next() {
i := atomic.AddUint32(&c, 1)
email = fmt.Sprintf("email%d", i%DatabaseSize)
q.Run()
}
})
}
func BenchmarkQueryByNameAndAge(b *testing.B) {
var c uint32
b.RunParallel(func(p *testing.PB) {
var name string
var age int
q := db.Query().PrepareFilter("Name", &name).PrepareFilter("Age", &age).Build()
for p.Next() {
i := atomic.AddUint32(&c, 1)
name = fmt.Sprintf("name%d", i%10000)
age = int(i % 100)
q.Run()
}
})
}