boltdb数据库Update代码流程分析

4 篇文章 0 订阅

2. bolt数据库Update代码流程分析

在bolt的官方文档中,关于创建一个读写事务的描述如下:

Read-write transactions

To start a read-write transaction, you can use the DB.Update() function:

err := db.Update(func(tx *bolt.Tx) error {
	...
	return nil
})

Inside the closure, you have a consistent view of the database. You commit the
transaction by returning nil at the end. You can also rollback the transaction
at any point by returning an error. All database operations are allowed inside
a read-write transaction.

Always check the return error as it will report any disk failures that can cause
your transaction to not complete. If you return an error within your closure
it will be passed through.

在源码中func (db *DB) Update(fn func(*Tx) error) error { }将fn作为变量传入,并完成增删改查一系列操作。

Update首先调用db.Begin(true)来获取读写锁并返回一个读写类型的事务指针。Begin内部还会调用beginRWTx()以执行获取锁的操作并释放已关闭的事务关联的页面。

函数内部还defer了一个匿名函数以执行事务处理失败后的回滚操作。

	// Make sure the transaction rolls back in the event of a panic.
	defer func() {
		if t.db != nil {
			t.rollback()
		}
	}()

接下来执行了用户传入fn函数,并通过标记t.managed以防止用户进行提交操作。

    // Mark as a managed tx so that the inner function cannot manually commit.
	t.managed = true
	// If an error is returned from the function then rollback and return error.
	err = fn(t)
	t.managed = false

最后会调用tx.commit(),并返回提交结果给用户。

关于blotdb相关的数据结构和算法,推荐阅读区块的持久化之BoltDB(一)到(五的系列文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丧心病狂の程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值