Skip to content

Commit

Permalink
[Go] rename Reference to Ref (#975)
Browse files Browse the repository at this point in the history
rename Reference to Ref
  • Loading branch information
chaokunyang authored Oct 5, 2023
1 parent 748022a commit 2ffee3c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 68 deletions.
8 changes: 4 additions & 4 deletions go/fury/fury.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
func NewFury(referenceTracking bool) *Fury {
fury := &Fury{
typeResolver: newTypeResolver(),
refResolver: newReferenceResolver(referenceTracking),
refResolver: newRefResolver(referenceTracking),
referenceTracking: referenceTracking,
language: XLANG,
buffer: NewByteBuffer(nil),
Expand Down Expand Up @@ -89,7 +89,7 @@ const (

type Fury struct {
typeResolver *typeResolver
refResolver *ReferenceResolver
refResolver *RefResolver
referenceTracking bool
language Language
bufferCallback BufferCallback
Expand Down Expand Up @@ -226,7 +226,7 @@ func (f *Fury) WriteReferencable(buffer *ByteBuffer, value reflect.Value) error
}

func (f *Fury) writeReferencableBySerializer(buffer *ByteBuffer, value reflect.Value, serializer Serializer) error {
if refWritten, err := f.refResolver.WriteReferenceOrNull(buffer, value); err == nil && !refWritten {
if refWritten, err := f.refResolver.WriteRefOrNull(buffer, value); err == nil && !refWritten {
// check ptr
if value.Kind() == reflect.Ptr {
switch value.Elem().Kind() {
Expand Down Expand Up @@ -353,7 +353,7 @@ func (f *Fury) ReadReferencable(buffer *ByteBuffer, value reflect.Value) error {
}

func (f *Fury) readReferencableBySerializer(buf *ByteBuffer, value reflect.Value, serializer Serializer) (err error) {
refId, err := f.refResolver.TryPreserveReferenceId(buf)
refId, err := f.refResolver.TryPreserveRefId(buf)
if err != nil {
return err
}
Expand Down
96 changes: 48 additions & 48 deletions go/fury/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,35 @@ const (
RefValueFlag int8 = 0
)

// ReferenceResolver class is used to track objects that have already been read or written.
type ReferenceResolver struct {
referenceTracking bool
writtenObjects map[referenceKey]int32
readObjects []reflect.Value
readReferenceIds []int32
readObject reflect.Value // last read object which is not a reference
// RefResolver class is used to track objects that have already been read or written.
type RefResolver struct {
refTracking bool
writtenObjects map[refKey]int32
readObjects []reflect.Value
readRefIds []int32
readObject reflect.Value // last read object which is not a reference
}

type referenceKey struct {
type refKey struct {
pointer unsafe.Pointer
length int // for slice and *array only
}

func newReferenceResolver(referenceTracking bool) *ReferenceResolver {
referenceResolver := &ReferenceResolver{
referenceTracking: referenceTracking,
writtenObjects: map[referenceKey]int32{},
func newRefResolver(refTracking bool) *RefResolver {
refResolver := &RefResolver{
refTracking: refTracking,
writtenObjects: map[refKey]int32{},
}
return referenceResolver
return refResolver
}

// WriteReferenceOrNull write reference and tag for the value if the value has been written previously,
// WriteRefOrNull write reference and tag for the value if the value has been written previously,
// write null/not-null tag otherwise. Returns true if no bytes need to be written for the object.
// See https://go101.org/article/value-part.html for internal structure definitions of common types.
// Note that for slice and substring, if the start addr or length are different, we take two objects as
// different references.
func (r *ReferenceResolver) WriteReferenceOrNull(buffer *ByteBuffer, value reflect.Value) (refWritten bool, err error) {
if !r.referenceTracking {
func (r *RefResolver) WriteRefOrNull(buffer *ByteBuffer, value reflect.Value) (refWritten bool, err error) {
if !r.refTracking {
if isNil(value) {
buffer.WriteInt8(NullFlag)
return true, nil
Expand All @@ -86,7 +86,7 @@ func (r *ReferenceResolver) WriteReferenceOrNull(buffer *ByteBuffer, value refle
length = value.Len()
case reflect.Interface:
value = value.Elem()
return r.WriteReferenceOrNull(buffer, value)
return r.WriteRefOrNull(buffer, value)
case reflect.String:
isNil = false
str := unsafeGetBytes(value.Interface().(string))
Expand All @@ -103,7 +103,7 @@ func (r *ReferenceResolver) WriteReferenceOrNull(buffer *ByteBuffer, value refle
buffer.WriteInt8(NullFlag)
return true, nil
} else {
refKey := referenceKey{pointer: unsafe.Pointer(value.Pointer()), length: length}
refKey := refKey{pointer: unsafe.Pointer(value.Pointer()), length: length}
if writtenId, ok := r.writtenObjects[refKey]; ok {
// The obj has been written previously.
buffer.WriteInt8(RefFlag)
Expand All @@ -122,16 +122,16 @@ func (r *ReferenceResolver) WriteReferenceOrNull(buffer *ByteBuffer, value refle
}
}

// ReadReferenceOrNull returns RefFlag if a Reference to a previously read object
// ReadRefOrNull returns RefFlag if a ref to a previously read object
// was read. Returns NullFlag if the object is null. Returns RefValueFlag if the object is not
// null and Reference tracking is not enabled or the object is first read.
func (r *ReferenceResolver) ReadReferenceOrNull(buffer *ByteBuffer) int8 {
// null and ref tracking is not enabled or the object is first read.
func (r *RefResolver) ReadRefOrNull(buffer *ByteBuffer) int8 {
refTag := buffer.ReadInt8()
if !r.referenceTracking {
if !r.refTracking {
return refTag
}
if refTag == RefFlag {
// read reference id and get object from reference resolver
// read ref id and get object from ref resolver
refId := buffer.ReadVarInt32()
r.readObject = r.GetReadObject(refId)
return RefFlag
Expand All @@ -141,11 +141,11 @@ func (r *ReferenceResolver) ReadReferenceOrNull(buffer *ByteBuffer) int8 {
return refTag
}

// PreserveReferenceId preserve a reference id, which is used by Reference / SetReadObject to
// PreserveRefId preserve a ref id, which is used by Reference / SetReadObject to
// set up reference for object that is first deserialized.
// Returns a reference id or -1 if reference is not enabled.
func (r *ReferenceResolver) PreserveReferenceId() (int32, error) {
if !r.referenceTracking {
// Returns a ref id or -1 if reference is not enabled.
func (r *RefResolver) PreserveRefId() (int32, error) {
if !r.refTracking {
return -1, nil
}
nextReadRefId_ := len(r.readObjects)
Expand All @@ -154,80 +154,80 @@ func (r *ReferenceResolver) PreserveReferenceId() (int32, error) {
}
nextReadRefId := int32(nextReadRefId_)
r.readObjects = append(r.readObjects, reflect.Value{})
r.readReferenceIds = append(r.readReferenceIds, nextReadRefId)
r.readRefIds = append(r.readRefIds, nextReadRefId)
return nextReadRefId, nil
}

func (r *ReferenceResolver) TryPreserveReferenceId(buffer *ByteBuffer) (int32, error) {
func (r *RefResolver) TryPreserveRefId(buffer *ByteBuffer) (int32, error) {
headFlag := buffer.ReadInt8()
if headFlag == RefFlag {
// read reference id and get object from reference resolver
// read ref id and get object from ref resolver
refId := buffer.ReadVarInt32()
r.readObject = r.GetReadObject(refId)
} else {
r.readObject = reflect.Value{}
if headFlag == RefValueFlag {
return r.PreserveReferenceId()
return r.PreserveRefId()
}
}
// `headFlag` except `REF_FLAG` can be used as stub reference id because we use
// `headFlag` except `REF_FLAG` can be used as stub ref id because we use
// `refId >= NOT_NULL_VALUE_FLAG` to read data.
return int32(headFlag), nil
}

// Reference tracking references relationship. Call this method immediately after composited object such as
// object array/map/collection/bean is created so that circular reference can be deserialized correctly.
func (r *ReferenceResolver) Reference(value reflect.Value) {
if !r.referenceTracking {
func (r *RefResolver) Reference(value reflect.Value) {
if !r.refTracking {
return
}
length := len(r.readReferenceIds)
refId := r.readReferenceIds[length-1]
r.readReferenceIds = r.readReferenceIds[:length-1]
length := len(r.readRefIds)
refId := r.readRefIds[length-1]
r.readRefIds = r.readRefIds[:length-1]
r.SetReadObject(refId, value)
}

// GetReadObject returns the object for the specified id.
func (r *ReferenceResolver) GetReadObject(refId int32) reflect.Value {
if !r.referenceTracking {
func (r *RefResolver) GetReadObject(refId int32) reflect.Value {
if !r.refTracking {
return reflect.Value{}
}
return r.readObjects[refId]
}

func (r *ReferenceResolver) GetCurrentReadObject() reflect.Value {
func (r *RefResolver) GetCurrentReadObject() reflect.Value {
return r.readObject
}

// SetReadObject sets the id for an object that has been read.
// id: The id from {@link #NextReadRefId}.
// object: the object that has been read
func (r *ReferenceResolver) SetReadObject(refId int32, value reflect.Value) {
if !r.referenceTracking {
func (r *RefResolver) SetReadObject(refId int32, value reflect.Value) {
if !r.refTracking {
return
}
if refId >= 0 {
r.readObjects[refId] = value
}
}

func (r *ReferenceResolver) reset() {
func (r *RefResolver) reset() {
r.resetRead()
r.resetWrite()
}

func (r *ReferenceResolver) resetRead() {
if !r.referenceTracking {
func (r *RefResolver) resetRead() {
if !r.refTracking {
return
}
r.readObjects = nil
r.readReferenceIds = nil
r.readRefIds = nil
r.readObject = reflect.Value{}
}

func (r *ReferenceResolver) resetWrite() {
func (r *RefResolver) resetWrite() {
if len(r.writtenObjects) > 0 {
r.writtenObjects = map[referenceKey]int32{}
r.writtenObjects = map[refKey]int32{}
}
}

Expand Down
26 changes: 13 additions & 13 deletions go/fury/reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

func TestReferenceResolver(t *testing.T) {
refResolver := newReferenceResolver(true)
refResolver := newRefResolver(true)
buf := NewByteBuffer(nil)
var values []interface{}
values = append(values, commonSlice()...)
Expand All @@ -31,36 +31,36 @@ func TestReferenceResolver(t *testing.T) {
bar := Bar{}
values = append(values, "", "str", &foo, &bar)
for _, data := range values {
refWritten, err := refResolver.WriteReferenceOrNull(buf, reflect.ValueOf(data))
refWritten, err := refResolver.WriteRefOrNull(buf, reflect.ValueOf(data))
require.Nil(t, err)
require.False(t, refWritten)
refWritten, err = refResolver.WriteReferenceOrNull(buf, reflect.ValueOf(data))
refWritten, err = refResolver.WriteRefOrNull(buf, reflect.ValueOf(data))
require.Nil(t, err)
require.True(t, refWritten)
}
refResolver.readObjects = make([]reflect.Value, len(refResolver.writtenObjects))
for range values {
require.Equal(t, refResolver.ReadReferenceOrNull(buf), RefValueFlag)
require.Equal(t, refResolver.ReadReferenceOrNull(buf), RefFlag)
require.Equal(t, refResolver.ReadRefOrNull(buf), RefValueFlag)
require.Equal(t, refResolver.ReadRefOrNull(buf), RefFlag)
}
{
s := []int{1, 2, 3}
require.True(t, same(s, s))
require.False(t, same(s, s[1:]))
refWritten, err := refResolver.WriteReferenceOrNull(buf, reflect.ValueOf(s))
refWritten, err := refResolver.WriteRefOrNull(buf, reflect.ValueOf(s))
require.Nil(t, err)
require.False(t, refWritten)
refWritten, err = refResolver.WriteReferenceOrNull(buf, reflect.ValueOf(s))
refWritten, err = refResolver.WriteRefOrNull(buf, reflect.ValueOf(s))
require.Nil(t, err)
require.True(t, refWritten)
refWritten, err = refResolver.WriteReferenceOrNull(buf, reflect.ValueOf(s[1:]))
refWritten, err = refResolver.WriteRefOrNull(buf, reflect.ValueOf(s[1:]))
require.Nil(t, err)
require.False(t, refWritten)
}
}

func TestNonReferenceResolver(t *testing.T) {
refResolver := newReferenceResolver(false)
refResolver := newRefResolver(false)
buf := NewByteBuffer(nil)
var values []interface{}
values = append(values, commonSlice()...)
Expand All @@ -69,16 +69,16 @@ func TestNonReferenceResolver(t *testing.T) {
bar := Bar{}
values = append(values, "", "str", &foo, &bar)
for _, data := range values {
refWritten, err := refResolver.WriteReferenceOrNull(buf, reflect.ValueOf(data))
refWritten, err := refResolver.WriteRefOrNull(buf, reflect.ValueOf(data))
require.Nil(t, err)
require.False(t, refWritten)
refWritten, err = refResolver.WriteReferenceOrNull(buf, reflect.ValueOf(data))
refWritten, err = refResolver.WriteRefOrNull(buf, reflect.ValueOf(data))
require.Nil(t, err)
require.False(t, refWritten)
}
for range values {
require.Equal(t, refResolver.ReadReferenceOrNull(buf), NotNullValueFlag)
require.Equal(t, refResolver.ReadReferenceOrNull(buf), NotNullValueFlag)
require.Equal(t, refResolver.ReadRefOrNull(buf), NotNullValueFlag)
require.Equal(t, refResolver.ReadRefOrNull(buf), NotNullValueFlag)
}
}

Expand Down
6 changes: 3 additions & 3 deletions go/fury/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (s stringSliceSerializer) Write(f *Fury, buf *ByteBuffer, value reflect.Val
return err
}
for _, str := range v {
if refWritten, err := f.refResolver.WriteReferenceOrNull(buf, reflect.ValueOf(str)); err == nil {
if refWritten, err := f.refResolver.WriteRefOrNull(buf, reflect.ValueOf(str)); err == nil {
if !refWritten {
if err := writeString(buf, str); err != nil {
return err
Expand All @@ -347,10 +347,10 @@ func (s stringSliceSerializer) Read(f *Fury, buf *ByteBuffer, type_ reflect.Type
r := make([]string, length, length)
f.refResolver.Reference(reflect.ValueOf(r))
for i := 0; i < length; i++ {
if refFlag := f.refResolver.ReadReferenceOrNull(buf); refFlag == RefValueFlag || refFlag == NotNullValueFlag {
if refFlag := f.refResolver.ReadRefOrNull(buf); refFlag == RefValueFlag || refFlag == NotNullValueFlag {
var nextReadRefId int32
if refFlag == RefValueFlag {
nextReadRefId, err = f.refResolver.PreserveReferenceId()
nextReadRefId, err = f.refResolver.PreserveRefId()
if err != nil {
return err
}
Expand Down

0 comments on commit 2ffee3c

Please sign in to comment.