美文网首页
BoltDB(四)Bucket 结构以及操作

BoltDB(四)Bucket 结构以及操作

作者: wayyyy | 来源:发表于2022-08-16 01:51 被阅读0次

Bucket 相当于一张表,一个 Bucket 就是一棵 B+ 树。

// 16 byte
const bucketHeaderSize = int(unsafe.Sizeof(bucket{}))
const (
    minFillPercent = 0.1
    maxFillPercent = 1.0
)

// DefaultFillPercent is the percentage that split pages are filled.
// This value can be changed by setting Bucket.FillPercent.
const DefaultFillPercent = 0.5

// Bucket represents a collection of key/value pairs inside the database.
// 一组key/value的集合,也就是一个b+树
type Bucket struct {
    *bucket  // 在内联时bucket主要用来存储其桶的value并在后面拼接所有的元素,即所谓的内联
    tx       *Tx                // the associated transaction
    buckets  map[string]*Bucket // subbucket cache
    page     *page              // inline page reference,内联页引用
    rootNode *node              // materialized node for the root page.
    nodes    map[pgid]*node     // 缓存的page

    // Sets the threshold for filling nodes when they split. By default,
    // the bucket will fill to 50% but it can be useful to increase this
    // amount if you know that your write workloads are mostly append-only.
    //
    // This is non-persisted across transactions so it must be set in every Tx.
    FillPercent float64  // 填充率
}

// bucket represents the on-file representation of a bucket.
// This is stored as the "value" of a bucket key. If the bucket is small enough,
// then its root page can be stored inline in the "value", after the bucket
// header. In the case of inline buckets, the "root" will be 0.
type bucket struct {
    root     pgid   // 根节点的 page id
    sequence uint64 // monotonically incrementing, used by NextSequence()
}

// newBucket returns a new bucket associated with a transaction.
func newBucket(tx *Tx) Bucket {
    var b = Bucket{tx: tx, FillPercent: DefaultFillPercent}
    if tx.writable {
        b.buckets = make(map[string]*Bucket)
        b.nodes = make(map[pgid]*node)
    }
    return b
}

内存比磁盘小,一般会实现 page cache 缓存部分 page,比如使用 LRU 算法。boltdb 没有实现,而是使用 mmap() 创建共享、只读的文件映射并调用 madvise(MADV_RANDOM),由操作系统 管理 page cache;

相关文章

网友评论

      本文标题:BoltDB(四)Bucket 结构以及操作

      本文链接:https://www.haomeiwen.com/subject/blrkgrtx.html