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.

38 lines
623 B

7 years ago
7 years ago
  1. package kv
  2. import (
  3. "bytes"
  4. "sort"
  5. )
  6. //----------------------------------------
  7. // KVPair
  8. /*
  9. Defined in types.proto
  10. type Pair struct {
  11. Key []byte
  12. Value []byte
  13. }
  14. */
  15. type Pairs []Pair
  16. // Sorting
  17. func (kvs Pairs) Len() int { return len(kvs) }
  18. func (kvs Pairs) Less(i, j int) bool {
  19. switch bytes.Compare(kvs[i].Key, kvs[j].Key) {
  20. case -1:
  21. return true
  22. case 0:
  23. return bytes.Compare(kvs[i].Value, kvs[j].Value) < 0
  24. case 1:
  25. return false
  26. default:
  27. panic("invalid comparison result")
  28. }
  29. }
  30. func (kvs Pairs) Swap(i, j int) { kvs[i], kvs[j] = kvs[j], kvs[i] }
  31. func (kvs Pairs) Sort() { sort.Sort(kvs) }