summaryrefslogtreecommitdiff
path: root/vendor/github.com/go-pg/pg/v10/internal/pool/reader.go
blob: b5d00807d6a2fc4c935b156af7b6e661286ca6b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package pool

import (
	"sync"
)

type Reader interface {
	Buffered() int

	Bytes() []byte
	Read([]byte) (int, error)
	ReadByte() (byte, error)
	UnreadByte() error
	ReadSlice(byte) ([]byte, error)
	Discard(int) (int, error)

	// ReadBytes(fn func(byte) bool) ([]byte, error)
	// ReadN(int) ([]byte, error)
	ReadFull() ([]byte, error)
	ReadFullTemp() ([]byte, error)
}

type ColumnInfo struct {
	Index    int16
	DataType int32
	Name     string
}

type ColumnAlloc struct {
	columns []ColumnInfo
}

func NewColumnAlloc() *ColumnAlloc {
	return new(ColumnAlloc)
}

func (c *ColumnAlloc) Reset() {
	c.columns = c.columns[:0]
}

func (c *ColumnAlloc) New(index int16, name []byte) *ColumnInfo {
	c.columns = append(c.columns, ColumnInfo{
		Index: index,
		Name:  string(name),
	})
	return &c.columns[len(c.columns)-1]
}

func (c *ColumnAlloc) Columns() []ColumnInfo {
	return c.columns
}

type ReaderContext struct {
	*BufReader
	ColumnAlloc *ColumnAlloc
}

func NewReaderContext() *ReaderContext {
	const bufSize = 1 << 20 // 1mb
	return &ReaderContext{
		BufReader:   NewBufReader(bufSize),
		ColumnAlloc: NewColumnAlloc(),
	}
}

var readerPool = sync.Pool{
	New: func() interface{} {
		return NewReaderContext()
	},
}

func GetReaderContext() *ReaderContext {
	rd := readerPool.Get().(*ReaderContext)
	return rd
}

func PutReaderContext(rd *ReaderContext) {
	rd.ColumnAlloc.Reset()
	readerPool.Put(rd)
}