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.

51 lines
1.0 KiB

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