~whereswaldon/forest-go

forest-go/grove/grove.go -rw-r--r-- 15.3 KiB
e3b60d5aChris Waldon feat(grove): support removing subtrees 19 days ago
                                                                                
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
Package grove implements an on-disk storage format for arbor forest
nodes. This hierarchical storage format is called a "grove", and
the management type implemented by this package satisfies the
forest.Store interface.

Note: this package is not yet complete.
*/
package grove

import (
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"sort"
	"strings"

	"git.sr.ht/~whereswaldon/forest-go"
	"git.sr.ht/~whereswaldon/forest-go/fields"
	"git.sr.ht/~whereswaldon/forest-go/store"
)

// File represents a type that supports file-like operations. *os.File
// implements this interface, and will likely be used most of the time.
// This interface exists mostly to simply testing.
type File interface {
	io.ReadWriteCloser
	Name() string
	Readdir(n int) ([]os.FileInfo, error)
}

// FS represents a type that acts as a filesystem. It can create and
// open files at specific paths
type FS interface {
	Open(path string) (File, error)
	Create(path string) (File, error)
	OpenFile(path string, flag int, perm os.FileMode) (File, error)
	Remove(path string) error
}

// RelativeFS is a file system that acts relative to a specific path
type RelativeFS struct {
	Root string
}

// ensure RelativeFS satisfies the FS interface
var _ FS = RelativeFS{}

func (r RelativeFS) resolve(path string) string {
	return filepath.Join(r.Root, path)
}

// Open opens the given path as an absolute path relative to the root
// of the RelativeFS
func (r RelativeFS) Open(path string) (File, error) {
	return os.Open(r.resolve(path))
}

// Create makes the given path as an absolute path relative to the root
// of the RelativeFS
func (r RelativeFS) Create(path string) (File, error) {
	return os.Create(r.resolve(path))
}

// OpenFile opens the given path as an absolute path relative to the root
// of the RelativeFS
func (r RelativeFS) OpenFile(path string, flag int, perm os.FileMode) (File, error) {
	return os.OpenFile(r.resolve(path), flag, perm)
}

// Remove removes the given path relative to the root
// of the RelativeFS.
func (r RelativeFS) Remove(path string) error {
	return os.Remove(r.resolve(path))
}

// Grove is an on-disk store for arbor forest nodes. It maintains internal
// in-memory caches in order to accelerate certain expensive operations.
// Because of this, it must be notified when new content appears on disk.
// The recommended way to handle this is to use file-system watching on
// the grove directory and to call Add() with any new nodes that appear
// (it is not an error to call Add() on a node already present in a store).
// Another (potentially more expensive) way to ensure consistency in the
// event of a disk modification is to call RebuildChildCache().
type Grove struct {
	FS
	NodeCache *store.MemoryStore
	*ChildCache
}

// New constructs a Grove that stores nodes in a hierarchy rooted at
// the given path.
func New(root string) (*Grove, error) {
	return NewWithFS(RelativeFS{root})
}

// NewWithFS constructs a Grove using the given FS implementation to
// access its nodes. This is primarily useful for testing.
func NewWithFS(fs FS) (*Grove, error) {
	if fs == nil {
		return nil, fmt.Errorf("fs cannot be nil")
	}
	return &Grove{
		FS:         fs,
		NodeCache:  store.NewMemoryStore(),
		ChildCache: NewChildCache(),
	}, nil
}

// Get searches the grove for a node with the given id. It returns the node if it was
// found, a boolean indicating whether it was found, and an error (if there was a
// problem searching for the node).
// The returned `present` will never be true unless the returned `node` holds an
// actual node struct. If the file holding a node exists on disk but was unable
// to be opened, read, or parsed, `present` will still be false.
func (g *Grove) Get(nodeID *fields.QualifiedHash) (node forest.Node, present bool, err error) {
	node, inCache, _ := g.NodeCache.Get(nodeID)
	if inCache {
		return node, true, nil
	}
	filename := nodeID.String()
	file, err := g.Open(filename)
	// if the file doesn't exist, just return false with no error
	if errors.Is(err, os.ErrNotExist) {
		return nil, false, nil
	}
	// if it's some other error, wrap it and return
	if err != nil {
		return nil, false, fmt.Errorf("failed opening node file \"%s\": %w", filename, err)
	}
	b, err := ioutil.ReadAll(file)
	if err != nil {
		return nil, false, fmt.Errorf("failed reading bytes from \"%s\": %w", filename, err)
	}
	node, err = forest.UnmarshalBinaryNode(b)
	if err != nil {
		return nil, false, fmt.Errorf("failed unmarshalling node from \"%s\": %w", filename, err)
	}
	_ = g.NodeCache.Add(node)
	return node, true, nil
}

// getAllNodeFileInfo returns a slice of information about all node files
// within the grove.
func (g *Grove) getAllNodeFileInfo() ([]os.FileInfo, error) {
	// open root of grove hierarchy so we can list its nodes
	rootDir, err := g.Open("")
	if err != nil {
		return nil, fmt.Errorf("failed opening grove root dir: %w", err)
	}
	defer rootDir.Close()
	info, err := rootDir.Readdir(-1) // read whole directory at once. Inefficient
	if err != nil {
		return nil, fmt.Errorf("failed listing files in grove: %w", err)
	}
	nodeInfo := make([]os.FileInfo, 0, len(info))
	// find all files that are plausibly nodes
	for _, fileInfo := range info {
		// search for the string form of all supported hash types
		for _, hashName := range fields.HashNames {
			if strings.HasPrefix(fileInfo.Name(), hashName) {
				nodeInfo = append(nodeInfo, fileInfo)
			}
		}
	}
	return nodeInfo, nil
}

// nodeFromInfo converts the info about a file into a node extracted from
// the contents of that file (it opens, reads, and parses the file).
func (g *Grove) nodeFromInfo(info os.FileInfo) (forest.Node, error) {
	nodeIDString := info.Name()
	nodeID := &fields.QualifiedHash{}
	if err := nodeID.UnmarshalText([]byte(nodeIDString)); err != nil {
		return nil, fmt.Errorf("unable to parse %s as a node id: %w", nodeIDString, err)
	}
	if node, present, _ := g.NodeCache.Get(nodeID); present {
		return node, nil
	}
	nodeFile, err := g.Open(info.Name())
	if err != nil {
		return nil, fmt.Errorf("failed opening node file %s: %w", info.Name(), err)
	}
	defer nodeFile.Close()
	nodeData, err := ioutil.ReadAll(nodeFile)
	if err != nil {
		return nil, fmt.Errorf("failed reading node file %s: %w", info.Name(), err)
	}
	node, err := forest.UnmarshalBinaryNode(nodeData)
	if err != nil {
		return nil, fmt.Errorf("failed parsing node file %s: %w", info.Name(), err)
	}
	_ = g.NodeCache.Add(node)
	return node, nil
}

// nodesFromInfo batch-converts a slice of file info into a slice of
// forest nodes by calling nodeFromInfo on each.
func (g *Grove) nodesFromInfo(info []os.FileInfo) ([]forest.Node, error) {
	nodes := make([]forest.Node, 0, len(info))
	for _, nodeFileInfo := range info {
		node, err := g.nodeFromInfo(nodeFileInfo)
		if err != nil {
			return nil, fmt.Errorf("failed transforming fileInfo into Node: %w", err)
		}
		nodes = append(nodes, node)
	}
	return nodes, nil
}

// allNodes returns a slice of every node in the grove.
func (g *Grove) allNodes() ([]forest.Node, error) {
	nodeInfo, err := g.getAllNodeFileInfo()
	if err != nil {
		return nil, fmt.Errorf("failed listing node file candidates: %w", err)
	}
	nodes, err := g.nodesFromInfo(nodeInfo)
	if err != nil {
		return nil, fmt.Errorf("failed converting node files into nodes: %w", err)
	}
	return nodes, nil
}

// Children returns the IDs of all known child nodes of the specified ID.
// Any error opening, reading, or parsing files in the grove that occurs
// during the search for child nodes will cause the entire operation to
// error.
func (g *Grove) Children(id *fields.QualifiedHash) ([]*fields.QualifiedHash, error) {
	children, inCache := g.ChildCache.Get(id)
	if inCache {
		return children, nil
	}
	if err := g.RebuildChildCache(); err != nil {
		return nil, fmt.Errorf("failed rebuilding child cache: %w", err)
	}
	children, inCache = g.ChildCache.Get(id)
	if !inCache {
		return []*fields.QualifiedHash{}, nil
	}

	return children, nil
}

// Recent returns a slice of the most recently-created nodes of the given type.
// The slice is sorted so that the most-recently-created nodes are at the beginning.
func (g *Grove) Recent(nodeType fields.NodeType, quantity int) ([]forest.Node, error) {
	nodes, err := g.allNodes()
	if err != nil {
		return nil, fmt.Errorf("failed getting all nodes from grove: %w", err)
	}
	// TODO: find a cleaner way to sort nodes by time
	sort.Slice(nodes, func(i, j int) bool {
		var a, b forest.CommonNode
		switch n := nodes[i].(type) {
		case *forest.Identity:
			a = n.CommonNode
		case *forest.Community:
			a = n.CommonNode
		case *forest.Reply:
			a = n.CommonNode
		}
		switch n := nodes[j].(type) {
		case *forest.Identity:
			b = n.CommonNode
		case *forest.Community:
			b = n.CommonNode
		case *forest.Reply:
			b = n.CommonNode
		}
		return a.Created > b.Created
	})
	rightType := make([]forest.Node, 0, quantity)
	for _, node := range nodes {
		switch node.(type) {
		case *forest.Identity:
			if nodeType == fields.NodeTypeIdentity {
				rightType = append(rightType, node)
			}
		case *forest.Community:
			if nodeType == fields.NodeTypeCommunity {
				rightType = append(rightType, node)
			}
		case *forest.Reply:
			if nodeType == fields.NodeTypeReply {
				rightType = append(rightType, node)
			}
		}
	}
	if len(rightType) > quantity {
		rightType = rightType[:quantity]
	}
	return rightType, nil
}

// RebuildChildCache must be called each time a node is inserted into the
// underlying storage without actually calling Add() on the grove. Without
// this, calls to Children() will not always include new results.
func (g *Grove) RebuildChildCache() error {
	nodes, err := g.allNodes()
	if err != nil {
		return fmt.Errorf("failed getting all nodes from grove: %w", err)
	}
	for _, node := range nodes {
		g.CacheChildInfo(node)
	}
	return nil
}

// CacheChildInfo updates the child cache information for the given node.
func (g *Grove) CacheChildInfo(node forest.Node) {
	// ensure we cache this node's relationship to its parent
	g.ChildCache.Add(node.ParentID(), node.ID())
	// ensure we cache this node's existence (if it turns out that we
	// never see a child for this node, the cache will still generate a
	// hit for 0 children)
	g.ChildCache.Add(node.ID())
}

// Add inserts the node into the grove. If the given node is already in the
// grove, Add will do nothing. It is not an error to insert a node more than
// once.
func (g *Grove) Add(node forest.Node) error {
	g.CacheChildInfo(node)
	if _, alreadyPresent, err := g.Get(node.ID()); err != nil {
		return fmt.Errorf("failed checking whether node already in grove: %w", err)
	} else if alreadyPresent {
		return nil
	}
	data, err := node.MarshalBinary()
	if err != nil {
		return fmt.Errorf("failed to serialize node: %w", err)
	}

	id := node.ID().String()
	nodeFile, err := g.Create(id)
	if err != nil {
		return fmt.Errorf("failed to create file for node %s: %w", id, err)
	}
	defer nodeFile.Close()

	_, err = nodeFile.Write(data)
	if err != nil {
		return fmt.Errorf("failed to write data to file for node %s: %w", id, err)
	}
	return nil
}

// GetIdentity returns an Identity node with the given ID (if it is present
// in the grove). This operation may be faster than using Get, as the grove
// may be able to do less search work when it knows the type of node you're
// looking for in advance.
//
// BUG(whereswaldon): The current implementation may return nodes of the
// wrong NodeType if they match the provided ID
func (g *Grove) GetIdentity(id *fields.QualifiedHash) (forest.Node, bool, error) {
	// this naiive implementation is not efficient, but works as a short-term
	// thing.
	//
	// TODO: change the on-disk representation so that operations like this can
	// be fast (store different node types in different directories, etc...)
	return g.Get(id)
}

// GetCommunity returns an Community node with the given ID (if it is present
// in the grove). This operation may be faster than using Get, as the grove
// may be able to do less search work when it knows the type of node you're
// looking for in advance.
//
// BUG(whereswaldon): The current implementation may return nodes of the
// wrong NodeType if they match the provided ID
func (g *Grove) GetCommunity(id *fields.QualifiedHash) (forest.Node, bool, error) {
	// this naiive implementation is not efficient, but works as a short-term
	// thing.
	//
	// TODO: change the on-disk representation so that operations like this can
	// be fast (store different node types in different directories, etc...)
	return g.Get(id)
}

// GetConversation returns an Conversation node with the given ID (if it is present
// in the grove). This operation may be faster than using Get, as the grove
// may be able to do less search work when it knows the type of node you're
// looking for and its parent node in advance.
//
// BUG(whereswaldon): The current implementation may return nodes of the
// wrong NodeType if they match the provided ID
func (g *Grove) GetConversation(communityID, conversationID *fields.QualifiedHash) (forest.Node, bool, error) {
	// this naiive implementation is not efficient, but works as a short-term
	// thing.
	//
	// TODO: change the on-disk representation so that operations like this can
	// be fast (store different node types in different directories, etc...)
	return g.Get(conversationID)
}

// GetReply returns an Reply node with the given ID (if it is present
// in the grove). This operation may be faster than using Get, as the grove
// may be able to do less search work when it knows the type of node you're
// looking for and its parent community and conversation node in advance.
//
// BUG(whereswaldon): The current implementation may return nodes of the
// wrong NodeType if they match the provided ID
func (g *Grove) GetReply(communityID, conversationID, replyID *fields.QualifiedHash) (forest.Node, bool, error) {
	// this naiive implementation is not efficient, but works as a short-term
	// thing.
	//
	// TODO: change the on-disk representation so that operations like this can
	// be fast (store different node types in different directories, etc...)
	return g.Get(replyID)
}

// CopyInto copies all nodes from the store into the provided store.
//
// BUG(whereswaldon): this method is not yet implemented. It requires
// more extensive file manipulation than other Grove methods (listing
// directory contents) and has therefore been deprioritized in favor
// of the functionality that can be implemented simply. However, it is
// implementable, and should be done as soon as is feasible.
func (g *Grove) CopyInto(other forest.Store) error {
	return fmt.Errorf("method CopyInto() is not currently implemented on Grove")
}

// RemoveSubtree removes the subtree rooted at the node
// with the provided ID from the grove.
func (g *Grove) RemoveSubtree(id *fields.QualifiedHash) error {
	children, err := g.Children(id)
	if err != nil {
		return fmt.Errorf("failed looking up children of %s: %w", id, err)
	}
	for _, child := range children {
		if err := g.RemoveSubtree(child); err != nil {
			return fmt.Errorf("failed removing children of %s: %w", child, err)
		}
	}
	child, _, err := g.Get(id)
	if err != nil {
		return fmt.Errorf("failed looking up child %s during removal: %w", id, err)
	}
	g.ChildCache.RemoveChild(child.ParentID(), id)
	g.ChildCache.RemoveParent(id)
	if err := g.NodeCache.RemoveSubtree(id); err != nil {
		return fmt.Errorf("failed removing node %s from internal cache: %w", id, err)
	}
	if err := g.Remove(id.String()); err != nil {
		return fmt.Errorf("failed removing node %s from filesystem: %w", id, err)
	}
	return nil
}