Add non-unique with index with options
This commit is contained in:
@@ -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/
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user