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.

61 lines
1.3 KiB

  1. package cassandra
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer"
  6. "github.com/gocql/gocql"
  7. )
  8. func (store *CassandraStore) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
  9. dir, name := genDirAndName(key)
  10. if err := store.session.Query(
  11. "INSERT INTO filemeta (directory,name,meta) VALUES(?,?,?) USING TTL ? ",
  12. dir, name, value, 0).Exec(); err != nil {
  13. return fmt.Errorf("kv insert: %s", err)
  14. }
  15. return nil
  16. }
  17. func (store *CassandraStore) KvGet(ctx context.Context, key []byte) (data []byte, err error) {
  18. dir, name := genDirAndName(key)
  19. if err := store.session.Query(
  20. "SELECT meta FROM filemeta WHERE directory=? AND name=?",
  21. dir, name).Consistency(gocql.One).Scan(&data); err != nil {
  22. if err != gocql.ErrNotFound {
  23. return nil, filer.ErrKvNotFound
  24. }
  25. }
  26. if len(data) == 0 {
  27. return nil, filer.ErrKvNotFound
  28. }
  29. return data, nil
  30. }
  31. func (store *CassandraStore) KvDelete(ctx context.Context, key []byte) (err error) {
  32. dir, name := genDirAndName(key)
  33. if err := store.session.Query(
  34. "DELETE FROM filemeta WHERE directory=? AND name=?",
  35. dir, name).Exec(); err != nil {
  36. return fmt.Errorf("kv delete: %v", err)
  37. }
  38. return nil
  39. }
  40. func genDirAndName(key []byte) (dir string, name string) {
  41. for len(key) < 8 {
  42. key = append(key, 0)
  43. }
  44. dir = string(key[:8])
  45. name = string(key[8:])
  46. return
  47. }