aboutsummaryrefslogtreecommitdiff
path: root/src/dedo.go
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2025-02-10 13:17:44 -0300
committerEuAndreh <eu@euandre.org>2025-02-10 13:17:44 -0300
commit0f7b28daf76f04779ac22fedb5c5312fc540a59a (patch)
tree432f3fddfe98f522725d27904113d22ffef644e1 /src/dedo.go
parentsrc/dedo.go: Remove separate RO concrete structs - snapshotT, roBucketT (diff)
downloaddedo-0f7b28daf76f04779ac22fedb5c5312fc540a59a.tar.gz
dedo-0f7b28daf76f04779ac22fedb5c5312fc540a59a.tar.xz
src/dedo.go: Simple rename Bucket -> bucketT
Diffstat (limited to 'src/dedo.go')
-rw-r--r--src/dedo.go162
1 files changed, 81 insertions, 81 deletions
diff --git a/src/dedo.go b/src/dedo.go
index 2bb805e..e474eaa 100644
--- a/src/dedo.go
+++ b/src/dedo.go
@@ -62,24 +62,24 @@ import (
type SnapshotI interface{
- Bucket([]byte) *Bucket
+ Bucket([]byte) *bucketT
Cursor() *Cursor
- ForEach(func([]byte, *Bucket) error) error
+ ForEach(func([]byte, *bucketT) error) error
WriteTo(io.Writer) (int64, error)
Check() <-chan error
}
type TransactionI interface{
- Bucket([]byte) *Bucket
+ Bucket([]byte) *bucketT
Cursor() *Cursor
- ForEach(func([]byte, *Bucket) error) error
+ ForEach(func([]byte, *bucketT) error) error
WriteTo(io.Writer) (int64, error)
Check() <-chan error
- CreateBucket ([]byte) (*Bucket, error)
- CreateBucketIfNotExists([]byte) (*Bucket, error)
+ CreateBucket ([]byte) (*bucketT, error)
+ CreateBucketIfNotExists([]byte) (*bucketT, error)
DeleteBucket([]byte) error
OnCommit(func())
}
@@ -104,7 +104,7 @@ type transactionT struct{
writable bool
db *DB
meta *meta
- root Bucket
+ root bucketT
pages map[pgid]*page
commitHandlers []func()
}
@@ -120,12 +120,12 @@ type bucket struct {
sequence uint64 /// monotonically incrementing, used by NextSequence()
}
-/// Bucket represents a distinct collection of key/value pairs inside the
+/// bucketT represents a distinct collection of key/value pairs inside the
/// database. Keys aren't unique across different buckets.
-type Bucket struct {
+type bucketT struct {
ref *bucket
tx *transactionT /// the associated transaction
- buckets map[string]*Bucket /// subbucket cache
+ buckets map[string]*bucketT /// subbucket cache
page *page /// inline page reference
rootNode *node /// materialized node for the root page
nodes map[pgid]*node /// node cache
@@ -143,7 +143,7 @@ type Bucket struct {
/// and return unexpected keys and/or values. You must reposition your cursor
/// after mutating data.
type Cursor struct {
- bucket *Bucket
+ bucket *bucketT
stack []elemRef
}
@@ -161,7 +161,7 @@ type inMemoryTx struct{
}
type InMemory struct{
- *pds.Map[[]byte, *Bucket]
+ *pds.Map[[]byte, *bucketT]
}
/// DB represents a collection of buckets persisted to a file on disk. All data
@@ -258,7 +258,7 @@ type freelist struct {
/// node represents an in-memory, deserialized page.
type node struct {
- bucket *Bucket
+ bucket *bucketT
isLeaf bool
unbalanced bool
spilled bool
@@ -573,29 +573,29 @@ func madvise(b []byte, advice int) (err error) {
}
/// newBucket() returns a new bucket associated with a transaction.
-func newBucket(tx *transactionT) Bucket {
- b := Bucket{tx: tx}
+func newBucket(tx *transactionT) bucketT {
+ b := bucketT{tx: tx}
if tx.writable {
- b.buckets = make(map[string]*Bucket)
+ b.buckets = make(map[string]*bucketT)
b.nodes = make(map[pgid]*node)
}
return b
}
-/// Bucket.Cursor() creates a cursor associated with the bucket. The cursor is
+/// bucketT.Cursor() creates a cursor associated with the bucket. The cursor is
/// only valid as long as the transaction is open. Do not use a cursor after
/// the transaction is closed.
-func (b *Bucket) Cursor() *Cursor {
+func (b *bucketT) Cursor() *Cursor {
return &Cursor{
bucket: b,
stack: make([]elemRef, 0),
}
}
-/// Bucket.Bucket() retrieves a nested bucket by name. Returns nil if the
+/// bucketT.Bucket() retrieves a nested bucket by name. Returns nil if the
/// bucket does not exist. The bucket instance is only valid for the lifetime
/// of the transaction.
-func (b *Bucket) Bucket(name []byte) *Bucket {
+func (b *bucketT) Bucket(name []byte) *bucketT {
if b.buckets != nil {
child := b.buckets[string(name)]
if child != nil {
@@ -621,9 +621,9 @@ func (b *Bucket) Bucket(name []byte) *Bucket {
return child
}
-/// Bucket.Helper() method that re-interprets a sub-bucket value from a parent
-/// into a Bucket
-func (b *Bucket) openBucket(value []byte) *Bucket {
+/// bucketT.Helper() method that re-interprets a sub-bucket value from a parent
+/// into a bucketT
+func (b *bucketT) openBucket(value []byte) *bucketT {
child := newBucket(b.tx)
// If this is a writable transaction then we need to copy the bucket
@@ -643,11 +643,11 @@ func (b *Bucket) openBucket(value []byte) *Bucket {
return &child
}
-/// Bucket.CreateBucket() creates a new bucket at the given key and returns the
+/// bucketT.CreateBucket() creates a new bucket at the given key and returns the
/// new bucket. Returns an error if the key already exists, if the bucket name
/// is blank, or if the bucket name is too long. The bucket instance is only
/// valid for the lifetime of the transaction.
-func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
+func (b *bucketT) CreateBucket(key []byte) (*bucketT, error) {
if b.tx.db == nil {
return nil, ErrTxClosed
} else if !b.tx.writable {
@@ -669,7 +669,7 @@ func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
}
// Create empty, inline bucket.
- bucket := Bucket{
+ bucket := bucketT{
ref: &bucket{},
rootNode: &node{isLeaf: true},
}
@@ -688,11 +688,11 @@ func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
return b.Bucket(key), nil
}
-/// Bucket.CreateBucketIfNotExists() creates a new bucket if it doesn't already
+/// bucketT.CreateBucketIfNotExists() creates a new bucket if it doesn't already
/// exist and returns a reference to it. Returns an error if the bucket name is
/// blank, or if the bucket name is too long. The bucket instance is only valid
/// for the lifetime of the transaction.
-func (b *Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error) {
+func (b *bucketT) CreateBucketIfNotExists(key []byte) (*bucketT, error) {
child, err := b.CreateBucket(key)
if err == ErrBucketExists {
return b.Bucket(key), nil
@@ -702,9 +702,9 @@ func (b *Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error) {
return child, nil
}
-/// Bucket.DeleteBucket() deletes a bucket at the given key. Returns an error
+/// bucketT.DeleteBucket() deletes a bucket at the given key. Returns an error
/// if the bucket does not exists, or if the key represents a non-bucket value.
-func (b *Bucket) DeleteBucket(key []byte) error {
+func (b *bucketT) DeleteBucket(key []byte) error {
if b.tx.db == nil {
return ErrTxClosed
} else if !b.tx.writable {
@@ -751,10 +751,10 @@ func (b *Bucket) DeleteBucket(key []byte) error {
return nil
}
-/// Bucket.Get() retrieves the value for a key in the bucket. Returns a nil
+/// bucketT.Get() retrieves the value for a key in the bucket. Returns a nil
/// value if the key does not exist or if the key is a nested bucket. The
/// returned value is only valid for the life of the transaction.
-func (b *Bucket) Get(key []byte) []byte {
+func (b *bucketT) Get(key []byte) []byte {
k, v, flags := b.Cursor().seek(key)
// Return nil if this is a bucket.
@@ -770,12 +770,12 @@ func (b *Bucket) Get(key []byte) []byte {
return v
}
-/// Bucket.Put() sets the value for a key in the bucket. If the key exist then
+/// bucketT.Put() sets the value for a key in the bucket. If the key exist then
/// its previous value will be overwritten. Supplied value must remain valid
/// for the life of the transaction. Returns an error if the bucket was created
/// from a read-only transaction, if the key is blank, if the key is too large,
/// or if the value is too large.
-func (b *Bucket) Put(key []byte, value []byte) error {
+func (b *bucketT) Put(key []byte, value []byte) error {
if b.tx.db == nil {
return ErrTxClosed
} else if !b.tx.writable {
@@ -804,10 +804,10 @@ func (b *Bucket) Put(key []byte, value []byte) error {
return nil
}
-/// Bucket.Delete() removes a key from the bucket. If the key does not exist
+/// bucketT.Delete() removes a key from the bucket. If the key does not exist
/// then nothing is done and a nil error is returned. Returns an error if the
/// bucket was created from a read-only transaction.
-func (b *Bucket) Delete(key []byte) error {
+func (b *bucketT) Delete(key []byte) error {
if b.tx.db == nil {
return ErrTxClosed
} else if !b.tx.writable {
@@ -835,7 +835,7 @@ func bytesLE(num uint64) []byte {
return arr
}
-func (b *Bucket) NextID() []byte {
+func (b *bucketT) NextID() []byte {
if b.tx.db == nil {
panic(ErrTxClosed)
} else if !b.tx.writable {
@@ -853,8 +853,8 @@ func (b *Bucket) NextID() []byte {
return bytesLE(b.ref.sequence)
}
-/// Bucket.NextSequence() returns an autoincrementing integer for the bucket.
-func (b *Bucket) NextSequence() (uint64, error) {
+/// bucketT.NextSequence() returns an autoincrementing integer for the bucket.
+func (b *bucketT) NextSequence() (uint64, error) {
if b.tx.db == nil {
return 0, ErrTxClosed
} else if !b.tx.writable {
@@ -872,11 +872,11 @@ func (b *Bucket) NextSequence() (uint64, error) {
return b.ref.sequence, nil
}
-/// Bucket.ForEach() executes a function for each key/value pair in a bucket.
+/// bucketT.ForEach() executes a function for each key/value pair in a bucket.
/// If the provided function returns an error then the iteration is stopped and
/// the error is returned to the caller. The provided function must not modify
/// the bucket; this will result in undefined behavior.
-func (b *Bucket) ForEach(fn func(k, v []byte) error) error {
+func (b *bucketT) ForEach(fn func(k, v []byte) error) error {
if b.tx.db == nil {
return ErrTxClosed
}
@@ -890,9 +890,9 @@ func (b *Bucket) ForEach(fn func(k, v []byte) error) error {
return nil
}
-/// Bucket.forEachPage() iterates over every page in a bucket, including inline
+/// bucketT.forEachPage() iterates over every page in a bucket, including inline
/// pages.
-func (b *Bucket) forEachPage(fn func(*page, int)) {
+func (b *bucketT) forEachPage(fn func(*page, int)) {
// If we have an inline page then just use that.
if b.page != nil {
fn(b.page, 0)
@@ -903,9 +903,9 @@ func (b *Bucket) forEachPage(fn func(*page, int)) {
b.tx.forEachPage(b.ref.root, 0, fn)
}
-/// Bucket.forEachPageNode() iterates over every page (or node) in a bucket.
+/// bucketT.forEachPageNode() iterates over every page (or node) in a bucket.
/// This also includes inline pages.
-func (b *Bucket) forEachPageNode(fn func(*page, *node, int)) {
+func (b *bucketT) forEachPageNode(fn func(*page, *node, int)) {
// If we have an inline page or root node then just use that.
if b.page != nil {
fn(b.page, nil, 0)
@@ -914,7 +914,7 @@ func (b *Bucket) forEachPageNode(fn func(*page, *node, int)) {
b._forEachPageNode(b.ref.root, 0, fn)
}
-func (b *Bucket) _forEachPageNode(
+func (b *bucketT) _forEachPageNode(
pgid pgid,
depth int,
fn func(*page, *node, int),
@@ -941,8 +941,8 @@ func (b *Bucket) _forEachPageNode(
}
}
-/// Bucket.spill() writes all the nodes for this bucket to dirty pages.
-func (b *Bucket) spill() error {
+/// bucketT.spill() writes all the nodes for this bucket to dirty pages.
+func (b *bucketT) spill() error {
// Spill all child buckets first.
for name, child := range b.buckets {
// If the child bucket is small enough and it has no child
@@ -1020,17 +1020,17 @@ func (b *Bucket) spill() error {
return nil
}
-/// Bucket.inlineable() returns true if a bucket is small enough to be written
+/// bucketT.inlineable() returns true if a bucket is small enough to be written
/// inline and if it contains no subbuckets. Otherwise returns false.
-func (b *Bucket) inlineable() bool {
+func (b *bucketT) inlineable() bool {
n := b.rootNode
- // Bucket must only contain a single leaf node.
+ // bucketT must only contain a single leaf node.
if n == nil || !n.isLeaf {
return false
}
- // Bucket is not inlineable if it contains subbuckets or if it goes
+ // bucketT is not inlineable if it contains subbuckets or if it goes
// beyond our threshold for inline bucket size.
size := pageHeaderSize
for _, inode := range n.inodes {
@@ -1046,14 +1046,14 @@ func (b *Bucket) inlineable() bool {
return true
}
-/// Bucket.maInlineBucketSize() returns the maximum total size of a bucket to
+/// bucketT.maInlineBucketSize() returns the maximum total size of a bucket to
/// make it a candidate for inlining.
-func (b *Bucket) maxInlineBucketSize() int {
+func (b *bucketT) maxInlineBucketSize() int {
return b.tx.db.pageSize / 4
}
-/// Bucket.write() allocates and writes a bucket to a byte slice.
-func (b *Bucket) write() []byte {
+/// bucketT.write() allocates and writes a bucket to a byte slice.
+func (b *bucketT) write() []byte {
// Allocate the appropriate size.
n := b.rootNode
value := make([]byte, bucketHeaderSize+n.size())
@@ -1069,8 +1069,8 @@ func (b *Bucket) write() []byte {
return value
}
-/// Bucket.rebalance() attempts to balance all nodes.
-func (b *Bucket) rebalance() {
+/// bucketT.rebalance() attempts to balance all nodes.
+func (b *bucketT) rebalance() {
for _, n := range b.nodes {
n.rebalance()
}
@@ -1079,9 +1079,9 @@ func (b *Bucket) rebalance() {
}
}
-/// Bucket.node() creates a node from a page and associates it with a given
+/// bucketT.node() creates a node from a page and associates it with a given
/// parent.
-func (b *Bucket) node(pgid pgid, parent *node) *node {
+func (b *bucketT) node(pgid pgid, parent *node) *node {
g.Assert(b.nodes != nil, "nodes map expected")
// Retrieve node if it's already been created.
@@ -1111,8 +1111,8 @@ func (b *Bucket) node(pgid pgid, parent *node) *node {
return n
}
-/// Bucket.free() recursively frees all pages in the bucket.
-func (b *Bucket) free() {
+/// bucketT.free() recursively frees all pages in the bucket.
+func (b *bucketT) free() {
if b.ref.root == 0 {
return
}
@@ -1128,8 +1128,8 @@ func (b *Bucket) free() {
b.ref.root = 0
}
-/// Bucket.dereference() removes all references to the old mmap.
-func (b *Bucket) dereference() {
+/// bucketT.dereference() removes all references to the old mmap.
+func (b *bucketT) dereference() {
if b.rootNode != nil {
b.rootNode.root().dereference()
}
@@ -1139,9 +1139,9 @@ func (b *Bucket) dereference() {
}
}
-/// Bucket.pageNode() returns the in-memory node, if it exists. Otherwise
+/// bucketT.pageNode() returns the in-memory node, if it exists. Otherwise
/// returns the underlying page.
-func (b *Bucket) pageNode(id pgid) (*page, *node) {
+func (b *bucketT) pageNode(id pgid) (*page, *node) {
// Inline buckets have a fake page embedded in their value so treat them
// differently. We'll return the rootNode (if available) or the fake
// page.
@@ -1684,17 +1684,17 @@ func initDB(db *DB, size int64) error {
}
/*
-func (tx *inMemoryTx) Bucket(name []byte) *Bucket {
+func (tx *inMemoryTx) Bucket(name []byte) *bucketT {
bucket, _ := tx.db.Get(name)
return bucket
}
-func (bucket *Bucket) value() []byte {
+func (bucket *bucketT) value() []byte {
return nil
}
// FIXME: split Tx/Snapshot
-func (tx *inMemoryTx) CreateBucket(name []byte) (*Bucket, error) {
+func (tx *inMemoryTx) CreateBucket(name []byte) (*bucketT, error) {
if tx.db == nil {
return nil, ErrTxClosed
}
@@ -1708,12 +1708,12 @@ func (tx *inMemoryTx) CreateBucket(name []byte) (*Bucket, error) {
return nil, ErrBucketExists
}
- bucket := &Bucket{}
+ bucket := &bucketT{}
tx.db.set(name, bucket)
return bucket, nil
}
-func (tx *inMemoryTx) CreateBucketIfNotExists(name []byte) (*Bucket, error) {
+func (tx *inMemoryTx) CreateBucketIfNotExists(name []byte) (*bucketT, error) {
bucket, err := tx.CreateBucket(name)
if err == ErrBucketExists {
return bucket.Bucket(name), nil
@@ -1732,7 +1732,7 @@ func (tx *inMemoryTx) DeleteBucket(name []byte) error {
return nil
}
-func (tx *inMemoryTx) ForEach(fn func([]byte, *Bucket) error) error {
+func (tx *inMemoryTx) ForEach(fn func([]byte, *bucketT) error) error {
return nil
}
@@ -1748,7 +1748,7 @@ func (tx *inMemoryTx) WriteTo(io.Writer) (int64, error) {
return 0, nil
}
-func (m *InMemory) set(key []byte, value *Bucket) {
+func (m *InMemory) set(key []byte, value *bucketT) {
m = &InMemory{m.Set(key, value)}
}
@@ -1781,7 +1781,7 @@ func (m *InMemory) View(func(SnapshotI) error) error {
}
func OpenMemory() DatabaseI {
- return &InMemory{pds.NewMap[[]byte, *Bucket](nil)}
+ return &InMemory{pds.NewMap[[]byte, *bucketT](nil)}
}
*/
@@ -3502,7 +3502,7 @@ func (tx *transactionT) Cursor() *Cursor {
/// transactionT.Bucket() retrieves a bucket by name. Returns nil if the bucket does not
/// exist. The bucket instance is only valid for the lifetime of the
/// transaction.
-func (tx *transactionT) Bucket(name []byte) *Bucket {
+func (tx *transactionT) Bucket(name []byte) *bucketT {
return tx.root.Bucket(name)
}
@@ -3510,7 +3510,7 @@ func (tx *transactionT) Bucket(name []byte) *Bucket {
/// already exists, if the bucket name is blank, or if the bucket name is too
/// long. The bucket instance is only valid for the lifetime of the
/// transaction.
-func (tx *transactionT) CreateBucket(name []byte) (*Bucket, error) {
+func (tx *transactionT) CreateBucket(name []byte) (*bucketT, error) {
return tx.root.CreateBucket(name)
}
@@ -3518,7 +3518,7 @@ func (tx *transactionT) CreateBucket(name []byte) (*Bucket, error) {
/// exist. Returns an error if the bucket name is blank, or if the bucket name
/// is too long. The bucket instance is only valid for the lifetime of the
/// transaction.
-func (tx *transactionT) CreateBucketIfNotExists(name []byte) (*Bucket, error) {
+func (tx *transactionT) CreateBucketIfNotExists(name []byte) (*bucketT, error) {
return tx.root.CreateBucketIfNotExists(name)
}
@@ -3531,7 +3531,7 @@ func (tx *transactionT) DeleteBucket(name []byte) error {
/// transactionT.ForEach() executes a function for each bucket in the root. If the
/// provided function returns an error then the iteration is stopped and the
/// error is returned to the caller.
-func (tx *transactionT) ForEach(fn func(name []byte, b *Bucket) error) error {
+func (tx *transactionT) ForEach(fn func(name []byte, b *bucketT) error) error {
return tx.root.ForEach(func(k, v []byte) error {
err := fn(k, tx.root.Bucket(k))
if err != nil {
@@ -3662,7 +3662,7 @@ func (tx *transactionT) close() {
// Clear all references.
tx.db = nil
tx.meta = nil
- tx.root = Bucket{tx: tx}
+ tx.root = bucketT{tx: tx}
tx.pages = nil
}
@@ -3774,7 +3774,7 @@ func isLeafPage(p *page) bool {
}
func (tx *transactionT) checkBucket(
- b *Bucket,
+ b *bucketT,
reachable map[pgid]*page,
freed map[pgid]bool,
ch chan error,
@@ -4047,7 +4047,7 @@ func listExec(args argsT, db DatabaseI, r io.Reader, w io.Writer) error {
if len(args.bucket) == 0 {
return snapshot.ForEach(func(
name []byte,
- bucket *Bucket,
+ bucket *bucketT,
) error {
fmt.Fprintf(w, "%s\n", string(name))
return nil