2020-09-02 04:58:57 +00:00
|
|
|
package cassandra
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-16 18:10:12 +00:00
|
|
|
"encoding/base64"
|
2020-09-03 01:39:24 +00:00
|
|
|
"fmt"
|
2020-09-02 04:58:57 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
2020-09-03 01:39:24 +00:00
|
|
|
"github.com/gocql/gocql"
|
2020-09-02 04:58:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (store *CassandraStore) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
|
2020-09-03 01:39:24 +00:00
|
|
|
dir, name := genDirAndName(key)
|
|
|
|
|
|
|
|
if err := store.session.Query(
|
|
|
|
"INSERT INTO filemeta (directory,name,meta) VALUES(?,?,?) USING TTL ? ",
|
|
|
|
dir, name, value, 0).Exec(); err != nil {
|
|
|
|
return fmt.Errorf("kv insert: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-09-02 04:58:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 01:39:24 +00:00
|
|
|
func (store *CassandraStore) KvGet(ctx context.Context, key []byte) (data []byte, err error) {
|
|
|
|
dir, name := genDirAndName(key)
|
|
|
|
|
|
|
|
if err := store.session.Query(
|
|
|
|
"SELECT meta FROM filemeta WHERE directory=? AND name=?",
|
2021-08-10 12:10:57 +00:00
|
|
|
dir, name).Scan(&data); err != nil {
|
2020-09-03 01:39:24 +00:00
|
|
|
if err != gocql.ErrNotFound {
|
|
|
|
return nil, filer.ErrKvNotFound
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
return nil, filer.ErrKvNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
2020-09-02 04:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *CassandraStore) KvDelete(ctx context.Context, key []byte) (err error) {
|
2020-09-03 01:39:24 +00:00
|
|
|
dir, name := genDirAndName(key)
|
|
|
|
|
|
|
|
if err := store.session.Query(
|
|
|
|
"DELETE FROM filemeta WHERE directory=? AND name=?",
|
|
|
|
dir, name).Exec(); err != nil {
|
|
|
|
return fmt.Errorf("kv delete: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func genDirAndName(key []byte) (dir string, name string) {
|
|
|
|
for len(key) < 8 {
|
|
|
|
key = append(key, 0)
|
|
|
|
}
|
|
|
|
|
2020-10-16 18:10:12 +00:00
|
|
|
dir = base64.StdEncoding.EncodeToString(key[:8])
|
|
|
|
name = base64.StdEncoding.EncodeToString(key[8:])
|
2020-09-03 01:39:24 +00:00
|
|
|
|
|
|
|
return
|
2020-09-02 04:58:57 +00:00
|
|
|
}
|