How BoltDB Write its Data?

2022-03-10 00:00:00 专区 订阅 付费 交易 评论

A-Ha!

Here’re three questions during reading the source code of BoltDB. I’ll explain our testing procedure to dive into the core of the BoltDB writing mechanism.

Code link: yhyddr/quicksilver

First

At first, my mentor posts a question: “Did BoltDB have a temporary file when starting a read/write transaction”.

Following the quickstart, all data is stored in a file on disk. Generally speaking, the file has a linerial structure, when inserting, updating and deleting, we have to re-arrange the space occupied by this file. A tempory file should be used in this procedure, for example, .swp file for VIM.

Let’s check whether BoltDB uses this method.

FileSystem Notify

We can use fsnotify to watch all the changes in a given directory. Here’s the code:

package main

import (
    "log"

    "github.com/fsnotify/fsnotify"
)

func main() {
    hang := make(chan bool)
    watcher, err := fsnotify.NewWatcher()
    if err != nil {
        log.Fatal(err)
    }

    watcher.Add("./")

    go func() {
        for {
            select {
            case e := <-watcher.Events:
                log.Println(e.Op.String(), e.Name)
            case err := <-watcher.Errors:
                log.Println(err)
            }
        }
    }()

    <-hang
}

相关文章