From dba08c48dc66c63270818e0feb8184317aaddee9 Mon Sep 17 00:00:00 2001 From: Ilja Neumann Date: Tue, 6 Oct 2020 18:30:50 +0200 Subject: [PATCH] Add non-unique with index with options --- accounts/pkg/indexer/index/cs3/non_unique.go | 38 ++++++++++++++++++++ accounts/pkg/indexer/indexer.go | 27 ++++++++++---- accounts/pkg/indexer/indexer_test.go | 23 ++++++++++++ 3 files changed, 82 insertions(+), 6 deletions(-) diff --git a/accounts/pkg/indexer/index/cs3/non_unique.go b/accounts/pkg/indexer/index/cs3/non_unique.go index 7462b75e3..e2c9c2bff 100644 --- a/accounts/pkg/indexer/index/cs3/non_unique.go +++ b/accounts/pkg/indexer/index/cs3/non_unique.go @@ -10,6 +10,9 @@ import ( "github.com/cs3org/reva/pkg/token" "github.com/cs3org/reva/pkg/token/manager/jwt" idxerrs "github.com/owncloud/ocis/accounts/pkg/indexer/errors" + "github.com/owncloud/ocis/accounts/pkg/indexer/index" + "github.com/owncloud/ocis/accounts/pkg/indexer/option" + "github.com/owncloud/ocis/accounts/pkg/indexer/registry" "google.golang.org/grpc/metadata" "io/ioutil" "net/http" @@ -19,6 +22,10 @@ import ( "strings" ) +func init() { + registry.IndexConstructorRegistry["cs3"]["non_unique"] = NewNonUniqueIndexWithOptions +} + type NonUnique struct { indexBy string typeName string @@ -33,6 +40,37 @@ type NonUnique struct { cs3conf *Config } +// NewNonUniqueIndexWithOptions instantiates a new UniqueIndex instance. Init() should be +// called afterward to ensure correct on-disk structure. +func NewNonUniqueIndexWithOptions(o ...option.Option) index.Index { + opts := &option.Options{} + for _, opt := range o { + opt(opts) + } + + return &NonUnique{ + indexBy: opts.IndexBy, + typeName: opts.TypeName, + filesDir: opts.FilesDir, + indexBaseDir: path.Join(opts.DataDir, "index.cs3"), + indexRootDir: path.Join(path.Join(opts.DataDir, "index.cs3"), strings.Join([]string{"non_unique", opts.TypeName, opts.IndexBy}, ".")), + cs3conf: &Config{ + ProviderAddr: opts.ProviderAddr, + DataURL: opts.DataURL, + DataPrefix: opts.DataPrefix, + JWTSecret: opts.JWTSecret, + ServiceUserName: "", + ServiceUserUUID: "", + }, + dataProvider: dataProviderClient{ + baseURL: singleJoiningSlash(opts.DataURL, opts.DataPrefix), + client: http.Client{ + Transport: http.DefaultTransport, + }, + }, + } +} + // NewNonUniqueIndex instantiates a new NonUniqueIndex instance. // /var/tmp/ocis-accounts/index.cs3/Pets/Bro* // ├── Brown/ diff --git a/accounts/pkg/indexer/indexer.go b/accounts/pkg/indexer/indexer.go index 217e9389d..126d9ab39 100644 --- a/accounts/pkg/indexer/indexer.go +++ b/accounts/pkg/indexer/indexer.go @@ -79,12 +79,27 @@ func (i Indexer) AddUniqueIndex(t interface{}, indexBy, pkName, entityDirName st func (i Indexer) AddNonUniqueIndex(t interface{}, indexBy, pkName, entityDirName string) error { strategy := getRegistryStrategy(i.newConfig) f := registry.IndexConstructorRegistry[strategy]["non_unique"] - idx := f( - option.WithTypeName(getTypeFQN(t)), - option.WithIndexBy(indexBy), - option.WithFilesDir(path.Join(i.config.DataDir, entityDirName)), - option.WithIndexBaseDir(path.Join(i.config.DataDir, i.config.IndexRootDirName)), - ) + var idx index.Index + + if strategy == "disk" { + idx = f( + option.WithTypeName(getTypeFQN(t)), + option.WithIndexBy(indexBy), + option.WithFilesDir(path.Join(i.newConfig.Repo.Disk.Path, entityDirName)), + option.WithDataDir(i.newConfig.Repo.Disk.Path), + ) + } else if strategy == "cs3" { + idx = f( + option.WithTypeName(getTypeFQN(t)), + option.WithIndexBy(indexBy), + option.WithFilesDir(path.Join(i.newConfig.Repo.Disk.Path, entityDirName)), + option.WithDataDir(i.newConfig.Repo.Disk.Path), + option.WithDataURL(i.newConfig.Repo.CS3.DataURL), + option.WithDataPrefix(i.newConfig.Repo.CS3.DataPrefix), + option.WithJWTSecret(i.newConfig.Repo.CS3.JWTSecret), + option.WithProviderAddr(i.newConfig.Repo.CS3.ProviderAddr), + ) + } i.indices.addIndex(getTypeFQN(t), pkName, idx) return idx.Init() diff --git a/accounts/pkg/indexer/indexer_test.go b/accounts/pkg/indexer/indexer_test.go index cdb42bed3..c652ce910 100644 --- a/accounts/pkg/indexer/indexer_test.go +++ b/accounts/pkg/indexer/indexer_test.go @@ -50,6 +50,29 @@ func TestIndexer_AddWithUniqueIndexCS3(t *testing.T) { _ = os.RemoveAll(dataDir) } +func TestIndexer_AddWithNonUniqueIndexCS3(t *testing.T) { + dataDir := WriteIndexTestDataCS3(t, TestData, "Id") + indexer := CreateIndexer(&config.Config{ + Repo: config.Repo{ + CS3: config.CS3{ + ProviderAddr: "0.0.0.0:9215", + DataURL: "http://localhost:9216", + DataPrefix: "data", + JWTSecret: "Pive-Fumkiu4", + }, + }, + }) + + err := indexer.AddNonUniqueIndex(&User{}, "UserName", "Id", "users") + assert.NoError(t, err) + + u := &User{Id: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"} + err = indexer.Add(u) + assert.NoError(t, err) + + _ = os.RemoveAll(dataDir) +} + func TestIndexer_FindByWithUniqueIndex(t *testing.T) { dataDir := WriteIndexTestData(t, TestData, "Id") indexer := NewIndexer(&Config{