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.

98 lines
1.5 KiB

  1. // Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
  2. // All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style license that can be
  5. // found in the LICENSE file.
  6. package leveldb
  7. import (
  8. "fmt"
  9. "sort"
  10. "github.com/syndtr/goleveldb/leveldb/storage"
  11. )
  12. func shorten(str string) string {
  13. if len(str) <= 8 {
  14. return str
  15. }
  16. return str[:3] + ".." + str[len(str)-3:]
  17. }
  18. var bunits = [...]string{"", "Ki", "Mi", "Gi"}
  19. func shortenb(bytes int) string {
  20. i := 0
  21. for ; bytes > 1024 && i < 4; i++ {
  22. bytes /= 1024
  23. }
  24. return fmt.Sprintf("%d%sB", bytes, bunits[i])
  25. }
  26. func sshortenb(bytes int) string {
  27. if bytes == 0 {
  28. return "~"
  29. }
  30. sign := "+"
  31. if bytes < 0 {
  32. sign = "-"
  33. bytes *= -1
  34. }
  35. i := 0
  36. for ; bytes > 1024 && i < 4; i++ {
  37. bytes /= 1024
  38. }
  39. return fmt.Sprintf("%s%d%sB", sign, bytes, bunits[i])
  40. }
  41. func sint(x int) string {
  42. if x == 0 {
  43. return "~"
  44. }
  45. sign := "+"
  46. if x < 0 {
  47. sign = "-"
  48. x *= -1
  49. }
  50. return fmt.Sprintf("%s%d", sign, x)
  51. }
  52. func minInt(a, b int) int {
  53. if a < b {
  54. return a
  55. }
  56. return b
  57. }
  58. func maxInt(a, b int) int {
  59. if a > b {
  60. return a
  61. }
  62. return b
  63. }
  64. type fdSorter []storage.FileDesc
  65. func (p fdSorter) Len() int {
  66. return len(p)
  67. }
  68. func (p fdSorter) Less(i, j int) bool {
  69. return p[i].Num < p[j].Num
  70. }
  71. func (p fdSorter) Swap(i, j int) {
  72. p[i], p[j] = p[j], p[i]
  73. }
  74. func sortFds(fds []storage.FileDesc) {
  75. sort.Sort(fdSorter(fds))
  76. }
  77. func ensureBuffer(b []byte, n int) []byte {
  78. if cap(b) < n {
  79. return make([]byte, n)
  80. }
  81. return b[:n]
  82. }