aboutsummaryrefslogtreecommitdiff
path: root/weed/filer2/filer.go
blob: 0592c78488cc08fc491971de8d7d45dbff58d333 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package filer2

import (
	"fmt"

	"github.com/chrislusf/seaweedfs/weed/filer2/embedded"
	"github.com/karlseguin/ccache"
	"strings"
)

type Filer struct {
	master         string
	store          FilerStore
	directoryCache *ccache.Cache
}

func NewFiler(master string) *Filer {
	return &Filer{
		master:         master,
		directoryCache: ccache.New(ccache.Configure().MaxSize(1000).ItemsToPrune(100)),
	}
}

func NewEmbeddedFiler(master string, dir string) (*Filer, error) {
	_, err := embedded.NewEmbeddedStore(dir)
	if err != nil {
		return nil, fmt.Errorf("failed to create embedded filer store: %v", err)
	}
	return &Filer{
		master: master,
		// store:  store,
	}, nil
}

func (f *Filer) CreateEntry(entry Entry) (error) {
	/*
	1. recursively ensure parent directory is created.
	2. get current parent directory, add link,
	3. add the file entry
	*/

	recursivelyEnsureDirectory(entry.Dir, func(parent, name string) error {
		return nil
	})

	return f.store.CreateEntry(entry)
}

func (f *Filer) AppendFileChunk(p FullPath, c FileChunk) (err error) {
	return f.store.AppendFileChunk(p, c)
}

func (f *Filer) FindEntry(p FullPath) (found bool, fileEntry Entry, err error) {
	return f.store.FindEntry(p)
}

func (f *Filer) DeleteEntry(p FullPath) (fileEntry Entry, err error) {
	return f.store.DeleteEntry(p)
}

func (f *Filer) ListDirectoryEntries(p FullPath) ([]Entry, error) {
	return f.store.ListDirectoryEntries(p)
}

func (f *Filer) UpdateEntry(entry Entry) (error) {
	return f.store.UpdateEntry(entry)
}

func recursivelyEnsureDirectory(fullPath string, fn func(parent, name string) error) (error) {
	if strings.HasSuffix(fullPath, "/") {
		fullPath = fullPath[0:len(fullPath)-1]
	}
	nextPathEnd := strings.LastIndex(fullPath, "/")
	if nextPathEnd < 0 {
		return nil
	}

	dirName := fullPath[nextPathEnd+1:]
	parentDirPath := fullPath[0:nextPathEnd]

	if parentDirPath == "" {
		parentDirPath = "/"
	}

	if err := recursivelyEnsureDirectory(parentDirPath, fn); err != nil {
		return err
	}

	if err := fn(parentDirPath, dirName); err != nil {
		return err
	}

	return nil
}

func (f *Filer) cacheGetDirectory(dirpath string) (error) {
	return nil
}