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

    package volume

    import (
        "github.com/syndtr/goleveldb/leveldb"
        "encoding/binary"
        "path/filepath"
        "strconv"
    )
    //文件索引结构体
    type LevelDBIndex struct {
        path string
        db   *leveldb.DB
    }
    //创建leveldb索引
    func NewLevelDBIndex(dir string, vid uint64) (index *LevelDBIndex, err error) {
        path := filepath.Join(dir, strconv.FormatUint(vid, 10) + ".index")
        index = new(LevelDBIndex)
        index.path = path
        index.db, err = leveldb.OpenFile(path, nil)
        return index, err
    }
    //实现index接口
    //文件是否存在  物理存在
    func (l *LevelDBIndex)Has(fid uint64) bool {
        key := make([]byte, 8)
        binary.BigEndian.PutUint64(key, fid)
        _, err := l.db.Get(key, nil)
        return err == nil
    }
    //获取文件
    func (l *LevelDBIndex)Get(fid uint64) (*FileInfo, error) {
        key := make([]byte, 8)
        binary.BigEndian.PutUint64(key, fid)
        data, err := l.db.Get(key, nil)
        if err != nil {
            return nil, err
        }
        fi := new(FileInfo)
        return fi, fi.UnMarshalBinary(data)
    }
    //存储文件
    func (l *LevelDBIndex)Set(fi *FileInfo) error {
        data := fi.MarshalBinary()
        return l.db.Put(data[:8], data, nil)
    }
    //删除文件
    func (l *LevelDBIndex)Delete(fid uint64) error {
        key := make([]byte, 8)
        binary.BigEndian.PutUint64(key, fid)
        return l.db.Delete(key, nil)
    }
    //关闭资源
    func (l *LevelDBIndex)Close() error {
        return l.db.Close()
    }

  • 相关阅读:
    pipenv install
    git删除缓存区中文件
    zsh切换bash bash切换zsh
    Mac下安装allure
    Linux基础命令之tail动态显示日志文件时关键字有颜色、高亮显示
    CentOS添加tailf命令
    阿里云服务器添加安全组,允许url+端口访问
    pytest中用例的识别与运行
    [转] 使用webpack4提升180%编译速度
    [转] 默认分包策略
  • 原文地址:https://www.cnblogs.com/zhangboyu/p/7461617.html
Copyright © 2011-2022 走看看