From 887354642c12bb45c13c9fca2dd10b69422a7fa0 Mon Sep 17 00:00:00 2001 From: Sergey Cherepanov Date: Wed, 1 Feb 2023 20:58:20 +0300 Subject: [PATCH] ldiff: Element by id --- app/ldiff/diff.go | 17 ++++++++++- app/ldiff/diff_test.go | 48 ++++++++++++++++++++++++++++++ app/ldiff/mock_ldiff/mock_ldiff.go | 15 ++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/app/ldiff/diff.go b/app/ldiff/diff.go index bd9ebcf2..dd49d980 100644 --- a/app/ldiff/diff.go +++ b/app/ldiff/diff.go @@ -51,7 +51,7 @@ var hashersPool = &sync.Pool{ }, } -var ErrElementNotFound = errors.New("element not found") +var ErrElementNotFound = errors.New("ldiff: element not found") // Element of data type Element struct { @@ -88,6 +88,8 @@ type Diff interface { Diff(ctx context.Context, dl Remote) (newIds, changedIds, removedIds []string, err error) // Elements retrieves all elements in the Diff Elements() []Element + // Element returns an element by id + Element(id string) (Element, error) // Ids retrieves ids of all elements in the Diff Ids() []string // Hash returns hash of all elements in the diff @@ -172,6 +174,19 @@ func (d *diff) Elements() (elements []Element) { return } +func (d *diff) Element(id string) (Element, error) { + d.mu.RLock() + defer d.mu.RUnlock() + el := d.sl.Get(&element{Element: Element{Id: id}, hash: xxhash.Sum64([]byte(id))}) + if el == nil { + return Element{}, ErrElementNotFound + } + if e, ok := el.Key().(*element); ok { + return e.Element, nil + } + return Element{}, ErrElementNotFound +} + func (d *diff) Hash() string { d.mu.RLock() defer d.mu.RUnlock() diff --git a/app/ldiff/diff_test.go b/app/ldiff/diff_test.go index 8d8db28d..682bf1d5 100644 --- a/app/ldiff/diff_test.go +++ b/app/ldiff/diff_test.go @@ -7,6 +7,7 @@ import ( "github.com/stretchr/testify/require" "gopkg.in/mgo.v2/bson" "math" + "sort" "testing" ) @@ -148,3 +149,50 @@ func TestDiff_Hash(t *testing.T) { assert.NotEmpty(t, h2) assert.NotEqual(t, h1, h2) } + +func TestDiff_Element(t *testing.T) { + d := New(16, 16) + for i := 0; i < 10; i++ { + d.Set(Element{Id: fmt.Sprint("id", i), Head: fmt.Sprint("head", i)}) + } + _, err := d.Element("not found") + assert.Equal(t, ErrElementNotFound, err) + + el, err := d.Element("id5") + require.NoError(t, err) + assert.Equal(t, "head5", el.Head) + + d.Set(Element{"id5", "otherHead"}) + el, err = d.Element("id5") + require.NoError(t, err) + assert.Equal(t, "otherHead", el.Head) +} + +func TestDiff_Ids(t *testing.T) { + d := New(16, 16) + var ids []string + for i := 0; i < 10; i++ { + id := fmt.Sprint("id", i) + d.Set(Element{Id: id, Head: fmt.Sprint("head", i)}) + ids = append(ids, id) + } + gotIds := d.Ids() + sort.Strings(gotIds) + assert.Equal(t, ids, gotIds) +} + +func TestDiff_Elements(t *testing.T) { + d := New(16, 16) + var els []Element + for i := 0; i < 10; i++ { + id := fmt.Sprint("id", i) + el := Element{Id: id, Head: fmt.Sprint("head", i)} + d.Set(el) + els = append(els, el) + } + gotEls := d.Elements() + sort.Slice(gotEls, func(i, j int) bool { + return gotEls[i].Id < gotEls[j].Id + }) + assert.Equal(t, els, gotEls) +} diff --git a/app/ldiff/mock_ldiff/mock_ldiff.go b/app/ldiff/mock_ldiff/mock_ldiff.go index 119d7042..328956fa 100644 --- a/app/ldiff/mock_ldiff/mock_ldiff.go +++ b/app/ldiff/mock_ldiff/mock_ldiff.go @@ -52,6 +52,21 @@ func (mr *MockDiffMockRecorder) Diff(arg0, arg1 interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Diff", reflect.TypeOf((*MockDiff)(nil).Diff), arg0, arg1) } +// Element mocks base method. +func (m *MockDiff) Element(arg0 string) (ldiff.Element, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Element", arg0) + ret0, _ := ret[0].(ldiff.Element) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Element indicates an expected call of Element. +func (mr *MockDiffMockRecorder) Element(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Element", reflect.TypeOf((*MockDiff)(nil).Element), arg0) +} + // Elements mocks base method. func (m *MockDiff) Elements() []ldiff.Element { m.ctrl.T.Helper()