Skip to content

Commit

Permalink
update BorrowDecoder api
Browse files Browse the repository at this point in the history
  • Loading branch information
francoispqt committed Apr 28, 2018
1 parent 083b067 commit 86e4b63
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 31 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,25 @@ You can either get a fresh `*gojay.Decoder` calling `dec := gojay.NewDecoder(io.

After using a decoder, you can release it by calling `dec.Release()`. Beware, if you reuse the decoder after releasing it, it will panic with an error of type `InvalidUsagePooledDecoderError`. If you want to fully benefit from the pooling, you must release your decoders after using.

Example getting a fresh an releasing:
```go
str := ""
dec := gojay.NewDecoder(strings.NewReader(`"test"`))
defer dec.Release()
if err := dec.Decode(&str); err != nil {
log.Fatal(err)
}
```
Example borrowing a decoder and releasing:
```go
str := ""
dec := gojay.BorrowDecoder(strings.NewReader(`"test"`))
defer dec.Release()
if err := dec.Decode(&str); err != nil {
log.Fatal(err)
}
```

`*gojay.Decoder` has multiple methods to decode to specific types:
* Decode
```go
Expand Down
24 changes: 12 additions & 12 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// If a JSON value is not appropriate for a given target type, or if a JSON number
// overflows the target type, UnmarshalArray skips that field and completes the unmarshaling as best it can.
func UnmarshalArray(data []byte, v UnmarshalerArray) error {
dec := BorrowDecoder(nil, 0)
dec := BorrowDecoder(nil)
defer dec.Release()
dec.data = make([]byte, len(data))
copy(dec.data, data)
Expand All @@ -35,7 +35,7 @@ func UnmarshalArray(data []byte, v UnmarshalerArray) error {
// If a JSON value is not appropriate for a given target type, or if a JSON number
// overflows the target type, UnmarshalObject skips that field and completes the unmarshaling as best it can.
func UnmarshalObject(data []byte, v UnmarshalerObject) error {
dec := BorrowDecoder(nil, 0)
dec := BorrowDecoder(nil)
defer dec.Release()
dec.data = make([]byte, len(data))
copy(dec.data, data)
Expand Down Expand Up @@ -73,53 +73,53 @@ func Unmarshal(data []byte, v interface{}) error {
var dec *Decoder
switch vt := v.(type) {
case *string:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
err = dec.decodeString(vt)
case *int:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
err = dec.decodeInt(vt)
case *int32:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
err = dec.decodeInt32(vt)
case *uint32:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
err = dec.decodeUint32(vt)
case *int64:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
err = dec.decodeInt64(vt)
case *uint64:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
err = dec.decodeUint64(vt)
case *float64:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
err = dec.decodeFloat64(vt)
case *bool:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
err = dec.decodeBool(vt)
case UnmarshalerObject:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = make([]byte, len(data))
copy(dec.data, data)
_, err = dec.decodeObject(vt)
case UnmarshalerArray:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = make([]byte, len(data))
copy(dec.data, data)
Expand Down
5 changes: 4 additions & 1 deletion decode_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ func NewDecoder(r io.Reader) *Decoder {
// BorrowDecoder borrows a Decoder from the pool.
// It takes an io.Reader implementation as data input.
// It initiates the done channel returned by Done().
func BorrowDecoder(r io.Reader, bufSize int) *Decoder {
func BorrowDecoder(r io.Reader) *Decoder {
return borrowDecoder(r, 512)
}
func borrowDecoder(r io.Reader, bufSize int) *Decoder {
select {
case dec := <-decPool:
dec.called = 0
Expand Down
6 changes: 5 additions & 1 deletion decode_stream_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ func (s stream) NewDecoder(r io.Reader) *StreamDecoder {
// BorrowDecoder borrows a StreamDecoder a decoder from the pool.
// It takes an io.Reader implementation as data input.
// It initiates the done channel returned by Done().
func (s stream) BorrowDecoder(r io.Reader, bufSize int) *StreamDecoder {
func (s stream) BorrowDecoder(r io.Reader) *StreamDecoder {
return s.borrowDecoder(r, 512)
}

func (s stream) borrowDecoder(r io.Reader, bufSize int) *StreamDecoder {
select {
case streamDec := <-streamDecPool:
streamDec.called = 0
Expand Down
8 changes: 4 additions & 4 deletions decode_stream_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestDecodeStreamBorrow(t *testing.T) {
dec := Stream.NewDecoder(nil)
streamDecPool <- dec
// borrow one decoder to the channel
nDec := Stream.BorrowDecoder(nil, 0)
nDec := Stream.BorrowDecoder(nil)
// make sure they are the same
assert.Equal(t, dec, nDec, "decoder added to the pool and new decoder should be the same")
}
Expand All @@ -27,7 +27,7 @@ func TestDecodeStreamBorrow1(t *testing.T) {
// reset streamDecPool
streamDecPool = make(chan *StreamDecoder, 1)
// borrow one decoder to the channel
nDec := Stream.BorrowDecoder(nil, 0)
nDec := Stream.BorrowDecoder(nil)
// make sure they are the same
assert.NotEqual(t, dec, nDec, "decoder added to the pool and new decoder should be the same")
}
Expand All @@ -39,7 +39,7 @@ func TestDecodeStreamBorrow2(t *testing.T) {
dec.data = make([]byte, 128)
streamDecPool <- dec
// borrow one decoder to the channel
nDec := Stream.BorrowDecoder(nil, 512)
nDec := Stream.BorrowDecoder(nil)
// make sure they are the same
assert.Equal(t, dec, nDec, "decoder added to the pool and new decoder should be the same")
assert.Equal(t, 512, len(nDec.data), "len of dec.data should be 512")
Expand All @@ -48,7 +48,7 @@ func TestDecodeStreamBorrow3(t *testing.T) {
// we override the pool chan
streamDecPool = make(chan *StreamDecoder, 16)
// borrow one decoder to the channel
nDec := Stream.BorrowDecoder(nil, 512)
nDec := Stream.BorrowDecoder(nil)
// make sure they are the same
assert.Equal(t, 512, len(nDec.data), "len of dec.data should be 512")
}
Expand Down
2 changes: 1 addition & 1 deletion decode_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func TestStreamDecodingErrNotSet(t *testing.T) {
}

func TestStreamDecodingPoolError(t *testing.T) {
dec := Stream.BorrowDecoder(nil, 0)
dec := Stream.BorrowDecoder(nil)
dec.Release()
defer func() {
err := recover()
Expand Down
24 changes: 12 additions & 12 deletions decode_unsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var Unsafe = decUnsafe{}
type decUnsafe struct{}

func (u decUnsafe) UnmarshalArray(data []byte, v UnmarshalerArray) error {
dec := BorrowDecoder(nil, 0)
dec := BorrowDecoder(nil)
defer dec.Release()
dec.data = data
dec.length = len(data)
Expand All @@ -29,7 +29,7 @@ func (u decUnsafe) UnmarshalArray(data []byte, v UnmarshalerArray) error {
}

func (u decUnsafe) UnmarshalObject(data []byte, v UnmarshalerObject) error {
dec := BorrowDecoder(nil, 0)
dec := BorrowDecoder(nil)
defer dec.Release()
dec.data = data
dec.length = len(data)
Expand All @@ -48,52 +48,52 @@ func (u decUnsafe) Unmarshal(data []byte, v interface{}) error {
var dec *Decoder
switch vt := v.(type) {
case *string:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
err = dec.decodeString(vt)
case *int:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
err = dec.decodeInt(vt)
case *int32:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
err = dec.decodeInt32(vt)
case *uint32:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
err = dec.decodeUint32(vt)
case *int64:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
err = dec.decodeInt64(vt)
case *uint64:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
err = dec.decodeUint64(vt)
case *float64:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
err = dec.decodeFloat64(vt)
case *bool:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
err = dec.decodeBool(vt)
case UnmarshalerObject:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
_, err = dec.decodeObject(vt)
case UnmarshalerArray:
dec = BorrowDecoder(nil, 0)
dec = BorrowDecoder(nil)
dec.length = len(data)
dec.data = data
_, err = dec.decodeArray(vt)
Expand Down

0 comments on commit 86e4b63

Please sign in to comment.