boltdb

网上关于boltdb的文章有很多,特别是微信公众号上,例如:

boltdb源码分析系列-事务-腾讯云开发者社区-腾讯云 (tencent.com)

这些文章都写的挺好,但不一定覆盖了我所关注的几个点,下面我把我关注的几个点就来下来。

node page bucket tx db的关系

  • 磁盘数据mmap到page内存区域,也可以理解为就是磁盘数据

    • page需要一段连续的内存
  • node封装的B+树节点数据结构
  • bucket一个B+树数据结构。可以理解成一个表
  • tx 读事务或读写事务
    • bucket是内存结构每个tx中都会生成一个
    • 会将tx中涉及到(读取过、修改过)的nodes都记录在bucket中
    • 读写事务最终写入磁盘时是需要重新申请新的page的,即不会修改原有的page
  • db整个数据库文件
    • db中的freelist记录了db文件中空闲的页(即已经可以释放掉的页)

tx.commit

在boltdb的 commit中才会执行b+树的rebalance操作,执行完后再进行写入磁盘的操作。也就是说在一个事务中涉及到的多次写操作,会最终在commit的时候同意执行写入磁盘spill操作。

func (tx *Tx) Commit() error {
_assert(!tx.managed, "managed tx commit not allowed")
if tx.db == nil {
return ErrTxClosed
} else if !tx.writable {
return ErrTxNotWritable
} // TODO(benbjohnson): Use vectorized I/O to write out dirty pages. // Rebalance nodes which have had deletions.
var startTime = time.Now()
tx.root.rebalance()
if tx.stats.Rebalance > 0 {
tx.stats.RebalanceTime += time.Since(startTime)
} // spill data onto dirty pages.
startTime = time.Now()
if err := tx.root.spill(); err != nil {
tx.rollback()
return err
}

也正因为txn中可能有多个key插入,所以split就可能会进行多次

func (n *node) split(pageSize int) []*node {
var nodes []*node node := n
for {
// Split node into two.
a, b := node.splitTwo(pageSize)
nodes = append(nodes, a) // If we can't split then exit the loop.
if b == nil {
break
} // Set node to b so it gets split on the next iteration.
node = b
} return nodes
} node.go

数据写入到磁盘的时候,是从下层节点往上层节点写的

// spill writes the nodes to dirty pages and splits nodes as it goes.
// Returns an error if dirty pages cannot be allocated.
func (n *node) spill() error {
var tx = n.bucket.tx
if n.spilled {
return nil
} // Spill child nodes first. Child nodes can materialize sibling nodes in
// the case of split-merge so we cannot use a range loop. We have to check
// the children size on every loop iteration.
sort.Sort(n.children)
for i := 0; i < len(n.children); i++ {
if err := n.children[i].spill(); err != nil {
return err
}
} // We no longer need the child list because it's only used for spill tracking.
n.children = nil // Split nodes into appropriate sizes. The first node will always be n.
var nodes = n.split(tx.db.pageSize) node.go

数据较大如何处理?直接将构造一个大的page将数据存储进去。与此同时,原先node关联的page可以释放掉了。因为整个是一个append only模式,原先的page在新事务生成,且没有其他读事务访问后就可以释放掉了。

    for _, node := range nodes {
// Add node's page to the freelist if it's not new.
if node.pgid > 0 {
tx.db.freelist.free(tx.meta.txid, tx.page(node.pgid))
node.pgid = 0
} // Allocate contiguous space for the node.
p, err := tx.allocate((node.size() / tx.db.pageSize) + 1)
if err != nil {
return err
} node.go

哪些node需要rebalance呢,size < 25% page_size或者中间节点小于2个key,叶子节点小于1个key。

func (n *node) rebalance() {
if !n.unbalanced {
return
}
n.unbalanced = false // Update statistics.
n.bucket.tx.stats.Rebalance++ // Ignore if node is above threshold (25%) and has enough keys.
var threshold = n.bucket.tx.db.pageSize / 4
if n.size() > threshold && len(n.inodes) > n.minKeys() {
return
} node.go

bucket中读到了node,就将node加入到bucket中,读到了就意味着这些node可能就会发生改变。它是在cursor移动的时候加入到bucket中的。

func (c *Cursor) node() *node {
_assert(len(c.stack) > 0, "accessing a node with a zero-length cursor stack") // If the top of the stack is a leaf node then just return it.
if ref := &c.stack[len(c.stack)-1]; ref.node != nil && ref.isLeaf() {
return ref.node
} // Start from root and traverse down the hierarchy.
var n = c.stack[0].node
if n == nil {
n = c.bucket.node(c.stack[0].page.id, nil)
}
for _, ref := range c.stack[:len(c.stack)-1] {
_assert(!n.isLeaf, "expected branch node")
n = n.childAt(int(ref.index))
}
_assert(n.isLeaf, "expected leaf node")
return n
}
// node creates a node from a page and associates it with a given parent.
func (b *Bucket) node(pgid pgid, parent *node) *node {
_assert(b.nodes != nil, "nodes map expected") // Retrieve node if it's already been created.
if n := b.nodes[pgid]; n != nil {
return n
} // Otherwise create a node and cache it.
n := &node{bucket: b, parent: parent}
if parent == nil {
b.rootNode = n
} else {
parent.children = append(parent.children, n)
} // Use the inline page if this is an inline bucket.
var p = b.page
if p == nil {
p = b.tx.page(pgid)
} // Read the page into the node and cache it.
n.read(p)
b.nodes[pgid] = n // Update statistics.
b.tx.stats.NodeCount++

freelist

它表示的是磁盘中已经释放的页

结构

  • ids 所有空闲页
  • pending {txid, pageids[]}即将释放的txid以及其关联的pageid
  • cache map索引

->pending 释放实际

  • tx.commit时会将事务中涉及到的老的node对应的page都放到pending中

    • node.spill中将关联的旧node(node与page对应)放到freelist的pending中

pending->release释放时机

tx的commit阶段会将事务涉及的原先老page放到freelist的pending中。

func (f *freelist) free(txid txid, p *page) {
if p.id <= 1 {
panic(fmt.Sprintf("cannot free page 0 or 1: %d", p.id))
} // Free page and all its overflow pages.
var ids = f.pending[txid]
for id := p.id; id <= p.id+pgid(p.overflow); id++ {
// Verify that page is not already free.
if f.cache[id] {
panic(fmt.Sprintf("page %d already freed", id))
} // Add to the freelist and cache.
ids = append(ids, id)
f.cache[id] = true
}
f.pending[txid] = ids
}

db.beginRWTx 开启读写事务的时候会尝试将过期的page释放掉

func (f *freelist) release(txid txid) {
m := make(pgids, 0)
for tid, ids := range f.pending {
if tid <= txid {
// Move transaction's pending pages to the available freelist.
// Don't remove from the cache since the page is still free.
m = append(m, ids...)
delete(f.pending, tid)
}
}
sort.Sort(m)
f.ids = pgids(f.ids).merge(m)
}

boltdb一瞥的更多相关文章

  1. 惊鸿一瞥(Glimpse)——开发之时即可掌控ASP.NET应用的性能

    今天要推荐的东西不是一篇文章,而是我实际使用的武器之一--用于ASP.NET应用性能诊断的大杀器.我的武器库中的武器之前已经介绍过Hangfire了,接下来我会不断和大家分享我使用的一些函数库和工具. ...

  2. influxdb和boltDB简介——底层本质类似LMDB,MVCC+B+树

    influxdb influxdb是最新的一个时间序列数据库,最新一两年才产生,但已经拥有极高的人气.influxdb 是用Go写的,0.9版本的influxdb对于之前会有很大的改变,后端存储有Le ...

  3. 关于时间序列数据库的思考——(1)运用hash文件(例如:RRD,Whisper) (2)运用LSM树来备份(例如:LevelDB,RocksDB,Cassandra) (3)运用B-树排序和k/v存储(例如:BoltDB,LMDB)

    转自:http://0351slc.com/portal.php?mod=view&aid=12 近期网络上呈现了有关catena.benchmarking boltdb等时刻序列存储办法的介 ...

  4. 2017值得一瞥的JavaScript相关技术趋势

    2017值得一瞥的JavaScript相关技术趋势从属于笔者的Web 前端入门与工程实践,推荐阅读2016-我的前端之路:工具化与工程化获得更多关于2016年前端总结.本文主要内容翻译自,笔者对于每个 ...

  5. BoltDB简单使用教程

    1.BoltDB简介 Bolt是一个纯粹Key/Value模型的程序.该项目的目标是为不需要完整数据库服务器(如Postgres或MySQL)的项目提供一个简单,快速,可靠的数据库. BoltDB只需 ...

  6. influxdb和boltDB简介——MVCC+B+树,Go写成,Bolt类似于LMDB,这个被认为是在现代kye/value存储中最好的,influxdb后端存储有LevelDB换成了BoltDB

    influxdb influxdb是最新的一个时间序列数据库,最新一两年才产生,但已经拥有极高的人气.influxdb 是用Go写的,0.9版本的influxdb对于之前会有很大的改变,后端存储有Le ...

  7. [转帖]influxdb和boltDB简介——MVCC+B+树,Go写成,Bolt类似于LMDB,这个被认为是在现代kye/value存储中最好的,influxdb后端存储有LevelDB换成了BoltDB

    influxdb和boltDB简介——MVCC+B+树,Go写成,Bolt类似于LMDB,这个被认为是在现代kye/value存储中最好的,influxdb后端存储有LevelDB换成了BoltDB ...

  8. Boltdb学习笔记之〇--概述

    更多精彩内容,请关注微信公众号:后端技术小屋 看了boltdb也有一阵子了,看完之后总想写点什么,因为感觉到这可能是个不小的坑,所以迟迟没有动笔(没错我的拖延症又犯了..).最近有一种流行的说法:如果 ...

  9. boltdb的实现和改进

    整个代码不是很复杂,可以从代码中理解如何实现. 特点:btree,很小巧,但实现了完整事务机制,稳定,即使丢电也不会导致数据库错误. 整个结构如下: meta page (前两页) --- > ...

  10. UCenter 基本原理一瞥

    UCenter 是国内最常用的会员整合系统,它定义了一套接口用于不同应用(系统)间的协作. 注册过程 通过某个应用注册时,应用会先调用 uc_client/client.php 中的 uc_user_ ...

随机推荐

  1. UE4 C++调用C# DLL(DllExport方法)

    之前看见园子里一篇C++调用C#的文章,参考后拿UE试一下.刚开始尝试了C# dll > CLR C++ DLL > Native C++ DLL的做法, 原生环境下是可以的,但是到UE4 ...

  2. UE4 绘制Gizmo

    Unity的Gizmos可以很方便的在编辑器下进行调试,Unreal中也有一些办法可以达到效果. 本文主要参考:https://zhuanlan.zhihu.com/p/363625037,进行了一些 ...

  3. C#语言:散修笔记

    文章目录 前言 数组的几种定义方法 out 和 ref 的区别 可变参数params 静态方法与非静态方法 >❀什么时候使用静态和非静态 构造函数 >❀类中方法的重载 >❀在类中输出 ...

  4. NumPy 数组迭代与合并详解

    NumPy 数组迭代 NumPy 数组迭代是访问和处理数组元素的重要方法.它允许您逐个或成组地遍历数组元素. 基本迭代 我们可以使用 Python 的基本 for 循环来迭代 NumPy 数组. 一维 ...

  5. Asp-Net-Core开发笔记:给SwaggerUI加上登录保护功能

    前言 在 SwaggerUI 中加入登录验证,是我很早前就做过的,不过之前的做法总感觉有点硬编码,最近 .Net8 增加了一个新特性:调用 MapSwagger().RequireAuthorizat ...

  6. golang 接口动态派发性能问题

    测试代码 package main type Duck interface { Quack() } type Cat struct { Name string } //go:noinline func ...

  7. Android 13 - Media框架(12)- MediaCodec(二)

    关注公众号免费阅读全文,进入音视频开发技术分享群! 前面一节我们学习了 MediaCodec 的创建以及配置过程,了解部分设计机制以及功能,这一节我们将继续学习其他方法. 1.start start ...

  8. centos7.x开机启动流程centos6.x开机启动流程

    centos6.x开机启动流程 开机加电bios自检 MBR引导将启动权交给硬盘 硬盘 0 柱面0磁道 1扇区512字节,其中 前466字节为引导 后 64字节分区表 2字节为分区结束标志 加载gru ...

  9. 查看CentOS版本的方法

    1.以下命令对于Linux发行版是通用的: root@MyMail ~ # uname Linux root@MyMail ~ # uname -r 2.6.18-164.el5 [root@loca ...

  10. RTOS官方文档学习

    任务与协程 区别 一个程序可以只有任务.只有协程.二者都有,但不可以通过队列/信号量互相传递数据 任务特点 任务之间可以互相独立 每个任务分配自己的堆栈,提高了RAM使用率 操作简单.按优先级抢占式执 ...