Skip to content

Commit

Permalink
dstore/events: Increment (value, index)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel committed Mar 31, 2021
1 parent 0b24b65 commit 474087d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
10 changes: 5 additions & 5 deletions dstore/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ func (m *Mem) EventsAdd(ctx context.Context, path string, data [][]byte) ([]*eve
return out, idx, nil
}

func (m *Mem) Increment(ctx context.Context, path string, name string, n int64) (int64, error) {
func (m *Mem) Increment(ctx context.Context, path string, name string, n int64) (int64, int64, error) {
doc, err := m.Get(ctx, path)
if err != nil {
return 0, err
return 0, 0, err
}
val := int64(0)
if doc != nil {
v, ok := doc.Get(name)
if err != nil {
return 0, err
return 0, 0, err
}
if !ok {
val = int64(0)
Expand All @@ -67,9 +67,9 @@ func (m *Mem) Increment(ctx context.Context, path string, name string, n int64)

next := val + n
if err := m.Set(ctx, path, map[string]interface{}{name: next}, MergeAll()); err != nil {
return 0, err
return 0, 0, err
}
return next, nil
return next, next - n + 1, nil
}

// EventPositions returns positions for event logs at the specified paths.
Expand Down
3 changes: 2 additions & 1 deletion dstore/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ type Events interface {
EventPositions(ctx context.Context, paths []string) (map[string]*Position, error)

// Increment document name at path n amount.
Increment(ctx context.Context, path string, name string, n int64) (int64, error)
// Returns the new value and the start of the index.
Increment(ctx context.Context, path string, name string, n int64) (int64, int64, error)
}

// Direction is ascending or descending.
Expand Down
6 changes: 4 additions & 2 deletions dstore/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,13 @@ func TestIncrement(t *testing.T) {
clock := tsutil.NewTestClock()
eds.SetClock(clock)

n, err := eds.Increment(context.TODO(), "/test/doc1", "count", 1)
n, i, err := eds.Increment(context.TODO(), "/test/doc1", "count", 1)
require.NoError(t, err)
require.Equal(t, int64(1), n)
require.Equal(t, int64(1), i)

n, err = eds.Increment(context.TODO(), "/test/doc1", "count", 5)
n, i, err = eds.Increment(context.TODO(), "/test/doc1", "count", 5)
require.NoError(t, err)
require.Equal(t, int64(6), n)
require.Equal(t, int64(2), i)
}

0 comments on commit 474087d

Please sign in to comment.