ldiff: Element by id
This commit is contained in:
parent
94696cc44e
commit
887354642c
@ -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()
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user