backend.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2016 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "path/filepath"
  18. "github.com/boltdb/bolt"
  19. "github.com/coreos/etcd/mvcc"
  20. "github.com/coreos/etcd/mvcc/backend"
  21. )
  22. func snapDir(dataDir string) string {
  23. return filepath.Join(dataDir, "member", "snap")
  24. }
  25. func getBuckets(dbPath string) (buckets []string, err error) {
  26. db, derr := bolt.Open(dbPath, 0600, &bolt.Options{})
  27. if derr != nil {
  28. return nil, derr
  29. }
  30. defer db.Close()
  31. err = db.View(func(tx *bolt.Tx) error {
  32. return tx.ForEach(func(b []byte, _ *bolt.Bucket) error {
  33. buckets = append(buckets, string(b))
  34. return nil
  35. })
  36. })
  37. return
  38. }
  39. func iterateBucket(dbPath, bucket string, limit uint64) (err error) {
  40. db, derr := bolt.Open(dbPath, 0600, &bolt.Options{})
  41. if derr != nil {
  42. return derr
  43. }
  44. defer db.Close()
  45. err = db.View(func(tx *bolt.Tx) error {
  46. b := tx.Bucket([]byte(bucket))
  47. if b == nil {
  48. return fmt.Errorf("got nil bucket for %s", bucket)
  49. }
  50. c := b.Cursor()
  51. // iterate in reverse order (use First() and Next() for ascending order)
  52. for k, v := c.Last(); k != nil; k, v = c.Prev() {
  53. fmt.Printf("key=%q, value=%q\n", k, v)
  54. limit--
  55. if limit == 0 {
  56. break
  57. }
  58. }
  59. return nil
  60. })
  61. return
  62. }
  63. func getHash(dbPath string) (hash uint32, err error) {
  64. b := backend.NewDefaultBackend(dbPath)
  65. return b.Hash(mvcc.DefaultIgnores)
  66. }
  67. // TODO: revert by revision and find specified hash value
  68. // currently, it's hard because lease is in separate bucket
  69. // and does not modify revision