func Wrap
Wrap creates a new ReadOnlyTree from an existing bptree.BPTree and a safety transformation function. If makeEntrySafeFn is nil, values will be returned as-is without transformation.
Package rotree provides a read-only wrapper for bptree.BPTree with safe value transformation.
Package rotree provides a read-only wrapper for bptree.BPTree with safe value transformation.
It is useful when you want to expose a read-only view of a tree while ensuring that the sensitive data cannot be modified.
Example:
1// Define a user structure with sensitive data
2type User struct {
3 Name string
4 Balance int
5 Internal string // sensitive field
6}
7
8// Create and populate the original tree
9privateTree := bptree.NewBPTree32()
10privateTree.Set("alice", &User{
11 Name: "Alice",
12 Balance: 100,
13 Internal: "sensitive",
14})
15
16// Create a safe transformation function that copies the struct
17// while excluding sensitive data
18makeEntrySafeFn := func(v any) any {
19 u := v.(*User)
20 return &User{
21 Name: u.Name,
22 Balance: u.Balance,
23 Internal: "", // omit sensitive data
24 }
25}
26
27// Create a read-only view of the tree
28PublicTree := rotree.Wrap(tree, makeEntrySafeFn)
29
30// Safely access the data
31value := roTree.Get("alice")
32user := value.(*User)
33// user.Name == "Alice"
34// user.Balance == 100
35// user.Internal == "" (sensitive data is filtered)
1type IReadOnlyTree interface {
2 Size() int
3 Has(key string) bool
4 Get(key string) any
5 GetByIndex(index int) (string, any)
6 Iterate(start, end string, cb bptree.IterCbFn) bool
7 ReverseIterate(start, end string, cb bptree.IterCbFn) bool
8 IterateByOffset(offset int, count int, cb bptree.IterCbFn) bool
9 ReverseIterateByOffset(offset int, count int, cb bptree.IterCbFn) bool
10}IReadOnlyTree defines the read-only operations available on a tree.
ReadOnlyTree wraps a bptree.BPTree and provides read-only access.
Get retrieves the value associated with the given key, converted to a safe format. It returns the value if the key exists, or nil if it doesn't.
GetByIndex retrieves the key-value pair at the specified index in the tree, with the value converted to a safe format.
Has checks whether a key exists in the tree.
Iterate performs an in-order traversal of the tree within the specified key range.
IterateByOffset performs an in-order traversal of the tree starting from the specified offset.
Remove is not supported on ReadOnlyTree and will panic.
ReverseIterate performs a reverse in-order traversal of the tree within the specified key range.
1func (roTree *ReadOnlyTree) ReverseIterateByOffset(offset int, count int, cb bptree.IterCbFn) boolReverseIterateByOffset performs a reverse in-order traversal of the tree starting from the specified offset.
Set is not supported on ReadOnlyTree and will panic.
Size returns the number of key-value pairs in the tree.