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.

89 lines
2.3 KiB

  1. package cassandra_store
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/filer"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/gocql/gocql"
  7. )
  8. /*
  9. Basically you need a table just like this:
  10. CREATE TABLE seaweed_files (
  11. path varchar,
  12. fids list<varchar>,
  13. PRIMARY KEY (path)
  14. );
  15. Need to match flat_namespace.FlatNamespaceStore interface
  16. Put(fullFileName string, fid string) (err error)
  17. Get(fullFileName string) (fid string, err error)
  18. Delete(fullFileName string) (fid string, err error)
  19. */
  20. type CassandraStore struct {
  21. cluster *gocql.ClusterConfig
  22. session *gocql.Session
  23. }
  24. func NewCassandraStore(keyspace string, hosts ...string) (c *CassandraStore, err error) {
  25. c = &CassandraStore{}
  26. c.cluster = gocql.NewCluster(hosts...)
  27. c.cluster.Keyspace = keyspace
  28. c.cluster.Consistency = gocql.Quorum
  29. c.session, err = c.cluster.CreateSession()
  30. if err != nil {
  31. glog.V(0).Infof("Failed to open cassandra store, hosts %v, keyspace %s", hosts, keyspace)
  32. }
  33. return
  34. }
  35. func (c *CassandraStore) Put(fullFileName string, fid string) (err error) {
  36. var input []string
  37. input = append(input, fid)
  38. if err := c.session.Query(
  39. `INSERT INTO seaweed_files (path, fids) VALUES (?, ?)`,
  40. fullFileName, input).Exec(); err != nil {
  41. glog.V(0).Infof("Failed to save file %s with id %s: %v", fullFileName, fid, err)
  42. return err
  43. }
  44. return nil
  45. }
  46. func (c *CassandraStore) Get(fullFileName string) (fid string, err error) {
  47. var output []string
  48. if err := c.session.Query(
  49. `select fids FROM seaweed_files WHERE path = ? LIMIT 1`,
  50. fullFileName).Consistency(gocql.One).Scan(&output); err != nil {
  51. if err != gocql.ErrNotFound {
  52. glog.V(0).Infof("Failed to find file %s: %v", fullFileName, fid, err)
  53. return "", filer.ErrNotFound
  54. }
  55. }
  56. if len(output) == 0 {
  57. return "", fmt.Errorf("No file id found for %s", fullFileName)
  58. }
  59. return output[0], nil
  60. }
  61. // Currently the fid is not returned
  62. func (c *CassandraStore) Delete(fullFileName string) (err error) {
  63. if err := c.session.Query(
  64. `DELETE FROM seaweed_files WHERE path = ?`,
  65. fullFileName).Exec(); err != nil {
  66. if err != gocql.ErrNotFound {
  67. glog.V(0).Infof("Failed to delete file %s: %v", fullFileName, err)
  68. }
  69. return err
  70. }
  71. return nil
  72. }
  73. func (c *CassandraStore) Close() {
  74. if c.session != nil {
  75. c.session.Close()
  76. }
  77. }