Skip to content

Commit

Permalink
Rename the term serde to codec
Browse files Browse the repository at this point in the history
Rename to a more familiar term for better human readability.
  • Loading branch information
masih committed Aug 18, 2022
1 parent 2b7e6eb commit bf19437
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 72 deletions.
32 changes: 16 additions & 16 deletions store/pogreb/pogreb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
32 changes: 16 additions & 16 deletions store/storethehash/storethehash.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type sthStorage struct {
valLock sync.RWMutex

primary *mhprimary.MultihashPrimary
vserde indexer.ValueSerde
vcodec indexer.ValueCodec
}

type sthIterator struct {
Expand All @@ -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
Expand All @@ -65,16 +65,16 @@ 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{
dir: dir,
store: s,
mlk: keymutex.New(0),
primary: primary,
vserde: vserde,
vcodec: vcodec,
}, nil
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
44 changes: 22 additions & 22 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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))

Expand All @@ -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)

Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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.
Expand All @@ -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))
}
Expand Down
Loading

0 comments on commit bf19437

Please sign in to comment.