| package impl |
| |
| import ( |
| "errors" |
| "strings" |
| |
| "veyron.io/veyron/veyron/services/mgmt/lib/fs" |
| "veyron.io/veyron/veyron2/ipc" |
| "veyron.io/veyron/veyron2/naming" |
| "veyron.io/veyron/veyron2/services/mgmt/application" |
| "veyron.io/veyron/veyron2/vlog" |
| ) |
| |
| // appRepoService implements the Application repository interface. |
| type appRepoService struct { |
| // store is the storage server used for storing application |
| // metadata. |
| // All objects share the same Memstore. |
| store *fs.Memstore |
| // storeRoot is a name in the directory under which all data will be |
| // stored. |
| storeRoot string |
| // suffix is the name of the application object. |
| suffix string |
| } |
| |
| var ( |
| errInvalidSuffix = errors.New("invalid suffix") |
| errOperationFailed = errors.New("operation failed") |
| errNotFound = errors.New("not found") |
| ) |
| |
| // NewApplicationService returns a new Application service implementation. |
| func NewApplicationService(store *fs.Memstore, storeRoot, suffix string) *appRepoService { |
| return &appRepoService{store: store, storeRoot: storeRoot, suffix: suffix} |
| } |
| |
| func parse(suffix string) (string, string, error) { |
| tokens := strings.Split(suffix, "/") |
| switch len(tokens) { |
| case 2: |
| return tokens[0], tokens[1], nil |
| case 1: |
| return tokens[0], "", nil |
| default: |
| return "", "", errInvalidSuffix |
| } |
| } |
| |
| func (i *appRepoService) Match(context ipc.ServerContext, profiles []string) (application.Envelope, error) { |
| vlog.VI(0).Infof("%v.Match(%v)", i.suffix, profiles) |
| empty := application.Envelope{} |
| name, version, err := parse(i.suffix) |
| if err != nil { |
| return empty, err |
| } |
| if version == "" { |
| return empty, errInvalidSuffix |
| } |
| |
| i.store.Lock() |
| defer i.store.Unlock() |
| |
| for _, profile := range profiles { |
| path := naming.Join("/applications", name, profile, version) |
| entry, err := i.store.BindObject(path).Get(context) |
| if err != nil { |
| continue |
| } |
| envelope, ok := entry.Value.(application.Envelope) |
| if !ok { |
| continue |
| } |
| return envelope, nil |
| } |
| return empty, errNotFound |
| } |
| |
| func (i *appRepoService) Put(context ipc.ServerContext, profiles []string, envelope application.Envelope) error { |
| vlog.VI(0).Infof("%v.Put(%v, %v)", i.suffix, profiles, envelope) |
| name, version, err := parse(i.suffix) |
| if err != nil { |
| return err |
| } |
| if version == "" { |
| return errInvalidSuffix |
| } |
| i.store.Lock() |
| defer i.store.Unlock() |
| // Transaction is rooted at "", so tname == tid. |
| tname, err := i.store.BindTransactionRoot("").CreateTransaction(context) |
| if err != nil { |
| return err |
| } |
| |
| for _, profile := range profiles { |
| path := naming.Join(tname, "/applications", name, profile, version) |
| |
| object := i.store.BindObject(path) |
| _, err := object.Put(context, envelope) |
| if err != nil { |
| return errOperationFailed |
| } |
| } |
| if err := i.store.BindTransaction(tname).Commit(context); err != nil { |
| return errOperationFailed |
| } |
| return nil |
| } |
| |
| func (i *appRepoService) Remove(context ipc.ServerContext, profile string) error { |
| vlog.VI(0).Infof("%v.Remove(%v)", i.suffix, profile) |
| name, version, err := parse(i.suffix) |
| if err != nil { |
| return err |
| } |
| i.store.Lock() |
| defer i.store.Unlock() |
| // Transaction is rooted at "", so tname == tid. |
| tname, err := i.store.BindTransactionRoot("").CreateTransaction(context) |
| if err != nil { |
| return err |
| } |
| path := naming.Join(tname, "/applications", name, profile) |
| if version != "" { |
| path += "/" + version |
| } |
| object := i.store.BindObject(path) |
| found, err := object.Exists(context) |
| if err != nil { |
| return errOperationFailed |
| } |
| if !found { |
| return errNotFound |
| } |
| if err := object.Remove(context); err != nil { |
| return errOperationFailed |
| } |
| if err := i.store.BindTransaction(tname).Commit(context); err != nil { |
| return errOperationFailed |
| } |
| return nil |
| } |
| |
| func (i *appRepoService) allApplications() ([]string, error) { |
| apps, err := i.store.BindObject("/applications").Children() |
| if err != nil { |
| return nil, err |
| } |
| return apps, nil |
| } |
| |
| func (i *appRepoService) allAppVersions(appName string) ([]string, error) { |
| profiles, err := i.store.BindObject(naming.Join("/applications", appName)).Children() |
| if err != nil { |
| return nil, err |
| } |
| uniqueVersions := make(map[string]struct{}) |
| for _, profile := range profiles { |
| versions, err := i.store.BindObject(naming.Join("/applications", appName, profile)).Children() |
| if err != nil { |
| return nil, err |
| } |
| for _, v := range versions { |
| uniqueVersions[v] = struct{}{} |
| } |
| } |
| versions := make([]string, len(uniqueVersions)) |
| index := 0 |
| for v, _ := range uniqueVersions { |
| versions[index] = v |
| index++ |
| } |
| return versions, nil |
| } |
| |
| func (i *appRepoService) GlobChildren__() (<-chan string, error) { |
| vlog.VI(0).Infof("%v.GlobChildren__()", i.suffix) |
| i.store.Lock() |
| defer i.store.Unlock() |
| |
| var elems []string |
| if i.suffix != "" { |
| elems = strings.Split(i.suffix, "/") |
| } |
| |
| var results []string |
| var err error |
| switch len(elems) { |
| case 0: |
| results, err = i.allApplications() |
| if err != nil { |
| return nil, err |
| } |
| case 1: |
| results, err = i.allAppVersions(elems[0]) |
| if err != nil { |
| return nil, err |
| } |
| case 2: |
| versions, err := i.allAppVersions(elems[0]) |
| if err != nil { |
| return nil, err |
| } |
| for _, v := range versions { |
| if v == elems[1] { |
| return nil, nil |
| } |
| } |
| return nil, errNotFound |
| default: |
| return nil, errNotFound |
| } |
| |
| ch := make(chan string, len(results)) |
| for _, r := range results { |
| ch <- r |
| } |
| close(ch) |
| return ch, nil |
| } |