-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode.go
299 lines (248 loc) · 7.34 KB
/
node.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"encoding/binary"
"fmt"
"io"
"strings"
)
type Node struct {
NumProperties uint64
PropertyListLen uint64
NameLen uint8
Name string
Properties []*Property
ArrayProperties []*ArrayProperty
NestedNodes []*Node
Length uint64
id uint64
endingID uint64 // ID of the last descendent node
}
// NewNode creates a new node and calculates some properties required to write to file
func NewNode(name string, properties []*Property, arrayProperties []*ArrayProperty, nestedNodes []*Node) *Node {
var propertyLength uint64
for _, p := range properties {
propertyLength += p.Size()
}
for _, p := range arrayProperties {
propertyLength += p.Size()
}
var nestedLength uint64
for _, n := range nestedNodes {
if n == nil {
continue
}
nestedLength += n.Length
}
return &Node{
NumProperties: uint64(len(properties) + len(arrayProperties)),
PropertyListLen: propertyLength,
NameLen: uint8(len(name)),
Name: name,
Properties: properties,
ArrayProperties: arrayProperties,
NestedNodes: nestedNodes,
Length: nestedLength + propertyLength + uint64(len(name)) + 25, // 8 + 8 + 8 + 1
}
}
// NewNodeSingleProperty creates a new node that only has one property
func NewNodeSingleProperty(name string, property *Property) *Node {
return NewNode(name, []*Property{property}, nil, nil)
}
func NewNodeSingleArrayProperty(name string, property *ArrayProperty) *Node {
return NewNode(name, nil, []*ArrayProperty{property}, nil)
}
func NewNodeInt32Slice(name string, i []int32) *Node {
return NewNodeSingleArrayProperty(name, NewArrayPropertyInt32Slice(i))
}
func NewNodeFloat64Slice(name string, i []float64) *Node {
return NewNodeSingleArrayProperty(name, NewArrayPropertyFloat64Slice(i))
}
// NewNodeInt32 creates a new node with a single int32 property
func NewNodeInt32(name string, i int32) *Node {
return NewNode(name, []*Property{NewPropertyInt32(i)}, nil, nil)
}
// NewNodeInt64 creates a new node with a single int64 property
func NewNodeInt64(name string, i int64) *Node {
return NewNode(name, []*Property{NewPropertyInt64(i)}, nil, nil)
}
// NewNodeString creates a new node with a single string property
func NewNodeString(name string, s string) *Node {
return NewNode(name, []*Property{NewPropertyString(s)}, nil, nil)
}
// NewNodeParent creates a node who only has children node, no properties
func NewNodeParent(name string, children ...*Node) *Node {
return NewNode(name, nil, nil, children)
}
// ShallowCopy returns a new node and shallow copies of any array type
// contained within the struct
func (n Node) ShallowCopy() *Node {
props := make([]*Property, len(n.Properties))
copy(props, n.Properties)
arrayProps := make([]*ArrayProperty, len(n.ArrayProperties))
copy(arrayProps, n.ArrayProperties)
newNodes := make([]*Node, len(n.NestedNodes))
copy(newNodes, n.NestedNodes)
return &Node{
NumProperties: n.NumProperties,
PropertyListLen: n.PropertyListLen,
NameLen: n.NameLen,
Name: n.Name,
Properties: props,
ArrayProperties: arrayProps,
NestedNodes: newNodes,
Length: n.Length,
id: n.id,
endingID: n.endingID,
}
}
func (n *Node) ApplyDiffs(allDiffs []Diff, curDifIndex int) (*Node, int) {
if n.Length == 0 {
return n, curDifIndex
}
if len(allDiffs) == 0 || len(allDiffs) == curDifIndex {
return n, curDifIndex
}
// if none of the diffs apply to any of the nodes or subnode..
if n.endingID < allDiffs[curDifIndex].NodeID() {
return n, curDifIndex
}
newDifIndex := curDifIndex
diffedNode := n
for newDifIndex < len(allDiffs) {
if n.id < allDiffs[newDifIndex].NodeID() {
break
}
if allDiffs[newDifIndex].NodeID() == n.id {
diffedNode, _ = allDiffs[newDifIndex].Apply(diffedNode)
}
newDifIndex++
if diffedNode == nil {
return diffedNode, newDifIndex
}
}
for i, nested := range diffedNode.NestedNodes {
diffedNode.NestedNodes[i], newDifIndex = nested.ApplyDiffs(allDiffs, newDifIndex)
}
var propertyLength uint64
for _, p := range diffedNode.Properties {
propertyLength += p.Size()
}
for _, p := range diffedNode.ArrayProperties {
propertyLength += p.Size()
}
diffedNode.PropertyListLen = propertyLength
var nestedLength uint64
for _, n := range diffedNode.NestedNodes {
if n == nil {
continue
}
// Length of 0 denotes empty node, but it still takes up space when
// we write it to disk, empty nodes take up 25 bytes
if n.Length == 0 {
nestedLength += 25
} else {
nestedLength += n.Length
}
}
diffedNode.Length = nestedLength + propertyLength + uint64(len(diffedNode.Name)) + 25
diffedNode.NameLen = uint8(len(diffedNode.Name))
diffedNode.NumProperties = uint64(len(diffedNode.Properties) + len(diffedNode.ArrayProperties))
return diffedNode, newDifIndex
}
func (node Node) Write(writer io.Writer, currentOffset uint64, endOfList bool) (uint64, error) {
offset := node.Length
if offset != 0 {
offset += currentOffset
}
err := binary.Write(writer, binary.LittleEndian, uint64(offset))
if err != nil {
return 0, err
}
err = binary.Write(writer, binary.LittleEndian, node.NumProperties)
if err != nil {
return 0, err
}
err = binary.Write(writer, binary.LittleEndian, node.PropertyListLen)
if err != nil {
return 0, err
}
err = binary.Write(writer, binary.LittleEndian, node.NameLen)
if err != nil {
return 0, err
}
_, err = writer.Write([]byte(node.Name))
if err != nil {
return 0, err
}
for _, p := range node.ArrayProperties {
err := p.Write(writer)
if err != nil {
return 0, nil
}
}
for _, p := range node.Properties {
err := p.Write(writer)
if err != nil {
return 0, nil
}
}
offsetSofar := currentOffset + 25 + uint64(node.NameLen) + node.PropertyListLen
for i, p := range node.NestedNodes {
offsetSofar, err = p.Write(writer, offsetSofar, len(node.NestedNodes)-1 == i)
if err != nil {
return 0, nil
}
}
return uint64(node.Length + currentOffset), err
}
// PropertyInfo looks at all properties contained within the node and computes
// how much space it takes up
// func (node Node) PropertyInfo() (int64, int64, []byte) {
// }
// Int32Slice treats as the node only has a single property and retrieves it as
// a Int32Slice
func (node *Node) Int32Slice() ([]int32, bool) {
if len(node.ArrayProperties) != 1 {
return nil, false
}
return node.ArrayProperties[0].AsInt32Slice(), true
}
// Float64Slice treats as the node only has a single property and retrieves it
// as a Float64Slice
func (node *Node) Float64Slice() ([]float64, bool) {
if len(node.ArrayProperties) != 1 {
return nil, false
}
return node.ArrayProperties[0].AsFloat64Slice(), true
}
func (node *Node) StringProperty() (string, bool) {
if len(node.Properties) != 1 {
return "", false
}
return node.Properties[0].AsString(), true
}
func (n Node) GetNodes(names ...string) []*Node {
if len(names) == 0 {
return []*Node{&n}
}
nodes := []*Node{}
for _, c := range n.NestedNodes {
if c.Name == names[0] {
nodes = append(nodes, c.GetNodes(names[1:]...)...)
}
}
return nodes
}
func (n *Node) String() string {
b := strings.Builder{}
b.WriteString(n.Name)
b.WriteString(":")
if len(n.Properties) > 0 {
b.WriteString(fmt.Sprint("", n.Properties, ""))
}
if len(n.NestedNodes) > 0 {
b.WriteString(fmt.Sprint("{", n.NestedNodes, "}"))
}
b.WriteString("\n")
return b.String()
}