2018-05-27 07:01:15 +00:00
|
|
|
package cassandra
|
|
|
|
|
|
|
|
import (
|
2019-03-15 22:55:34 +00:00
|
|
|
"context"
|
2018-05-27 07:01:15 +00:00
|
|
|
"fmt"
|
2020-03-08 01:01:39 +00:00
|
|
|
"github.com/gocql/gocql"
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
2018-05-27 07:01:15 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2020-03-08 01:01:39 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2018-08-19 22:17:55 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2018-05-27 07:01:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-09-01 07:21:19 +00:00
|
|
|
filer.Stores = append(filer.Stores, &CassandraStore{})
|
2018-05-27 07:01:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CassandraStore struct {
|
|
|
|
cluster *gocql.ClusterConfig
|
|
|
|
session *gocql.Session
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *CassandraStore) GetName() string {
|
|
|
|
return "cassandra"
|
|
|
|
}
|
|
|
|
|
2020-01-29 17:09:55 +00:00
|
|
|
func (store *CassandraStore) Initialize(configuration util.Configuration, prefix string) (err error) {
|
2018-05-27 07:01:15 +00:00
|
|
|
return store.initialize(
|
2020-01-29 17:09:55 +00:00
|
|
|
configuration.GetString(prefix+"keyspace"),
|
|
|
|
configuration.GetStringSlice(prefix+"hosts"),
|
2020-09-25 03:22:47 +00:00
|
|
|
configuration.GetString(prefix+"username"),
|
|
|
|
configuration.GetString(prefix+"password"),
|
2018-05-27 07:01:15 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-09-25 03:22:47 +00:00
|
|
|
func (store *CassandraStore) initialize(keyspace string, hosts []string, username string, password string) (err error) {
|
2018-05-27 07:01:15 +00:00
|
|
|
store.cluster = gocql.NewCluster(hosts...)
|
2020-09-25 03:22:47 +00:00
|
|
|
if username != "" && password != "" {
|
|
|
|
store.cluster.Authenticator = gocql.PasswordAuthenticator{Username: username, Password: password}
|
|
|
|
}
|
2018-05-27 07:01:15 +00:00
|
|
|
store.cluster.Keyspace = keyspace
|
|
|
|
store.cluster.Consistency = gocql.LocalQuorum
|
|
|
|
store.session, err = store.cluster.CreateSession()
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("Failed to open cassandra store, hosts %v, keyspace %s", hosts, keyspace)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-31 06:09:16 +00:00
|
|
|
func (store *CassandraStore) BeginTransaction(ctx context.Context) (context.Context, error) {
|
2019-03-31 06:08:29 +00:00
|
|
|
return ctx, nil
|
|
|
|
}
|
2019-03-31 06:09:16 +00:00
|
|
|
func (store *CassandraStore) CommitTransaction(ctx context.Context) error {
|
2019-03-31 06:08:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-03-31 06:09:16 +00:00
|
|
|
func (store *CassandraStore) RollbackTransaction(ctx context.Context) error {
|
2019-03-31 06:08:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (store *CassandraStore) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
2018-05-27 07:01:15 +00:00
|
|
|
|
|
|
|
dir, name := entry.FullPath.DirAndName()
|
|
|
|
meta, err := entry.EncodeAttributesAndChunks()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("encode %s: %s", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
2020-09-03 18:00:20 +00:00
|
|
|
if len(entry.Chunks) > 50 {
|
|
|
|
meta = util.MaybeGzipData(meta)
|
|
|
|
}
|
|
|
|
|
2018-05-27 07:01:15 +00:00
|
|
|
if err := store.session.Query(
|
2019-01-09 04:26:19 +00:00
|
|
|
"INSERT INTO filemeta (directory,name,meta) VALUES(?,?,?) USING TTL ? ",
|
|
|
|
dir, name, meta, entry.TtlSec).Exec(); err != nil {
|
2018-05-27 07:01:15 +00:00
|
|
|
return fmt.Errorf("insert %s: %s", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (store *CassandraStore) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
2018-05-27 07:01:15 +00:00
|
|
|
|
2019-03-15 22:55:34 +00:00
|
|
|
return store.InsertEntry(ctx, entry)
|
2018-05-27 07:01:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (store *CassandraStore) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer.Entry, err error) {
|
2018-05-27 07:01:15 +00:00
|
|
|
|
|
|
|
dir, name := fullpath.DirAndName()
|
|
|
|
var data []byte
|
|
|
|
if err := store.session.Query(
|
|
|
|
"SELECT meta FROM filemeta WHERE directory=? AND name=?",
|
|
|
|
dir, name).Consistency(gocql.One).Scan(&data); err != nil {
|
|
|
|
if err != gocql.ErrNotFound {
|
2020-03-08 01:01:39 +00:00
|
|
|
return nil, filer_pb.ErrNotFound
|
2018-05-27 07:01:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
2020-03-08 01:01:39 +00:00
|
|
|
return nil, filer_pb.ErrNotFound
|
2018-05-27 07:01:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
entry = &filer.Entry{
|
2018-05-27 07:01:15 +00:00
|
|
|
FullPath: fullpath,
|
|
|
|
}
|
2020-09-03 18:00:20 +00:00
|
|
|
err = entry.DecodeAttributesAndChunks(util.MaybeDecompressData(data))
|
2018-05-27 07:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
2020-03-23 07:01:34 +00:00
|
|
|
func (store *CassandraStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) error {
|
2018-05-27 07:01:15 +00:00
|
|
|
|
|
|
|
dir, name := fullpath.DirAndName()
|
|
|
|
|
|
|
|
if err := store.session.Query(
|
|
|
|
"DELETE FROM filemeta WHERE directory=? AND name=?",
|
|
|
|
dir, name).Exec(); err != nil {
|
2018-05-31 03:24:57 +00:00
|
|
|
return fmt.Errorf("delete %s : %v", fullpath, err)
|
2019-12-13 08:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-23 07:01:34 +00:00
|
|
|
func (store *CassandraStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) error {
|
2019-12-13 08:23:05 +00:00
|
|
|
|
|
|
|
if err := store.session.Query(
|
|
|
|
"DELETE FROM filemeta WHERE directory=?",
|
|
|
|
fullpath).Exec(); err != nil {
|
|
|
|
return fmt.Errorf("delete %s : %v", fullpath, err)
|
2018-05-27 07:01:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 03:24:57 +00:00
|
|
|
return nil
|
2018-05-27 07:01:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (store *CassandraStore) ListDirectoryPrefixedEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool, limit int, prefix string) (entries []*filer.Entry, err error) {
|
|
|
|
return nil, filer.ErrUnsupportedListDirectoryPrefixed
|
2020-08-05 17:19:16 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 07:01:34 +00:00
|
|
|
func (store *CassandraStore) ListDirectoryEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool,
|
2020-09-01 07:21:19 +00:00
|
|
|
limit int) (entries []*filer.Entry, err error) {
|
2018-05-27 07:01:15 +00:00
|
|
|
|
|
|
|
cqlStr := "SELECT NAME, meta FROM filemeta WHERE directory=? AND name>? ORDER BY NAME ASC LIMIT ?"
|
|
|
|
if inclusive {
|
|
|
|
cqlStr = "SELECT NAME, meta FROM filemeta WHERE directory=? AND name>=? ORDER BY NAME ASC LIMIT ?"
|
|
|
|
}
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
var name string
|
|
|
|
iter := store.session.Query(cqlStr, string(fullpath), startFileName, limit).Iter()
|
|
|
|
for iter.Scan(&name, &data) {
|
2020-09-01 07:21:19 +00:00
|
|
|
entry := &filer.Entry{
|
2020-03-23 07:01:34 +00:00
|
|
|
FullPath: util.NewFullPath(string(fullpath), name),
|
2018-05-27 07:01:15 +00:00
|
|
|
}
|
2020-09-03 18:00:20 +00:00
|
|
|
if decodeErr := entry.DecodeAttributesAndChunks(util.MaybeDecompressData(data)); decodeErr != nil {
|
2018-05-27 07:01:15 +00:00
|
|
|
err = decodeErr
|
|
|
|
glog.V(0).Infof("list %s : %v", entry.FullPath, err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
entries = append(entries, entry)
|
|
|
|
}
|
|
|
|
if err := iter.Close(); err != nil {
|
|
|
|
glog.V(0).Infof("list iterator close: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries, err
|
|
|
|
}
|
2020-03-15 03:30:26 +00:00
|
|
|
|
|
|
|
func (store *CassandraStore) Shutdown() {
|
|
|
|
store.session.Close()
|
|
|
|
}
|