dongpu42006096 2015-06-08 10:49
浏览 86
已采纳

Boltdb-key-Value数据存储完全在Go中

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.

As this is the case,is there any connection pooling concept like various clients connecting and accessing the database at the same time.? Is this possible in boltdb?Like there are various connections reading and writing in the database at the same time.How it can be implemented?

  • 写回答

2条回答 默认 最新

  • dongyan6235 2015-06-08 15:16
    关注

    A Bolt database is usually embedded into a larger program and is not used over the network like you would with shared databases (think SQLite vs MySQL). Using Bolt is a bit like having a persistent map[[]byte][]byte if that were possible. Depending on what you are doing, you might want to just use something like Redis.

    That said, if you need to use Bolt this way, it is not very difficult to wrap with a simple server. Here is an example that writes/reads keys from a Bolt DB over HTTP. You can use Keep-Alive for connection pooling.

    Code at: https://github.com/skyec/boltdb-server

    package main
    
    import (
        "flag"
        "fmt"
        "io/ioutil"
        "log"
        "net/http"
        "time"
    
        "github.com/boltdb/bolt"
        "github.com/gorilla/mux"
    )
    
    type server struct {
        db *bolt.DB
    }
    
    func newServer(filename string) (s *server, err error) {
        s = &server{}
        s.db, err = bolt.Open(filename, 0600, &bolt.Options{Timeout: 1 * time.Second})
        return
    }
    
    func (s *server) Put(bucket, key, contentType string, val []byte) error {
        return s.db.Update(func(tx *bolt.Tx) error {
            b, err := tx.CreateBucketIfNotExists([]byte(bucket))
            if err != nil {
                return err
            }
            if err = b.Put([]byte(key), val); err != nil {
                return err
            }
            return b.Put([]byte(fmt.Sprintf("%s-ContentType", key)), []byte(contentType))
        })
    }
    
    func (s *server) Get(bucket, key string) (ct string, data []byte, err error) {
        s.db.View(func(tx *bolt.Tx) error {
            b := tx.Bucket([]byte(bucket))
            r := b.Get([]byte(key))
            if r != nil {
                data = make([]byte, len(r))
                copy(data, r)
            }
    
            r = b.Get([]byte(fmt.Sprintf("%s-ContentType", key)))
            ct = string(r)
            return nil
        })
        return
    }
    
    func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
        vars := mux.Vars(r)
    
        if vars["bucket"] == "" || vars["key"] == "" {
            http.Error(w, "Missing bucket or key", http.StatusBadRequest)
            return
        }
    
        switch r.Method {
        case "POST", "PUT":
            data, err := ioutil.ReadAll(r.Body)
            if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
            }
            err = s.Put(vars["bucket"], vars["key"], r.Header.Get("Content-Type"), data)
            w.WriteHeader(http.StatusOK)
        case "GET":
            ct, data, err := s.Get(vars["bucket"], vars["key"])
            if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
            }
            w.Header().Add("Content-Type", ct)
            w.Write(data)
        }
    }
    
    func main() {
        var (
            addr   string
            dbfile string
        )
    
        flag.StringVar(&addr, "l", ":9988", "Address to listen on")
        flag.StringVar(&dbfile, "db", "/var/data/bolt.db", "Bolt DB file")
        flag.Parse()
    
        log.Println("Using Bolt DB file:", dbfile)
        log.Println("Listening on:", addr)
    
        server, err := newServer(dbfile)
        if err != nil {
            log.Fatalf("Error: %s", err)
        }
    
        router := mux.NewRouter()
        router.Handle("/v1/buckets/{bucket}/keys/{key}", server)
        http.Handle("/", router)
    
        log.Fatal(http.ListenAndServe(addr, nil))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度