aboutsummaryrefslogtreecommitdiff
path: root/meta.go
blob: cc6263793d4336b4c50bdd1500c3b1ba16c88d25 (plain) (blame)
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
package bolt

import (
	"errors"
)

const magic uint32 = 0xED0CDAED

var (
	// ErrInvalid is returned when a data file is not a Bolt-formatted database.
	ErrInvalid = errors.New("invalid database")

	// ErrVersionMismatch is returned when the data file was created with a
	// different version of Bolt.
	ErrVersionMismatch = errors.New("version mismatch")
)

type meta struct {
	magic    uint32
	version  uint32
	pageSize uint32
	flags    uint32
	buckets  pgid
	freelist pgid
	pgid     pgid
	txid     txid
}

// validate checks the marker bytes and version of the meta page to ensure it matches this binary.
func (m *meta) validate() error {
	if m.magic != magic {
		return ErrInvalid
	} else if m.version != version {
		return ErrVersionMismatch
	}
	return nil
}

// copy copies one meta object to another.
func (m *meta) copy(dest *meta) {
	dest.magic = m.magic
	dest.version = m.version
	dest.pageSize = m.pageSize
	dest.buckets = m.buckets
	dest.freelist = m.freelist
	dest.pgid = m.pgid
	dest.txid = m.txid
}

// write writes the meta onto a page.
func (m *meta) write(p *page) {
	// Page id is either going to be 0 or 1 which we can determine by the transaction ID.
	p.id = pgid(m.txid % 2)
	p.flags |= metaPageFlag

	m.copy(p.meta())
}