diff options
Diffstat (limited to 'weed')
| -rw-r--r-- | weed/filer/filer_conf.go | 48 | ||||
| -rw-r--r-- | weed/filer/filer_conf_test.go | 31 | ||||
| -rw-r--r-- | weed/pb/filer_pb/filer_pb_helper.go | 6 |
3 files changed, 85 insertions, 0 deletions
diff --git a/weed/filer/filer_conf.go b/weed/filer/filer_conf.go new file mode 100644 index 000000000..079794def --- /dev/null +++ b/weed/filer/filer_conf.go @@ -0,0 +1,48 @@ +package filer + +import ( + "github.com/chrislusf/seaweedfs/weed/glog" + "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" + "github.com/golang/protobuf/proto" + "github.com/viant/ptrie" +) + +type FilerConf struct { + rules ptrie.Trie +} + +func NewFilerConf(data []byte) (fc *FilerConf) { + fc = &FilerConf{ + rules: ptrie.New(), + } + + conf := &filer_pb.FilerConf{} + err := proto.UnmarshalText(string(data), conf) + if err != nil { + glog.Errorf("unable to parse filer conf: %v", err) + return + } + + fc.doLoadConf(conf) + return fc +} + +func (fc *FilerConf) doLoadConf(conf *filer_pb.FilerConf) { + for _, location := range conf.Locations { + err := fc.rules.Put([]byte(location.LocationPrefix), location) + if err != nil { + glog.Errorf("put location prefix: %v", err) + } + } +} + +func (fc *FilerConf) MatchStorageRule(path string) (pathConf *filer_pb.FilerConf_PathConf){ + fc.rules.MatchPrefix([]byte(path), func(key []byte, value interface{}) bool { + pathConf = value.(*filer_pb.FilerConf_PathConf) + return true + }) + if pathConf == nil { + return &filer_pb.FilerConf_PathConf{} + } + return pathConf +} diff --git a/weed/filer/filer_conf_test.go b/weed/filer/filer_conf_test.go new file mode 100644 index 000000000..6a7b875d3 --- /dev/null +++ b/weed/filer/filer_conf_test.go @@ -0,0 +1,31 @@ +package filer + +import ( + "testing" + + "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" + "github.com/stretchr/testify/assert" + "github.com/viant/ptrie" +) + +func TestFilerConf(t *testing.T) { + + fc := &FilerConf{ + rules: ptrie.New(), + } + conf := &filer_pb.FilerConf{Locations: []*filer_pb.FilerConf_PathConf{ + { + LocationPrefix: "/buckets/abc", + Collection: "abc", + }, + { + LocationPrefix: "/buckets/abcd", + Collection: "abcd", + }, + }} + fc.doLoadConf(conf) + + assert.Equal(t, "abc", fc.MatchStorageRule("/buckets/abc/jasdf").Collection) + assert.Equal(t, "abcd", fc.MatchStorageRule("/buckets/abcd/jasdf").Collection) + +} diff --git a/weed/pb/filer_pb/filer_pb_helper.go b/weed/pb/filer_pb/filer_pb_helper.go index 75e37ca1b..fb989a02e 100644 --- a/weed/pb/filer_pb/filer_pb_helper.go +++ b/weed/pb/filer_pb/filer_pb_helper.go @@ -8,6 +8,7 @@ import ( "github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/storage/needle" + "github.com/viant/ptrie" ) func ToFileIdObject(fileIdStr string) (*FileId, error) { @@ -138,3 +139,8 @@ func IsRename(event *SubscribeMetadataResponse) bool { event.EventNotification.OldEntry != nil && event.Directory != event.EventNotification.NewParentPath } + +var _ = ptrie.KeyProvider(&FilerConf_PathConf{}) +func (fp *FilerConf_PathConf) Key() interface{} { + return fp.LocationPrefix +} |
