MCPcopy
hub / github.com/etcd-io/bbolt / ReadPage

Function ReadPage

internal/guts_cli/guts_cli.go:21–70  ·  view source on GitHub ↗

ReadPage reads Page info & full Page data from a path. This is not transactionally safe.

(path string, pageID uint64)

Source from the content-addressed store, hash-verified

19// ReadPage reads Page info & full Page data from a path.
20// This is not transactionally safe.
21func ReadPage(path string, pageID uint64) (*common.Page, []byte, error) {
22 // Find Page size.
23 pageSize, hwm, err := ReadPageAndHWMSize(path)
24 if err != nil {
25 return nil, nil, fmt.Errorf("read Page size: %s", err)
26 }
27
28 // Open database file.
29 f, err := os.Open(path)
30 if err != nil {
31 return nil, nil, err
32 }
33 defer f.Close()
34
35 // Read one block into buffer.
36 buf := make([]byte, pageSize)
37 if n, err := f.ReadAt(buf, int64(pageID*pageSize)); err != nil {
38 return nil, nil, err
39 } else if n != len(buf) {
40 return nil, nil, io.ErrUnexpectedEOF
41 }
42
43 // Determine total number of blocks.
44 p := common.LoadPage(buf)
45 if p.Id() != common.Pgid(pageID) {
46 return nil, nil, fmt.Errorf("error: %w due to unexpected Page id: %d != %d", ErrCorrupt, p.Id(), pageID)
47 }
48 overflowN := p.Overflow()
49 if overflowN >= uint32(hwm)-3 { // we exclude 2 Meta pages and the current Page.
50 return nil, nil, fmt.Errorf("error: %w, Page claims to have %d overflow pages (>=hwm=%d). Interrupting to avoid risky OOM", ErrCorrupt, overflowN, hwm)
51 }
52
53 if overflowN == 0 {
54 return p, buf, nil
55 }
56
57 // Re-read entire Page (with overflow) into buffer.
58 buf = make([]byte, (uint64(overflowN)+1)*pageSize)
59 if n, err := f.ReadAt(buf, int64(pageID*pageSize)); err != nil {
60 return nil, nil, err
61 } else if n != len(buf) {
62 return nil, nil, io.ErrUnexpectedEOF
63 }
64 p = common.LoadPage(buf)
65 if p.Id() != common.Pgid(pageID) {
66 return nil, nil, fmt.Errorf("error: %w due to unexpected Page id: %d != %d", ErrCorrupt, p.Id(), pageID)
67 }
68
69 return p, buf, nil
70}
71
72func WritePage(path string, pageBuf []byte) error {
73 page := common.LoadPage(pageBuf)

Callers 15

corruptRootPageFunction · 0.92
CopyPageFunction · 0.92
ClearPageElementsFunction · 0.92
clearFreelistInMetaPageFunction · 0.92
traverseMethod · 0.92
TestFindPathsToKeyFunction · 0.92
pageItemFuncFunction · 0.92

Calls 7

LoadPageFunction · 0.92
PgidTypeAlias · 0.92
ReadPageAndHWMSizeFunction · 0.85
IdMethod · 0.80
OverflowMethod · 0.80
ErrorfMethod · 0.65
CloseMethod · 0.45