blob: ebab6487e08f03fe3a337c81324e2df5b21d9aa0 [file] [log] [blame]
Abhay Kumar40252eb2025-10-13 13:25:53 +00001// Copyright 2016 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package ioutil
16
17import (
18 "io"
19
20 "go.etcd.io/etcd/client/pkg/v3/verify"
21)
22
23var defaultBufferBytes = 128 * 1024
24
25// PageWriter implements the io.Writer interface so that writes will
26// either be in page chunks or from flushing.
27type PageWriter struct {
28 w io.Writer
29 // pageOffset tracks the page offset of the base of the buffer
30 pageOffset int
31 // pageBytes is the number of bytes per page
32 pageBytes int
33 // bufferedBytes counts the number of bytes pending for write in the buffer
34 bufferedBytes int
35 // buf holds the write buffer
36 buf []byte
37 // bufWatermarkBytes is the number of bytes the buffer can hold before it needs
38 // to be flushed. It is less than len(buf) so there is space for slack writes
39 // to bring the writer to page alignment.
40 bufWatermarkBytes int
41}
42
43// NewPageWriter creates a new PageWriter. pageBytes is the number of bytes
44// to write per page. pageOffset is the starting offset of io.Writer.
45func NewPageWriter(w io.Writer, pageBytes, pageOffset int) *PageWriter {
46 verify.Assert(pageBytes > 0, "invalid pageBytes (%d) value, it must be greater than 0", pageBytes)
47 return &PageWriter{
48 w: w,
49 pageOffset: pageOffset,
50 pageBytes: pageBytes,
51 buf: make([]byte, defaultBufferBytes+pageBytes),
52 bufWatermarkBytes: defaultBufferBytes,
53 }
54}
55
56func (pw *PageWriter) Write(p []byte) (n int, err error) {
57 if len(p)+pw.bufferedBytes <= pw.bufWatermarkBytes {
58 // no overflow
59 copy(pw.buf[pw.bufferedBytes:], p)
60 pw.bufferedBytes += len(p)
61 return len(p), nil
62 }
63 // complete the slack page in the buffer if unaligned
64 slack := pw.pageBytes - ((pw.pageOffset + pw.bufferedBytes) % pw.pageBytes)
65 if slack != pw.pageBytes {
66 partial := slack > len(p)
67 if partial {
68 // not enough data to complete the slack page
69 slack = len(p)
70 }
71 // special case: writing to slack page in buffer
72 copy(pw.buf[pw.bufferedBytes:], p[:slack])
73 pw.bufferedBytes += slack
74 n = slack
75 p = p[slack:]
76 if partial {
77 // avoid forcing an unaligned flush
78 return n, nil
79 }
80 }
81 // buffer contents are now page-aligned; clear out
82 if err = pw.Flush(); err != nil {
83 return n, err
84 }
85 // directly write all complete pages without copying
86 if len(p) > pw.pageBytes {
87 pages := len(p) / pw.pageBytes
88 c, werr := pw.w.Write(p[:pages*pw.pageBytes])
89 n += c
90 if werr != nil {
91 return n, werr
92 }
93 p = p[pages*pw.pageBytes:]
94 }
95 // write remaining tail to buffer
96 c, werr := pw.Write(p)
97 n += c
98 return n, werr
99}
100
101// Flush flushes buffered data.
102func (pw *PageWriter) Flush() error {
103 _, err := pw.flush()
104 return err
105}
106
107func (pw *PageWriter) flush() (int, error) {
108 if pw.bufferedBytes == 0 {
109 return 0, nil
110 }
111 n, err := pw.w.Write(pw.buf[:pw.bufferedBytes])
112 pw.pageOffset = (pw.pageOffset + pw.bufferedBytes) % pw.pageBytes
113 pw.bufferedBytes = 0
114 return n, err
115}