mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-10-19 20:44:28 +02:00
* cassandra: do not mask KvGet backend errors as not-found * use errors.Is for the gocql sentinel check
67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package cassandra2
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
|
|
gocql "github.com/apache/cassandra-gocql-driver/v2"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
func (store *Cassandra2Store) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
|
|
dir, name := genDirAndName(key)
|
|
|
|
if err := store.session.Query(
|
|
"INSERT INTO filemeta (dirhash,directory,name,meta) VALUES(?,?,?,?) USING TTL ? ",
|
|
util.HashStringToLong(dir), dir, name, value, 0).Exec(); err != nil {
|
|
return fmt.Errorf("kv insert: %s", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (store *Cassandra2Store) KvGet(ctx context.Context, key []byte) (data []byte, err error) {
|
|
dir, name := genDirAndName(key)
|
|
|
|
if err := store.session.Query(
|
|
"SELECT meta FROM filemeta WHERE dirhash=? AND directory=? AND name=?",
|
|
util.HashStringToLong(dir), dir, name).Scan(&data); err != nil {
|
|
if errors.Is(err, gocql.ErrNotFound) {
|
|
return nil, filer.ErrKvNotFound
|
|
}
|
|
return nil, fmt.Errorf("kv get: %w", err)
|
|
}
|
|
|
|
if len(data) == 0 {
|
|
return nil, filer.ErrKvNotFound
|
|
}
|
|
|
|
return data, nil
|
|
}
|
|
|
|
func (store *Cassandra2Store) KvDelete(ctx context.Context, key []byte) (err error) {
|
|
dir, name := genDirAndName(key)
|
|
|
|
if err := store.session.Query(
|
|
"DELETE FROM filemeta WHERE dirhash=? AND directory=? AND name=?",
|
|
util.HashStringToLong(dir), dir, name).Exec(); err != nil {
|
|
return fmt.Errorf("kv delete: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func genDirAndName(key []byte) (dir string, name string) {
|
|
for len(key) < 8 {
|
|
key = append(key, 0)
|
|
}
|
|
|
|
dir = base64.StdEncoding.EncodeToString(key[:8])
|
|
name = base64.StdEncoding.EncodeToString(key[8:])
|
|
|
|
return
|
|
}
|