a correct implementation of filer
This commit is contained in:
@@ -1,28 +1,17 @@
|
||||
package weed_server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/syndtr/goleveldb/leveldb"
|
||||
"github.com/syndtr/goleveldb/leveldb/util"
|
||||
"code.google.com/p/weed-fs/go/filer"
|
||||
"code.google.com/p/weed-fs/go/glog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
/*
|
||||
1. level db is only for local instance
|
||||
2. db stores two types of pairs
|
||||
<path/to/dir, sub folder names>
|
||||
<path/to/file, file id>
|
||||
So, to list a directory, just get the directory entry, and iterate the current directory files
|
||||
Care must be taken to maintain the <dir, sub dirs> and <file, fileid> pairs.
|
||||
3.
|
||||
*/
|
||||
type FilerServer struct {
|
||||
port string
|
||||
master string
|
||||
collection string
|
||||
db *leveldb.DB
|
||||
filer filer.Filer
|
||||
}
|
||||
|
||||
func NewFilerServer(r *http.ServeMux, port int, master string, dir string, collection string) (fs *FilerServer, err error) {
|
||||
@@ -32,7 +21,8 @@ func NewFilerServer(r *http.ServeMux, port int, master string, dir string, colle
|
||||
port: ":" + strconv.Itoa(port),
|
||||
}
|
||||
|
||||
if fs.db, err = leveldb.OpenFile(dir, nil); err != nil {
|
||||
if fs.filer, err = filer.NewFilerEmbedded(dir); err != nil {
|
||||
glog.Fatal("Can not start filer in dir:", dir)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -40,127 +30,3 @@ func NewFilerServer(r *http.ServeMux, port int, master string, dir string, colle
|
||||
|
||||
return fs, nil
|
||||
}
|
||||
|
||||
func (fs *FilerServer) CreateFile(fullFileName string, fid string) (err error) {
|
||||
fs.ensureFileFolder(fullFileName)
|
||||
return fs.db.Put([]byte(fullFileName), []byte(fid), nil)
|
||||
}
|
||||
|
||||
func (fs *FilerServer) FindFile(fullFileName string) (fid string, err error) {
|
||||
return fs.findEntry(fullFileName)
|
||||
}
|
||||
|
||||
func (fs *FilerServer) ListDirectories(fullpath string) (dirs []string, err error) {
|
||||
data, e := fs.db.Get([]byte(fullpath), nil)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
val := string(data)
|
||||
if val == "" {
|
||||
return nil, nil
|
||||
}
|
||||
return strings.Split(val, ":"), nil
|
||||
}
|
||||
|
||||
func (fs *FilerServer) ListFiles(fullpath string, start, limit int) (files []string) {
|
||||
if !strings.HasSuffix(fullpath, "/") {
|
||||
fullpath += "/"
|
||||
}
|
||||
iter := fs.db.NewIterator(&util.Range{Start: []byte(fullpath)}, nil)
|
||||
startCounter, limitCounter := -1, 0
|
||||
for iter.Next() {
|
||||
startCounter++
|
||||
if startCounter < start {
|
||||
continue
|
||||
}
|
||||
limitCounter++
|
||||
if limit > 0 {
|
||||
if limitCounter > limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
key := string(iter.Key())
|
||||
if !strings.HasPrefix(key, fullpath) {
|
||||
break
|
||||
}
|
||||
fileName := key[len(fullpath):]
|
||||
if fileName == "" {
|
||||
continue //skip the directory entry
|
||||
}
|
||||
if strings.Contains(fileName, "/") {
|
||||
break
|
||||
}
|
||||
files = append(files, fileName)
|
||||
}
|
||||
iter.Release()
|
||||
return
|
||||
}
|
||||
|
||||
func (fs *FilerServer) Delete(fullpath string, isForceDirectoryRemoval bool) (fid string, isFile bool, err error) {
|
||||
val, e := fs.findEntry(fullpath)
|
||||
if e != nil {
|
||||
return "", false, e
|
||||
}
|
||||
if strings.Contains(val, ",") {
|
||||
return val, true, fs.db.Delete([]byte(fullpath), nil)
|
||||
}
|
||||
// deal with directory
|
||||
if !strings.HasSuffix(fullpath, "/") {
|
||||
fullpath += "/"
|
||||
}
|
||||
iter := fs.db.NewIterator(&util.Range{Start: []byte(fullpath)}, nil)
|
||||
counter := 0
|
||||
for iter.Next() {
|
||||
counter++
|
||||
if counter > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
iter.Release()
|
||||
if counter > 0 {
|
||||
return "", false, errors.New("Force Deletion Not Supported Yet")
|
||||
}
|
||||
return "", false, fs.db.Delete([]byte(fullpath), nil)
|
||||
}
|
||||
|
||||
func (fs *FilerServer) findEntry(fullpath string) (value string, err error) {
|
||||
data, e := fs.db.Get([]byte(fullpath), nil)
|
||||
if e != nil {
|
||||
return "", e
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
func (fs *FilerServer) ensureFileFolder(fullFileName string) (err error) {
|
||||
parts := strings.Split(fullFileName, "/")
|
||||
path := "/"
|
||||
for i := 1; i < len(parts); i++ {
|
||||
sub := parts[i]
|
||||
if i == len(parts)-1 {
|
||||
sub = ""
|
||||
}
|
||||
if err = fs.ensureFolderHasEntry(path, sub); err != nil {
|
||||
return
|
||||
}
|
||||
if sub != "" {
|
||||
path = path + sub + "/"
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (fs *FilerServer) ensureFolderHasEntry(path string, sub string) (err error) {
|
||||
val, e := fs.findEntry(path)
|
||||
if e == leveldb.ErrNotFound {
|
||||
return fs.db.Put([]byte(path), []byte(sub), nil)
|
||||
} else if e != nil {
|
||||
return e
|
||||
}
|
||||
list := strings.Split(val, ":")
|
||||
for _, v := range list {
|
||||
if v == sub {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
list = append(list, sub)
|
||||
return fs.db.Put([]byte(path), []byte(strings.Join(list, ":")), nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user