/
iterator.go
33 lines (29 loc) · 881 Bytes
/
iterator.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
package mmap
//
// Implementation of pattern Iterator to scan a Page
// The scan is made in reverse, from the last byte to the first (right to left).
//
type PageIterator struct {
p Page
current int
}
//
// Return false when there is no other record to read.
// Return true if the cursor has found the beginning of the Page
//
func (it *PageIterator) HasNext() bool {
if RECORD_TOTAL_HEADER_SIZE < it.current && it.current <= it.p.Use() {
return true
}
return false
}
//
// Return the next pointer to the byte array casted in a ByteRecord struct (provides methods).
// Use an cursor to know the current Record index in the Page.
// Update the cursor (current) to minus the payload of the Record
//
func (it *PageIterator) Next() ByteRecord {
r := ByteRecord(it.p[:it.current])
it.current -= int(r.keyLen()+r.valLen()) + int(RECORD_TOTAL_HEADER_SIZE)
return r
}