This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreftable.go
97 lines (79 loc) · 1.7 KB
/
reftable.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
/*
Copyright 2020 Google LLC
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
*/
package reftable
import (
"io"
"os"
)
type fileBlockSource struct {
f *os.File
sz uint64
}
// NewFileBlockSource opens a file on local disk as a BlockSource
func NewFileBlockSource(name string) (BlockSource, error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
return nil, err
}
return &fileBlockSource{f, uint64(fi.Size())}, nil
}
func (bs *fileBlockSource) Size() uint64 {
return bs.sz
}
func (bs *fileBlockSource) ReadBlock(off uint64, size int) ([]byte, error) {
if off >= bs.sz {
return nil, io.EOF
}
if off+uint64(size) > bs.sz {
size = int(bs.sz - off)
}
b := make([]byte, size)
n, err := bs.f.ReadAt(b, int64(off))
if err != nil {
return nil, err
}
return b[:n], nil
}
func (bs *fileBlockSource) Close() error {
return bs.f.Close()
}
// ReadRef reads a ref record by name.
func ReadRef(tab Table, name string) (*RefRecord, error) {
it, err := tab.SeekRef(name)
if err != nil {
return nil, err
}
var ref RefRecord
ok, err := it.NextRef(&ref)
if err != nil || !ok {
return nil, err
}
if ref.RefName != name {
return nil, nil
}
return &ref, nil
}
// ReadRef reads the most recent log record of a certain ref.
func ReadLogAt(tab Table, name string, updateIndex uint64) (*LogRecord, error) {
it, err := tab.SeekLog(name, updateIndex)
if err != nil {
return nil, err
}
var rec LogRecord
ok, err := it.NextLog(&rec)
if err != nil || !ok {
return nil, err
}
if rec.RefName != name {
return nil, nil
}
return &rec, nil
}