-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.go
43 lines (35 loc) · 829 Bytes
/
cursor.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
package main
import "unsafe"
type Cursor struct {
Table *Table
RowNum uint32
EndOfTable bool
}
func tableStart(table *Table) *Cursor {
cursor := new(Cursor)
cursor.Table = table
cursor.RowNum = 0
cursor.EndOfTable = table.NumRows == 0
return cursor
}
func tableEnd(table *Table) *Cursor {
cursor := new(Cursor)
cursor.Table = table
cursor.RowNum = table.NumRows
cursor.EndOfTable = true
return cursor
}
func cursorValue(cursor *Cursor) unsafe.Pointer {
rowNum := cursor.RowNum
pageNum := rowNum / RowsPerPage
page := getPage(cursor.Table.Pager, pageNum)
rowOffset := rowNum % RowsPerPage
byteOffset := rowOffset * RowSize
return unsafe.Add(page, byteOffset)
}
func cursorAdvance(cursor *Cursor) {
cursor.RowNum += 1
if cursor.RowNum >= cursor.Table.NumRows {
cursor.EndOfTable = true
}
}