store: refactor interface (#11)
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
@@ -24,14 +24,14 @@ func (s *Scope) Options() store.Options {
|
||||
return o
|
||||
}
|
||||
|
||||
func (s *Scope) Read(ctx context.Context, key string, opts ...store.ReadOption) ([]*store.Record, error) {
|
||||
func (s *Scope) Read(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error {
|
||||
key = fmt.Sprintf("%v/%v", s.prefix, key)
|
||||
return s.Store.Read(ctx, key, opts...)
|
||||
return s.Store.Read(ctx, key, val, opts...)
|
||||
}
|
||||
|
||||
func (s *Scope) Write(ctx context.Context, r *store.Record, opts ...store.WriteOption) error {
|
||||
r.Key = fmt.Sprintf("%v/%v", s.prefix, r.Key)
|
||||
return s.Store.Write(ctx, r, opts...)
|
||||
func (s *Scope) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error {
|
||||
key = fmt.Sprintf("%v/%v", s.prefix, key)
|
||||
return s.Store.Write(ctx, key, val, opts...)
|
||||
}
|
||||
|
||||
func (s *Scope) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error {
|
||||
|
@@ -41,16 +41,12 @@ func (c *syncStore) processQueue(index int) {
|
||||
if !ir.expiresAt.IsZero() && time.Now().After(ir.expiresAt) {
|
||||
continue
|
||||
}
|
||||
nr := &store.Record{
|
||||
Key: ir.key,
|
||||
}
|
||||
nr.Value = make([]byte, len(ir.value))
|
||||
copy(nr.Value, ir.value)
|
||||
var opts []store.WriteOption
|
||||
if !ir.expiresAt.IsZero() {
|
||||
nr.Expiry = time.Until(ir.expiresAt)
|
||||
opts = append(opts, store.WriteTTL(ir.expiresAt.Sub(time.Now())))
|
||||
}
|
||||
// Todo = internal queue also has to hold the corresponding store.WriteOptions
|
||||
if err := c.syncOpts.Stores[index+1].Write(c.storeOpts.Context, nr); err != nil {
|
||||
if err := c.syncOpts.Stores[index+1].Write(c.storeOpts.Context, ir.key, ir.value, opts...); err != nil {
|
||||
// some error, so queue for retry and bail
|
||||
q.PushBack(ir)
|
||||
return
|
||||
|
@@ -96,12 +96,16 @@ func (c *syncStore) List(ctx context.Context, opts ...store.ListOption) ([]strin
|
||||
return c.syncOpts.Stores[0].List(ctx, opts...)
|
||||
}
|
||||
|
||||
func (c *syncStore) Read(ctx context.Context, key string, opts ...store.ReadOption) ([]*store.Record, error) {
|
||||
return c.syncOpts.Stores[0].Read(ctx, key, opts...)
|
||||
func (c *syncStore) Exists(ctx context.Context, key string) error {
|
||||
return c.syncOpts.Stores[0].Exists(ctx, key)
|
||||
}
|
||||
|
||||
func (c *syncStore) Write(ctx context.Context, r *store.Record, opts ...store.WriteOption) error {
|
||||
return c.syncOpts.Stores[0].Write(ctx, r, opts...)
|
||||
func (c *syncStore) Read(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error {
|
||||
return c.syncOpts.Stores[0].Read(ctx, key, val, opts...)
|
||||
}
|
||||
|
||||
func (c *syncStore) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error {
|
||||
return c.syncOpts.Stores[0].Write(ctx, key, val, opts...)
|
||||
}
|
||||
|
||||
// Delete removes a key from the sync
|
||||
|
@@ -47,11 +47,7 @@ func (b *Basic) Generate(acc *auth.Account, opts ...token.GenerateOption) (*toke
|
||||
|
||||
// write to the store
|
||||
key := uuid.New().String()
|
||||
err = b.store.Write(context.Background(), &store.Record{
|
||||
Key: fmt.Sprintf("%v%v", StorePrefix, key),
|
||||
Value: bytes,
|
||||
Expiry: options.Expiry,
|
||||
})
|
||||
err = b.store.Write(context.Background(), fmt.Sprintf("%v%v", StorePrefix, key), bytes, store.WriteTTL(options.Expiry))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -67,17 +63,16 @@ func (b *Basic) Generate(acc *auth.Account, opts ...token.GenerateOption) (*toke
|
||||
// Inspect a token
|
||||
func (b *Basic) Inspect(t string) (*auth.Account, error) {
|
||||
// lookup the token in the store
|
||||
recs, err := b.store.Read(context.Background(), StorePrefix+t)
|
||||
var val []byte
|
||||
err := b.store.Read(context.Background(), StorePrefix+t, val)
|
||||
if err == store.ErrNotFound {
|
||||
return nil, token.ErrInvalidToken
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bytes := recs[0].Value
|
||||
|
||||
// unmarshal the bytes
|
||||
var acc *auth.Account
|
||||
if err := json.Unmarshal(bytes, &acc); err != nil {
|
||||
if err := json.Unmarshal(val, &acc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user