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.

65 lines
1.6 KiB

  1. package elastic
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. jsoniter "github.com/json-iterator/go"
  8. elastic "github.com/olivere/elastic/v7"
  9. )
  10. func (store *ElasticStore) KvDelete(ctx context.Context, key []byte) (err error) {
  11. deleteResult, err := store.client.Delete().
  12. Index(indexKV).
  13. Type(indexType).
  14. Id(string(key)).
  15. Do(ctx)
  16. if err == nil {
  17. if deleteResult.Result == "deleted" || deleteResult.Result == "not_found" {
  18. return nil
  19. }
  20. }
  21. glog.Errorf("delete key(id:%s) %v.", string(key), err)
  22. return fmt.Errorf("delete key %v.", err)
  23. }
  24. func (store *ElasticStore) KvGet(ctx context.Context, key []byte) (value []byte, err error) {
  25. searchResult, err := store.client.Get().
  26. Index(indexKV).
  27. Type(indexType).
  28. Id(string(key)).
  29. Do(ctx)
  30. if elastic.IsNotFound(err) {
  31. return value, filer.ErrKvNotFound
  32. }
  33. if searchResult != nil && searchResult.Found {
  34. esEntry := &ESKVEntry{}
  35. if err := jsoniter.Unmarshal(searchResult.Source, esEntry); err == nil {
  36. return esEntry.Value, nil
  37. }
  38. }
  39. glog.Errorf("find key(%s),%v.", string(key), err)
  40. return value, filer.ErrKvNotFound
  41. }
  42. func (store *ElasticStore) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
  43. esEntry := &ESKVEntry{value}
  44. val, err := jsoniter.Marshal(esEntry)
  45. if err != nil {
  46. glog.Errorf("insert key(%s) %v.", string(key), err)
  47. return fmt.Errorf("insert key %v.", err)
  48. }
  49. _, err = store.client.Index().
  50. Index(indexKV).
  51. Type(indexType).
  52. Id(string(key)).
  53. BodyJson(string(val)).
  54. Do(ctx)
  55. if err != nil {
  56. return fmt.Errorf("kv put: %v", err)
  57. }
  58. return nil
  59. }