Skip to content

Commit

Permalink
Sink ByteView
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed May 25, 2020
1 parent 75f5c70 commit bbc607a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
19 changes: 15 additions & 4 deletions byteview.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
//
// A ByteView is meant to be used as a value type, not
// a pointer (like a time.Time).
// 封装了一个string与byte[]的统一接口,也就是说用byteview提供的接口,可以屏蔽掉string与byte[]的不同,使用时可以不用考虑是string还是byte[]
type ByteView struct {
// If b is non-nil, b is used, else s is used.
// 当b是nil 的时候,s作为存储内容的容器
Expand All @@ -45,6 +46,7 @@ func (v ByteView) Len() int {
}

// ByteSlice returns a copy of the data as a byte slice.
// 按[]byte返回一个拷贝
func (v ByteView) ByteSlice() []byte {
if v.b != nil {
return cloneBytes(v.b)
Expand All @@ -53,6 +55,7 @@ func (v ByteView) ByteSlice() []byte {
}

// String returns the data as a string, making a copy if necessary.
// 按string返回一个拷贝
func (v ByteView) String() string {
if v.b != nil {
return string(v.b)
Expand All @@ -61,6 +64,7 @@ func (v ByteView) String() string {
}

// At returns the byte at index i.
// 返回第i个byte
func (v ByteView) At(i int) byte {
if v.b != nil {
return v.b[i]
Expand All @@ -69,6 +73,7 @@ func (v ByteView) At(i int) byte {
}

// Slice slices the view between the provided from and to indices.
// 返回ByteView的某个片断,不拷贝
func (v ByteView) Slice(from, to int) ByteView {
if v.b != nil {
return ByteView{b: v.b[from:to]}
Expand All @@ -77,6 +82,7 @@ func (v ByteView) Slice(from, to int) ByteView {
}

// SliceFrom slices the view from the provided index until the end.
// 返回ByteView的从某个位置开始的片断,不拷贝
func (v ByteView) SliceFrom(from int) ByteView {
if v.b != nil {
return ByteView{b: v.b[from:]}
Expand All @@ -85,24 +91,25 @@ func (v ByteView) SliceFrom(from int) ByteView {
}

// Copy copies b into dest and returns the number of bytes copied.
// 将ByteView按[]byte拷贝出来
func (v ByteView) Copy(dest []byte) int {
if v.b != nil {
return copy(dest, v.b)
}
return copy(dest, v.s)
}

// Equal returns whether the bytes in b are the same as the bytes in
// b2.
// Equal returns whether the bytes in b are the same as the bytes in b2.
// 判断2个ByteView是否相等
func (v ByteView) Equal(b2 ByteView) bool {
if b2.b == nil {
return v.EqualString(b2.s)
}
return v.EqualBytes(b2.b)
}

// EqualString returns whether the bytes in b are the same as the bytes
// in s.
// EqualString returns whether the bytes in b are the same as the bytes in s.
// 判断ByteView是否和string相等
func (v ByteView) EqualString(s string) bool {
if v.b == nil {
return v.s == s
Expand All @@ -121,6 +128,7 @@ func (v ByteView) EqualString(s string) bool {

// EqualBytes returns whether the bytes in b are the same as the bytes
// in b2.
// 判断ByteView是否和[]byte相等
func (v ByteView) EqualBytes(b2 []byte) bool {
if v.b != nil {
return bytes.Equal(v.b, b2)
Expand All @@ -138,6 +146,7 @@ func (v ByteView) EqualBytes(b2 []byte) bool {
}

// Reader returns an io.ReadSeeker for the bytes in v.
// 对ByteView创建一个io.ReadSeeker
func (v ByteView) Reader() io.ReadSeeker {
if v.b != nil {
return bytes.NewReader(v.b)
Expand All @@ -146,6 +155,7 @@ func (v ByteView) Reader() io.ReadSeeker {
}

// ReadAt implements io.ReaderAt on the bytes in v.
// 读取从off开始的后面的数据,其实下面调用的SliceFrom,这是封装成了io.Reader的一个ReadAt方法的形式
func (v ByteView) ReadAt(p []byte, off int64) (n int, err error) {
if off < 0 {
return 0, errors.New("view: invalid offset")
Expand All @@ -161,6 +171,7 @@ func (v ByteView) ReadAt(p []byte, off int64) (n int, err error) {
}

// WriteTo implements io.WriterTo on the bytes in v.
// 向w流中写入v
func (v ByteView) WriteTo(w io.Writer) (n int64, err error) {
var m int
if v.b != nil {
Expand Down
7 changes: 7 additions & 0 deletions sinks.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,12 @@ func (s *protoSink) SetProto(m proto.Message) error {
// AllocatingByteSliceSink returns a Sink that allocates
// a byte slice to hold the received value and assigns
// it to *dst. The memory is not retained by groupcache.
// 分配一个字节切片来保存接收到的数据
func AllocatingByteSliceSink(dst *[]byte) Sink {
return &allocBytesSink{dst: dst}
}

// 有一个字节切片指针类型的属性dst
type allocBytesSink struct {
dst *[]byte
v ByteView
Expand All @@ -225,6 +227,7 @@ func (s *allocBytesSink) view() (ByteView, error) {
return s.v, nil
}

// 设置allocBytesSink的v,同时复制v中的b或者s丢给dst
func (s *allocBytesSink) setView(v ByteView) error {
if v.b != nil {
*s.dst = cloneBytes(v.b)
Expand All @@ -235,6 +238,7 @@ func (s *allocBytesSink) setView(v ByteView) error {
return nil
}

// 这个得从下面的setBytesOwned开始往上看
func (s *allocBytesSink) SetProto(m proto.Message) error {
b, err := proto.Marshal(m)
if err != nil {
Expand All @@ -243,10 +247,12 @@ func (s *allocBytesSink) SetProto(m proto.Message) error {
return s.setBytesOwned(b)
}

// 复制一份b,然后调用setBytesOwned
func (s *allocBytesSink) SetBytes(b []byte) error {
return s.setBytesOwned(cloneBytes(b))
}

// 使用b设置allocBytesSink的dst和ByteView
func (s *allocBytesSink) setBytesOwned(b []byte) error {
if s.dst == nil {
return errors.New("nil AllocatingByteSliceSink *[]byte dst")
Expand All @@ -257,6 +263,7 @@ func (s *allocBytesSink) setBytesOwned(b []byte) error {
return nil
}

// 字符串转成[]byte后进行和上面类似的操作
func (s *allocBytesSink) SetString(v string) error {
if s.dst == nil {
return errors.New("nil AllocatingByteSliceSink *[]byte dst")
Expand Down

0 comments on commit bbc607a

Please sign in to comment.