Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 68 lines (57 sloc) 2.456 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.protocol import TBinaryProtocol

from pycassa.cassandra.c10 import Cassandra
from pycassa.cassandra.constants import (CASSANDRA_07, CASSANDRA_08, CASSANDRA_10)
from pycassa.cassandra.ttypes import AuthenticationRequest
from pycassa.util import compatible

DEFAULT_SERVER = 'localhost:9160'
DEFAULT_PORT = 9160

LOWEST_COMPATIBLE_VERSION = 17

class ApiMismatch(Exception): pass

class Connection(Cassandra.Client):
    """Encapsulation of a client session."""

    def __init__(self, keyspace, server, framed_transport=True, timeout=None,
                 credentials=None, api_version=None):
        self.keyspace = None
        self.server = server
        server = server.split(':')
        if len(server) <= 1:
            port = 9160
        else:
            port = server[1]
        host = server[0]
        socket = TSocket.TSocket(host, int(port))
        if timeout is not None:
            socket.setTimeout(timeout*1000.0)
        if framed_transport:
            self.transport = TTransport.TFramedTransport(socket)
        else:
            self.transport = TTransport.TBufferedTransport(socket)
        protocol = TBinaryProtocol.TBinaryProtocolAccelerated(self.transport)
        Cassandra.Client.__init__(self, protocol)
        self.transport.open()

        if api_version is None:
            server_api_version = self.describe_version()
            if compatible(CASSANDRA_10, server_api_version):
                self.version = CASSANDRA_10
            if compatible(CASSANDRA_08, server_api_version):
                self.version = CASSANDRA_08
            elif compatible(CASSANDRA_07, server_api_version):
                self.version = CASSANDRA_07
            else:
                raise ApiMismatch("Thrift API version incompatibility: " \
                                  "server version %s is not Cassandra 0.7, 0.8, or 1.0" %
                                  (server_api_version))
        else:
            self.version = api_version

        self.set_keyspace(keyspace)

        if credentials is not None:
            request = AuthenticationRequest(credentials=credentials)
            self.login(request)

    def set_keyspace(self, keyspace):
        if keyspace != self.keyspace:
            Cassandra.Client.set_keyspace(self, keyspace)
            self.keyspace = keyspace

    def close(self):
        self.transport.close()
Something went wrong with that request. Please try again.