github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/connectors/cassandra/cassandra.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package cassandra
    22  
    23  import (
    24  	"github.com/gocql/gocql"
    25  	"github.com/pkg/errors"
    26  	"github.com/uber-go/dosa"
    27  	"github.com/uber-go/dosa/connectors/base"
    28  	"gopkg.in/yaml.v2"
    29  )
    30  
    31  const localhostipv4 = "127.0.0.1"
    32  
    33  // Connector implements the data access to the cassandra cluster
    34  type Connector struct {
    35  	base.Connector
    36  	config   *gocql.ClusterConfig
    37  	Session  *gocql.Session
    38  	KsMapper KeyspaceMapper
    39  }
    40  
    41  // NewConnector creates an instance of Connector
    42  func NewConnector(clusterConfig *gocql.ClusterConfig, ksMapper KeyspaceMapper, next dosa.Connector) (dosa.Connector, error) {
    43  	session, err := clusterConfig.CreateSession()
    44  	if err != nil {
    45  		return nil, errors.Wrap(err, "failed to create session to Cassandra")
    46  	}
    47  
    48  	c := Connector{
    49  		config:   clusterConfig,
    50  		KsMapper: ksMapper,
    51  		Session:  session,
    52  	}
    53  	return &c, nil
    54  }
    55  
    56  // Shutdown cleans up the connector
    57  func (c *Connector) Shutdown() error {
    58  	c.Session.Close()
    59  	return nil
    60  }
    61  
    62  func init() {
    63  	dosa.RegisterConnector("cassandra", func(args dosa.CreationArgs) (dosa.Connector, error) {
    64  		// set up defaults for configuration
    65  		config := gocql.NewCluster(localhostipv4)
    66  		keyspaceMapper := KeyspaceMapper(DefaultKeyspaceMapper)
    67  
    68  		// parameters were provided, so lets use them
    69  		if args != nil {
    70  			// TODO the most common case is hosts-only so make that easier
    71  			if configYaml, ok := args["yaml"]; ok {
    72  				if err := yaml.Unmarshal([]byte(configYaml.(string)), config); err != nil {
    73  					return nil, errors.Wrap(err, "Unable to parse yaml configuration")
    74  				}
    75  			}
    76  
    77  			if mapper, ok := args["keyspace_mapper"]; ok {
    78  				if m, ok := mapper.(KeyspaceMapper); ok {
    79  					keyspaceMapper = m
    80  				} else {
    81  					return nil, errors.Errorf("provided keyspace_mapper (type %T) does not implement KeyspaceMapper", mapper)
    82  				}
    83  			}
    84  		}
    85  
    86  		connector, err := NewConnector(config, keyspaceMapper, nil)
    87  		return connector, err
    88  	})
    89  }