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.

56 lines
1.0 KiB

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] += 1
  22. return
  23. } else {
  24. ret[i] = byte(0x00)
  25. if i == 0 {
  26. // Overflow
  27. return nil
  28. }
  29. }
  30. }
  31. return nil
  32. }
  33. // See DB interface documentation for more information.
  34. func IsKeyInDomain(key, start, end []byte, isReverse bool) bool {
  35. if !isReverse {
  36. if bytes.Compare(key, start) < 0 {
  37. return false
  38. }
  39. if end != nil && bytes.Compare(end, key) <= 0 {
  40. return false
  41. }
  42. return true
  43. } else {
  44. if start != nil && bytes.Compare(start, key) < 0 {
  45. return false
  46. }
  47. if end != nil && bytes.Compare(key, end) <= 0 {
  48. return false
  49. }
  50. return true
  51. }
  52. }