pagewriter.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // 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. package ioutil
  15. import (
  16. "io"
  17. )
  18. var defaultBufferBytes = 128 * 1024
  19. // PageWriter implements the io.Writer interface so that writes will
  20. // either be in page chunks or from flushing.
  21. type PageWriter struct {
  22. w io.Writer
  23. // pageOffset tracks the page offset of the base of the buffer
  24. pageOffset int
  25. // pageBytes is the number of bytes per page
  26. pageBytes int
  27. // bufferedBytes counts the number of bytes pending for write in the buffer
  28. bufferedBytes int
  29. // buf holds the write buffer
  30. buf []byte
  31. // bufWatermarkBytes is the number of bytes the buffer can hold before it needs
  32. // to be flushed. It is less than len(buf) so there is space for slack writes
  33. // to bring the writer to page alignment.
  34. bufWatermarkBytes int
  35. }
  36. // NewPageWriter creates a new PageWriter. pageBytes is the number of bytes
  37. // to write per page. pageOffset is the starting offset of io.Writer.
  38. func NewPageWriter(w io.Writer, pageBytes, pageOffset int) *PageWriter {
  39. return &PageWriter{
  40. w: w,
  41. pageOffset: pageOffset,
  42. pageBytes: pageBytes,
  43. buf: make([]byte, defaultBufferBytes+pageBytes),
  44. bufWatermarkBytes: defaultBufferBytes,
  45. }
  46. }
  47. func (pw *PageWriter) Write(p []byte) (n int, err error) {
  48. if len(p)+pw.bufferedBytes <= pw.bufWatermarkBytes {
  49. // no overflow
  50. copy(pw.buf[pw.bufferedBytes:], p)
  51. pw.bufferedBytes += len(p)
  52. return len(p), nil
  53. }
  54. // complete the slack page in the buffer if unaligned
  55. slack := pw.pageBytes - ((pw.pageOffset + pw.bufferedBytes) % pw.pageBytes)
  56. if slack != pw.pageBytes {
  57. partial := slack > len(p)
  58. if partial {
  59. // not enough data to complete the slack page
  60. slack = len(p)
  61. }
  62. // special case: writing to slack page in buffer
  63. copy(pw.buf[pw.bufferedBytes:], p[:slack])
  64. pw.bufferedBytes += slack
  65. n = slack
  66. p = p[slack:]
  67. if partial {
  68. // avoid forcing an unaligned flush
  69. return n, nil
  70. }
  71. }
  72. // buffer contents are now page-aligned; clear out
  73. if err = pw.Flush(); err != nil {
  74. return n, err
  75. }
  76. // directly write all complete pages without copying
  77. if len(p) > pw.pageBytes {
  78. pages := len(p) / pw.pageBytes
  79. c, werr := pw.w.Write(p[:pages*pw.pageBytes])
  80. n += c
  81. if werr != nil {
  82. return n, werr
  83. }
  84. p = p[pages*pw.pageBytes:]
  85. }
  86. // write remaining tail to buffer
  87. c, werr := pw.Write(p)
  88. n += c
  89. return n, werr
  90. }
  91. func (pw *PageWriter) Flush() error {
  92. if pw.bufferedBytes == 0 {
  93. return nil
  94. }
  95. _, err := pw.w.Write(pw.buf[:pw.bufferedBytes])
  96. pw.pageOffset = (pw.pageOffset + pw.bufferedBytes) % pw.pageBytes
  97. pw.bufferedBytes = 0
  98. return err
  99. }