diff --git a/accounts/pkg/indexer/index/cs3/autoincrement.go b/accounts/pkg/indexer/index/cs3/autoincrement.go index 465521588..ded4d9aa1 100644 --- a/accounts/pkg/indexer/index/cs3/autoincrement.go +++ b/accounts/pkg/indexer/index/cs3/autoincrement.go @@ -15,6 +15,7 @@ import ( idxerrs "github.com/owncloud/ocis/accounts/pkg/indexer/errors" user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" + rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" v1beta11 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" "github.com/cs3org/reva/pkg/rgrpc/todo/pool" @@ -385,3 +386,34 @@ func (idx *Autoincrement) next() (int, error) { } return latest + 1, nil } + +func (idx *Autoincrement) getAuthenticatedContext(ctx context.Context) (context.Context, error) { + t, err := idx.authenticate(ctx) + if err != nil { + return nil, err + } + ctx = metadata.AppendToOutgoingContext(ctx, token.TokenHeader, t) + return ctx, nil +} + +// Delete deletes the index folder from its storage. +func (idx *Autoincrement) Delete() error { + ctx, err := idx.getAuthenticatedContext(context.Background()) + if err != nil { + return err + } + + res, err := idx.storageProvider.Delete(ctx, &provider.DeleteRequest{ + Ref: &provider.Reference{ + Spec: &provider.Reference_Path{Path: path.Join("/meta", idx.indexRootDir)}, + }, + }) + if err != nil { + return err + } + if res.Status.Code != rpc.Code_CODE_OK { + return fmt.Errorf("error deleting index root dir: %v", idx.indexRootDir) + } + + return nil +} diff --git a/accounts/pkg/indexer/index/cs3/non_unique.go b/accounts/pkg/indexer/index/cs3/non_unique.go index c017d791b..d0a9dd923 100644 --- a/accounts/pkg/indexer/index/cs3/non_unique.go +++ b/accounts/pkg/indexer/index/cs3/non_unique.go @@ -10,6 +10,8 @@ import ( "path/filepath" "strings" + rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" + user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" v1beta11 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" @@ -408,3 +410,25 @@ func (idx *NonUnique) getAuthenticatedContext(ctx context.Context) (context.Cont ctx = metadata.AppendToOutgoingContext(ctx, token.TokenHeader, t) return ctx, nil } + +// Delete deletes the index folder from its storage. +func (idx *NonUnique) Delete() error { + ctx, err := idx.getAuthenticatedContext(context.Background()) + if err != nil { + return err + } + + res, err := idx.storageProvider.Delete(ctx, &provider.DeleteRequest{ + Ref: &provider.Reference{ + Spec: &provider.Reference_Path{Path: path.Join("/meta", idx.indexRootDir)}, + }, + }) + if err != nil { + return err + } + if res.Status.Code != rpc.Code_CODE_OK { + return fmt.Errorf("error deleting index root dir: %v", idx.indexRootDir) + } + + return nil +} diff --git a/accounts/pkg/indexer/index/cs3/unique.go b/accounts/pkg/indexer/index/cs3/unique.go index a5526b47c..9768eac6b 100644 --- a/accounts/pkg/indexer/index/cs3/unique.go +++ b/accounts/pkg/indexer/index/cs3/unique.go @@ -11,6 +11,7 @@ import ( "strings" user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" + rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" v1beta11 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" "github.com/cs3org/reva/pkg/rgrpc/todo/pool" @@ -370,3 +371,34 @@ func (idx *Unique) authenticate(ctx context.Context) (token string, err error) { } return idx.tokenManager.MintToken(ctx, u) } + +func (idx *Unique) getAuthenticatedContext(ctx context.Context) (context.Context, error) { + t, err := idx.authenticate(ctx) + if err != nil { + return nil, err + } + ctx = metadata.AppendToOutgoingContext(ctx, token.TokenHeader, t) + return ctx, nil +} + +// Delete deletes the index folder from its storage. +func (idx *Unique) Delete() error { + ctx, err := idx.getAuthenticatedContext(context.Background()) + if err != nil { + return err + } + + res, err := idx.storageProvider.Delete(ctx, &provider.DeleteRequest{ + Ref: &provider.Reference{ + Spec: &provider.Reference_Path{Path: path.Join("/meta", idx.indexRootDir)}, + }, + }) + if err != nil { + return err + } + if res.Status.Code != rpc.Code_CODE_OK { + return fmt.Errorf("error deleting index root dir: %v", idx.indexRootDir) + } + + return nil +} diff --git a/accounts/pkg/indexer/index/disk/autoincrement.go b/accounts/pkg/indexer/index/disk/autoincrement.go index 170e274b9..279b5edb8 100644 --- a/accounts/pkg/indexer/index/disk/autoincrement.go +++ b/accounts/pkg/indexer/index/disk/autoincrement.go @@ -19,11 +19,11 @@ import ( // Autoincrement are fields for an index of type autoincrement. type Autoincrement struct { - indexBy string - typeName string - filesDir string - indexBaseDir string - indexRootDir string + indexBy string + typeName string + filesDir string + indexBaseDir string + indexRootDir string bound *option.Bound } @@ -54,12 +54,12 @@ func NewAutoincrementIndex(o ...option.Option) index.Index { } return &Autoincrement{ - indexBy: opts.IndexBy, - typeName: opts.TypeName, - filesDir: opts.FilesDir, - bound: opts.Bound, - indexBaseDir: path.Join(opts.DataDir, "index.disk"), - indexRootDir: path.Join(path.Join(opts.DataDir, "index.disk"), strings.Join([]string{"autoincrement", opts.TypeName, opts.IndexBy}, ".")), + indexBy: opts.IndexBy, + typeName: opts.TypeName, + filesDir: opts.FilesDir, + bound: opts.Bound, + indexBaseDir: path.Join(opts.DataDir, "index.disk"), + indexRootDir: path.Join(path.Join(opts.DataDir, "index.disk"), strings.Join([]string{"autoincrement", opts.TypeName, opts.IndexBy}, ".")), } } @@ -260,3 +260,8 @@ func (idx *Autoincrement) next() (int, error) { return latest + 1, nil } + +// Delete deletes the index folder from its storage. +func (idx *Autoincrement) Delete() error { + return nil +} diff --git a/accounts/pkg/indexer/index/disk/non_unique.go b/accounts/pkg/indexer/index/disk/non_unique.go index 8f0c7d694..b4df3db5b 100644 --- a/accounts/pkg/indexer/index/disk/non_unique.go +++ b/accounts/pkg/indexer/index/disk/non_unique.go @@ -233,3 +233,8 @@ func (idx *NonUnique) TypeName() string { func (idx *NonUnique) FilesDir() string { return idx.filesDir } + +// Delete deletes the index folder from its storage. +func (idx *NonUnique) Delete() error { + return nil +} diff --git a/accounts/pkg/indexer/index/disk/unique.go b/accounts/pkg/indexer/index/disk/unique.go index 7b9436223..6a5374469 100644 --- a/accounts/pkg/indexer/index/disk/unique.go +++ b/accounts/pkg/indexer/index/disk/unique.go @@ -220,3 +220,8 @@ func isValidSymlink(path string) (err error) { return } + +// Delete deletes the index folder from its storage. +func (idx *Unique) Delete() error { + return nil +} diff --git a/accounts/pkg/indexer/index/index.go b/accounts/pkg/indexer/index/index.go index e6f62862e..9ad7aee7b 100644 --- a/accounts/pkg/indexer/index/index.go +++ b/accounts/pkg/indexer/index/index.go @@ -13,4 +13,5 @@ type Index interface { IndexBy() string TypeName() string FilesDir() string + Delete() error // Delete deletes the index folder from its storage. } diff --git a/accounts/pkg/indexer/indexer.go b/accounts/pkg/indexer/indexer.go index 01426e585..064ac6d75 100644 --- a/accounts/pkg/indexer/indexer.go +++ b/accounts/pkg/indexer/indexer.go @@ -43,11 +43,17 @@ func getRegistryStrategy(cfg *config.Config) string { } func (i Indexer) Reset() error { - for k := range i.indices { - delete(i.indices, k) - } + // TODO: is the root "index.cs3" folder somewhere? + // basically every implementation should take care of deleting its root folder. - // TODO: delete indexes from storage (cs3 / disk) + for j := range i.indices { + for _, indices := range i.indices[j].IndicesByField { + for _, idx := range indices { + _ = idx.Delete() + } + } + delete(i.indices, j) + } return nil } diff --git a/accounts/pkg/service/v0/index.go b/accounts/pkg/service/v0/index.go index a0eeedce2..58517cb80 100644 --- a/accounts/pkg/service/v0/index.go +++ b/accounts/pkg/service/v0/index.go @@ -14,9 +14,8 @@ func (s Service) RebuildIndex(ctx context.Context, request *proto.RebuildIndexRe if err := s.index.Reset(); err != nil { return err } - response.Indices = []string{"foo", "bar"} - if err := createIndices(s.index, s.Config); err != nil { + if err := recreateContainers(s.index, s.Config); err != nil { return err } @@ -25,8 +24,8 @@ func (s Service) RebuildIndex(ctx context.Context, request *proto.RebuildIndexRe return nil } -// createIndices adds all indices to the indexer that we have for this service. -func createIndices(idx *indexer.Indexer, cfg *config.Config) error { +// recreateContainers adds all indices to the indexer that we have for this service. +func recreateContainers(idx *indexer.Indexer, cfg *config.Config) error { // Accounts if err := idx.AddIndex(&proto.Account{}, "DisplayName", "Id", "accounts", "non_unique", nil, true); err != nil { return err