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.

53 lines
1.1 KiB

11 years ago
11 years ago
  1. // +build ignore
  2. package metastore
  3. import (
  4. "code.google.com/p/weed-fs/go/glog"
  5. "errors"
  6. "github.com/coreos/go-etcd/etcd"
  7. "strings"
  8. )
  9. // store data on etcd
  10. type MetaStoreEtcdBacking struct {
  11. client *etcd.Client
  12. }
  13. func NewMetaStoreEtcdBacking(etcdCluster string) *MetaStoreEtcdBacking {
  14. m := &MetaStoreEtcdBacking{}
  15. m.client = etcd.NewClient(strings.Split(etcdCluster, ","))
  16. return m
  17. }
  18. func (m MetaStoreEtcdBacking) Set(path, val string) error {
  19. res, e := m.client.Set(path, val, 0)
  20. glog.V(2).Infof("etcd set response: %+v\n", res)
  21. return e
  22. }
  23. func (m MetaStoreEtcdBacking) Get(path string) (string, error) {
  24. results, err := m.client.Get(path)
  25. for i, res := range results {
  26. glog.V(2).Infof("[%d] get response: %+v\n", i, res)
  27. }
  28. if err != nil {
  29. return "", err
  30. }
  31. if results[0].Key != path {
  32. return "", errors.New("Key Not Found:" + path)
  33. }
  34. return results[0].Value, nil
  35. }
  36. func (m MetaStoreEtcdBacking) Has(path string) (ok bool) {
  37. results, err := m.client.Get(path)
  38. if err != nil {
  39. return false
  40. }
  41. if results[0].Key != path {
  42. return false
  43. }
  44. return true
  45. }