-
Notifications
You must be signed in to change notification settings - Fork 156
/
repo.go
64 lines (56 loc) · 2.24 KB
/
repo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package om
import (
"context"
"errors"
"time"
"github.com/redis/rueidis"
"github.com/redis/rueidis/internal/cmds"
)
type (
// FtCreateSchema is the FT.CREATE command builder
FtCreateSchema = cmds.FtCreateSchema
// FtSearchIndex is the FT.SEARCH command builder
FtSearchIndex = cmds.FtSearchIndex
// FtAggregateIndex is the FT.AGGREGATE command builder
FtAggregateIndex = cmds.FtAggregateIndex
// FtAlterSchema is the FT.ALTERINDEX command builder
FtAlterIndex = cmds.FtAlterIndex
// Arbitrary is alias to cmds.Arbitrary. This allows user build arbitrary command in Repository.CreateIndex
Arbitrary = cmds.Arbitrary
)
var (
// ErrVersionMismatch indicates that the optimistic update failed. That is someone else had already changed the entity.
ErrVersionMismatch = errors.New("object version mismatched, please retry")
// ErrEmptyHashRecord indicates the requested hash entity is not found.
ErrEmptyHashRecord = errors.New("hash object not found")
)
// IsRecordNotFound checks if the error is indicating the requested entity is not found.
func IsRecordNotFound(err error) bool {
return rueidis.IsRedisNil(err) || err == ErrEmptyHashRecord
}
// Repository is backed by HashRepository or JSONRepository
type Repository[T any] interface {
NewEntity() (entity *T)
Fetch(ctx context.Context, id string) (*T, error)
FetchCache(ctx context.Context, id string, ttl time.Duration) (v *T, err error)
Search(ctx context.Context, cmdFn func(search FtSearchIndex) rueidis.Completed) (int64, []*T, error)
Aggregate(ctx context.Context, cmdFn func(agg FtAggregateIndex) rueidis.Completed) (*AggregateCursor, error)
Save(ctx context.Context, entity *T) (err error)
SaveMulti(ctx context.Context, entity ...*T) (errs []error)
Remove(ctx context.Context, id string) error
CreateIndex(ctx context.Context, cmdFn func(schema FtCreateSchema) rueidis.Completed) error
AlterIndex(ctx context.Context, cmdFn func(alter FtAlterIndex) rueidis.Completed) error
DropIndex(ctx context.Context) error
IndexName() string
}
type RepositoryOption func(Repository[any])
func WithIndexName(name string) RepositoryOption {
return func(r Repository[any]) {
switch repo := r.(type) {
case *HashRepository[any]:
repo.idx = name
case *JSONRepository[any]:
repo.idx = name
}
}
}