util.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package wal
  14. import (
  15. "fmt"
  16. "log"
  17. "os"
  18. )
  19. func Exist(dirpath string) bool {
  20. names, err := readDir(dirpath)
  21. if err != nil {
  22. return false
  23. }
  24. return len(names) != 0
  25. }
  26. // searchIndex returns the last array index of names whose raft index section is
  27. // equal to or smaller than the given index.
  28. // The given names MUST be sorted.
  29. func searchIndex(names []string, index uint64) (int, bool) {
  30. for i := len(names) - 1; i >= 0; i-- {
  31. name := names[i]
  32. _, curIndex, err := parseWalName(name)
  33. if err != nil {
  34. log.Panicf("parse correct name should never fail: %v", err)
  35. }
  36. if index >= curIndex {
  37. return i, true
  38. }
  39. }
  40. return -1, false
  41. }
  42. // names should have been sorted based on sequence number.
  43. // isValidSeq checks whether seq increases continuously.
  44. func isValidSeq(names []string) bool {
  45. var lastSeq uint64
  46. for _, name := range names {
  47. curSeq, _, err := parseWalName(name)
  48. if err != nil {
  49. log.Panicf("parse correct name should never fail: %v", err)
  50. }
  51. if lastSeq != 0 && lastSeq != curSeq-1 {
  52. return false
  53. }
  54. lastSeq = curSeq
  55. }
  56. return true
  57. }
  58. // readDir returns the filenames in wal directory.
  59. func readDir(dirpath string) ([]string, error) {
  60. dir, err := os.Open(dirpath)
  61. if err != nil {
  62. return nil, err
  63. }
  64. defer dir.Close()
  65. names, err := dir.Readdirnames(-1)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return names, nil
  70. }
  71. func checkWalNames(names []string) []string {
  72. wnames := make([]string, 0)
  73. for _, name := range names {
  74. if _, _, err := parseWalName(name); err != nil {
  75. log.Printf("wal: parse %s error: %v", name, err)
  76. continue
  77. }
  78. wnames = append(wnames, name)
  79. }
  80. return wnames
  81. }
  82. func parseWalName(str string) (seq, index uint64, err error) {
  83. var num int
  84. num, err = fmt.Sscanf(str, "%016x-%016x.wal", &seq, &index)
  85. if num != 2 && err == nil {
  86. err = fmt.Errorf("bad wal name: %s", str)
  87. }
  88. return
  89. }
  90. func walName(seq, index uint64) string {
  91. return fmt.Sprintf("%016x-%016x.wal", seq, index)
  92. }
  93. func max(a, b int64) int64 {
  94. if a > b {
  95. return a
  96. }
  97. return b
  98. }