-
Notifications
You must be signed in to change notification settings - Fork 0
/
postlist.go
274 lines (233 loc) · 5.94 KB
/
postlist.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
package main
import (
"fmt"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-ipfs-blockstore"
cbor "github.com/ipfs/go-ipld-cbor"
)
const postsPerNode = 4
type MerkleList struct {
bs blockstore.Blockstore
root *MerkleListNode
}
type MerkleListNode struct {
Posts []*cid.Cid
Children []*childLink
Depth int
}
type childLink struct {
// Beg is the lowest timestamp on any post in this child
Beg uint64
// End is the highest timestamp on any post in this child
End uint64
// Node is the hash link to the child object
Node *cid.Cid
}
func LoadMerkleList(bs blockstore.Blockstore, c *cid.Cid) (*MerkleList, error) {
if c == nil {
return &MerkleList{
bs: bs,
root: nil,
}, nil
}
node, err := getNode(bs, c)
if err != nil {
return nil, err
}
return &MerkleList{
bs: bs,
root: node,
}, nil
}
func NewPostsNode(posts []*cid.Cid) *MerkleListNode {
return &MerkleListNode{
Depth: 0,
Posts: posts,
}
}
func NewChildrenNode(children []*childLink, depth int) *MerkleListNode {
return &MerkleListNode{
Depth: depth,
Children: children,
}
}
func (mln *MerkleListNode) getChildLink(bs blockstore.Blockstore) (*childLink, error) {
cid, err := putNode(bs, mln)
if err != nil {
return nil, err
}
if len(mln.Posts) > 0 {
begNode, err := getPost(bs, mln.Posts[0])
if err != nil {
return nil, err
}
endNode, err := getPost(bs, mln.Posts[len(mln.Posts) - 1])
if err != nil {
return nil, err
}
child := &childLink {
Beg: begNode.CreatedAt,
End: endNode.CreatedAt,
Node: cid,
}
return child, nil
} else if len(mln.Children) > 0 {
child := &childLink {
Beg: mln.Children[0].Beg,
End: mln.Children[len(mln.Children) - 1].End,
Node: cid,
}
return child, nil
} else {
panic("no posts or children on node, why")
}
}
// InsertPost inserts the given Smor in order into the merklelist
func (ml *MerkleList) InsertPost(p *Smor) error {
c, err := putPost(ml.bs, p)
if err != nil {
return err
}
if ml.root == nil {
// base case of an empty tree, just make a new node with the thing in it
ml.root = &MerkleListNode{
Posts: []*cid.Cid{c},
}
return nil
}
// pass it off to the recursive function (also pass our 'blockstore' so it can persist state)
extra, err := ml.root.insertPost(ml.bs, p.CreatedAt, c)
if err != nil {
return err
}
if extra != nil {
ml.splitNode(extra)
}
return nil
}
func (ml *MerkleList) splitNode(mln *MerkleListNode) error {
rootCl, err := ml.root.getChildLink(ml.bs)
if err != nil {
return err
}
mlnCl, err := mln.getChildLink(ml.bs)
if err != nil {
return err
}
children := []*childLink{
rootCl,
mlnCl,
}
ml.root = NewChildrenNode(children, ml.root.Depth + 1)
return nil
}
// Inserting a post into the leaf node it belongs in, base case of insertPost()
func (mln *MerkleListNode) insertIntoLeaf(bs blockstore.Blockstore, time uint64, c *cid.Cid) (*MerkleListNode, error) {
// iterate from the end to the front, we expect most 'inserts' to be 'append'
var i int
for i = len(mln.Posts) - 1; i >= 0; i-- {
sm, err := mln.getPostByIndex(bs, i)
if err != nil {
return nil, err
}
if time >= sm.CreatedAt {
// insert it here!
// snippet below from golang slice tricks
mln.Posts = append(mln.Posts[:i+1], append([]*cid.Cid{c}, mln.Posts[i+1:]...)...)
break
}
}
if i == -1 {
// if we make it here, our post occurs before every other post, insert it to the front
mln.Posts = append([]*cid.Cid{c}, mln.Posts...)
}
// now check if we need to split
if len(mln.Posts) > postsPerNode {
fmt.Println("Splitting node...")
/* split this node into two
Go from:
[ ............... ]
To:
[ X X ]
| |--------|
| |
[ .......] [ .........]
*/
extra := mln.Posts[postsPerNode:]
mln.Posts = mln.Posts[:postsPerNode]
if len(extra) > 1 {
panic("don't handle this case yet")
}
return NewPostsNode(extra), nil
}
return nil, nil
}
func (mln *MerkleListNode) insertPost(bs blockstore.Blockstore, time uint64, c *cid.Cid) (*MerkleListNode, error) {
// Base case, no child nodes, insert in this node
if mln.Depth == 0 {
return mln.insertIntoLeaf(bs, time, c)
}
// recursive case, find the child it belongs in
for i := len(mln.Children) - 1; i >= 0; i-- {
if time >= mln.Children[i].Beg || i == 0 {
var extra *MerkleListNode
err := mln.mutateChild(bs, i, func(cmlnn *MerkleListNode) error {
// inserting
ex, err := cmlnn.insertPost(bs, time, c)
if err != nil {
return err
}
extra = ex
return nil
})
if err != nil {
return nil, err
}
if extra != nil {
// if we're at end of the array, append
cl, err := extra.getChildLink(bs)
if err != nil {
return nil, err
}
if i == len(mln.Children) - 1 {
mln.Children = append(mln.Children, cl)
} else {
mln.Children = append(mln.Children[:i+1], append([]*childLink{cl}, mln.Children[i+1:]...)...)
}
if len(mln.Children) > postsPerNode {
// Splitting child node
extra := mln.Children[postsPerNode:]
mln.Children = mln.Children[:postsPerNode]
if len(extra) > 1 {
panic("don't handle this case yet")
}
return NewChildrenNode(extra, mln.Depth), nil
}
}
return nil, nil
}
}
panic("shouldnt ever get here...")
}
// mutateChild loads the given child from its hash, applys the given function
// to it, then rehashes it and updates the link in the children array
func (mln *MerkleListNode) mutateChild(bs blockstore.Blockstore, i int, mutateFunc func(*MerkleListNode) error) error {
ch := mln.Children[i]
blk, err := bs.Get(ch.Node)
if err != nil {
return err
}
var childNode MerkleListNode
if err := cbor.DecodeInto(blk.RawData(), &childNode); err != nil {
return err
}
if err := mutateFunc(&childNode); err != nil {
return err
}
chl, err := childNode.getChildLink(bs)
if err != nil {
return err
}
mln.Children[i] = chl
return nil
}