seaweedfs/weed/filer2/cassandra/cassandra_store.go

154 lines
4.1 KiB
Go
Raw Normal View History

2018-05-27 15:01:15 +08:00
package cassandra
import (
2019-03-16 06:55:34 +08:00
"context"
2018-05-27 15:01:15 +08:00
"fmt"
"github.com/chrislusf/seaweedfs/weed/filer2"
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/util"
2018-08-20 06:18:37 +08:00
"github.com/gocql/gocql"
2018-05-27 15:01:15 +08:00
)
func init() {
filer2.Stores = append(filer2.Stores, &CassandraStore{})
}
type CassandraStore struct {
cluster *gocql.ClusterConfig
session *gocql.Session
}
func (store *CassandraStore) GetName() string {
return "cassandra"
}
func (store *CassandraStore) Initialize(configuration util.Configuration, prefix string) (err error) {
2018-05-27 15:01:15 +08:00
return store.initialize(
configuration.GetString(prefix+"keyspace"),
configuration.GetStringSlice(prefix+"hosts"),
2018-05-27 15:01:15 +08:00
)
}
func (store *CassandraStore) initialize(keyspace string, hosts []string) (err error) {
store.cluster = gocql.NewCluster(hosts...)
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 14:09:16 +08:00
func (store *CassandraStore) BeginTransaction(ctx context.Context) (context.Context, error) {
return ctx, nil
}
2019-03-31 14:09:16 +08:00
func (store *CassandraStore) CommitTransaction(ctx context.Context) error {
return nil
}
2019-03-31 14:09:16 +08:00
func (store *CassandraStore) RollbackTransaction(ctx context.Context) error {
return nil
}
2019-03-16 06:55:34 +08:00
func (store *CassandraStore) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
2018-05-27 15:01:15 +08:00
dir, name := entry.FullPath.DirAndName()
meta, err := entry.EncodeAttributesAndChunks()
if err != nil {
return fmt.Errorf("encode %s: %s", entry.FullPath, err)
}
if err := store.session.Query(
2019-01-09 12:26:19 +08:00
"INSERT INTO filemeta (directory,name,meta) VALUES(?,?,?) USING TTL ? ",
dir, name, meta, entry.TtlSec).Exec(); err != nil {
2018-05-27 15:01:15 +08:00
return fmt.Errorf("insert %s: %s", entry.FullPath, err)
}
return nil
}
2019-03-16 06:55:34 +08:00
func (store *CassandraStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
2018-05-27 15:01:15 +08:00
2019-03-16 06:55:34 +08:00
return store.InsertEntry(ctx, entry)
2018-05-27 15:01:15 +08:00
}
2019-03-16 06:55:34 +08:00
func (store *CassandraStore) FindEntry(ctx context.Context, fullpath filer2.FullPath) (entry *filer2.Entry, err error) {
2018-05-27 15:01:15 +08: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 {
return nil, filer2.ErrNotFound
2018-05-27 15:01:15 +08:00
}
}
if len(data) == 0 {
return nil, filer2.ErrNotFound
2018-05-27 15:01:15 +08:00
}
entry = &filer2.Entry{
FullPath: fullpath,
}
err = entry.DecodeAttributesAndChunks(data)
if err != nil {
return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
}
return entry, nil
}
2019-03-16 06:55:34 +08:00
func (store *CassandraStore) DeleteEntry(ctx context.Context, fullpath filer2.FullPath) error {
2018-05-27 15:01:15 +08:00
dir, name := fullpath.DirAndName()
if err := store.session.Query(
"DELETE FROM filemeta WHERE directory=? AND name=?",
dir, name).Exec(); err != nil {
return fmt.Errorf("delete %s : %v", fullpath, err)
}
return nil
}
func (store *CassandraStore) DeleteFolderChildren(ctx context.Context, fullpath filer2.FullPath) error {
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 15:01:15 +08:00
}
return nil
2018-05-27 15:01:15 +08:00
}
2019-03-16 06:55:34 +08:00
func (store *CassandraStore) ListDirectoryEntries(ctx context.Context, fullpath filer2.FullPath, startFileName string, inclusive bool,
2018-05-27 15:01:15 +08:00
limit int) (entries []*filer2.Entry, err error) {
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) {
entry := &filer2.Entry{
FullPath: filer2.NewFullPath(string(fullpath), name),
}
if decodeErr := entry.DecodeAttributesAndChunks(data); decodeErr != nil {
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
}