buffer.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import "io"
  10. const defaultBufSize = 4096
  11. // A buffer which is used for both reading and writing.
  12. // This is possible since communication on each connection is synchronous.
  13. // In other words, we can't write and read simultaneously on the same connection.
  14. // The buffer is similar to bufio.Reader / Writer but zero-copy-ish
  15. // Also highly optimized for this particular use case.
  16. type buffer struct {
  17. buf []byte
  18. rd io.Reader
  19. idx int
  20. length int
  21. }
  22. func newBuffer(rd io.Reader) *buffer {
  23. var b [defaultBufSize]byte
  24. return &buffer{
  25. buf: b[:],
  26. rd: rd,
  27. }
  28. }
  29. // fill reads into the buffer until at least _need_ bytes are in it
  30. func (b *buffer) fill(need int) error {
  31. // move existing data to the beginning
  32. if b.length > 0 && b.idx > 0 {
  33. copy(b.buf[0:b.length], b.buf[b.idx:])
  34. }
  35. // grow buffer if necessary
  36. // TODO: let the buffer shrink again at some point
  37. // Maybe keep the org buf slice and swap back?
  38. if need > len(b.buf) {
  39. // Round up to the next multiple of the default size
  40. newBuf := make([]byte, ((need/defaultBufSize)+1)*defaultBufSize)
  41. copy(newBuf, b.buf)
  42. b.buf = newBuf
  43. }
  44. b.idx = 0
  45. for {
  46. n, err := b.rd.Read(b.buf[b.length:])
  47. b.length += n
  48. if err == nil {
  49. if b.length < need {
  50. continue
  51. }
  52. return nil
  53. }
  54. if b.length >= need && err == io.EOF {
  55. return nil
  56. }
  57. return err
  58. }
  59. }
  60. // returns next N bytes from buffer.
  61. // The returned slice is only guaranteed to be valid until the next read
  62. func (b *buffer) readNext(need int) ([]byte, error) {
  63. if b.length < need {
  64. // refill
  65. if err := b.fill(need); err != nil {
  66. return nil, err
  67. }
  68. }
  69. b.idx += need
  70. b.length -= need
  71. return b.buf[b.idx-need : b.idx], nil
  72. }
  73. // returns a buffer with the requested size.
  74. // If possible, a slice from the existing buffer is returned.
  75. // Otherwise a bigger buffer is made.
  76. // Only one buffer (total) can be used at a time.
  77. func (b *buffer) takeBuffer(length int) []byte {
  78. if b.length > 0 {
  79. return nil
  80. }
  81. // test (cheap) general case first
  82. if length <= defaultBufSize || length <= cap(b.buf) {
  83. return b.buf[:length]
  84. }
  85. if length < maxPacketSize {
  86. b.buf = make([]byte, length)
  87. return b.buf
  88. }
  89. return make([]byte, length)
  90. }
  91. // shortcut which can be used if the requested buffer is guaranteed to be
  92. // smaller than defaultBufSize
  93. // Only one buffer (total) can be used at a time.
  94. func (b *buffer) takeSmallBuffer(length int) []byte {
  95. if b.length == 0 {
  96. return b.buf[:length]
  97. }
  98. return nil
  99. }
  100. // takeCompleteBuffer returns the complete existing buffer.
  101. // This can be used if the necessary buffer size is unknown.
  102. // Only one buffer (total) can be used at a time.
  103. func (b *buffer) takeCompleteBuffer() []byte {
  104. if b.length == 0 {
  105. return b.buf
  106. }
  107. return nil
  108. }