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()
}

index_levedb.go的更多相关文章

随机推荐

  1. 万水千山ABP - 单租户时,成功保存数据后,数据不显示

    问题描述: ABP 禁用了多租户,在编辑一个实体记录后,能成功地保存数据,但数据列表中看不到这条记录.打开数据表查看,发现该实体记录的 TenantId 字段值成了 Null , 而不是预期的默认租户 ...

  2. java日期操作常用工具

    java日期操作常用工具 package com..util; import java.sql.Timestamp; import java.text.SimpleDateFormat; import ...

  3. AngularJs 学习笔记(四)服务

    模型是指$scope上保存的包含瞬时状态数据的JavaScript对象. 服务是一个单例对象,只会被$injector实例化一次,并且是在需要的时候才会被创建,服务提供了把与特定功能相关联的方法集中在 ...

  4. win10安装wmi报错问题

    在win10上,安装wmi,首先下载https://pypi.python.org/pypi/WMI/#downloads,将wmi下载下来 安装过程中,会报错,No Python installat ...

  5. AI 学习之路

    前言:本文章纯属自己学习路线纪录,不喜勿喷. 最近AI很火,几乎是个程序员 都要去学习AI,作为一个菜鸡小前端,我也踏上了学习AI的方向. 在学习之中,最开始遇到了很多的困难,比如你不知道如何切入进来 ...

  6. gitlab钩子搭建

    目标:在本地开发机上push代码到GitLab仓库时,通过钩子同步到测试服务器 准备工作GitLab 服务器一台测试服务器一台本地开发服务器一台 1.在gitlab上新建一个项目,名称test2.在本 ...

  7. ExtJs Sencha Cmd创建项目以及编译项目

    一:创建项目 sencha sdk tool2.0无法创建api为sencha-touch-2.2.1的项目,需要使用SenchaCmd代替sencha sdk tool,其步骤如下: 1,下载安装s ...

  8. remove the nth node from the end of the list

    problem description: remove the nth node from the end of the list for example: given: 1->2->3 ...

  9. mysql-索引、关系、范式

    索引 几乎所有的索引都是建立在字段之上 索引:系统根据某种算法,将已有的数据(未来可能新增的数据也算),单独建立一个文件,这个文件能够快速的匹配数据,并且能够快速的找到对应的表中的记录 索引意义 能够 ...

  10. RabbitMQ In JAVA 介绍及使用

    介绍: RabbitMQ是开源的消息中间件,它是轻量级的,支持多种消息传递协议,可以部署在分布式和联合配置中,以满足高级别.高可用性需求.并且可在许多操作系统和云环境上运行,并为大多数流行语言提供了广 ...