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()
}
index_levedb.go的更多相关文章
随机推荐
- rails中select不能响应多选的解决办法
在rails4.2中如果你写如下代码,post的select无法传回多选内容,即使你select设置为多选: <select id='id_size' name='name_size' mult ...
- C# 如何将PDF转为多种图像文件格式(Png/Bmp/Emf/Tiff)
PDF是一种在我们日常工作学习中最常用到的文档格式之一,但常常也会因为文档的不易编辑的特点,在遇到需要编辑PDF文档内容或者转换文件格式的情况时让人苦恼.通常对于开发者而言,可选择通过使用组件的方式来 ...
- mybatis中#{}与${}的区别
今天学习了下mybatis的查询,了解到了#{}与${}的区别, 配置文件如下: <?xml version="1.0" encoding="UTF-8" ...
- Mongodb3.6 基操命令(二)——如何使用help
前言 在上一篇文章Mongodb3.6 快速入门(一)中,我们主要使用两个命令: 1.mongod #启动服务 2.mongo #连接mongodb 对于刚接触mongo的人来说,该怎么给命令传递参数 ...
- spiral matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- Read程序员的困境有感
看完这篇文章,我真的害怕了,码农遍地都是,我就是,改变从今天做起,不走码农生活 首先, 打造你自己的私人项目.你需要不断地打磨自己的技艺.如果工作本身并不能帮助你做到这一点,就捡起那些你感兴趣的问题, ...
- 数据库scheme设计(9.4 小结)
通过这一章的内容,希望能够让大家明白一个道理,“数据库系统的性能不是优化出来的,更多的是设计出来的”.数据库Schema 的设计并不如很多人想象的那样只是一个简单的对象对应实现,而是一个系统工程.要想 ...
- Java编程语言下Selenium 对于下拉框,单选,多选等选择器的操作
WebElement selector = driver.findElement(By.id("Selector")); Select select = new Select(se ...
- 二分查找算法的C++和PHP实现
C++实现方式: #include<iostream> #include<stdlib.h>#include<algorithm> using namespace ...
- .Net中stirng转Systen.Type的一种实现思路
今天在上班的过程中,许长时间未联系的大学小伙伴发来消息,带着一个疑问来找我. 他的需求是type动态添加,这对我来说当然很easy,用泛型就好了, 随后,手起刀落,Demo就写出来,如下: 写了一个方 ...