forked from akrennmair/epos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
59 lines (47 loc) · 1.21 KB
/
query.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
package epos
import (
"fmt"
"strconv"
)
// Query takes a query in the form of a (possibly nested) Condition, and returns
// a Result object.
func (c *Collection) Query(q Condition) (*Result, error) {
fields := getFields(q)
for _, field := range fields {
_, ok := c.indexes[field]
if !ok {
return nil, fmt.Errorf("no index on field '%s'", field)
}
}
ids := q.match(c.indexes)
return newResult(c, ids), nil
}
// QueryId returns a Result object that will exactly deliver
// the object with the requested ID.
func (c *Collection) QueryId(id Id) (*Result, error) {
return c.Query(&id)
}
// QueryAll returns a Result object that will deliver
// all objects in the object store in no particular order.
func (c *Collection) QueryAll() (*Result, error) {
ids := []Id{}
for id_str := range c.store.Keys() {
id, err := strconv.ParseInt(id_str, 10, 64)
if err == nil {
ids = append(ids, Id(id))
}
}
return newResult(c, ids), nil
}
func getFields(q Condition) []string {
raw_fields := q.getFields()
fields_map := make(map[string]bool)
for _, field := range raw_fields {
fields_map[field] = true
}
fields := []string{}
for k, _ := range fields_map {
fields = append(fields, k)
}
return fields
}