-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathpage.go
157 lines (135 loc) · 3.03 KB
/
page.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
// Copyright 2017 The go-sqlite Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sqlite3
import "fmt"
// PageKind describes what kind of page is.
type PageKind byte
/*
An index B-Tree internal node
An index B-Tree leaf node
A table B-Tree internal node
A table B-Tree leaf node
An overflow page
A freelist page
A pointer map page
The locking page
*/
const (
intKeyKind PageKind = 0x01
zeroDataKind PageKind = 0x02
leafDataKind PageKind = 0x04
leafKind PageKind = 0x08
BTreeInteriorIndexKind = zeroDataKind
BTreeInteriorTableKind = leafDataKind | intKeyKind
BTreeLeafIndexKind = zeroDataKind | leafKind
BTreeLeafTableKind = leafDataKind | intKeyKind | leafKind
pkLockByte
pkFreelistTrunk
pkFreelistLeaf
pkPayloadOverflow
pkPointerMap
)
func (pk PageKind) String() string {
switch pk {
case BTreeInteriorIndexKind:
return "BTreeInteriorIndex"
case BTreeInteriorTableKind:
return "BTreeInteriorTable"
case BTreeLeafIndexKind:
return "BTreeLeafIndex"
case BTreeLeafTableKind:
return "BTreeLeafTable"
}
panic(fmt.Sprintf("sqlite3: invalid PageKind value (0x%02x)", byte(pk)))
}
/*
func newPage(i int, pb pageBuffer) (Page, error) {
pk := PageKind(pb.buf[0])
switch pk {
case BTreeInteriorIndexKind:
panic("not implemented")
case BTreeInteriorTableKind:
panic("not implemented")
case BTreeLeafIndexKind:
panic("not implemented")
case BTreeLeafTableKind:
return newBtreeLeafTable(i, pb)
}
panic(fmt.Errorf("invalid PageKind value (%d)", pk))
}
*/
// page is a page loaded from disk.
type page struct {
id int
pos int
buf []byte
}
func (p *page) ID() int {
return p.id
}
func (p *page) Kind() PageKind {
offset := 0
if p.id == 1 {
offset = 100
}
return PageKind(p.buf[0+offset])
}
func (p *page) PageSize() int {
return len(p.buf)
}
func (p *page) Seek(offset int64, whence int) (ret int64, err error) {
switch whence {
case 0:
offset := int(offset)
if offset > len(p.buf) {
return 0, fmt.Errorf("sqlite: offset too big (%d)", offset)
}
p.pos = offset
case 1:
offset := int(offset)
pos := p.pos + offset
if pos > len(p.buf) {
return 0, fmt.Errorf("sqlite: offset too big (%d)", offset)
}
p.pos = pos
case 2:
offset := int(offset)
pos := len(p.buf) - offset
if pos < 0 {
return 0, fmt.Errorf("sqlite: offset too big (%d)", offset)
}
p.pos = pos
}
return int64(p.pos), nil
}
func (p *page) Pos() int {
return p.pos
}
func (p *page) Bytes() []byte {
return p.buf[p.pos:]
}
func (p *page) Decode(ptr interface{}) error {
n, err := unmarshal(p.buf[p.pos:], ptr)
if err != nil {
return err
}
p.pos += int(n)
return err
}
func (p *page) Read(data []byte) (int, error) {
n := copy(data, p.buf[p.pos:p.pos+len(data)])
if n != len(data) {
return n, fmt.Errorf("error. read too few bytes: %d. want %d", n, len(data))
}
p.pos += n
return n, nil
}
func (p *page) Varint() (int64, int) {
v, n := varint(p.Bytes())
if n <= 0 {
return v, n
}
p.pos += int(n)
return v, n
}