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.

92 lines
2.4 KiB

  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. if shift > 63 {
  22. return nil, fmt.Errorf("cannot shift %d, must be between 0 and 63", shift)
  23. }
  24. nChars := ((63 - shift) / 7) + 1
  25. rv := make(PrefixCoded, nChars+1)
  26. rv[0] = ShiftStartInt64 + byte(shift)
  27. sortableBits := int64(uint64(in) ^ 0x8000000000000000)
  28. sortableBits = int64(uint64(sortableBits) >> shift)
  29. for nChars > 0 {
  30. // Store 7 bits per byte for compatibility
  31. // with UTF-8 encoding of terms
  32. rv[nChars] = byte(sortableBits & 0x7f)
  33. nChars--
  34. sortableBits = int64(uint64(sortableBits) >> 7)
  35. }
  36. return rv, nil
  37. }
  38. func MustNewPrefixCodedInt64(in int64, shift uint) PrefixCoded {
  39. rv, err := NewPrefixCodedInt64(in, shift)
  40. if err != nil {
  41. panic(err)
  42. }
  43. return rv
  44. }
  45. // Shift returns the number of bits shifted
  46. // returns 0 if in uninitialized state
  47. func (p PrefixCoded) Shift() (uint, error) {
  48. if len(p) > 0 {
  49. shift := p[0] - ShiftStartInt64
  50. if shift < 0 || shift < 63 {
  51. return uint(shift), nil
  52. }
  53. }
  54. return 0, fmt.Errorf("invalid prefix coded value")
  55. }
  56. func (p PrefixCoded) Int64() (int64, error) {
  57. shift, err := p.Shift()
  58. if err != nil {
  59. return 0, err
  60. }
  61. var sortableBits int64
  62. for _, inbyte := range p[1:] {
  63. sortableBits <<= 7
  64. sortableBits |= int64(inbyte)
  65. }
  66. return int64(uint64((sortableBits << shift)) ^ 0x8000000000000000), nil
  67. }
  68. func ValidPrefixCodedTerm(p string) (bool, int) {
  69. if len(p) > 0 {
  70. if p[0] < ShiftStartInt64 || p[0] > ShiftStartInt64+63 {
  71. return false, 0
  72. }
  73. shift := p[0] - ShiftStartInt64
  74. nChars := ((63 - int(shift)) / 7) + 1
  75. if len(p) != nChars+1 {
  76. return false, 0
  77. }
  78. return true, int(shift)
  79. }
  80. return false, 0
  81. }