forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
99 lines (88 loc) · 2.02 KB
/
index.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
package types
import (
"encoding/binary"
"strings"
"time"
"github.com/dgraph-io/dgraph/tok"
"github.com/dgraph-io/dgraph/x"
geom "github.com/twpayne/go-geom"
)
const (
// Posting list keys are prefixed with this rune if it is a mutation meant for
// the index.
indexRune = ':'
dateFormat1 = "2006-01-02"
dateFormat2 = "2006-01-02T15:04:05"
)
func IndexTokens(sv Val) ([]string, error) {
switch sv.Tid {
case GeoID:
return IndexGeoTokens(sv.Value.(geom.T))
case Int32ID:
return IntIndex(sv.Value.(int32))
case FloatID:
return FloatIndex(sv.Value.(float64))
case DateID:
return DateIndex(sv.Value.(time.Time))
case DateTimeID:
return TimeIndex(sv.Value.(time.Time))
case StringID:
return DefaultIndexKeys(sv.Value.(string)), nil
default:
return nil, x.Errorf("Invalid type. Cannot be indexed")
}
return nil, nil
}
// DefaultIndexKeys tokenizes data as a string and return keys for indexing.
func DefaultIndexKeys(val string) []string {
words := strings.Fields(val)
tokens := make([]string, 0, 5)
for _, it := range words {
if it == "_nil_" {
tokens = append(tokens, it)
continue
}
tokenizer, err := tok.NewTokenizer([]byte(it))
if err != nil {
return nil
}
for {
s := tokenizer.Next()
if s == nil {
break
}
tokens = append(tokens, string(s))
}
tokenizer.Destroy()
}
return tokens
}
func encodeInt(val int32) ([]string, error) {
buf := make([]byte, 5)
binary.BigEndian.PutUint32(buf[1:], uint32(val))
if val < 0 {
buf[0] = 0
} else {
buf[0] = 1
}
return []string{string(buf)}, nil
}
// IntIndex indexs int type.
func IntIndex(val int32) ([]string, error) {
return encodeInt(val)
}
// FloatIndex indexs float type.
func FloatIndex(val float64) ([]string, error) {
in := int32(val)
return encodeInt(in)
}
// DateIndex indexs time type.
func DateIndex(val time.Time) ([]string, error) {
in := int32(val.Year())
return encodeInt(in)
}
// TimeIndex indexs time type.
func TimeIndex(val time.Time) ([]string, error) {
in := int32(val.Year())
return encodeInt(in)
}