Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

rotree source pure

Package rotree provides a read-only wrapper for bptree.BPTree with safe value transformation.

Overview

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:

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)

Functions 1

func Wrap

1func Wrap(tree *bptree.BPTree, makeEntrySafeFn func(any) any) *ReadOnlyTree
source

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.

Types 2

type IReadOnlyTree

interface
 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}
source

IReadOnlyTree defines the read-only operations available on a tree.

type ReadOnlyTree

struct
1type ReadOnlyTree struct {
2	tree            *bptree.BPTree
3	makeEntrySafeFn func(any) any
4}
source

ReadOnlyTree wraps a bptree.BPTree and provides read-only access.

Methods on ReadOnlyTree

func Get

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) Get(key string) any
source

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.

func GetByIndex

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) GetByIndex(index int) (string, any)
source

GetByIndex retrieves the key-value pair at the specified index in the tree, with the value converted to a safe format.

func Has

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) Has(key string) bool
source

Has checks whether a key exists in the tree.

func Iterate

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) Iterate(start, end string, cb bptree.IterCbFn) bool
source

Iterate performs an in-order traversal of the tree within the specified key range.

func IterateByOffset

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) IterateByOffset(offset int, count int, cb bptree.IterCbFn) bool
source

IterateByOffset performs an in-order traversal of the tree starting from the specified offset.

func Remove

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) Remove(key string) (value any, removed bool)
source

Remove is not supported on ReadOnlyTree and will panic.

func ReverseIterate

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) ReverseIterate(start, end string, cb bptree.IterCbFn) bool
source

ReverseIterate performs a reverse in-order traversal of the tree within the specified key range.

func ReverseIterateByOffset

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) ReverseIterateByOffset(offset int, count int, cb bptree.IterCbFn) bool
source

ReverseIterateByOffset performs a reverse in-order traversal of the tree starting from the specified offset.

func Set

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) Set(key string, value any) bool
source

Set is not supported on ReadOnlyTree and will panic.

func Size

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) Size() int
source

Size returns the number of key-value pairs in the tree.

Imports 1

Source Files 3