fix linter

This commit is contained in:
A.Unger
2020-10-07 16:10:44 +02:00
committed by Ilja Neumann
parent 9d285e2ec5
commit 815d07f71c
11 changed files with 88 additions and 108 deletions

View File

@@ -4,6 +4,7 @@ import (
"fmt"
)
// AlreadyExistsErr implements the Error interface.
type AlreadyExistsErr struct {
TypeName, Key, Value string
}
@@ -12,11 +13,13 @@ func (e *AlreadyExistsErr) Error() string {
return fmt.Sprintf("%s with %s=%s does already exist", e.TypeName, e.Key, e.Value)
}
// IsAlreadyExistsErr checks whether an error is of type AlreadyExistsErr.
func IsAlreadyExistsErr(e error) bool {
_, ok := e.(*AlreadyExistsErr)
return ok
}
// NotFoundErr implements the Error interface.
type NotFoundErr struct {
TypeName, Key, Value string
}
@@ -25,6 +28,7 @@ func (e *NotFoundErr) Error() string {
return fmt.Sprintf("%s with %s=%s not found", e.TypeName, e.Key, e.Value)
}
// IsNotFoundErr checks whether an error is of type IsNotFoundErr.
func IsNotFoundErr(e error) bool {
_, ok := e.(*NotFoundErr)
return ok

View File

@@ -26,6 +26,7 @@ func init() {
registry.IndexConstructorRegistry["cs3"]["non_unique"] = NewNonUniqueIndexWithOptions
}
// NonUnique are fields for an index of type non_unique.
type NonUnique struct {
indexBy string
typeName string
@@ -40,8 +41,15 @@ type NonUnique struct {
cs3conf *Config
}
// NewNonUniqueIndexWithOptions instantiates a new UniqueIndex instance. Init() should be
// called afterward to ensure correct on-disk structure.
// NewNonUniqueIndexWithOptions instantiates a new NonUniqueIndex instance.
// /var/tmp/ocis-accounts/index.cs3/Pets/Bro*
// ├── Brown/
// │ └── rebef-123 -> /var/tmp/testfiles-395764020/pets/rebef-123
// ├── Green/
// │ ├── goefe-789 -> /var/tmp/testfiles-395764020/pets/goefe-789
// │ └── xadaf-189 -> /var/tmp/testfiles-395764020/pets/xadaf-189
// └── White/
// └── wefwe-456 -> /var/tmp/testfiles-395764020/pets/wefwe-456
func NewNonUniqueIndexWithOptions(o ...option.Option) index.Index {
opts := &option.Options{}
for _, opt := range o {
@@ -71,32 +79,7 @@ func NewNonUniqueIndexWithOptions(o ...option.Option) index.Index {
}
}
// NewNonUniqueIndex instantiates a new NonUniqueIndex instance.
// /var/tmp/ocis-accounts/index.cs3/Pets/Bro*
// ├── Brown/
// │ └── rebef-123 -> /var/tmp/testfiles-395764020/pets/rebef-123
// ├── Green/
// │ ├── goefe-789 -> /var/tmp/testfiles-395764020/pets/goefe-789
// │ └── xadaf-189 -> /var/tmp/testfiles-395764020/pets/xadaf-189
// └── White/
// └── wefwe-456 -> /var/tmp/testfiles-395764020/pets/wefwe-456
func NewNonUniqueIndex(typeName, indexBy, filesDir, indexBaseDir string, cfg *Config) NonUnique {
return NonUnique{
indexBy: indexBy,
typeName: typeName,
filesDir: filesDir,
indexBaseDir: indexBaseDir,
indexRootDir: path.Join(indexBaseDir, strings.Join([]string{"non_unique", typeName, indexBy}, ".")),
cs3conf: cfg,
dataProvider: dataProviderClient{
baseURL: singleJoiningSlash(cfg.DataURL, cfg.DataPrefix),
client: http.Client{
Transport: http.DefaultTransport,
},
},
}
}
// Init initializes a non_unique index.
func (idx *NonUnique) Init() error {
tokenManager, err := jwt.New(map[string]interface{}{
"secret": idx.cs3conf.JWTSecret,
@@ -133,6 +116,7 @@ func (idx *NonUnique) Init() error {
return nil
}
// Lookup exact lookup by value.
func (idx *NonUnique) Lookup(v string) ([]string, error) {
var matches = make([]string, 0)
ctx, err := idx.getAuthenticatedContext(context.Background())
@@ -157,6 +141,7 @@ func (idx *NonUnique) Lookup(v string) ([]string, error) {
return matches, nil
}
// Add a new value to the index.
func (idx *NonUnique) Add(id, v string) (string, error) {
ctx, err := idx.getAuthenticatedContext(context.Background())
if err != nil {
@@ -179,6 +164,7 @@ func (idx *NonUnique) Add(id, v string) (string, error) {
return newName, nil
}
// Remove a value v from an index.
func (idx *NonUnique) Remove(id string, v string) error {
ctx, err := idx.getAuthenticatedContext(context.Background())
if err != nil {
@@ -225,6 +211,7 @@ func (idx *NonUnique) Remove(id string, v string) error {
return nil
}
// Update index from <oldV> to <newV>.
func (idx *NonUnique) Update(id, oldV, newV string) error {
if err := idx.Remove(id, oldV); err != nil {
return err
@@ -237,6 +224,7 @@ func (idx *NonUnique) Update(id, oldV, newV string) error {
return nil
}
// Search allows for glob search on the index.
func (idx *NonUnique) Search(pattern string) ([]string, error) {
ctx, err := idx.getAuthenticatedContext(context.Background())
if err != nil {
@@ -280,14 +268,17 @@ func (idx *NonUnique) Search(pattern string) ([]string, error) {
return matches, nil
}
// IndexBy undocumented.
func (idx *NonUnique) IndexBy() string {
return idx.indexBy
}
// TypeName undocumented.
func (idx *NonUnique) TypeName() string {
return idx.typeName
}
// FilesDir undocumented.
func (idx *NonUnique) FilesDir() string {
return idx.filesDir
}

View File

@@ -22,6 +22,7 @@ import (
"strings"
)
// Unique are fields for an index of type non_unique.
type Unique struct {
indexBy string
typeName string
@@ -36,6 +37,7 @@ type Unique struct {
cs3conf *Config
}
// Config represents cs3conf. Should be deprecated in favor of config.Config.
type Config struct {
ProviderAddr string
DataURL string
@@ -82,25 +84,7 @@ func NewUniqueIndexWithOptions(o ...option.Option) index.Index {
return u
}
// NewUniqueIndex instantiates a new UniqueIndex instance. Init() should be
// called afterward to ensure correct on-disk structure.
func NewUniqueIndex(typeName, indexBy, filesDir, indexBaseDir string, cfg *Config) Unique {
return Unique{
indexBy: indexBy,
typeName: typeName,
filesDir: filesDir,
indexBaseDir: indexBaseDir,
indexRootDir: path.Join(indexBaseDir, strings.Join([]string{"unique", typeName, indexBy}, ".")),
cs3conf: cfg,
dataProvider: dataProviderClient{
baseURL: singleJoiningSlash(cfg.DataURL, cfg.DataPrefix),
client: http.Client{
Transport: http.DefaultTransport,
},
},
}
}
// Init initializes a unique index.
func (idx *Unique) Init() error {
tokenManager, err := jwt.New(map[string]interface{}{
"secret": idx.cs3conf.JWTSecret,
@@ -151,6 +135,7 @@ func (idx *Unique) Add(id, v string) (string, error) {
return newName, nil
}
// Lookup exact lookup by value.
func (idx *Unique) Lookup(v string) ([]string, error) {
searchPath := path.Join(idx.indexRootDir, v)
oldname, err := idx.resolveSymlink(searchPath)
@@ -165,7 +150,7 @@ func (idx *Unique) Lookup(v string) ([]string, error) {
return []string{oldname}, nil
}
// 97d28b57
// Remove a value v from an index.
func (idx *Unique) Remove(id string, v string) error {
searchPath := path.Join(idx.indexRootDir, v)
_, err := idx.resolveSymlink(searchPath)
@@ -203,6 +188,7 @@ func (idx *Unique) Remove(id string, v string) error {
return err
}
// Update index from <oldV> to <newV>.
func (idx *Unique) Update(id, oldV, newV string) error {
if err := idx.Remove(id, oldV); err != nil {
return err
@@ -215,6 +201,7 @@ func (idx *Unique) Update(id, oldV, newV string) error {
return nil
}
// Search allows for glob search on the index.
func (idx *Unique) Search(pattern string) ([]string, error) {
ctx := context.Background()
t, err := idx.authenticate(ctx)
@@ -253,14 +240,17 @@ func (idx *Unique) Search(pattern string) ([]string, error) {
}
// IndexBy undocumented.
func (idx *Unique) IndexBy() string {
return idx.indexBy
}
// TypeName undocumented.
func (idx *Unique) TypeName() string {
return idx.typeName
}
// FilesDir undocumented.
func (idx *Unique) FilesDir() string {
return idx.filesDir
}

View File

@@ -2,7 +2,6 @@ package disk
import (
"errors"
"fmt"
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"
@@ -53,18 +52,7 @@ func NewNonUniqueIndexWithOptions(o ...option.Option) index.Index {
}
}
// NewNonUniqueIndex instantiates a new NonUniqueIndex instance. Init() should be
// called afterward to ensure correct on-disk structure.
func NewNonUniqueIndex(typeName, indexBy, filesDir, indexBaseDir string) NonUniqueIndex {
return NonUniqueIndex{
indexBy: indexBy,
typeName: typeName,
filesDir: filesDir,
indexBaseDir: indexBaseDir,
indexRootDir: path.Join(indexBaseDir, fmt.Sprintf("%sBy%s", typeName, indexBy)),
}
}
// Init initializes a unique index.
func (idx NonUniqueIndex) Init() error {
if _, err := os.Stat(idx.filesDir); err != nil {
return err
@@ -77,6 +65,7 @@ func (idx NonUniqueIndex) Init() error {
return nil
}
// Lookup exact lookup by value.
func (idx NonUniqueIndex) Lookup(v string) ([]string, error) {
searchPath := path.Join(idx.indexRootDir, v)
fi, err := ioutil.ReadDir(searchPath)
@@ -100,6 +89,7 @@ func (idx NonUniqueIndex) Lookup(v string) ([]string, error) {
return ids, nil
}
// Add adds a value to the index, returns the path to the root-document
func (idx NonUniqueIndex) Add(id, v string) (string, error) {
oldName := path.Join(idx.filesDir, id)
newName := path.Join(idx.indexRootDir, v, id)
@@ -117,6 +107,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 {
res, err := filepath.Glob(path.Join(idx.indexRootDir, "/*/", id))
if err != nil {
@@ -132,6 +123,7 @@ func (idx NonUniqueIndex) Remove(id string, v string) error {
return nil
}
// Update index from <oldV> to <newV>.
func (idx NonUniqueIndex) Update(id, oldV, newV string) (err error) {
oldDir := path.Join(idx.indexRootDir, oldV)
oldPath := path.Join(oldDir, id)
@@ -170,6 +162,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) {
paths, err := filepath.Glob(path.Join(idx.indexRootDir, pattern, "*"))
if err != nil {
@@ -183,14 +176,17 @@ func (idx NonUniqueIndex) Search(pattern string) ([]string, error) {
return paths, nil
}
// IndexBy undocumented.
func (idx NonUniqueIndex) IndexBy() string {
return idx.indexBy
}
// TypeName undocumented.
func (idx NonUniqueIndex) TypeName() string {
return idx.typeName
}
// FilesDir undocumented.
func (idx NonUniqueIndex) FilesDir() string {
return idx.filesDir
}

View File

@@ -62,18 +62,7 @@ func NewUniqueIndexWithOptions(o ...option.Option) index.Index {
}
}
// NewUniqueIndex instantiates a new UniqueIndex instance. Init() should be
// called afterward to ensure correct on-disk structure.
func NewUniqueIndex(typeName, indexBy, filesDir, indexBaseDir string) Unique {
return Unique{
indexBy: indexBy,
typeName: typeName,
filesDir: filesDir,
indexBaseDir: indexBaseDir,
indexRootDir: path.Join(indexBaseDir, strings.Join([]string{"unique", typeName, indexBy}, ".")),
}
}
// Init initializes a unique index.
func (idx *Unique) Init() error {
if _, err := os.Stat(idx.filesDir); err != nil {
return err
@@ -86,6 +75,7 @@ func (idx *Unique) Init() error {
return nil
}
// Add adds a value to the index, returns the path to the root-document
func (idx Unique) Add(id, v string) (string, error) {
oldName := path.Join(idx.filesDir, id)
newName := path.Join(idx.indexRootDir, v)
@@ -97,6 +87,7 @@ func (idx Unique) Add(id, v string) (string, error) {
return newName, err
}
// Remove a value v from an index.
func (idx Unique) Remove(id string, v string) (err error) {
searchPath := path.Join(idx.indexRootDir, v)
if err = isValidSymlink(searchPath); err != nil {
@@ -106,8 +97,7 @@ func (idx Unique) Remove(id string, v string) (err error) {
return os.Remove(searchPath)
}
// unique.github.com.owncloud.ocis.accounts.pkg.indexer.User.UserName
// unique.github.com.owncloud.ocis.accounts.pkg.indexer.User.UserName/UserName
// Lookup exact lookup by value.
func (idx Unique) Lookup(v string) (resultPath []string, err error) {
searchPath := path.Join(idx.indexRootDir, v)
if err = isValidSymlink(searchPath); err != nil {
@@ -127,6 +117,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) {
oldPath := path.Join(idx.indexRootDir, oldV)
if err = isValidSymlink(oldPath); err != nil {
@@ -149,6 +140,7 @@ func (idx Unique) Update(id, oldV, newV string) (err error) {
return
}
// Search allows for glob search on the index.
func (idx Unique) Search(pattern string) ([]string, error) {
paths, err := filepath.Glob(path.Join(idx.indexRootDir, pattern))
if err != nil {
@@ -176,14 +168,17 @@ func (idx Unique) Search(pattern string) ([]string, error) {
return res, nil
}
// IndexBy undocumented.
func (idx Unique) IndexBy() string {
return idx.indexBy
}
// TypeName undocumented.
func (idx Unique) TypeName() string {
return idx.typeName
}
// FilesDir undocumented.
func (idx Unique) FilesDir() string {
return idx.filesDir
}

View File

@@ -8,37 +8,23 @@ import (
"github.com/owncloud/ocis/accounts/pkg/indexer/index"
"github.com/owncloud/ocis/accounts/pkg/indexer/option"
"github.com/owncloud/ocis/accounts/pkg/indexer/registry"
"github.com/rs/zerolog"
"path"
)
// Indexer is a facade to configure and query over multiple indices.
type Indexer struct {
repoConfig *config.Config
config *Config
indices typeMap
config *config.Config
indices typeMap
}
type Config struct {
DataDir string
IndexRootDirName string
Log zerolog.Logger
}
func NewIndexer(cfg *Config) *Indexer {
// CreateIndexer creates a new Indexer.
func CreateIndexer(cfg *config.Config) *Indexer {
return &Indexer{
config: cfg,
indices: typeMap{},
}
}
func CreateIndexer(cfg *config.Config) *Indexer {
return &Indexer{
repoConfig: cfg,
indices: typeMap{},
}
}
func getRegistryStrategy(cfg *config.Config) string {
if cfg.Repo.Disk.Path != "" {
return "disk"
@@ -47,8 +33,9 @@ func getRegistryStrategy(cfg *config.Config) string {
return "cs3"
}
// AddIndex adds a new index to the indexer receiver.
func (i Indexer) AddIndex(t interface{}, indexBy, pkName, entityDirName, indexType string) error {
strategy := getRegistryStrategy(i.repoConfig)
strategy := getRegistryStrategy(i.config)
f := registry.IndexConstructorRegistry[strategy][indexType]
var idx index.Index
@@ -56,19 +43,19 @@ func (i Indexer) AddIndex(t interface{}, indexBy, pkName, entityDirName, indexTy
idx = f(
option.WithTypeName(getTypeFQN(t)),
option.WithIndexBy(indexBy),
option.WithFilesDir(path.Join(i.repoConfig.Repo.Disk.Path, entityDirName)),
option.WithDataDir(i.repoConfig.Repo.Disk.Path),
option.WithFilesDir(path.Join(i.config.Repo.Disk.Path, entityDirName)),
option.WithDataDir(i.config.Repo.Disk.Path),
)
} else if strategy == "cs3" {
idx = f(
option.WithTypeName(getTypeFQN(t)),
option.WithIndexBy(indexBy),
option.WithFilesDir(path.Join(i.repoConfig.Repo.Disk.Path, entityDirName)),
option.WithDataDir(i.repoConfig.Repo.Disk.Path),
option.WithDataURL(i.repoConfig.Repo.CS3.DataURL),
option.WithDataPrefix(i.repoConfig.Repo.CS3.DataPrefix),
option.WithJWTSecret(i.repoConfig.Repo.CS3.JWTSecret),
option.WithProviderAddr(i.repoConfig.Repo.CS3.ProviderAddr),
option.WithFilesDir(path.Join(i.config.Repo.Disk.Path, entityDirName)),
option.WithDataDir(i.config.Repo.Disk.Path),
option.WithDataURL(i.config.Repo.CS3.DataURL),
option.WithDataPrefix(i.config.Repo.CS3.DataPrefix),
option.WithJWTSecret(i.config.Repo.CS3.JWTSecret),
option.WithProviderAddr(i.config.Repo.CS3.ProviderAddr),
)
}
@@ -94,6 +81,7 @@ func (i Indexer) Add(t interface{}) error {
return nil
}
// FindBy finds a value on an index by field and value.
func (i Indexer) FindBy(t interface{}, field string, val string) ([]string, error) {
typeName := getTypeFQN(t)
resultPaths := make([]string, 0)
@@ -123,6 +111,7 @@ func (i Indexer) FindBy(t interface{}, field string, val string) ([]string, erro
return result, nil
}
// Delete deletes all indexed fields of a given type t on the Indexer.
func (i Indexer) Delete(t interface{}) error {
typeName := getTypeFQN(t)
if fields, ok := i.indices[typeName]; ok {
@@ -140,6 +129,7 @@ func (i Indexer) Delete(t interface{}) error {
return nil
}
// FindByPartial allows for glob search across all indexes.
func (i Indexer) FindByPartial(t interface{}, field string, pattern string) ([]string, error) {
typeName := getTypeFQN(t)
resultPaths := make([]string, 0)
@@ -170,6 +160,7 @@ func (i Indexer) FindByPartial(t interface{}, field string, pattern string) ([]s
}
// Update updates all indexes on a value <from> to a value <to>.
func (i Indexer) Update(from, to interface{}) error {
typeNameFrom := getTypeFQN(from)
typeNameTo := getTypeFQN(to)

View File

@@ -20,59 +20,63 @@ type Options struct {
ProviderAddr string
}
// WithJWTSecret sets the JWTSecret field.
func WithJWTSecret(val string) Option {
return func(o *Options) {
o.JWTSecret = val
}
}
// WithDataURL sets the DataURl field.
func WithDataURL(val string) Option {
return func(o *Options) {
o.DataURL = val
}
}
// WithDataPrefix sets the DataPrefix field.
func WithDataPrefix(val string) Option {
return func(o *Options) {
o.DataPrefix = val
}
}
// WithEntityDirName sets the EntityDirName field.
func WithEntityDirName(val string) Option {
return func(o *Options) {
o.EntityDirName = val
}
}
// WithDataDir sets the DataDir option.
func WithDataDir(val string) Option {
return func(o *Options) {
o.DataDir = val
}
}
// WithTypeName sets the TypeName option.
func WithTypeName(val string) Option {
return func(o *Options) {
o.TypeName = val
}
}
// WithIndexBy sets the option IndexBy
func WithIndexBy(val string) Option {
return func(o *Options) {
o.IndexBy = val
}
}
func WithIndexBaseDir(val string) Option {
return func(o *Options) {
o.IndexBaseDir = val
}
}
// WithFilesDir sets the option FilesDir
func WithFilesDir(val string) Option {
return func(o *Options) {
o.FilesDir = val
}
}
// WithProviderAddr sets the option ProviderAddr
func WithProviderAddr(val string) Option {
return func(o *Options) {
o.ProviderAddr = val

View File

@@ -5,6 +5,7 @@ import (
"github.com/owncloud/ocis/accounts/pkg/indexer/option"
)
// IndexConstructor is a constructor function for creating index.Index.
type IndexConstructor func(o ...option.Option) index.Index
// IndexConstructorRegistry undocumented.

View File

@@ -8,14 +8,17 @@ import (
"testing"
)
// TestUser is a user.
type TestUser struct {
Id, UserName, Email string
}
// TestPet is a pet.
type TestPet struct {
Id, Kind, Color, Name string
}
// TestData mock data.
var TestData = map[string][]interface{}{
"users": {
TestUser{Id: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"},
@@ -31,6 +34,7 @@ var TestData = map[string][]interface{}{
},
}
// WriteIndexTestData writes mock data to disk.
func WriteIndexTestData(t *testing.T, m map[string][]interface{}, pk string) string {
rootDir := CreateTmpDir(t)
for dirName := range m {
@@ -55,6 +59,7 @@ func WriteIndexTestData(t *testing.T, m map[string][]interface{}, pk string) str
return rootDir
}
// WriteIndexTestDataCS3 writes more data to disk.
func WriteIndexTestDataCS3(t *testing.T, m map[string][]interface{}, pk string) string {
rootDir := "/var/tmp/ocis/storage/users/data"
for dirName := range m {

View File

@@ -22,8 +22,10 @@ func newAccountOptions(opts ...account.Option) account.Options {
// AccountID serves as key for the account uuid in the context
const AccountID string = "Account-Id"
// RoleIDs serves as key for the roles in the context
const RoleIDs string = "Role-Ids"
// UUIDKey serves as key for the account uuid in the context
// Deprecated: UUIDKey exists for compatibility reasons. Use AccountID instead.
var UUIDKey struct{}

View File

@@ -1,4 +1,5 @@
package external
//
//import (
// "context"