You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

intDecoder.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2019 Couchbase, Inc.
  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 zap
  15. import (
  16. "encoding/binary"
  17. "fmt"
  18. "github.com/blevesearch/bleve/index/scorch/segment"
  19. )
  20. type chunkedIntDecoder struct {
  21. startOffset uint64
  22. dataStartOffset uint64
  23. chunkOffsets []uint64
  24. curChunkBytes []byte
  25. data []byte
  26. r *segment.MemUvarintReader
  27. }
  28. func newChunkedIntDecoder(buf []byte, offset uint64) *chunkedIntDecoder {
  29. rv := &chunkedIntDecoder{startOffset: offset, data: buf}
  30. var n, numChunks uint64
  31. var read int
  32. if offset == termNotEncoded {
  33. numChunks = 0
  34. } else {
  35. numChunks, read = binary.Uvarint(buf[offset+n : offset+n+binary.MaxVarintLen64])
  36. }
  37. n += uint64(read)
  38. if cap(rv.chunkOffsets) >= int(numChunks) {
  39. rv.chunkOffsets = rv.chunkOffsets[:int(numChunks)]
  40. } else {
  41. rv.chunkOffsets = make([]uint64, int(numChunks))
  42. }
  43. for i := 0; i < int(numChunks); i++ {
  44. rv.chunkOffsets[i], read = binary.Uvarint(buf[offset+n : offset+n+binary.MaxVarintLen64])
  45. n += uint64(read)
  46. }
  47. rv.dataStartOffset = offset + n
  48. return rv
  49. }
  50. func (d *chunkedIntDecoder) loadChunk(chunk int) error {
  51. if d.startOffset == termNotEncoded {
  52. d.r = segment.NewMemUvarintReader([]byte(nil))
  53. return nil
  54. }
  55. if chunk >= len(d.chunkOffsets) {
  56. return fmt.Errorf("tried to load freq chunk that doesn't exist %d/(%d)",
  57. chunk, len(d.chunkOffsets))
  58. }
  59. end, start := d.dataStartOffset, d.dataStartOffset
  60. s, e := readChunkBoundary(chunk, d.chunkOffsets)
  61. start += s
  62. end += e
  63. d.curChunkBytes = d.data[start:end]
  64. if d.r == nil {
  65. d.r = segment.NewMemUvarintReader(d.curChunkBytes)
  66. } else {
  67. d.r.Reset(d.curChunkBytes)
  68. }
  69. return nil
  70. }
  71. func (d *chunkedIntDecoder) reset() {
  72. d.startOffset = 0
  73. d.dataStartOffset = 0
  74. d.chunkOffsets = d.chunkOffsets[:0]
  75. d.curChunkBytes = d.curChunkBytes[:0]
  76. d.data = d.data[:0]
  77. if d.r != nil {
  78. d.r.Reset([]byte(nil))
  79. }
  80. }
  81. func (d *chunkedIntDecoder) isNil() bool {
  82. return d.curChunkBytes == nil
  83. }
  84. func (d *chunkedIntDecoder) readUvarint() (uint64, error) {
  85. return d.r.ReadUvarint()
  86. }
  87. func (d *chunkedIntDecoder) SkipUvarint() {
  88. d.r.SkipUvarint()
  89. }
  90. func (d *chunkedIntDecoder) SkipBytes(count int) {
  91. d.r.SkipBytes(count)
  92. }
  93. func (d *chunkedIntDecoder) Len() int {
  94. return d.r.Len()
  95. }