2022-03-17 09:49:26 +00:00
|
|
|
package arangodb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2022-03-18 02:12:25 +00:00
|
|
|
"github.com/arangodb/go-driver"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
2022-03-17 09:49:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (store *ArangodbStore) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
|
|
|
|
model := &Model{
|
2022-03-18 02:12:25 +00:00
|
|
|
Key: hashString(".kvstore." + string(key)),
|
|
|
|
Directory: ".kvstore." + string(key),
|
2022-03-17 09:49:26 +00:00
|
|
|
Meta: bytesToArray(value),
|
|
|
|
}
|
|
|
|
|
2022-03-19 02:51:16 +00:00
|
|
|
exists, err := store.kvCollection.DocumentExists(ctx, model.Key)
|
2022-03-17 09:49:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("kv put: %v", err)
|
|
|
|
}
|
|
|
|
if exists {
|
2022-03-19 02:51:16 +00:00
|
|
|
_, err = store.kvCollection.UpdateDocument(ctx, model.Key, model)
|
2022-03-17 09:49:26 +00:00
|
|
|
} else {
|
2022-03-19 02:51:16 +00:00
|
|
|
_, err = store.kvCollection.CreateDocument(ctx, model)
|
2022-03-17 09:49:26 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("kv put: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (store *ArangodbStore) KvGet(ctx context.Context, key []byte) (value []byte, err error) {
|
|
|
|
var model Model
|
2022-03-19 02:51:16 +00:00
|
|
|
_, err = store.kvCollection.ReadDocument(ctx, hashString(".kvstore."+string(key)), &model)
|
2022-03-18 02:12:25 +00:00
|
|
|
if driver.IsNotFound(err) {
|
|
|
|
return nil, filer.ErrKvNotFound
|
|
|
|
}
|
2022-03-17 09:49:26 +00:00
|
|
|
if err != nil {
|
2022-03-18 02:12:25 +00:00
|
|
|
glog.Errorf("kv get: %s %v", string(key), err)
|
2022-03-17 09:49:26 +00:00
|
|
|
return nil, filer.ErrKvNotFound
|
|
|
|
}
|
|
|
|
return arrayToBytes(model.Meta), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *ArangodbStore) KvDelete(ctx context.Context, key []byte) (err error) {
|
2022-03-19 02:51:16 +00:00
|
|
|
_, err = store.kvCollection.RemoveDocument(ctx, hashString(".kvstore."+string(key)))
|
2022-03-17 09:49:26 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("kv del: %v", err)
|
|
|
|
return filer.ErrKvNotFound
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|