diff --git a/store/pogreb/pogreb.go b/store/pogreb/pogreb.go index 2311f84..bed75cd 100644 --- a/store/pogreb/pogreb.go +++ b/store/pogreb/pogreb.go @@ -41,7 +41,7 @@ type pStorage struct { store *pogreb.DB mlk *keymutex.KeyMutex valLock sync.RWMutex - vserde indexer.ValueSerde + vcodec indexer.ValueCodec } type pogrebIter struct { @@ -52,12 +52,12 @@ type pogrebIter struct { // New creates a new indexer.Interface implemented by a pogreb-based value // store. // -// The given indexer.ValueSerde is used to serialize and deserialize values. -// If it is set to nil, indexer.JsonValueSerde is used. -func New(dir string, vserde indexer.ValueSerde) (indexer.Interface, error) { +// The given indexer.ValueCodec is used to serialize and deserialize values. +// If it is set to nil, indexer.JsonValueCodec is used. +func New(dir string, vcodec indexer.ValueCodec) (indexer.Interface, error) { opts := pogreb.Options{BackgroundSyncInterval: DefaultSyncInterval} - if vserde == nil { - vserde = indexer.JsonValueSerde{} + if vcodec == nil { + vcodec = indexer.JsonValueCodec{} } s, err := pogreb.Open(dir, &opts) if err != nil { @@ -67,7 +67,7 @@ func New(dir string, vserde indexer.ValueSerde) (indexer.Interface, error) { dir: dir, store: s, mlk: keymutex.New(0), - vserde: vserde, + vcodec: vcodec, }, nil } @@ -134,7 +134,7 @@ func (s *pStorage) RemoveProvider(ctx context.Context, providerID peer.ID) error // If a value was found, skip it if the provider is different than the // one being removed. if valueData != nil { - value, err := s.vserde.UnmarshalValue(valueData) + value, err := s.vcodec.UnmarshalValue(valueData) if err != nil { return err } @@ -221,7 +221,7 @@ func (it *pogrebIter) Next() (multihash.Multihash, []indexer.Value, error) { continue } - valueKeys, err := it.s.vserde.UnmarshalValueKeys(valKeysData) + valueKeys, err := it.s.vcodec.UnmarshalValueKeys(valKeysData) if err != nil { return nil, nil, err } @@ -248,7 +248,7 @@ func (s *pStorage) getValueKeys(k []byte) ([][]byte, error) { return nil, nil } - return s.vserde.UnmarshalValueKeys(valueKeysData) + return s.vcodec.UnmarshalValueKeys(valueKeysData) } func (s *pStorage) get(k []byte) ([]indexer.Value, bool, error) { @@ -292,7 +292,7 @@ func (s *pStorage) putIndex(m multihash.Multihash, valKey []byte) error { } // Store the new list of value keys for the multihash. - b, err := s.vserde.MarshalValueKeys(append(existingValKeys, valKey)) + b, err := s.vcodec.MarshalValueKeys(append(existingValKeys, valKey)) if err != nil { return err } @@ -328,7 +328,7 @@ func (s *pStorage) removeIndex(m multihash.Multihash, value indexer.Value) error valueKeys[len(valueKeys)-1] = nil valueKeys = valueKeys[:len(valueKeys)-1] // Update the list of value-keys that the multihash maps to. - b, err := s.vserde.MarshalValueKeys(valueKeys) + b, err := s.vcodec.MarshalValueKeys(valueKeys) if err != nil { return err } @@ -358,7 +358,7 @@ func (s *pStorage) updateValue(value indexer.Value, saveNew bool) ([]byte, error if valData == nil { if saveNew { // Store the new value. - valData, err := s.vserde.MarshalValue(value) + valData, err := s.vcodec.MarshalValue(value) if err != nil { return nil, err } @@ -371,7 +371,7 @@ func (s *pStorage) updateValue(value indexer.Value, saveNew bool) ([]byte, error } // Found previous value. If it is different, then update it. - newValData, err := s.vserde.MarshalValue(value) + newValData, err := s.vcodec.MarshalValue(value) if err != nil { return nil, err } @@ -413,7 +413,7 @@ func (s *pStorage) getValues(key []byte, valueKeys [][]byte) ([]indexer.Value, e valueKeys = valueKeys[:len(valueKeys)-1] continue } - val, err := s.vserde.UnmarshalValue(valData) + val, err := s.vcodec.UnmarshalValue(valData) if err != nil { s.valLock.RUnlock() return nil, err @@ -438,7 +438,7 @@ func (s *pStorage) getValues(key []byte, valueKeys [][]byte) ([]indexer.Value, e } // Update the values this multihash maps to. - b, err := s.vserde.MarshalValueKeys(valueKeys) + b, err := s.vcodec.MarshalValueKeys(valueKeys) if err != nil { return nil, err } diff --git a/store/storethehash/storethehash.go b/store/storethehash/storethehash.go index 63322f3..67c3416 100644 --- a/store/storethehash/storethehash.go +++ b/store/storethehash/storethehash.go @@ -35,7 +35,7 @@ type sthStorage struct { valLock sync.RWMutex primary *mhprimary.MultihashPrimary - vserde indexer.ValueSerde + vcodec indexer.ValueCodec } type sthIterator struct { @@ -47,9 +47,9 @@ type sthIterator struct { // New creates a new indexer.Interface implemented by a storethehash-based // value store. // -// The given indexer.ValueSerde is used to serialize and deserialize values. -// If it is set to nil, indexer.JsonValueSerde is used. -func New(ctx context.Context, dir string, vserde indexer.ValueSerde, options ...sth.Option) (indexer.Interface, error) { +// The given indexer.ValueCodec is used to serialize and deserialize values. +// If it is set to nil, indexer.JsonValueCodec is used. +func New(ctx context.Context, dir string, vcodec indexer.ValueCodec, options ...sth.Option) (indexer.Interface, error) { // Using a single file to store index and data. This may change in the // future, and we may choose to set a max. size to files. Having several // files for storage increases complexity but minimizes the overhead of @@ -65,8 +65,8 @@ func New(ctx context.Context, dir string, vserde indexer.ValueSerde, options ... if err != nil { return nil, fmt.Errorf("error opening storethehash index: %w", err) } - if vserde == nil { - vserde = indexer.JsonValueSerde{} + if vcodec == nil { + vcodec = indexer.JsonValueCodec{} } s.Start() return &sthStorage{ @@ -74,7 +74,7 @@ func New(ctx context.Context, dir string, vserde indexer.ValueSerde, options ... store: s, mlk: keymutex.New(0), primary: primary, - vserde: vserde, + vcodec: vcodec, }, nil } @@ -152,7 +152,7 @@ func (s *sthStorage) RemoveProvider(ctx context.Context, providerID peer.ID) err // If a value was found, skip it if the provider is different than the // one being removed. if found { - value, err := s.vserde.UnmarshalValue(valueData) + value, err := s.vcodec.UnmarshalValue(valueData) if err != nil { return err } @@ -254,7 +254,7 @@ func (it *sthIterator) Next() (multihash.Multihash, []indexer.Value, error) { continue } - valueKeys, err := it.storage.vserde.UnmarshalValueKeys(valueKeysData) + valueKeys, err := it.storage.vcodec.UnmarshalValueKeys(valueKeysData) if err != nil { return nil, nil, err } @@ -282,7 +282,7 @@ func (s *sthStorage) getValueKeys(k []byte) ([][]byte, error) { return nil, nil } - return s.vserde.UnmarshalValueKeys(valueKeysData) + return s.vcodec.UnmarshalValueKeys(valueKeysData) } func (s *sthStorage) get(k []byte) ([]indexer.Value, bool, error) { @@ -326,7 +326,7 @@ func (s *sthStorage) putIndex(m multihash.Multihash, valKey []byte) error { } // Store the new list of value keys for the multihash. - b, err := s.vserde.MarshalValueKeys(append(existingValKeys, valKey)) + b, err := s.vcodec.MarshalValueKeys(append(existingValKeys, valKey)) if err != nil { return err } @@ -359,7 +359,7 @@ func (s *sthStorage) updateValue(value indexer.Value, saveNew bool) ([]byte, err if !found { if saveNew { // Store the new value. - valData, err := s.vserde.MarshalValue(value) + valData, err := s.vcodec.MarshalValue(value) if err != nil { return nil, err } @@ -372,7 +372,7 @@ func (s *sthStorage) updateValue(value indexer.Value, saveNew bool) ([]byte, err } // Found previous value. If it is different, then update it. - newValData, err := s.vserde.MarshalValue(value) + newValData, err := s.vcodec.MarshalValue(value) if err != nil { return nil, err } @@ -409,7 +409,7 @@ func (s *sthStorage) removeIndex(m multihash.Multihash, value indexer.Value) err valueKeys[len(valueKeys)-1] = nil valueKeys = valueKeys[:len(valueKeys)-1] // Update the list of value-keys that the multihash maps to. - b, err := s.vserde.MarshalValueKeys(valueKeys) + b, err := s.vcodec.MarshalValueKeys(valueKeys) if err != nil { return err } @@ -447,7 +447,7 @@ func (s *sthStorage) getValues(key []byte, valueKeys [][]byte) ([]indexer.Value, valueKeys = valueKeys[:len(valueKeys)-1] continue } - val, err := s.vserde.UnmarshalValue(valData) + val, err := s.vcodec.UnmarshalValue(valData) if err != nil { s.valLock.RUnlock() return nil, err @@ -472,7 +472,7 @@ func (s *sthStorage) getValues(key []byte, valueKeys [][]byte) ([]indexer.Value, } // Update the values this multihash maps to. - b, err := s.vserde.MarshalValueKeys(valueKeys) + b, err := s.vcodec.MarshalValueKeys(valueKeys) if err != nil { return nil, err } diff --git a/value.go b/value.go index 4344154..011ba73 100644 --- a/value.go +++ b/value.go @@ -11,12 +11,12 @@ import ( ) var ( - _ ValueSerde = (*JsonValueSerde)(nil) - _ ValueSerde = (*BinaryValueSerde)(nil) + _ ValueCodec = (*JsonValueCodec)(nil) + _ ValueCodec = (*BinaryValueCodec)(nil) - // ErrSerdeOverflow signals that unexpected size was encountered while + // ErrCodecOverflow signals that unexpected size was encountered while // unmarshalling bytes to Value. - ErrSerdeOverflow = errors.New("overflow") + ErrCodecOverflow = errors.New("overflow") ) type ( @@ -32,8 +32,8 @@ type ( MetadataBytes []byte `json:"m,omitempty"` } - // ValueSerde represents Value serializer and deserializer to/from bytes. - ValueSerde interface { + // ValueCodec represents Value serializer and deserializer to/from bytes. + ValueCodec interface { // MarshalValue serializes a single value. MarshalValue(Value) ([]byte, error) // UnmarshalValue deserializes a single value. @@ -44,13 +44,13 @@ type ( UnmarshalValueKeys([]byte) ([][]byte, error) } - // JsonValueSerde serializes and deserializes Value as JSON. + // JsonValueCodec serializes and deserializes Value as JSON. // See: json.Marshal, json.Unmarshal - JsonValueSerde struct{} + JsonValueCodec struct{} - // BinaryValueSerde serializes and deserializes Value as binary sections + // BinaryValueCodec serializes and deserializes Value as binary sections // prepended with byte length as varint. - BinaryValueSerde struct{} + BinaryValueCodec struct{} ) // Match return true if both values have the same ProviderID and ContextID. @@ -74,25 +74,25 @@ func (v Value) MatchEqual(other Value) (isMatch bool, isEqual bool) { return } -func (JsonValueSerde) MarshalValue(v Value) ([]byte, error) { +func (JsonValueCodec) MarshalValue(v Value) ([]byte, error) { return json.Marshal(&v) } -func (JsonValueSerde) UnmarshalValue(b []byte) (v Value, err error) { +func (JsonValueCodec) UnmarshalValue(b []byte) (v Value, err error) { err = json.Unmarshal(b, &v) return } -func (JsonValueSerde) MarshalValueKeys(vk [][]byte) ([]byte, error) { +func (JsonValueCodec) MarshalValueKeys(vk [][]byte) ([]byte, error) { return json.Marshal(&vk) } -func (JsonValueSerde) UnmarshalValueKeys(b []byte) (vk [][]byte, err error) { +func (JsonValueCodec) UnmarshalValueKeys(b []byte) (vk [][]byte, err error) { err = json.Unmarshal(b, &vk) return } -func (BinaryValueSerde) MarshalValue(v Value) ([]byte, error) { +func (BinaryValueCodec) MarshalValue(v Value) ([]byte, error) { pid := []byte(v.ProviderID) pl := len(pid) upl := uint64(pl) @@ -120,7 +120,7 @@ func (BinaryValueSerde) MarshalValue(v Value) ([]byte, error) { // If a failure occurs during serialization an error is returned along with // the partially deserialized value keys. Only nil error means complete and // successful deserialization. -func (BinaryValueSerde) UnmarshalValue(b []byte) (Value, error) { +func (BinaryValueCodec) UnmarshalValue(b []byte) (Value, error) { var v Value buf := bytes.NewBuffer(b) @@ -131,7 +131,7 @@ func (BinaryValueSerde) UnmarshalValue(b []byte) (Value, error) { } size := int(usize) if size < 0 || size > buf.Len() { - return Value{}, ErrSerdeOverflow + return Value{}, ErrCodecOverflow } v.ProviderID = peer.ID(buf.Next(size)) @@ -142,7 +142,7 @@ func (BinaryValueSerde) UnmarshalValue(b []byte) (Value, error) { } size = int(usize) if size < 0 || size > buf.Len() { - return v, ErrSerdeOverflow + return v, ErrCodecOverflow } v.ContextID = buf.Next(size) @@ -153,7 +153,7 @@ func (BinaryValueSerde) UnmarshalValue(b []byte) (Value, error) { } size = int(usize) if size < 0 || size > buf.Len() { - return v, ErrSerdeOverflow + return v, ErrCodecOverflow } v.MetadataBytes = buf.Next(size) if buf.Len() != 0 { @@ -162,7 +162,7 @@ func (BinaryValueSerde) UnmarshalValue(b []byte) (Value, error) { return v, nil } -func (BinaryValueSerde) MarshalValueKeys(vk [][]byte) ([]byte, error) { +func (BinaryValueCodec) MarshalValueKeys(vk [][]byte) ([]byte, error) { var buf bytes.Buffer for _, v := range vk { vl := len(v) @@ -179,7 +179,7 @@ func (BinaryValueSerde) MarshalValueKeys(vk [][]byte) ([]byte, error) { // If a failure occurs during serialization an error is returned along with // the partially deserialized value keys. Only nil error means complete and // successful deserialization. -func (BinaryValueSerde) UnmarshalValueKeys(b []byte) ([][]byte, error) { +func (BinaryValueCodec) UnmarshalValueKeys(b []byte) ([][]byte, error) { var vk [][]byte buf := bytes.NewBuffer(b) // Decode each value key. @@ -190,7 +190,7 @@ func (BinaryValueSerde) UnmarshalValueKeys(b []byte) ([][]byte, error) { } size := int(usize) if size < 0 || size > buf.Len() { - return vk, ErrSerdeOverflow + return vk, ErrCodecOverflow } vk = append(vk, buf.Next(size)) } diff --git a/value_serde_bench_test.go b/value_codec_bench_test.go similarity index 80% rename from value_serde_bench_test.go rename to value_codec_bench_test.go index 9f200e7..ceb3a64 100644 --- a/value_serde_bench_test.go +++ b/value_codec_bench_test.go @@ -15,23 +15,23 @@ const ( valuesPerProvider = 1_000 ) -func BenchmarkBinaryValueSerde_MarshalValue(b *testing.B) { - benchmarkMarshalValue(b, indexer.BinaryValueSerde{}) +func BenchmarkBinaryValueCodec_MarshalValue(b *testing.B) { + benchmarkMarshalValue(b, indexer.BinaryValueCodec{}) } -func BenchmarkJsonValueSerde_MarshalValue(b *testing.B) { - benchmarkMarshalValue(b, indexer.JsonValueSerde{}) +func BenchmarkJsonValueCodec_MarshalValue(b *testing.B) { + benchmarkMarshalValue(b, indexer.JsonValueCodec{}) } -func BenchmarkBinaryValueSerde_UnmarshalValue(b *testing.B) { - benchmarkUnmarshalValue(b, indexer.BinaryValueSerde{}) +func BenchmarkBinaryValueCodec_UnmarshalValue(b *testing.B) { + benchmarkUnmarshalValue(b, indexer.BinaryValueCodec{}) } -func BenchmarkJsonValueSerde_UnmarshalValue(b *testing.B) { - benchmarkUnmarshalValue(b, indexer.JsonValueSerde{}) +func BenchmarkJsonValueCodec_UnmarshalValue(b *testing.B) { + benchmarkUnmarshalValue(b, indexer.JsonValueCodec{}) } -func benchmarkMarshalValue(b *testing.B, subject indexer.ValueSerde) { +func benchmarkMarshalValue(b *testing.B, subject indexer.ValueCodec) { values, size := generateRandomValues(b, providerCount, valuesPerProvider) b.SetBytes(int64(size)) b.ReportAllocs() @@ -47,7 +47,7 @@ func benchmarkMarshalValue(b *testing.B, subject indexer.ValueSerde) { }) } -func benchmarkUnmarshalValue(b *testing.B, subject indexer.ValueSerde) { +func benchmarkUnmarshalValue(b *testing.B, subject indexer.ValueCodec) { values, size := generateRandomValues(b, providerCount, valuesPerProvider) var svalues [][]byte for _, v := range values { diff --git a/value_serde_test.go b/value_codec_test.go similarity index 90% rename from value_serde_test.go rename to value_codec_test.go index c5633af..472334b 100644 --- a/value_serde_test.go +++ b/value_codec_test.go @@ -11,21 +11,21 @@ import ( "github.com/multiformats/go-varint" ) -func TestValueSerde_MarshalUnmarshal(t *testing.T) { +func TestValueCodec_MarshalUnmarshal(t *testing.T) { wantValues, _ := generateRandomValues(t, 14, 13) wantValueKeys := generateRandomValueKeys(43) tests := []struct { name string - subject indexer.ValueSerde + subject indexer.ValueCodec }{ { name: "json", - subject: indexer.JsonValueSerde{}, + subject: indexer.JsonValueCodec{}, }, { name: "binary", - subject: indexer.BinaryValueSerde{}, + subject: indexer.BinaryValueCodec{}, }, } for _, test := range tests { @@ -58,7 +58,7 @@ func TestValueSerde_MarshalUnmarshal(t *testing.T) { } } -func TestBinaryValueSerde_MarshalValueMalformedBytes(t *testing.T) { +func TestBinaryValueCodec_MarshalValueMalformedBytes(t *testing.T) { tests := []struct { name string value func(buf *bytes.Buffer) @@ -117,7 +117,7 @@ func TestBinaryValueSerde_MarshalValueMalformedBytes(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - subject := indexer.BinaryValueSerde{} + subject := indexer.BinaryValueCodec{} buf := bytes.Buffer{} test.value(&buf) _, err := subject.UnmarshalValue(buf.Bytes()) @@ -131,8 +131,8 @@ func TestBinaryValueSerde_MarshalValueMalformedBytes(t *testing.T) { } } -func TestBinaryValueSerde_MarshalUnmarshalEmptyValues(t *testing.T) { - subject := indexer.BinaryValueSerde{} +func TestBinaryValueCodec_MarshalUnmarshalEmptyValues(t *testing.T) { + subject := indexer.BinaryValueCodec{} sv, err := subject.MarshalValue(indexer.Value{}) if err != nil { t.Fatal(err)