zoukankan      html  css  js  c++  java
  • bolt_storage.go

    package storage
    //bolt存储引擎实现  
    import (
        "github.com/boltdb/bolt"
        "time"
    )

    var wukong_documents = []byte("wukong_documents")
    //bolt结构体 实现  同时实现storage接口
    type boltStorage struct {
        db *bolt.DB
    }
    //实现存储引擎map集合  实现value  对应的函数  
    func openBoltStorage(path string) (Storage, error) {
        db, err := bolt.Open(path, 0600, &bolt.Options{Timeout: 3600 * time.Second})
        if err != nil {
            return nil, err
        }
        err = db.Update(func(tx *bolt.Tx) error {
            _, err := tx.CreateBucketIfNotExists(wukong_documents)
            return err
        })
        if err != nil {
            db.Close()
            return nil, err
        }
        return &boltStorage{db}, nil
    }

    func (s *boltStorage) WALName() string {
        return s.db.Path()
    }

    func (s *boltStorage) Set(k []byte, v []byte) error {
        return s.db.Update(func(tx *bolt.Tx) error {
            return tx.Bucket(wukong_documents).Put(k, v)
        })
    }

    func (s *boltStorage) Get(k []byte) (b []byte, err error) {
        err = s.db.View(func(tx *bolt.Tx) error {
            b = tx.Bucket(wukong_documents).Get(k)
            return nil
        })
        return
    }

    func (s *boltStorage) Delete(k []byte) error {
        return s.db.Update(func(tx *bolt.Tx) error {
            return tx.Bucket(wukong_documents).Delete(k)
        })
    }

    func (s *boltStorage) ForEach(fn func(k, v []byte) error) error {
        return s.db.View(func(tx *bolt.Tx) error {
            b := tx.Bucket(wukong_documents)
            c := b.Cursor()
            for k, v := c.First(); k != nil; k, v = c.Next() {
                if err := fn(k, v); err != nil {
                    return err
                }
            }
            return nil
        })
    }

    func (s *boltStorage) Close() error {
        return s.db.Close()
    }

  • 相关阅读:
    一些坑点
    [Luogu P4168] [Violet]蒲公英 (分块)
    冬令营颓废笔记
    WC2019 填坑记
    [Luogu P1829] [国家集训队]Crash的数字表格 / JZPTAB (莫比乌斯反演)
    [Luogu P2522] [HAOI2011]Problem b (莫比乌斯反演)
    [Luogu P3327] [SDOI2015]约数个数和 (莫比乌斯反演)
    [Luogu P3455] [POI2007]ZAP-Queries (莫比乌斯反演 )
    [Luogu P2257] YY的GCD (莫比乌斯函数)
    杭电 1166 敌兵布阵 (线段树)
  • 原文地址:https://www.cnblogs.com/zhangboyu/p/7461659.html
Copyright © 2011-2022 走看看