-
Notifications
You must be signed in to change notification settings - Fork 0
/
linequeue.go
218 lines (200 loc) · 4.89 KB
/
linequeue.go
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package linequeue //file based line queue with lines counter
import(
"io"
"os"
"bytes"
"encoding/binary"
)
const SIZEOFCURSOR int = 8
const SIZEOFCOUNTER int = 4
const SIZEOFHEADER int = 20
type Linequeue struct {
autoshrink bool
linesize int
lines uint32
afile *os.File
}
func isExists( path string ) bool {
_,err := os.Stat( path )
if err == nil {
return true
}
return os.IsExist( err ) //os.IsNotExist(err)?
}
func readCounter( afile *os.File ) uint32 {
var lines uint32 = 0
if afile!=nil {
b := make( []byte, SIZEOFCOUNTER )
n,err := afile.ReadAt( b,int64(SIZEOFCURSOR*2) )
if err==nil && n==SIZEOFCOUNTER { //byte2uint32
buf := bytes.NewBuffer( b )
binary.Read(buf, binary.BigEndian, &lines)
}
}
return lines
}
func writeCounter( afile *os.File,lines uint32 ) bool{
flag := false
if afile!=nil {
buf := bytes.NewBuffer( []byte{} ) //uint32 to byte
binary.Write( buf, binary.BigEndian, lines )
_,err := afile.WriteAt( buf.Bytes(), int64(SIZEOFCURSOR*2) )
flag = (err==nil)
}
return flag
}
func readCursor( afile *os.File ) int64 {
var pos int64 = 0
if afile!=nil {
b := make( []byte, SIZEOFCURSOR )
n,err := afile.ReadAt( b,0 )
if err==nil && n==SIZEOFCURSOR { //byte2int64
buf := bytes.NewBuffer( b )
binary.Read(buf, binary.BigEndian, &pos)
}
}
return pos
}
func writeCursor( afile *os.File,pos int64 ) bool{
flag := false
if afile!=nil {
buf := bytes.NewBuffer( []byte{} ) //int64 to byte
binary.Write( buf, binary.BigEndian, pos )
_,err := afile.WriteAt( buf.Bytes(),0 )
flag = (err==nil)
}
return flag
}
func readPrevious( afile *os.File ) int64 {
pos := int64(SIZEOFCURSOR)
if afile!=nil {
b := make( []byte, SIZEOFCURSOR )
n,err := afile.ReadAt( b,int64(SIZEOFCURSOR) )
if err==nil && n==SIZEOFCURSOR { //byte2int64
buf := bytes.NewBuffer( b )
binary.Read(buf, binary.BigEndian, &pos)
}
}
return pos
}
func writePrevious( afile *os.File,pos int64 ) bool{
flag := false
if afile!=nil {
buf := bytes.NewBuffer( []byte{} ) //int64 to byte
binary.Write( buf, binary.BigEndian, pos )
_,err := afile.WriteAt( buf.Bytes(),int64(SIZEOFCURSOR) )
flag = (err==nil)
}
return flag
}
func (lq *Linequeue)Empty() {
if lq.afile!=nil {
lq.afile.Truncate( int64(SIZEOFHEADER) )
writeCursor( lq.afile,int64(SIZEOFHEADER) )
writePrevious( lq.afile,int64(SIZEOFHEADER) )
writeCounter( lq.afile,uint32(0) )
lq.lines = 0
}
}
func (lq *Linequeue)GetLines() uint32{
return lq.lines
}
func (lq *Linequeue)PutInto(ln string) (nbytes int,lines uint32){
nbytes = 0
if lq.afile!=nil {
lq.afile.Seek( 0,2 ) //end of file
nbytes,_ = lq.afile.WriteString( ln+"\n" )
if nbytes>0 {
lq.lines ++
writeCounter(lq.afile,lq.lines)
}
}
return nbytes,lq.lines
}
func (lq *Linequeue)RollBackOneStep() (flag bool,pos int64) { //only one step
flag = false
pos = readPrevious( lq.afile )
if pos<readCursor( lq.afile ) {
writeCursor( lq.afile,pos )
flag = true
}
return flag,pos
}
func (lq *Linequeue)TakeOut() (string,int){
ln := ""
sz := 0
b := make( []byte,lq.linesize )
if lq.afile!=nil {
pos := readCursor( lq.afile )
if pos>0 {
o,err := lq.afile.Seek( pos,0 )
if err==nil && o==pos {
for {
n,err := lq.afile.Read(b)
if err==nil {
if n<lq.linesize { b = b[:n] }
i := bytes.IndexByte( b,'\n' )
if i>=0 {
ln += string( b[:i] )
o += int64(i)
break
}else{
ln += string( b )
o += int64(n)
}
}else if err==io.EOF {
break
}
}
}
if o==pos { //Not forward
if lq.autoshrink {
fi,s_err := lq.afile.Stat()
if s_err==nil {
if fi.Size() == o {
lq.afile.Truncate( int64(SIZEOFHEADER) )
writeCursor( lq.afile,int64(SIZEOFHEADER) )
writePrevious( lq.afile,int64(SIZEOFHEADER) )
writeCounter( lq.afile,uint32(0) )
lq.lines = 0
}
}
}
}else{
writeCursor( lq.afile,o+1 )
writePrevious( lq.afile,pos )
}
sz = int(o-pos)
}
}
return ln,sz
}
func SetFile( fname string,lnsize int,autoshrink bool ) (*Linequeue,bool){
flag := false
var err error = nil
afile, err := os.OpenFile(fname, os.O_RDWR | os.O_CREATE, 0x644)
if err == nil {
fi,s_err := afile.Stat()
if s_err==nil {
sz := fi.Size()
if sz==0 {
writeCursor( afile,int64(SIZEOFHEADER) ) //cur pos
writePrevious( afile,int64(SIZEOFHEADER) ) //pre pos
writeCounter( afile,uint32(0) )
}
flag = true
}
}
lq := &Linequeue{
afile: afile,
linesize: lnsize,
lines: readCounter( afile ),
autoshrink: autoshrink,
}
return lq,flag
}
func (lq *Linequeue)CloseFile() {
if lq.afile!=nil {
lq.afile.Close()
}
}