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.

78 lines
1.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package db
  2. import (
  3. "bytes"
  4. )
  5. func cp(bz []byte) (ret []byte) {
  6. ret = make([]byte, len(bz))
  7. copy(ret, bz)
  8. return ret
  9. }
  10. // Returns a slice of the same length (big endian)
  11. // except incremented by one.
  12. // Returns nil on overflow (e.g. if bz bytes are all 0xFF)
  13. // CONTRACT: len(bz) > 0
  14. func cpIncr(bz []byte) (ret []byte) {
  15. if len(bz) == 0 {
  16. panic("cpIncr expects non-zero bz length")
  17. }
  18. ret = cp(bz)
  19. for i := len(bz) - 1; i >= 0; i-- {
  20. if ret[i] < byte(0xFF) {
  21. ret[i]++
  22. return
  23. }
  24. ret[i] = byte(0x00)
  25. if i == 0 {
  26. // Overflow
  27. return nil
  28. }
  29. }
  30. return nil
  31. }
  32. // Returns a slice of the same length (big endian)
  33. // except decremented by one.
  34. // Returns nil on underflow (e.g. if bz bytes are all 0x00)
  35. // CONTRACT: len(bz) > 0
  36. func cpDecr(bz []byte) (ret []byte) {
  37. if len(bz) == 0 {
  38. panic("cpDecr expects non-zero bz length")
  39. }
  40. ret = cp(bz)
  41. for i := len(bz) - 1; i >= 0; i-- {
  42. if ret[i] > byte(0x00) {
  43. ret[i]--
  44. return
  45. }
  46. ret[i] = byte(0xFF)
  47. if i == 0 {
  48. // Underflow
  49. return nil
  50. }
  51. }
  52. return nil
  53. }
  54. // See DB interface documentation for more information.
  55. func IsKeyInDomain(key, start, end []byte, isReverse bool) bool {
  56. if !isReverse {
  57. if bytes.Compare(key, start) < 0 {
  58. return false
  59. }
  60. if end != nil && bytes.Compare(end, key) <= 0 {
  61. return false
  62. }
  63. return true
  64. } else {
  65. if start != nil && bytes.Compare(start, key) < 0 {
  66. return false
  67. }
  68. if end != nil && bytes.Compare(key, end) <= 0 {
  69. return false
  70. }
  71. return true
  72. }
  73. }