forked from elastic/go-txfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
164 lines (137 loc) · 3.46 KB
/
util.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
158
159
160
161
162
163
164
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package txfile
import (
"math/bits"
)
// pagingWriter supports writing entries into a linked (pre-allocated) list of
// pages.
type pagingWriter struct {
ids idList
buf []byte
pageSize uint
extraHeader uint
onPage func(id PageID, buf []byte) reason
// current page state
i int
off uint
hdr *listPage
page []byte
payload []byte
count uint32
}
const maxUint uint = ^uint(0)
func newPagingWriter(
ids idList,
pageSize uint,
extraHeader uint,
onPage func(id PageID, buf []byte) reason,
) *pagingWriter {
if len(ids) == 0 {
return nil
}
buf := make([]byte, len(ids)*int(pageSize))
// prelink all pages, in case some are not written to
off := 0
for _, id := range ids[1:] {
hdr, _ := castListPage(buf[off:])
hdr.next.Set(id)
off += int(pageSize)
}
w := &pagingWriter{
ids: ids,
buf: buf,
pageSize: pageSize,
extraHeader: extraHeader,
onPage: onPage,
}
w.prepareNext("")
return w
}
func (w *pagingWriter) Write(entry []byte) reason {
const op = "txfile/write-meta-list"
if w == nil {
return nil
}
if len(w.payload) < len(entry) {
if err := w.flushCurrent(op); err != nil {
return err
}
}
n := copy(w.payload, entry)
w.payload = w.payload[n:]
w.count++
return nil
}
func (w *pagingWriter) Flush() reason {
const op = "txfile/flush-meta-list"
if w == nil {
return nil
}
if err := w.finalizePage(); err != nil {
return err
}
for w.i < len(w.ids) {
// update to next page
w.prepareNext(op)
if err := w.finalizePage(); err != nil {
return err
}
}
return nil
}
func (w *pagingWriter) flushCurrent(op string) (err reason) {
if err = w.finalizePage(); err == nil {
err = w.prepareNext(op)
}
return
}
func (w *pagingWriter) finalizePage() reason {
w.hdr.count.Set(w.count)
if w.onPage != nil {
if err := w.onPage(w.ids[w.i], w.page); err != nil {
return err
}
}
w.count = 0
w.off += w.pageSize
w.i++
return nil
}
func (w *pagingWriter) prepareNext(op string) reason {
if w.i >= len(w.ids) {
return errOp(op).of(InternalError).report("Not enough pages pre-allocated")
}
w.page = w.buf[w.off : w.off+w.pageSize]
w.hdr, w.payload = castListPage(w.page)
w.payload = w.payload[w.extraHeader:]
return nil
}
func isPowerOf2(v uint64) bool {
// an uint is a power of two if exactly one bit is set ->
return v > 0 && (v&(v-1)) == 0
}
// nextPowerOf2 computes the next power of two value of `u`, such that
// nextPowerOf2(u) > u
// The input value must not have the highest bit being set.
func nextPowerOf2(u uint64) uint64 {
b := uint64(bits.LeadingZeros64(u))
return uint64(1) << (64 - b)
}
func ignoreReason(fn func() reason) func() {
return func() { _ = fn() }
}