-
Notifications
You must be signed in to change notification settings - Fork 0
/
bson.go
174 lines (159 loc) · 3.95 KB
/
bson.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
package bsonex
import (
"bytes"
"encoding/json"
"fmt"
"strings"
)
type BSON []byte
func (b BSON) String() string {
return string(BSONEX{BSON: b}.MustToJson())
}
func (b BSON) Lookup(key string) (val Value) {
if key == "" {
return
}
val = Value{valueType: TypeDocument, valueData: b}
sp := strings.Split(key, ".")
for _, k := range sp {
val = BSON(val.valueData).lookupOne(k)
if val.valueType == TypeEmpty {
return
}
}
return
}
func (b BSON) lookupOne(key string) (val Value) {
elements := b[4 : len(b)-1]
keyb := []byte(key)
for elements != nil {
ckey, cval, next := getElement(elements)
if ckey != nil && bytes.Equal(ckey, keyb) {
return cval
}
elements = next
}
return
}
type toSearchValue struct {
b []byte
}
func NewToSearchValue(v interface{}) (b toSearchValue, err error) {
if s, ok := v.(string); ok {
return toSearchValue{[]byte(s)}, nil
}
bs, err := Marshal(M{"v": v})
if err != nil {
return
}
// bson_size(4) + type(1) + key(v,1) + \0 (1) ...value.... \0 (1)
return toSearchValue{bs[4+1+1+1 : len(bs)-1]}, nil
}
// FastContains 可以在未解析BSON的时候先快速判断一下是否包含待查找的内容,
// 避免执行每个文档都执行Unmarshal加快查找速度。
// 需要注意的是这里查找并不精确,必要的情况下仍然需要再次Unmarshal再确认一次。
func (b BSON) FastContains(v toSearchValue) bool {
return bytes.Contains(b, v.b)
}
func (b BSON) Map() (vals M) {
vals = make(M)
elements := b[4 : len(b)-1]
for elements != nil {
ckey, cval, next := getElement(elements)
if ckey != nil {
vals[string(ckey)] = cval.Value()
}
elements = next
}
return
}
func (b BSON) ToValueMap() (vals map[string]Value) {
vals = make(map[string]Value)
elements := b[4 : len(b)-1]
for elements != nil {
ckey, cval, next := getElement(elements)
if ckey != nil {
vals[string(ckey)] = cval
}
elements = next
}
return
}
func (b BSON) Array() (arr []interface{}) {
elements := b[4 : len(b)-1]
for elements != nil {
ckey, cval, next := getElement(elements)
if ckey != nil {
arr = append(arr, cval.Value())
}
elements = next
}
return
}
func (b BSON) ToValueArray() (arr []Value) {
elements := b[4 : len(b)-1]
for elements != nil {
ckey, cval, next := getElement(elements)
if ckey != nil {
arr = append(arr, cval)
}
elements = next
}
return
}
func (b BSON) Unmarshal(out interface{}) (err error) {
return Unmarshal(b, out)
}
func (b BSON) ToJson() (s []byte, err error) {
return json.Marshal(b.Map())
}
func (b BSON) MustToJson() (s []byte) {
s, err := b.ToJson()
if err != nil {
panic(err)
}
return
}
func getElement(b BSON) (key []byte, val Value, next BSON) {
if len(b) == 0 {
return nil, val, nil
}
elementType := b[0]
keyStart, keyEnd := 1, bytes.IndexByte(b, 0x00)
key = b[keyStart:keyEnd]
var valb []byte
switch elementType {
case TypeDouble, TypeDatetime, TypeTimestamp, TypeInt64:
valb = b[keyEnd+1 : keyEnd+1+8]
case TypeString, TypeJSCode, TypeSymbol, TypeJSCodeScope:
strLen := getint(b[keyEnd+1 : keyEnd+1+4])
valb = b[keyEnd+1 : keyEnd+1+4+strLen]
case TypeDocument, TypeArray:
Len := getint(b[keyEnd+1 : keyEnd+1+4])
valb = b[keyEnd+1 : keyEnd+1+Len]
case TypeBinary:
Len := getint(b[keyEnd+1 : keyEnd+1+4])
valb = b[keyEnd+1 : keyEnd+1+4+1+Len]
case TypeUndefined, TypeNull, TypeMinKey, TypeMaxKey:
// no value
case TypeObjectId:
valb = b[keyEnd+1 : keyEnd+1+12]
case TypeBoolean:
valb = b[keyEnd+1 : keyEnd+1+1]
case TypeRegex:
i := bytes.IndexByte(b[keyEnd+1:], 0x00)
i2 := bytes.IndexByte(b[keyEnd+1+i+1:], 0x00)
valb = b[keyEnd+1 : keyEnd+1+i+1+i2+1]
case TypeDBPointer:
strLen := getint(b[keyEnd+1 : keyEnd+1+4])
valb = b[keyEnd+1 : keyEnd+1+4+int(strLen)+12]
case TypeInt32:
valb = b[keyEnd+1 : keyEnd+1+4]
case TypeDecimal128:
valb = b[keyEnd+1 : keyEnd+1+16]
default:
panic(fmt.Sprintf("invalid bson type %#v", elementType))
}
val, next = Value{elementType, valb}, b[len(key)+1+len(valb)+1:]
return
}