2015-01-06 06:58:30 +08:00
|
|
|
package embedded_filer
|
2014-04-10 00:44:58 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2016-06-03 09:09:14 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2014-04-10 00:44:58 +08:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/util"
|
|
|
|
)
|
|
|
|
|
2014-07-21 14:12:49 +08:00
|
|
|
/*
|
|
|
|
The entry in level db has this format:
|
|
|
|
key: genKey(dirId, fileName)
|
|
|
|
value: []byte(fid)
|
|
|
|
And genKey(dirId, fileName) use first 4 bytes to store dirId, and rest for fileName
|
|
|
|
*/
|
|
|
|
|
2014-04-10 00:44:58 +08:00
|
|
|
type FileListInLevelDb struct {
|
|
|
|
db *leveldb.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFileListInLevelDb(dir string) (fl *FileListInLevelDb, err error) {
|
|
|
|
fl = &FileListInLevelDb{}
|
|
|
|
if fl.db, err = leveldb.OpenFile(dir, nil); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-06 06:58:30 +08:00
|
|
|
func genKey(dirId filer.DirectoryId, fileName string) []byte {
|
2014-04-10 00:44:58 +08:00
|
|
|
ret := make([]byte, 0, 4+len(fileName))
|
|
|
|
for i := 3; i >= 0; i-- {
|
|
|
|
ret = append(ret, byte(dirId>>(uint(i)*8)))
|
|
|
|
}
|
|
|
|
ret = append(ret, []byte(fileName)...)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2015-01-06 06:58:30 +08:00
|
|
|
func (fl *FileListInLevelDb) CreateFile(dirId filer.DirectoryId, fileName string, fid string) (err error) {
|
2014-04-10 00:44:58 +08:00
|
|
|
glog.V(4).Infoln("directory", dirId, "fileName", fileName, "fid", fid)
|
|
|
|
return fl.db.Put(genKey(dirId, fileName), []byte(fid), nil)
|
|
|
|
}
|
2015-01-06 06:58:30 +08:00
|
|
|
func (fl *FileListInLevelDb) DeleteFile(dirId filer.DirectoryId, fileName string) (fid string, err error) {
|
2014-04-10 00:44:58 +08:00
|
|
|
if fid, err = fl.FindFile(dirId, fileName); err != nil {
|
2015-08-22 05:09:33 +08:00
|
|
|
if err == leveldb.ErrNotFound {
|
|
|
|
return "", nil
|
|
|
|
}
|
2014-04-10 00:44:58 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = fl.db.Delete(genKey(dirId, fileName), nil)
|
|
|
|
return fid, err
|
|
|
|
}
|
2015-01-06 06:58:30 +08:00
|
|
|
func (fl *FileListInLevelDb) FindFile(dirId filer.DirectoryId, fileName string) (fid string, err error) {
|
2014-04-10 00:44:58 +08:00
|
|
|
data, e := fl.db.Get(genKey(dirId, fileName), nil)
|
2016-09-08 11:35:54 +08:00
|
|
|
if e == leveldb.ErrNotFound {
|
|
|
|
return "", filer.ErrNotFound
|
|
|
|
} else if e != nil {
|
2014-04-10 00:44:58 +08:00
|
|
|
return "", e
|
|
|
|
}
|
|
|
|
return string(data), nil
|
|
|
|
}
|
2015-01-06 06:58:30 +08:00
|
|
|
func (fl *FileListInLevelDb) ListFiles(dirId filer.DirectoryId, lastFileName string, limit int) (files []filer.FileEntry) {
|
2014-04-10 00:44:58 +08:00
|
|
|
glog.V(4).Infoln("directory", dirId, "lastFileName", lastFileName, "limit", limit)
|
|
|
|
dirKey := genKey(dirId, "")
|
|
|
|
iter := fl.db.NewIterator(&util.Range{Start: genKey(dirId, lastFileName)}, nil)
|
2014-04-10 12:01:48 +08:00
|
|
|
limitCounter := 0
|
2014-04-10 00:44:58 +08:00
|
|
|
for iter.Next() {
|
2014-04-10 12:01:48 +08:00
|
|
|
key := iter.Key()
|
|
|
|
if !bytes.HasPrefix(key, dirKey) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fileName := string(key[len(dirKey):])
|
|
|
|
if fileName == lastFileName {
|
|
|
|
continue
|
|
|
|
}
|
2014-04-10 00:44:58 +08:00
|
|
|
limitCounter++
|
|
|
|
if limit > 0 {
|
|
|
|
if limitCounter > limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-01-06 06:58:30 +08:00
|
|
|
files = append(files, filer.FileEntry{Name: fileName, Id: filer.FileId(string(iter.Value()))})
|
2014-04-10 00:44:58 +08:00
|
|
|
}
|
|
|
|
iter.Release()
|
|
|
|
return
|
|
|
|
}
|