forked from djherbis/buffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartition.go
More file actions
executable file
·186 lines (154 loc) · 3.02 KB
/
partition.go
File metadata and controls
executable file
·186 lines (154 loc) · 3.02 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package buffer
import (
"encoding/gob"
"errors"
"io"
"math"
)
type partition struct {
List
Pool
}
// NewPartition returns a Buffer which uses a Pool to extend or shrink its size as needed.
// It automatically allocates new buffers with pool.Get() to extend is length, and
// pool.Put() to release unused buffers as it shrinks.
func NewPartition(pool Pool, buffers ...Buffer) BufferPeek {
return &partition{
Pool: pool,
List: buffers,
}
}
func (buf *partition) Cap() int64 {
return math.MaxInt64
}
func (buf *partition) GetDataLen() int64 {
var dataLen int
for _, v := range buf.List {
src := v.(*memory)
dataLen += len(src.Bytes())
}
return int64(dataLen)
}
func (buf *partition) ReadPeekByIndex(partitionIndex int) (data []byte, n int, err error) {
if len(buf.List) <= partitionIndex {
return nil, n, io.EOF
}
buffer := buf.List[partitionIndex]
src, ret := buffer.(*memory)
if !ret {
panic("type error")
}
data = make([]byte, src.Len())
p := data
n = copy(p, src.Bytes())
return data, n, nil
}
func (buf *partition) Read(p []byte) (n int, err error) {
for len(p) > 0 {
if len(buf.List) == 0 {
return n, io.EOF
}
buffer := buf.List[0]
if Empty(buffer) {
buf.Pool.Put(buf.Pop())
continue
}
m, er := buffer.Read(p)
n += m
p = p[m:]
if er != nil && er != io.EOF {
return n, er
}
}
return n, nil
}
func (buf *partition) ReadPeek() (data []byte, n int, err error) {
i := 0
if len(buf.List) == 0 {
return nil, n, io.EOF
}
data = make([]byte, buf.GetDataLen())
p := data
lenList := len(buf.List)
for i < lenList {
buffer := buf.List[i]
src, ret := buffer.(*memory)
if !ret {
panic("type error")
}
m := copy(p, src.Bytes())
if m == 0 {
return data, n, nil
}
n += m
p = p[m:]
i++
}
return data, n, nil
}
func (buf *partition) WriteByIndex(p []byte, off int64) (n int, err error) {
var i int64
pMem := buf.Pool.(*memPool)
if pMem.N < int64(len(p)) {
return n, errors.New("The data is greater than one slice")
}
lenList := len(buf.List)
for len(p) > 0 {
for i = 0; i < off-int64(lenList)+1; i++ {
next, err := buf.Pool.Get()
if err != nil {
return n, err
}
buf.Push(next)
}
//覆盖写
buffer := buf.List[off]
buffer.Reset()
m, er := buffer.Write(p)
n += m
p = p[m:]
if er == io.ErrShortWrite {
er = nil
} else if er != nil {
return n, er
}
}
return n, nil
}
func (buf *partition) Write(p []byte) (n int, err error) {
for len(p) > 0 {
if len(buf.List) == 0 {
next, err := buf.Pool.Get()
if err != nil {
return n, err
}
buf.Push(next)
}
buffer := buf.List[len(buf.List)-1]
if Full(buffer) {
next, err := buf.Pool.Get()
if err != nil {
return n, err
}
buf.Push(next)
continue
}
m, er := buffer.Write(p)
n += m
p = p[m:]
if er == io.ErrShortWrite {
er = nil
} else if er != nil {
return n, er
}
}
return n, nil
}
func (buf *partition) Reset() {
for len(buf.List) > 0 {
buf.Pool.Put(buf.Pop())
}
}
func init() {
gob.Register(&partition{})
}