diff --git a/dstore/events.go b/dstore/events.go index a00d839..b7b7912 100644 --- a/dstore/events.go +++ b/dstore/events.go @@ -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) @@ -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. diff --git a/dstore/events/events.go b/dstore/events/events.go index 9d1f3ef..3029b66 100644 --- a/dstore/events/events.go +++ b/dstore/events/events.go @@ -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. diff --git a/dstore/events/events_test.go b/dstore/events/events_test.go index f9724ed..a33d573 100644 --- a/dstore/events/events_test.go +++ b/dstore/events/events_test.go @@ -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) }