Make indexes configurable for being case insensitive

This commit is contained in:
Benedikt Kulmann
2020-10-19 17:53:53 +02:00
parent 5b45655fc0
commit dee9959e69
12 changed files with 198 additions and 123 deletions
+7
View File
@@ -127,6 +127,13 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
EnvVars: []string{"ACCOUNTS_STORAGE_CS3_DATA_PREFIX"},
Destination: &cfg.Repo.CS3.DataPrefix,
},
&cli.StringFlag{
Name: "storage-cs3-jwt-secret",
Value: "Pive-Fumkiu4",
Usage: "Used to create JWT to talk to reva, should equal reva's jwt-secret",
EnvVars: []string{"ACCOUNTS_STORAGE_CS3_JWT_SECRET"},
Destination: &cfg.Repo.CS3.JWTSecret,
},
&cli.StringFlag{
Name: "service-user-uuid",
Value: "95cb8724-03b2-11eb-a0a6-c33ef8ef53ad",
+24 -19
View File
@@ -28,11 +28,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
tokenManager token.Manager
storageProvider provider.ProviderAPIClient
@@ -53,11 +53,11 @@ func NewAutoincrementIndex(o ...option.Option) index.Index {
}
u := &Autoincrement{
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{"autoincrement", opts.TypeName, opts.IndexBy}, ".")),
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{"autoincrement", opts.TypeName, opts.IndexBy}, ".")),
cs3conf: &Config{
ProviderAddr: opts.ProviderAddr,
DataURL: opts.DataURL,
@@ -115,7 +115,7 @@ func (idx *Autoincrement) Init() error {
}
// Lookup exact lookup by value.
func (idx Autoincrement) Lookup(v string) ([]string, error) {
func (idx *Autoincrement) Lookup(v string) ([]string, error) {
searchPath := path.Join(idx.indexRootDir, v)
oldname, err := idx.resolveSymlink(searchPath)
if err != nil {
@@ -130,7 +130,7 @@ func (idx Autoincrement) Lookup(v string) ([]string, error) {
}
// Add a new value to the index.
func (idx Autoincrement) Add(id, v string) (string, error) {
func (idx *Autoincrement) Add(id, v string) (string, error) {
var newName string
if v == "" {
next, err := idx.next()
@@ -153,7 +153,7 @@ func (idx Autoincrement) Add(id, v string) (string, error) {
}
// Remove a value v from an index.
func (idx Autoincrement) Remove(id string, v string) error {
func (idx *Autoincrement) Remove(id string, v string) error {
if v == "" {
return nil
}
@@ -194,7 +194,7 @@ func (idx Autoincrement) Remove(id string, v string) error {
}
// Update index from <oldV> to <newV>.
func (idx Autoincrement) Update(id, oldV, newV string) error {
func (idx *Autoincrement) Update(id, oldV, newV string) error {
if err := idx.Remove(id, oldV); err != nil {
return err
}
@@ -207,7 +207,7 @@ func (idx Autoincrement) Update(id, oldV, newV string) error {
}
// Search allows for glob search on the index.
func (idx Autoincrement) Search(pattern string) ([]string, error) {
func (idx *Autoincrement) Search(pattern string) ([]string, error) {
ctx := context.Background()
t, err := idx.authenticate(ctx)
if err != nil {
@@ -244,18 +244,23 @@ func (idx Autoincrement) Search(pattern string) ([]string, error) {
return matches, nil
}
// CaseInsensitive undocumented.
func (idx *Autoincrement) CaseInsensitive() bool {
return false
}
// IndexBy undocumented.
func (idx Autoincrement) IndexBy() string {
func (idx *Autoincrement) IndexBy() string {
return idx.indexBy
}
// TypeName undocumented.
func (idx Autoincrement) TypeName() string {
func (idx *Autoincrement) TypeName() string {
return idx.typeName
}
// FilesDir undocumented.
func (idx Autoincrement) FilesDir() string {
func (idx *Autoincrement) FilesDir() string {
return idx.filesDir
}
@@ -347,7 +352,7 @@ func (idx *Autoincrement) authenticate(ctx context.Context) (token string, err e
return idx.tokenManager.MintToken(ctx, u)
}
func (idx Autoincrement) next() (int, error) {
func (idx *Autoincrement) next() (int, error) {
ctx := context.Background()
t, err := idx.authenticate(ctx)
if err != nil {
+17 -10
View File
@@ -28,11 +28,12 @@ func init() {
// NonUnique are fields for an index of type non_unique.
type NonUnique struct {
indexBy string
typeName string
filesDir string
indexBaseDir string
indexRootDir string
caseInsensitive bool
indexBy string
typeName string
filesDir string
indexBaseDir string
indexRootDir string
tokenManager token.Manager
storageProvider provider.ProviderAPIClient
@@ -57,11 +58,12 @@ func NewNonUniqueIndexWithOptions(o ...option.Option) index.Index {
}
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}, ".")),
caseInsensitive: opts.CaseInsensitive,
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,
@@ -274,6 +276,11 @@ func (idx *NonUnique) Search(pattern string) ([]string, error) {
return matches, nil
}
// CaseInsensitive undocumented.
func (idx *NonUnique) CaseInsensitive() bool {
return idx.caseInsensitive
}
// IndexBy undocumented.
func (idx *NonUnique) IndexBy() string {
return idx.indexBy
+17 -10
View File
@@ -24,11 +24,12 @@ import (
// Unique are fields for an index of type non_unique.
type Unique struct {
indexBy string
typeName string
filesDir string
indexBaseDir string
indexRootDir string
caseInsensitive bool
indexBy string
typeName string
filesDir string
indexBaseDir string
indexRootDir string
tokenManager token.Manager
storageProvider provider.ProviderAPIClient
@@ -60,11 +61,12 @@ func NewUniqueIndexWithOptions(o ...option.Option) index.Index {
}
u := &Unique{
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{"unique", opts.TypeName, opts.IndexBy}, ".")),
caseInsensitive: opts.CaseInsensitive,
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{"unique", opts.TypeName, opts.IndexBy}, ".")),
cs3conf: &Config{
ProviderAddr: opts.ProviderAddr,
DataURL: opts.DataURL,
@@ -245,6 +247,11 @@ func (idx *Unique) Search(pattern string) ([]string, error) {
return matches, nil
}
// CaseInsensitive undocumented.
func (idx *Unique) CaseInsensitive() bool {
return idx.caseInsensitive
}
// IndexBy undocumented.
func (idx *Unique) IndexBy() string {
return idx.indexBy
@@ -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}, ".")),
}
}
@@ -74,7 +74,7 @@ var (
)
// Init initializes an autoincrement index.
func (idx Autoincrement) Init() error {
func (idx *Autoincrement) Init() error {
if _, err := os.Stat(idx.filesDir); err != nil {
return err
}
@@ -87,7 +87,7 @@ func (idx Autoincrement) Init() error {
}
// Lookup exact lookup by value.
func (idx Autoincrement) Lookup(v string) ([]string, error) {
func (idx *Autoincrement) Lookup(v string) ([]string, error) {
searchPath := path.Join(idx.indexRootDir, v)
if err := isValidSymlink(searchPath); err != nil {
if os.IsNotExist(err) {
@@ -106,7 +106,7 @@ func (idx Autoincrement) Lookup(v string) ([]string, error) {
}
// Add a new value to the index.
func (idx Autoincrement) Add(id, v string) (string, error) {
func (idx *Autoincrement) Add(id, v string) (string, error) {
nextID, err := idx.next()
if err != nil {
return "", err
@@ -127,7 +127,7 @@ func (idx Autoincrement) Add(id, v string) (string, error) {
}
// Remove a value v from an index.
func (idx Autoincrement) Remove(id string, v string) error {
func (idx *Autoincrement) Remove(id string, v string) error {
if v == "" {
return nil
}
@@ -136,7 +136,7 @@ func (idx Autoincrement) Remove(id string, v string) error {
}
// Update index from <oldV> to <newV>.
func (idx Autoincrement) Update(id, oldV, newV string) error {
func (idx *Autoincrement) Update(id, oldV, newV string) error {
oldPath := path.Join(idx.indexRootDir, oldV)
if err := isValidSymlink(oldPath); err != nil {
if os.IsNotExist(err) {
@@ -160,7 +160,7 @@ func (idx Autoincrement) Update(id, oldV, newV string) error {
}
// Search allows for glob search on the index.
func (idx Autoincrement) Search(pattern string) ([]string, error) {
func (idx *Autoincrement) Search(pattern string) ([]string, error) {
paths, err := filepath.Glob(path.Join(idx.indexRootDir, pattern))
if err != nil {
return nil, err
@@ -187,18 +187,23 @@ func (idx Autoincrement) Search(pattern string) ([]string, error) {
return res, nil
}
// CaseInsensitive undocumented.
func (idx *Autoincrement) CaseInsensitive() bool {
return false
}
// IndexBy undocumented.
func (idx Autoincrement) IndexBy() string {
func (idx *Autoincrement) IndexBy() string {
return idx.indexBy
}
// TypeName undocumented.
func (idx Autoincrement) TypeName() string {
func (idx *Autoincrement) TypeName() string {
return idx.typeName
}
// FilesDir undocumented.
func (idx Autoincrement) FilesDir() string {
func (idx *Autoincrement) FilesDir() string {
return idx.filesDir
}
@@ -234,7 +239,7 @@ func readDir(dirname string) ([]os.FileInfo, error) {
return list, nil
}
func (idx Autoincrement) next() (int, error) {
func (idx *Autoincrement) next() (int, error) {
files, err := readDir(idx.indexRootDir)
if err != nil {
return -1, err
+29 -22
View File
@@ -14,7 +14,7 @@ import (
"github.com/owncloud/ocis/accounts/pkg/indexer/registry"
)
// NonUniqueIndex is able to index an document by a key which might contain non-unique values
// NonUnique is able to index an document by a key which might contain non-unique values
//
// /var/tmp/testfiles-395764020/index.disk/PetByColor/
// ├── Brown
@@ -24,12 +24,13 @@ import (
// │ └── xadaf-189 -> /var/tmp/testfiles-395764020/pets/xadaf-189
// └── White
// └── wefwe-456 -> /var/tmp/testfiles-395764020/pets/wefwe-456
type NonUniqueIndex struct {
indexBy string
typeName string
filesDir string
indexBaseDir string
indexRootDir string
type NonUnique struct {
caseInsensitive bool
indexBy string
typeName string
filesDir string
indexBaseDir string
indexRootDir string
}
func init() {
@@ -44,17 +45,18 @@ func NewNonUniqueIndexWithOptions(o ...option.Option) index.Index {
opt(opts)
}
return &NonUniqueIndex{
indexBy: opts.IndexBy,
typeName: opts.TypeName,
filesDir: opts.FilesDir,
indexBaseDir: path.Join(opts.DataDir, "index.disk"),
indexRootDir: path.Join(path.Join(opts.DataDir, "index.disk"), strings.Join([]string{"non_unique", opts.TypeName, opts.IndexBy}, ".")),
return &NonUnique{
caseInsensitive: opts.CaseInsensitive,
indexBy: opts.IndexBy,
typeName: opts.TypeName,
filesDir: opts.FilesDir,
indexBaseDir: path.Join(opts.DataDir, "index.disk"),
indexRootDir: path.Join(path.Join(opts.DataDir, "index.disk"), strings.Join([]string{"non_unique", opts.TypeName, opts.IndexBy}, ".")),
}
}
// Init initializes a unique index.
func (idx NonUniqueIndex) Init() error {
func (idx *NonUnique) Init() error {
if _, err := os.Stat(idx.filesDir); err != nil {
return err
}
@@ -67,7 +69,7 @@ func (idx NonUniqueIndex) Init() error {
}
// Lookup exact lookup by value.
func (idx NonUniqueIndex) Lookup(v string) ([]string, error) {
func (idx *NonUnique) Lookup(v string) ([]string, error) {
searchPath := path.Join(idx.indexRootDir, v)
fi, err := ioutil.ReadDir(searchPath)
if os.IsNotExist(err) {
@@ -91,7 +93,7 @@ func (idx NonUniqueIndex) Lookup(v string) ([]string, error) {
}
// Add adds a value to the index, returns the path to the root-document
func (idx NonUniqueIndex) Add(id, v string) (string, error) {
func (idx *NonUnique) Add(id, v string) (string, error) {
if v == "" {
return "", nil
}
@@ -112,7 +114,7 @@ func (idx NonUniqueIndex) Add(id, v string) (string, error) {
}
// Remove a value v from an index.
func (idx NonUniqueIndex) Remove(id string, v string) error {
func (idx *NonUnique) Remove(id string, v string) error {
if v == "" {
return nil
}
@@ -144,7 +146,7 @@ func (idx NonUniqueIndex) Remove(id string, v string) error {
}
// Update index from <oldV> to <newV>.
func (idx NonUniqueIndex) Update(id, oldV, newV string) (err error) {
func (idx *NonUnique) Update(id, oldV, newV string) (err error) {
oldDir := path.Join(idx.indexRootDir, oldV)
oldPath := path.Join(oldDir, id)
newDir := path.Join(idx.indexRootDir, newV)
@@ -183,7 +185,7 @@ func (idx NonUniqueIndex) Update(id, oldV, newV string) (err error) {
}
// Search allows for glob search on the index.
func (idx NonUniqueIndex) Search(pattern string) ([]string, error) {
func (idx *NonUnique) Search(pattern string) ([]string, error) {
paths, err := filepath.Glob(path.Join(idx.indexRootDir, pattern, "*"))
if err != nil {
return nil, err
@@ -196,17 +198,22 @@ func (idx NonUniqueIndex) Search(pattern string) ([]string, error) {
return paths, nil
}
// CaseInsensitive undocumented.
func (idx *NonUnique) CaseInsensitive() bool {
return idx.caseInsensitive
}
// IndexBy undocumented.
func (idx NonUniqueIndex) IndexBy() string {
func (idx *NonUnique) IndexBy() string {
return idx.indexBy
}
// TypeName undocumented.
func (idx NonUniqueIndex) TypeName() string {
func (idx *NonUnique) TypeName() string {
return idx.typeName
}
// FilesDir undocumented.
func (idx NonUniqueIndex) FilesDir() string {
func (idx *NonUnique) FilesDir() string {
return idx.filesDir
}
+25 -18
View File
@@ -35,11 +35,12 @@ import (
// }
//
type Unique struct {
indexBy string
typeName string
filesDir string
indexBaseDir string
indexRootDir string
caseInsensitive bool
indexBy string
typeName string
filesDir string
indexBaseDir string
indexRootDir string
}
func init() {
@@ -55,11 +56,12 @@ func NewUniqueIndexWithOptions(o ...option.Option) index.Index {
}
return &Unique{
indexBy: opts.IndexBy,
typeName: opts.TypeName,
filesDir: opts.FilesDir,
indexBaseDir: path.Join(opts.DataDir, "index.disk"),
indexRootDir: path.Join(path.Join(opts.DataDir, "index.disk"), strings.Join([]string{"unique", opts.TypeName, opts.IndexBy}, ".")),
caseInsensitive: opts.CaseInsensitive,
indexBy: opts.IndexBy,
typeName: opts.TypeName,
filesDir: opts.FilesDir,
indexBaseDir: path.Join(opts.DataDir, "index.disk"),
indexRootDir: path.Join(path.Join(opts.DataDir, "index.disk"), strings.Join([]string{"unique", opts.TypeName, opts.IndexBy}, ".")),
}
}
@@ -77,7 +79,7 @@ func (idx *Unique) Init() error {
}
// Add adds a value to the index, returns the path to the root-document
func (idx Unique) Add(id, v string) (string, error) {
func (idx *Unique) Add(id, v string) (string, error) {
if v == "" {
return "", nil
}
@@ -92,7 +94,7 @@ func (idx Unique) Add(id, v string) (string, error) {
}
// Remove a value v from an index.
func (idx Unique) Remove(id string, v string) (err error) {
func (idx *Unique) Remove(id string, v string) (err error) {
if v == "" {
return nil
}
@@ -101,7 +103,7 @@ func (idx Unique) Remove(id string, v string) (err error) {
}
// Lookup exact lookup by value.
func (idx Unique) Lookup(v string) (resultPath []string, err error) {
func (idx *Unique) Lookup(v string) (resultPath []string, err error) {
searchPath := path.Join(idx.indexRootDir, v)
if err = isValidSymlink(searchPath); err != nil {
if os.IsNotExist(err) {
@@ -120,7 +122,7 @@ func (idx Unique) Lookup(v string) (resultPath []string, err error) {
}
// Update index from <oldV> to <newV>.
func (idx Unique) Update(id, oldV, newV string) (err error) {
func (idx *Unique) Update(id, oldV, newV string) (err error) {
oldPath := path.Join(idx.indexRootDir, oldV)
if err = isValidSymlink(oldPath); err != nil {
if os.IsNotExist(err) {
@@ -143,7 +145,7 @@ func (idx Unique) Update(id, oldV, newV string) (err error) {
}
// Search allows for glob search on the index.
func (idx Unique) Search(pattern string) ([]string, error) {
func (idx *Unique) Search(pattern string) ([]string, error) {
paths, err := filepath.Glob(path.Join(idx.indexRootDir, pattern))
if err != nil {
return nil, err
@@ -170,18 +172,23 @@ func (idx Unique) Search(pattern string) ([]string, error) {
return res, nil
}
// CaseInsensitive undocumented.
func (idx *Unique) CaseInsensitive() bool {
return idx.caseInsensitive
}
// IndexBy undocumented.
func (idx Unique) IndexBy() string {
func (idx *Unique) IndexBy() string {
return idx.indexBy
}
// TypeName undocumented.
func (idx Unique) TypeName() string {
func (idx *Unique) TypeName() string {
return idx.typeName
}
// FilesDir undocumented.
func (idx Unique) FilesDir() string {
func (idx *Unique) FilesDir() string {
return idx.filesDir
}
+1
View File
@@ -9,6 +9,7 @@ type Index interface {
Remove(id string, v string) error
Update(id, oldV, newV string) error
Search(pattern string) ([]string, error)
CaseInsensitive() bool
IndexBy() string
TypeName() string
FilesDir() string
+23 -2
View File
@@ -4,6 +4,7 @@ package indexer
import (
"fmt"
"path"
"strings"
"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/accounts/pkg/indexer/errors"
@@ -42,13 +43,14 @@ func getRegistryStrategy(cfg *config.Config) string {
}
// AddIndex adds a new index to the indexer receiver.
func (i Indexer) AddIndex(t interface{}, indexBy, pkName, entityDirName, indexType string, bound *option.Bound) error {
func (i Indexer) AddIndex(t interface{}, indexBy, pkName, entityDirName, indexType string, bound *option.Bound, caseInsensitive bool) error {
strategy := getRegistryStrategy(i.config)
f := registry.IndexConstructorRegistry[strategy][indexType]
var idx index.Index
if strategy == "disk" {
idx = f(
option.CaseInsensitive(caseInsensitive),
option.WithEntity(t),
option.WithBounds(bound),
option.WithTypeName(getTypeFQN(t)),
@@ -58,6 +60,7 @@ func (i Indexer) AddIndex(t interface{}, indexBy, pkName, entityDirName, indexTy
)
} else if strategy == "cs3" {
idx = f(
option.CaseInsensitive(caseInsensitive),
option.WithEntity(t),
option.WithBounds(bound),
option.WithTypeName(getTypeFQN(t)),
@@ -84,6 +87,9 @@ func (i Indexer) Add(t interface{}) ([]IdxAddResult, error) {
for _, idx := range indices {
pkVal := valueOf(t, fields.PKFieldName)
idxByVal := valueOf(t, idx.IndexBy())
if idx.CaseInsensitive() {
idxByVal = strings.ToLower(idxByVal)
}
value, err := idx.Add(pkVal, idxByVal)
if err != nil {
return []IdxAddResult{}, err
@@ -105,7 +111,11 @@ func (i Indexer) FindBy(t interface{}, field string, val string) ([]string, erro
resultPaths := make([]string, 0)
if fields, ok := i.indices[typeName]; ok {
for _, idx := range fields.IndicesByField[field] {
res, err := idx.Lookup(val)
idxVal := val
if idx.CaseInsensitive() {
idxVal = strings.ToLower(idxVal)
}
res, err := idx.Lookup(idxVal)
if err != nil {
if errors.IsNotFoundErr(err) {
continue
@@ -137,6 +147,9 @@ func (i Indexer) Delete(t interface{}) error {
for _, idx := range indices {
pkVal := valueOf(t, fields.PKFieldName)
idxByVal := valueOf(t, idx.IndexBy())
if idx.CaseInsensitive() {
idxByVal = strings.ToLower(idxByVal)
}
if err := idx.Remove(pkVal, idxByVal); err != nil {
return err
}
@@ -153,6 +166,10 @@ func (i Indexer) FindByPartial(t interface{}, field string, pattern string) ([]s
resultPaths := make([]string, 0)
if fields, ok := i.indices[typeName]; ok {
for _, idx := range fields.IndicesByField[field] {
idxPattern := pattern
if idx.CaseInsensitive() {
idxPattern = strings.ToLower(idxPattern)
}
res, err := idx.Search(pattern)
if err != nil {
if errors.IsNotFoundErr(err) {
@@ -195,6 +212,10 @@ func (i Indexer) Update(from, to interface{}) error {
if oldV == newV {
continue
}
if idx.CaseInsensitive() {
oldV = strings.ToLower(oldV)
newV = strings.ToLower(newV)
}
if oldV == "" {
if _, err := idx.Add(pkVal, newV); err != nil {
return err
+11 -11
View File
@@ -21,7 +21,7 @@ func TestIndexer_CS3_AddWithUniqueIndex(t *testing.T) {
assert.NoError(t, err)
indexer := createCs3Indexer()
err = indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil)
err = indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil, false)
assert.NoError(t, err)
u := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"}
@@ -36,7 +36,7 @@ func TestIndexer_CS3_AddWithNonUniqueIndex(t *testing.T) {
assert.NoError(t, err)
indexer := createCs3Indexer()
err = indexer.AddIndex(&User{}, "UserName", "ID", "users", "non_unique", nil)
err = indexer.AddIndex(&User{}, "UserName", "ID", "users", "non_unique", nil, false)
assert.NoError(t, err)
u := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"}
@@ -51,7 +51,7 @@ func TestIndexer_Disk_FindByWithUniqueIndex(t *testing.T) {
assert.NoError(t, err)
indexer := createDiskIndexer(dataDir)
err = indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil)
err = indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil, false)
assert.NoError(t, err)
u := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"}
@@ -70,7 +70,7 @@ func TestIndexer_Disk_AddWithUniqueIndex(t *testing.T) {
assert.NoError(t, err)
indexer := createDiskIndexer(dataDir)
err = indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil)
err = indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil, false)
assert.NoError(t, err)
u := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"}
@@ -85,7 +85,7 @@ func TestIndexer_Disk_AddWithNonUniqueIndex(t *testing.T) {
assert.NoError(t, err)
indexer := createDiskIndexer(dataDir)
err = indexer.AddIndex(&Pet{}, "Kind", "ID", "pets", "non_unique", nil)
err = indexer.AddIndex(&Pet{}, "Kind", "ID", "pets", "non_unique", nil, false)
assert.NoError(t, err)
pet1 := Pet{ID: "goefe-789", Kind: "Hog", Color: "Green", Name: "Dicky"}
@@ -110,7 +110,7 @@ func TestIndexer_Disk_AddWithAutoincrementIndex(t *testing.T) {
assert.NoError(t, err)
indexer := createDiskIndexer(dataDir)
err = indexer.AddIndex(&User{}, "UID", "ID", "users", "autoincrement", &option.Bound{Lower: 5})
err = indexer.AddIndex(&User{}, "UID", "ID", "users", "autoincrement", &option.Bound{Lower: 5}, false)
assert.NoError(t, err)
res1, err := indexer.Add(Data["users"][0])
@@ -136,7 +136,7 @@ func TestIndexer_Disk_DeleteWithNonUniqueIndex(t *testing.T) {
assert.NoError(t, err)
indexer := createDiskIndexer(dataDir)
err = indexer.AddIndex(&Pet{}, "Kind", "ID", "pets", "non_unique", nil)
err = indexer.AddIndex(&Pet{}, "Kind", "ID", "pets", "non_unique", nil, false)
assert.NoError(t, err)
pet1 := Pet{ID: "goefe-789", Kind: "Hog", Color: "Green", Name: "Dicky"}
@@ -159,7 +159,7 @@ func TestIndexer_Disk_SearchWithNonUniqueIndex(t *testing.T) {
assert.NoError(t, err)
indexer := createDiskIndexer(dataDir)
err = indexer.AddIndex(&Pet{}, "Name", "ID", "pets", "non_unique", nil)
err = indexer.AddIndex(&Pet{}, "Name", "ID", "pets", "non_unique", nil, false)
assert.NoError(t, err)
pet1 := Pet{ID: "goefe-789", Kind: "Hog", Color: "Green", Name: "Dicky"}
@@ -183,10 +183,10 @@ func TestIndexer_Disk_UpdateWithUniqueIndex(t *testing.T) {
assert.NoError(t, err)
indexer := createDiskIndexer(dataDir)
err = indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil)
err = indexer.AddIndex(&User{}, "UserName", "ID", "users", "unique", nil, false)
assert.NoError(t, err)
err = indexer.AddIndex(&User{}, "Email", "ID", "users", "unique", nil)
err = indexer.AddIndex(&User{}, "Email", "ID", "users", "unique", nil, false)
assert.NoError(t, err)
user1 := &User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"}
@@ -236,7 +236,7 @@ func TestIndexer_Disk_UpdateWithNonUniqueIndex(t *testing.T) {
assert.NoError(t, err)
indexer := createDiskIndexer(dataDir)
err = indexer.AddIndex(&Pet{}, "Name", "ID", "pets", "non_unique", nil)
err = indexer.AddIndex(&Pet{}, "Name", "ID", "pets", "non_unique", nil, false)
assert.NoError(t, err)
pet1 := Pet{ID: "goefe-789", Kind: "Hog", Color: "Green", Name: "Dicky"}
+10 -1
View File
@@ -12,6 +12,9 @@ type Bound struct {
// Options defines the available options for this package.
type Options struct {
CaseInsensitive bool
Bound *Bound
// Disk Options
TypeName string
IndexBy string
@@ -20,7 +23,6 @@ type Options struct {
DataDir string
EntityDirName string
Entity interface{}
Bound *Bound
// CS3 options
DataURL string
@@ -29,6 +31,13 @@ type Options struct {
ProviderAddr string
}
// CaseInsensitive sets the CaseInsensitive field.
func CaseInsensitive(val bool) Option {
return func(o *Options) {
o.CaseInsensitive = val
}
}
// WithBounds sets the Bounds field.
func WithBounds(val *Bound) Option {
return func(o *Options) {
+8 -9
View File
@@ -75,45 +75,44 @@ func New(opts ...Option) (s *Service, err error) {
}
func (s Service) buildIndex() (*indexer.Indexer, error) {
s.Config.Repo.CS3.JWTSecret = "Pive-Fumkiu4"
idx := indexer.CreateIndexer(s.Config)
// Accounts
if err := idx.AddIndex(&proto.Account{}, "DisplayName", "Id", "accounts", "non_unique", nil); err != nil {
if err := idx.AddIndex(&proto.Account{}, "DisplayName", "Id", "accounts", "non_unique", nil, true); err != nil {
return nil, err
}
if err := idx.AddIndex(&proto.Account{}, "Mail", "Id", "accounts", "unique", nil); err != nil {
if err := idx.AddIndex(&proto.Account{}, "Mail", "Id", "accounts", "unique", nil, true); err != nil {
return nil, err
}
if err := idx.AddIndex(&proto.Account{}, "OnPremisesSamAccountName", "Id", "accounts", "unique", nil); err != nil {
if err := idx.AddIndex(&proto.Account{}, "OnPremisesSamAccountName", "Id", "accounts", "unique", nil, true); err != nil {
return nil, err
}
if err := idx.AddIndex(&proto.Account{}, "PreferredName", "Id", "accounts", "unique", nil); err != nil {
if err := idx.AddIndex(&proto.Account{}, "PreferredName", "Id", "accounts", "unique", nil, true); err != nil {
return nil, err
}
if err := idx.AddIndex(&proto.Account{}, "UidNumber", "Id", "accounts", "autoincrement", &option.Bound{
Lower: s.Config.Index.UID.Lower,
Upper: s.Config.Index.UID.Upper,
}); err != nil {
}, false); err != nil {
return nil, err
}
// Groups
if err := idx.AddIndex(&proto.Group{}, "OnPremisesSamAccountName", "Id", "groups", "unique", nil); err != nil {
if err := idx.AddIndex(&proto.Group{}, "OnPremisesSamAccountName", "Id", "groups", "unique", nil, true); err != nil {
return nil, err
}
if err := idx.AddIndex(&proto.Group{}, "DisplayName", "Id", "groups", "non_unique", nil); err != nil {
if err := idx.AddIndex(&proto.Group{}, "DisplayName", "Id", "groups", "non_unique", nil, true); err != nil {
return nil, err
}
if err := idx.AddIndex(&proto.Group{}, "GidNumber", "Id", "groups", "autoincrement", &option.Bound{
Lower: s.Config.Index.GID.Lower,
Upper: s.Config.Index.GID.Upper,
}); err != nil {
}, false); err != nil {
return nil, err
}