-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.go
82 lines (66 loc) · 1.94 KB
/
collection.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
package nodedb
import (
"time"
)
type Collection struct {
Conn *Connection
Name string
}
// NewNode creates and inserts a new node into the collection (runs SQL insert command), this way a node has allways a valid ID
// The ID may never be zero. A node that you use in your code must allways have a valid representation in the database.
func (c *Collection) NewNode() (*Node, error) {
return insertNode(c, setupNode(c))
}
func (c *Collection) NewNodes(count int) ([]*Node, error) {
result := make([]*Node, count, count)
for i := 0; i < count; i++ {
result[i] = setupNode(c)
}
return insertNodes(c, result)
}
// LoadNode loads a node by its ID, provide withContent = true, if the payload (content) should
// be loaded with the node.
func (c *Collection) LoadNode(nodeID int64, withContent bool) (*Node, error) {
return loadNode(c, nodeID, withContent)
}
func (c *Collection) LoadNodes(nodeIDs []int64, withContent bool) ([]*Node, error) {
return loadNodes(c, nodeIDs, withContent)
}
func (c *Collection) LoadNodesInSequence(nodeIDs []int64, withContent bool) ([]*Node, error) {
nodes, err := loadNodes(c, nodeIDs, withContent)
if err != nil {
return nil, err
}
nmap := make(map[int64]*Node)
for _, n := range nodes {
nmap[n.ID] = n
}
for i, id := range nodeIDs {
nodes[i] = nmap[id]
}
return nodes, nil
}
func (c *Collection) UpdateNode(update *Node) error {
return updateNode(c, update)
}
func (c *Collection) UpdateNodes(updates []*Node) error {
return updateNodes(c, updates)
}
func (c *Collection) DeleteNode(update *Node) error {
return deleteNode(c, update)
}
func (c *Collection) DeleteNodes(updates []*Node) error {
return deleteNodes(c, updates)
}
func setupNode(c *Collection) *Node {
result := newNode()
result.Created = time.Now()
result.Start = time.Now()
result.End = c.Conn.End
result.Deleted = c.Conn.Deleted
result.OwnerID = c.Conn.RootID
result.Up = c.Conn.RootID
result.Left = 0
result.Right = 0
return result
}