Bolt是受LMDB、hyc_symas激发的纯key/value存储。Since Bolt is meant to be used as such a low-level piece of functionality, simplicity is key. The API will be small and only focus on getting values and setting values. That’s it.
[hyc_symas]: https://twitter.com/hyc_symas
[lmdb]: http://symas.com/mdb/

Getting Started

Installing

安装Boltdb数据库$ go get github.com/boltdb/bolt/...
This will retrieve the library and install the bolt command line utility into your $GOBIN path.

Opening a database

Bolt中的顶级对象为DB,它代表磁盘上的一个单独的文件,代表数据的consistent snapshot。To open your database, simply use the bolt.Open() function:

package main
import ("log""github.com/boltdb/bolt"
)
func main() {// Open the my.db data file in your current directory.// It will be created if it doesn't exist.db, err := bolt.Open("my.db", 0600, nil)if err != nil {log.Fatal(err)}defer db.Close()...
}

Open使用给定的路径创建并打开一个数据库。Options配置为nil,Bolt会使用默认选项打开数据库。db.go 150 func Open(path string, mode os.FileMode, options *Options) (*DB, error)

  • 如果!options.ReadOnly(read-write模式),数据库文件是独占锁定的(仅仅一个process能够grab lock)
  • 如果options.ReadOnly,数据库文件是共享锁定的(多个进程可以同时获得该锁)
db, err := bolt.Open("my.db", 0600, &bolt.Options{Timeout: 1 * time.Second})

Please note that Bolt obtains a file lock on the data file so multiple processes cannot open the same database at the same time. Opening an already open Bolt database will cause it to hang until the other process closes it. To prevent an indefinite wait you can pass a timeout option to the Open() function:

func Open(path string, mode os.FileMode, options *Options) (*DB, error) {var db = &DB{opened: true}   // db是指向DB结构体的指针// Set default options if no options are provided.if options == nil { options = DefaultOptions }db.NoGrowSync = options.NoGrowSyncdb.MmapFlags = options.MmapFlags// Set default values for later DB operations.db.MaxBatchSize = DefaultMaxBatchSizedb.MaxBatchDelay = DefaultMaxBatchDelaydb.AllocSize = DefaultAllocSizeflag := os.O_RDWR      // 如果没指定options.ReadOnly,则文件打开方式为os.O_RDWRif options.ReadOnly {  // 如果指定options.ReadOnly,则文件打开方式为os.O_RDONLYflag = os.O_RDONLYdb.readOnly = true}// Open data file and separate sync handler for metadata writes.db.path = pathvar err errorif db.file, err = os.OpenFile(db.path, flag|os.O_CREATE, mode); err != nil {_ = db.close()return nil, err}// 锁定文件,这样使用Bolt read-write模式下的其他进程不能同时使用数据库。// Lock file so that other processes using Bolt in read-write mode cannot// use the database  at the same time. This would cause corruption since// the two processes would write meta pages and free pages separately.// The database file is locked exclusively (only one process can grab the lock)// if !options.ReadOnly.// 如果!options.ReadOnly,数据库文件是独占锁定的(仅仅一个process能够grab lock)// 如果options.ReadOnly,数据库文件是共享锁定的(多个进程可以同时获得该锁)// The database file is locked using the shared lock (more than one process may// hold a lock at the same time) otherwise (options.ReadOnly is set).if err := flock(db, mode, !db.readOnly, options.Timeout); err != nil {_ = db.close()return nil, err}// Default values for test hooksdb.ops.writeAt = db.file.WriteAt// Initialize the database if it doesn't exist.if info, err := db.file.Stat(); err != nil {return nil, err} else if info.Size() == 0 {// Initialize new files with meta pages.  使用meta page初始化新文件if err := db.init(); err != nil {return nil, err}} else { // Read the first meta page to determine the page size.  读取meta page取出页大小var buf [0x1000]byteif _, err := db.file.ReadAt(buf[:], 0); err == nil {m := db.pageInBuffer(buf[:], 0).meta()if err := m.validate(); err != nil {// If we can't read the page size, we can assume it's the same// as the OS -- since that's how the page size was chosen in the// first place.// If the first page is invalid and this OS uses a different// page size than what the database was created with then we// are out of luck and cannot access the database.db.pageSize = os.Getpagesize()} else {db.pageSize = int(m.pageSize)}}}// Initialize page pool.db.pagePool = sync.Pool{New: func() interface{} {return make([]byte, db.pageSize)},}// Memory map the data file. 映射数据文件if err := db.mmap(options.InitialMmapSize); err != nil {_ = db.close()return nil, err}// Read in the freelist. 读入Freelistdb.freelist = newFreelist()db.freelist.read(db.page(db.meta().freelist))  return db, nil  // Mark the database as opened and return.
}

Boltdb源码分析——bolt.Open相关推荐

  1. BoltDB 源码分析

    BoltDB 源码分析 node CopyOnWrite解决读写冲突 Element bucket cursor 内存分配 inline bucket Cursor rebalance meta ba ...

  2. JStorm与Storm源码分析(七)--BasicBoltExecutor与装饰模式

    在Storm中IBasicBolt的主要作用是为用户提供一种更为简单的Bolt编写方式,更为简单体现在Storm框架本身帮你处理了所发出消息的Ack.Fail和Anchor操作,而这部分操作是由执行器 ...

  3. Apache Storm 实时流处理系统ACK机制以及源码分析

    1.ACK机制简介 Storm的可靠性是指Storm会告知用户每一个消息单元是否在一个指定的时间(timeout)内被完全处理.完全处理的意思是该MessageId绑定的源Tuple以及由该源Tupl ...

  4. Apache Storm 实时流处理系统通信机制源码分析

    我们今天就来仔细研究一下Apache Storm 2.0.0-SNAPSHOT的通信机制.下面我将从大致思想以及源码分析,然后我们细致分析实时流处理系统中源码通信机制研究. 1. 简介 Worker间 ...

  5. storm-kafka源码分析

    storm-kafka源码分析 @(KAFKA)[kafka, 大数据, storm] storm-kafka源码分析 一概述 一代码结构 二orgapachestormkafka 三orgapach ...

  6. 【源码分析】storm拓扑运行全流程源码分析

    [源码分析]storm拓扑运行全流程源码分析 @(STORM)[storm] 源码分析storm拓扑运行全流程源码分析 一拓扑提交流程 一stormpy 1storm jar 2def jar 3ex ...

  7. Storm源码分析之四: Trident源码分析

    Storm源码分析之四: Trident源码分析 @(STORM)[storm] Storm源码分析之四 Trident源码分析 一概述 0小结 1简介 2关键类 1Spout的创建 2spout的消 ...

  8. SOFA 源码分析 —— 服务发布过程

    前言 SOFA 包含了 RPC 框架,底层通信框架是 bolt ,基于 Netty 4,今天将通过 SOFA-RPC 源码中的例子,看看他是如何发布一个服务的. 示例代码 下面的代码在 com.ali ...

  9. Golang|区块链UTXO集源码分析

    区块链UTXO集源码分析 资源 go实现区块链 前提 在未实现UTXO集之前,假设系统需要查询某个钱包地址的余额,系统需要遍历区块链的所有区块,当区块链非常长时,这种做法的成本太高了. UTXO集是未 ...

最新文章

  1. QoS、IPv6、软交换和VoIP技术受质疑
  2. 通过python建立一个web服务查看服务器上的文本、图片、视频等文件
  3. 使用快嘉框架开发项目示例
  4. 通过Java反编译揭开一些问题的真相
  5. Docker最全教程之Python爬网实战(二十二)
  6. 表格中序号怎计算机课程视频,【答疑】在Excel表格里输入了文字后怎么下拉顺序号啊?如何在表格里添加序号? - 视频教程线上学...
  7. element 方法返回的boolean被当成字符串了_quot;==quot;和 equals 方法有什么区别
  8. 计算机维修和维护实训报告,计算机维护与维修实训报告书.docx
  9. Aqua Data Studio 18.5.0导出insert语句
  10. ARP欺骗+DNS欺骗
  11. 什么是人工神经网络模型,人工神经网络模型定义
  12. 电磁阀、电磁铁的工作原理说明
  13. scratch趣味编程——挖矿小游戏
  14. mac桌面文件不见了怎么办?
  15. 深度学习英语-迭代法(01)故事法,一个新的尝试
  16. 手游服务器常用架构图
  17. HDR视频生态系统纵览
  18. 大数据比较 同比与环比的区别
  19. 2021湖南省计算机类考研
  20. node启动之后内存占用过高解决方案

热门文章

  1. 计算机网络组成原理——基本概念
  2. 移动操作系统的优劣及下一代移动系统展望
  3. 电影《乌云背后的幸福线》观后感
  4. Verilog中parameter使用
  5. 小白重装系统教程_大神教你小白一键重装系统
  6. 制作电影影评网的html5代码,微信小程序之电影影评小程序制作代码
  7. Pygame从0实战10(泡泡小游戏添加音效)
  8. 【100个 Unity实用技能】| C# 中List 使用Exists方法判断是否存在符合条件的元素对象
  9. Word插入希腊字母及特殊符号 分类整
  10. Android 内存泄露分析