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.

35 lines
613 B

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package db
  2. func IteratePrefix(db DB, prefix []byte) Iterator {
  3. var start, end []byte
  4. if len(prefix) == 0 {
  5. start = BeginningKey()
  6. end = EndingKey()
  7. } else {
  8. start = cp(prefix)
  9. end = cpIncr(prefix)
  10. }
  11. return db.Iterator(start, end)
  12. }
  13. //----------------------------------------
  14. func cp(bz []byte) (ret []byte) {
  15. ret = make([]byte, len(bz))
  16. copy(ret, bz)
  17. return ret
  18. }
  19. // CONTRACT: len(bz) > 0
  20. func cpIncr(bz []byte) (ret []byte) {
  21. ret = cp(bz)
  22. for i := len(bz) - 1; i >= 0; i-- {
  23. if ret[i] < byte(0xFF) {
  24. ret[i] += 1
  25. return
  26. } else {
  27. ret[i] = byte(0x00)
  28. }
  29. }
  30. return EndingKey()
  31. }