2017-06-17 08:36:05 +02:00
|
|
|
package jsoniter
|
|
|
|
|
2017-06-17 16:42:11 +02:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
)
|
2017-06-17 11:14:34 +02:00
|
|
|
|
2017-07-09 10:09:23 +02:00
|
|
|
// IteratorPool a thread safe pool of iterators with same configuration
|
|
|
|
type IteratorPool interface {
|
|
|
|
BorrowIterator(data []byte) *Iterator
|
|
|
|
ReturnIterator(iter *Iterator)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StreamPool a thread safe pool of streams with same configuration
|
|
|
|
type StreamPool interface {
|
|
|
|
BorrowStream(writer io.Writer) *Stream
|
|
|
|
ReturnStream(stream *Stream)
|
|
|
|
}
|
|
|
|
|
2017-06-17 11:14:34 +02:00
|
|
|
func (cfg *frozenConfig) BorrowStream(writer io.Writer) *Stream {
|
2017-06-17 08:36:05 +02:00
|
|
|
select {
|
|
|
|
case stream := <-cfg.streamPool:
|
2017-06-17 11:14:34 +02:00
|
|
|
stream.Reset(writer)
|
2017-06-17 08:36:05 +02:00
|
|
|
return stream
|
|
|
|
default:
|
2017-06-17 11:14:34 +02:00
|
|
|
return NewStream(cfg, writer, 512)
|
2017-06-17 08:36:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-17 11:14:34 +02:00
|
|
|
func (cfg *frozenConfig) ReturnStream(stream *Stream) {
|
2017-06-18 16:21:54 +02:00
|
|
|
stream.Error = nil
|
2017-06-17 08:36:05 +02:00
|
|
|
select {
|
|
|
|
case cfg.streamPool <- stream:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-17 11:14:34 +02:00
|
|
|
func (cfg *frozenConfig) BorrowIterator(data []byte) *Iterator {
|
2017-06-17 08:36:05 +02:00
|
|
|
select {
|
2017-06-17 15:11:23 +02:00
|
|
|
case iter := <-cfg.iteratorPool:
|
2017-06-17 08:36:05 +02:00
|
|
|
iter.ResetBytes(data)
|
|
|
|
return iter
|
|
|
|
default:
|
|
|
|
return ParseBytes(cfg, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-17 11:14:34 +02:00
|
|
|
func (cfg *frozenConfig) ReturnIterator(iter *Iterator) {
|
2017-06-18 16:21:54 +02:00
|
|
|
iter.Error = nil
|
2017-06-17 08:36:05 +02:00
|
|
|
select {
|
|
|
|
case cfg.iteratorPool <- iter:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|