buffer.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. offset := b.idx
  70. b.idx += need
  71. b.length -= need
  72. return b.buf[offset:b.idx], nil
  73. }
  74. // returns a buffer with the requested size.
  75. // If possible, a slice from the existing buffer is returned.
  76. // Otherwise a bigger buffer is made.
  77. // Only one buffer (total) can be used at a time.
  78. func (b *buffer) takeBuffer(length int) []byte {
  79. if b.length > 0 {
  80. return nil
  81. }
  82. // test (cheap) general case first
  83. if length <= defaultBufSize || length <= cap(b.buf) {
  84. return b.buf[:length]
  85. }
  86. if length < maxPacketSize {
  87. b.buf = make([]byte, length)
  88. return b.buf
  89. }
  90. return make([]byte, length)
  91. }
  92. // shortcut which can be used if the requested buffer is guaranteed to be
  93. // smaller than defaultBufSize
  94. // Only one buffer (total) can be used at a time.
  95. func (b *buffer) takeSmallBuffer(length int) []byte {
  96. if b.length == 0 {
  97. return b.buf[:length]
  98. }
  99. return nil
  100. }
  101. // takeCompleteBuffer returns the complete existing buffer.
  102. // This can be used if the necessary buffer size is unknown.
  103. // Only one buffer (total) can be used at a time.
  104. func (b *buffer) takeCompleteBuffer() []byte {
  105. if b.length == 0 {
  106. return b.buf
  107. }
  108. return nil
  109. }