arangodb_store_kv.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package arangodb
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/arangodb/go-driver"
  6. "github.com/seaweedfs/seaweedfs/weed/filer"
  7. "github.com/seaweedfs/seaweedfs/weed/glog"
  8. )
  9. func (store *ArangodbStore) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
  10. model := &Model{
  11. Key: hashString(".kvstore." + string(key)),
  12. Directory: ".kvstore." + string(key),
  13. Meta: bytesToArray(value),
  14. }
  15. exists, err := store.kvCollection.DocumentExists(ctx, model.Key)
  16. if err != nil {
  17. return fmt.Errorf("kv put: %v", err)
  18. }
  19. if exists {
  20. _, err = store.kvCollection.UpdateDocument(ctx, model.Key, model)
  21. } else {
  22. _, err = store.kvCollection.CreateDocument(ctx, model)
  23. }
  24. if err != nil {
  25. return fmt.Errorf("kv put: %v", err)
  26. }
  27. return nil
  28. }
  29. func (store *ArangodbStore) KvGet(ctx context.Context, key []byte) (value []byte, err error) {
  30. var model Model
  31. _, err = store.kvCollection.ReadDocument(ctx, hashString(".kvstore."+string(key)), &model)
  32. if driver.IsNotFound(err) {
  33. return nil, filer.ErrKvNotFound
  34. }
  35. if err != nil {
  36. glog.Errorf("kv get: %s %v", string(key), err)
  37. return nil, filer.ErrKvNotFound
  38. }
  39. return arrayToBytes(model.Meta), nil
  40. }
  41. func (store *ArangodbStore) KvDelete(ctx context.Context, key []byte) (err error) {
  42. _, err = store.kvCollection.RemoveDocument(ctx, hashString(".kvstore."+string(key)))
  43. if err != nil {
  44. glog.Errorf("kv del: %v", err)
  45. return filer.ErrKvNotFound
  46. }
  47. return nil
  48. }