<s> import os , sys <EOL> import time <EOL> import string <EOL> import base64 <EOL> from dbconstants import * <EOL> from dbinterface import * <EOL> import appscale_logger <EOL> import pycassa <EOL> from pycassa . system_manager import * <EOL> from thrift import Thrift <EOL> from thrift . transport import TSocket <EOL> from thrift . transport import TTransport <EOL> from thrift . protocol import TBinaryProtocol <EOL> from pycassa . cassandra . ttypes import NotFoundException <EOL> ERROR_DEFAULT = "<STR_LIT>" <EOL> SCHEMA_TABLE = "<STR_LIT>" <EOL> SCHEMA_TABLE_SCHEMA = [ '<STR_LIT>' ] <EOL> MAIN_TABLE = "<STR_LIT>" <EOL> PERSISTENT_CONNECTION = False <EOL> PROFILING = False <EOL> DEFAULT_HOST = "<STR_LIT:localhost>" <EOL> DEFAULT_PORT = <NUM_LIT> <EOL> CONSISTENCY_ONE = pycassa . cassandra . ttypes . ConsistencyLevel . ONE <EOL> CONSISTENCY_QUORUM = pycassa . cassandra . ttypes . ConsistencyLevel . QUORUM <EOL> MAX_ROW_COUNT = <NUM_LIT> <EOL> table_cache = { } <EOL> class DatastoreProxy ( AppDBInterface ) : <EOL> def __init__ ( self , logger = appscale_logger . getLogger ( "<STR_LIT>" ) ) : <EOL> f = open ( APPSCALE_HOME + '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> self . host = f . read ( ) <EOL> self . port = DEFAULT_PORT <EOL> self . pool = pycassa . ConnectionPool ( keyspace = '<STR_LIT>' , <EOL> server_list = [ self . host + "<STR_LIT::>" + str ( self . port ) ] , <EOL> prefill = False ) <EOL> f = open ( APPSCALE_HOME + '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> host = f . read ( ) <EOL> sys = SystemManager ( host + "<STR_LIT::>" + str ( DEFAULT_PORT ) ) <EOL> try : <EOL> sys . create_column_family ( '<STR_LIT>' , <EOL> SCHEMA_TABLE , <EOL> comparator_type = UTF8_TYPE ) <EOL> except Exception , e : <EOL> print "<STR_LIT>" % str ( e ) <EOL> pass <EOL> self . logger = logger <EOL> def logTiming ( self , function , start_time , end_time ) : <EOL> if PROFILING : <EOL> self . logger . debug ( function + "<STR_LIT>" + str ( end_time - start_time ) + "<STR_LIT>" ) <EOL> def get_entity ( self , table_name , row_key , column_names ) : <EOL> error = [ ERROR_DEFAULT ] <EOL> list = error <EOL> row_key = table_name + '<STR_LIT:/>' + row_key <EOL> try : <EOL> cf = pycassa . ColumnFamily ( self . pool , <EOL> string . replace ( table_name , '<STR_LIT:->' , '<STR_LIT:a>' ) ) <EOL> result = cf . get ( row_key , columns = column_names ) <EOL> for column in column_names : <EOL> list . append ( result [ column ] ) <EOL> except NotFoundException : <EOL> list [ <NUM_LIT:0> ] += "<STR_LIT>" <EOL> return list <EOL> except Exception , ex : <EOL> list [ <NUM_LIT:0> ] += ( "<STR_LIT>" % ex ) <EOL> return list <EOL> if len ( list ) == <NUM_LIT:1> : <EOL> list [ <NUM_LIT:0> ] += "<STR_LIT>" <EOL> return list <EOL> def put_entity ( self , table_name , row_key , column_names , cell_values ) : <EOL> error = [ ERROR_DEFAULT ] <EOL> list = error <EOL> if table_name not in table_cache : <EOL> self . create_table ( table_name , column_names ) <EOL> row_key = table_name + '<STR_LIT:/>' + row_key <EOL> cell_dict = { } <EOL> for index , ii in enumerate ( column_names ) : <EOL> cell_dict [ ii ] = cell_values [ index ] <EOL> try : <EOL> cf = pycassa . ColumnFamily ( self . pool , string . replace ( table_name , '<STR_LIT:->' , '<STR_LIT:a>' ) ) <EOL> except NotFoundException : <EOL> print "<STR_LIT>" % table_name <EOL> list [ <NUM_LIT:0> ] += ( "<STR_LIT>" % table_name ) <EOL> return list <EOL> cf . insert ( row_key , cell_dict ) <EOL> list . append ( "<STR_LIT:0>" ) <EOL> return list <EOL> def put_entity_dict ( self , table_name , row_key , value_dict ) : <EOL> raise NotImplementedError ( "<STR_LIT>" % self . __class__ ) <EOL> def get_table ( self , table_name , column_names ) : <EOL> error = [ ERROR_DEFAULT ] <EOL> result = error <EOL> keyslices = [ ] <EOL> start_key = table_name + "<STR_LIT:/>" <EOL> end_key = table_name + '<STR_LIT>' <EOL> try : <EOL> cf = pycassa . ColumnFamily ( self . pool , string . replace ( table_name , '<STR_LIT:->' , '<STR_LIT:a>' ) ) <EOL> keyslices = cf . get_range ( columns = column_names , <EOL> start = start_key , <EOL> finish = end_key ) <EOL> keyslices = list ( keyslices ) <EOL> except pycassa . NotFoundException , ex : <EOL> self . logger . debug ( "<STR_LIT>" % ex ) <EOL> return result <EOL> except Exception , ex : <EOL> self . logger . debug ( "<STR_LIT>" % ex ) <EOL> result [ <NUM_LIT:0> ] += "<STR_LIT>" + str ( ex ) <EOL> return result <EOL> for ii , entry in enumerate ( keyslices ) : <EOL> orddic = entry [ <NUM_LIT:1> ] <EOL> ordering_dict = { } <EOL> for col in orddic : <EOL> val = orddic [ col ] <EOL> ordering_dict [ col ] = val <EOL> if len ( ordering_dict ) == <NUM_LIT:0> : <EOL> continue <EOL> for column in column_names : <EOL> try : <EOL> result . append ( ordering_dict [ column ] ) <EOL> except : <EOL> result [ <NUM_LIT:0> ] += "<STR_LIT>" <EOL> return result <EOL> def delete_row ( self , table_name , row_key ) : <EOL> error = [ ERROR_DEFAULT ] <EOL> ret = error <EOL> row_key = table_name + '<STR_LIT:/>' + row_key <EOL> try : <EOL> cf = pycassa . ColumnFamily ( self . pool , string . replace ( table_name , '<STR_LIT:->' , '<STR_LIT:a>' ) ) <EOL> cf . remove ( row_key ) <EOL> except Exception , ex : <EOL> self . logger . debug ( "<STR_LIT>" % ex ) <EOL> ret [ <NUM_LIT:0> ] += ( "<STR_LIT>" % ex ) <EOL> return ret <EOL> ret . append ( "<STR_LIT:0>" ) <EOL> return ret <EOL> def get_schema ( self , table_name ) : <EOL> error = [ ERROR_DEFAULT ] <EOL> result = error <EOL> ret = self . get_entity ( SCHEMA_TABLE , <EOL> table_name , <EOL> SCHEMA_TABLE_SCHEMA ) <EOL> if len ( ret ) > <NUM_LIT:1> : <EOL> schema = ret [ <NUM_LIT:1> ] <EOL> else : <EOL> error [ <NUM_LIT:0> ] = ret [ <NUM_LIT:0> ] + "<STR_LIT>" <EOL> return error <EOL> schema = schema . split ( '<STR_LIT::>' ) <EOL> result = result + schema <EOL> return result <EOL> def delete_table ( self , table_name ) : <EOL> error = [ ERROR_DEFAULT ] <EOL> result = error <EOL> keyslices = [ ] <EOL> curtime = self . timestamp ( ) <EOL> start_key = table_name + "<STR_LIT:/>" <EOL> end_key = table_name + '<STR_LIT>' <EOL> try : <EOL> cf = pycassa . ColumnFamily ( self . pool , string . replace ( table_name , '<STR_LIT:->' , '<STR_LIT:a>' ) ) <EOL> cf . truncate ( ) <EOL> self . delete_row ( SCHEMA_TABLE , row_key ) <EOL> except Exception , ex : <EOL> self . logger . debug ( "<STR_LIT>" % ex ) <EOL> result [ <NUM_LIT:0> ] += ( "<STR_LIT>" % ex ) <EOL> return result <EOL> if table_name not in table_cache : <EOL> result [ <NUM_LIT:0> ] += "<STR_LIT>" <EOL> return result <EOL> if table_name in table_cache : <EOL> del table_cache [ table_name ] <EOL> return result <EOL> def create_table ( self , table_name , column_names ) : <EOL> if table_name == SCHEMA_TABLE : <EOL> return <EOL> columns = '<STR_LIT::>' . join ( column_names ) <EOL> row_key = table_name <EOL> ret = self . get_entity ( SCHEMA_TABLE , row_key , SCHEMA_TABLE_SCHEMA ) <EOL> if ret [ <NUM_LIT:0> ] != ERROR_DEFAULT : <EOL> f = open ( APPSCALE_HOME + '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> host = f . read ( ) <EOL> sysman = SystemManager ( host + "<STR_LIT::>" + str ( DEFAULT_PORT ) ) <EOL> print "<STR_LIT>" % table_name <EOL> try : <EOL> sysman . create_column_family ( '<STR_LIT>' , string . replace ( table_name , '<STR_LIT:->' , '<STR_LIT:a>' ) , comparator_type = UTF8_TYPE ) <EOL> print "<STR_LIT>" <EOL> self . put_entity ( SCHEMA_TABLE , row_key , SCHEMA_TABLE_SCHEMA , [ columns ] ) <EOL> except Exception , e : <EOL> print "<STR_LIT>" % str ( e ) <EOL> return <EOL> table_cache [ table_name ] = <NUM_LIT:1> <EOL> def __setup_connection ( self ) : <EOL> return self . pool . get ( ) <EOL> def __close_connection ( self , client ) : <EOL> if client : <EOL> self . pool . return_conn ( client ) <EOL> def timestamp ( self ) : <EOL> return int ( time . time ( ) * <NUM_LIT:1000> ) </s>
