-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (76 loc) · 1.78 KB
/
main.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
package main
import (
"context"
"flag"
"log"
"github.com/shamcode/simd/debug"
"github.com/shamcode/simd/executor"
"github.com/shamcode/simd/indexes/hash"
"github.com/shamcode/simd/namespace"
"github.com/shamcode/simd/query"
"github.com/shamcode/simd/record"
"github.com/shamcode/simd/sort"
"github.com/shamcode/simd/where"
)
type User struct {
ID int64
Name string
}
func (u *User) GetID() int64 { return u.ID }
var userFields = record.NewFields()
var id = record.NewIDGetter[*User]()
var name = record.ComparableGetter[*User, string]{
Field: userFields.New("name"),
Get: func(item *User) string { return item.Name },
}
func main() {
debugEnabled := flag.Bool("debug", false, "enabled debug")
flag.Parse()
store := namespace.CreateNamespace[*User]()
queryBuilder := query.NewBuilder[*User]
queryExecutor := executor.CreateQueryExecutor(store)
// if debug enabled, add logging for query
if *debugEnabled {
queryBuilder = debug.WrapCreateQueryBuilder(queryBuilder)
queryExecutor = debug.WrapQueryExecutor(queryExecutor, func(s string) {
log.Printf("SIMD QUERY: %s", s)
})
}
store.AddIndex(hash.NewComparableHashIndex(id, true))
store.AddIndex(hash.NewComparableHashIndex(name, false))
for _, user := range []*User{
{
ID: 1,
Name: "Foo",
},
{
ID: 2,
Name: "Bar",
},
{
ID: 3,
Name: "Faz",
},
} {
err := store.Insert(user)
if nil != err {
log.Fatal(err)
}
}
query := queryBuilder(
query.Where(id, where.GT, 1),
query.Sort(sort.Asc(name)),
).Query()
ctx := context.Background()
cur, total, err := queryExecutor.FetchAllAndTotal(ctx, query)
if nil != err {
log.Fatal(err)
}
for cur.Next(ctx) {
log.Printf("%#v", cur.Item())
}
if err := cur.Err(); nil != err {
log.Fatal(err)
}
log.Printf("total: %d", total)
}