prefix_coded.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2014 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 numeric
  15. import "fmt"
  16. const ShiftStartInt64 byte = 0x20
  17. // PrefixCoded is a byte array encoding of
  18. // 64-bit numeric values shifted by 0-63 bits
  19. type PrefixCoded []byte
  20. func NewPrefixCodedInt64(in int64, shift uint) (PrefixCoded, error) {
  21. rv, _, err := NewPrefixCodedInt64Prealloc(in, shift, nil)
  22. return rv, err
  23. }
  24. func NewPrefixCodedInt64Prealloc(in int64, shift uint, prealloc []byte) (
  25. rv PrefixCoded, preallocRest []byte, err error) {
  26. if shift > 63 {
  27. return nil, prealloc, fmt.Errorf("cannot shift %d, must be between 0 and 63", shift)
  28. }
  29. nChars := ((63 - shift) / 7) + 1
  30. size := int(nChars + 1)
  31. if len(prealloc) >= size {
  32. rv = PrefixCoded(prealloc[0:size])
  33. preallocRest = prealloc[size:]
  34. } else {
  35. rv = make(PrefixCoded, size)
  36. }
  37. rv[0] = ShiftStartInt64 + byte(shift)
  38. sortableBits := int64(uint64(in) ^ 0x8000000000000000)
  39. sortableBits = int64(uint64(sortableBits) >> shift)
  40. for nChars > 0 {
  41. // Store 7 bits per byte for compatibility
  42. // with UTF-8 encoding of terms
  43. rv[nChars] = byte(sortableBits & 0x7f)
  44. nChars--
  45. sortableBits = int64(uint64(sortableBits) >> 7)
  46. }
  47. return rv, preallocRest, nil
  48. }
  49. func MustNewPrefixCodedInt64(in int64, shift uint) PrefixCoded {
  50. rv, err := NewPrefixCodedInt64(in, shift)
  51. if err != nil {
  52. panic(err)
  53. }
  54. return rv
  55. }
  56. // Shift returns the number of bits shifted
  57. // returns 0 if in uninitialized state
  58. func (p PrefixCoded) Shift() (uint, error) {
  59. if len(p) > 0 {
  60. shift := p[0] - ShiftStartInt64
  61. if shift < 0 || shift < 63 {
  62. return uint(shift), nil
  63. }
  64. }
  65. return 0, fmt.Errorf("invalid prefix coded value")
  66. }
  67. func (p PrefixCoded) Int64() (int64, error) {
  68. shift, err := p.Shift()
  69. if err != nil {
  70. return 0, err
  71. }
  72. var sortableBits int64
  73. for _, inbyte := range p[1:] {
  74. sortableBits <<= 7
  75. sortableBits |= int64(inbyte)
  76. }
  77. return int64(uint64((sortableBits << shift)) ^ 0x8000000000000000), nil
  78. }
  79. func ValidPrefixCodedTerm(p string) (bool, int) {
  80. return ValidPrefixCodedTermBytes([]byte(p))
  81. }
  82. func ValidPrefixCodedTermBytes(p []byte) (bool, int) {
  83. if len(p) > 0 {
  84. if p[0] < ShiftStartInt64 || p[0] > ShiftStartInt64+63 {
  85. return false, 0
  86. }
  87. shift := p[0] - ShiftStartInt64
  88. nChars := ((63 - int(shift)) / 7) + 1
  89. if len(p) != nChars+1 {
  90. return false, 0
  91. }
  92. return true, int(shift)
  93. }
  94. return false, 0
  95. }