<commit_before>from scaliendb_client import *
import time
from datetime import datetime
from datetime import date

SDBP_SUCCESS = 0
SDBP_API_ERROR = -1

SDBP_PARTIAL = -101
SDBP_FAILURE = -102

SDBP_NOMASTER = -201
SDBP_NOCONNECTION = -202
SDBP_NOPRIMARY = -203

SDBP_MASTER_TIMEOUT = -301
SDBP_GLOBAL_TIMEOUT = -302
SDBP_PRIMARY_TIMEOUT = -303

SDBP_NOSERVICE = -401
SDBP_FAILED = -402
SDBP_BADSCHEMA = -403

SDBP_CONSISTENCY_ANY = 0
SDBP_CONSISTENCY_RYW = 1
SDBP_CONSISTENCY_STRICT = 2

SDBP_BATCH_DEFAULT = 0
SDBP_BATCH_NOAUTOSUBMIT	= 1
SDBP_BATCH_SINGLE = 2

def composite(*args):
    c = ""
    for a in args:
        i = Client._typemap(a)
        c = "%s/%s" %  (c, i)
    return c
    	        
def str_status(status):
    if status == SDBP_SUCCESS:
        return "SDBP_SUCCESS"
    elif status == SDBP_API_ERROR:
        return "SDBP_API_ERROR"
    elif status == SDBP_PARTIAL:
        return "SDBP_PARTIAL"
    elif status == SDBP_FAILURE:
        return "SDBP_FAILURE"
    elif status == SDBP_NOMASTER:
        return "SDBP_NOMASTER"
    elif status == SDBP_NOCONNECTION:
        return "SDBP_NOCONNECTION"
    elif status == SDBP_NOPRIMARY:
        return "SDBP_NOPRIMARY"
    elif status == SDBP_MASTER_TIMEOUT:
        return "SDBP_MASTER_TIMEOUT"
    elif status == SDBP_GLOBAL_TIMEOUT:
        return "SDBP_GLOBAL_TIMEOUT"
    elif status == SDBP_PRIMARY_TIMEOUT:
        return "SDBP_PRIMARY_TIMEOUT"
    elif status == SDBP_NOSERVICE:
        return "SDBP_NOSERVICE"
    elif status == SDBP_FAILED:
        return "SDBP_FAILED"
    elif status == SDBP_BADSCHEMA:
        return "SDBP_BADSCHEMA"
    return "<UNKNOWN>"

# =============================================================================================
#
# Error
#
# =============================================================================================

class Error(Exception):
    def __init__(self, status, strerror=None):
        self.status = status
        self.strerror = strerror
    
    def __str__(self):
        if self.strerror != None:
            return str_status(self.status) + ": " + self.strerror
        return str_status(self.status)

# =============================================================================================
#
# Client
#
# =============================================================================================

class Client:

    # =========================================================================================
    #
    # Result
    #
    # =========================================================================================

    class Result:
        def __init__(self, cptr):
            self._cptr = cptr

        def __del__(self):
            self.close()
        
        def __iter__(self):
            self.begin()
            return self
        
        def close(self):
            SDBP_ResultClose(self._cptr)
            self._cptr = None
        
        def key(self):
            return SDBP_ResultKey(self._cptr)
        
        def value(self):
            return SDBP_ResultValue(self._cptr)

        def signed_number(self):
            return SDBP_ResultSignedNumber(self._cptr)
        
        def number(self):
            return SDBP_ResultNumber(self._cptr)
        
        def begin(self):
            return SDBP_ResultBegin(self._cptr)
        
        def is_end(self):
            return SDBP_ResultIsEnd(self._cptr)
        
        def is_finished(self):
            return SDBP_ResultIsFinished(self._cptr)
                
        def next(self):
            if self.is_end():
                raise StopIteration
            SDBP_ResultNext(self._cptr)
            return self
        
        def command_status(self):
            return SDBP_ResultCommandStatus(self._cptr)
        
        def transport_status(self):
            return SDBP_ResultTransportStatus(self._cptr)
        
        def connectivity_status(self):
            return SDBP_ResultConnectivityStatus(self._cptr)
        
        def timeout_status(self):
            return SDBP_ResultTimeoutStatus(self._cptr)
        
        def num_nodes(self):
            return SDBP_ResultNumNodes(self._cptr)
        
        def node_id(self, node=0):
            return SDBP_ResultNodeID(self._cptr, node)
        
        def elapsed_time(self):
            return SDBP_ResultElapsedTime(self._cptr)
        
        def keys(self):
            keys = []
            self.begin()
            while not self.is_end():
                keys.append(self.key())
                self.next()
            return keys
        
        def key_values(self):
            kv = {}
            self.begin()
            while not self.is_end():
                kv[self.key()] = self.value()
                self.next()
            return kv
    
    # =========================================================================================
    #
    # Quorum
    #
    # =========================================================================================

    class Quorum:
        def __init__(self, client, quorum_id, name):
            self._client = client
            self._quorum_id = quorum_id
            self.name = name

    # =========================================================================================
    #
    # Database
    #
    # =========================================================================================

    class Database:
        def __init__(self, client, database_id, name):
            self._client = client
            self._database_id = database_id
            self.name = name

        def get_table(self, name):
            tables = self.get_tables()
            for table in tables:
                if table.name == name:
                    return table;
            return None

        def get_tables(self):
            num_tables = SDBP_GetNumTables(self._client._cptr, self._database_id)
            tables = []
            for i in xrange(num_tables):
                tableID = SDBP_GetTableIDAt(self._client._cptr, self._database_id, i)
                name = SDBP_GetTableNameAt(self._client._cptr, self._database_id, i)
                tables.append(Client.Table(self._client, self, tableID, name))
            return tables
        
        def create_table(self, name, quorum=None):
            if quorum is None:
                quorums = self._client.get_quorums()
                if len(quorums) < 1:
                    raise Error(SDBP_BADSCHEMA, "No quorums")
                quorum = quorums[0]
            status = SDBP_CreateTable(self._client._cptr, self._database_id, quorum._quorum_id, name)
            self._client._result = Client.Result(SDBP_GetResult(self._client._cptr))
            self._client._check_status(status)
            return self.get_table(name)
        
        def rename_database(self, name):
            status = SDBP_RenameDatabase(self._client._cptr, self._database_id, name)
            self._client._check_status(status)
            self.name = name

        def delete_database(self):
            status = SDBP_DeleteDatabase(self._client._cptr, self._database_id)
            self._client._check_status(status)


    # =========================================================================================
    #
    # Table
    #
    # =========================================================================================

    class Table:
        def __init__(self, client, database, table_id, name):
            self._client = client
            self.database = database
            self._table_id = table_id
            self.name = name

        def rename_table(self, name):
            status = SDBP_RenameTable(self._client._cptr, self._table_id, name)
            self._client._check_status(status)
            self.name = name
        
        def delete_table(self):
            status = SDBP_DeleteTable(self._client._cptr, self._table_id)
            self._client._check_status(status)
        
        def truncate_table(self):
            status = SDBP_TruncateTable(self._client._cptr, self._table_id)
            self._client._check_status(status)
                        
        def get(self, key):
            return self._client._get(self._table_id, key)

        def set(self, key, value):
            return self._client._set(self._table_id, key, value)

        def add(self, key, number):
            return self._client._add(self._table_id, key, number)
        
        def delete(self, key):
            return self._client._delete(self._table_id, key)

        def count(self, prefix="", start_key="", end_key="", direction="forward"):
            return self._client._count(self._table_id, prefix, start_key, end_key, direction)
        
        def get_key_iterator(self, prefix="", start_key="", end_key="", count=-1, direction="forward"):
            return self._client.Iterator(self, prefix, start_key, end_key, count, direction, False)
    
        def get_key_value_iterator(self, prefix="", start_key="", end_key="", count=-1, direction="forward"):
            return self._client.Iterator(self, prefix, start_key, end_key, count, direction, True)
            
        def get_sequence(self, key):
            return self._client.Sequence(self, key)
            

    # =========================================================================================
    #
    # Iterator
    #
    # =========================================================================================

    class Iterator:
        def __init__(self, table, prefix, start_key, end_key, count, direction, values):
            self._table = table
            self._start_key = start_key
            self._end_key = end_key
            self._prefix = prefix
            self._count = count
            self._direction = direction
            self._values = values
            self._gran = 100
            self._result = []
            self._pos = 0
            self._len = 0

        def _query(self, skip):
            num = self._gran
            if self._count > 0 and self._count < self._gran:
                num = self._count
            if self._values:
                self._result = self._table._client._list_key_values(self._table._table_id, self._prefix, self._start_key, self._end_key, num, self._direction, skip).items()
            else:
                self._result = self._table._client._list_keys(self._table._table_id, self._prefix, self._start_key, self._end_key, num, self._direction, skip)
            #self._result.sort()
            self._len = len(self._result)
            self._pos = 0
        
        def __iter__(self):
            return self

        def next(self):
            if self._count == 0:
                raise StopIteration
            if self._len == 0:
                self._query(False)
            else:
                self._pos += 1
                if self._pos == self._len:
                    if self._len < self._gran:
                        raise StopIteration
                    if self._values:
                        self._start_key = self._result[self._len-1][0]
                    else:
                        self._start_key = self._result[self._len-1]
                    self._query(True)                        
            if self._len == 0:
                raise StopIteration
            self._count -= 1
            return self._result[self._pos]

    # =============================================================================================
    #
    # Sequence
    #
    # =============================================================================================

    class Sequence:
        def __init__(self, table, key):
            self._table = table
            self._key = key
        
        def set_granularity(self, gran):
            pass
        
        def reset(self):
            self._table._client._sequence_set(self._table._table_id, self._key, 1)

        def set(self, value):
            self._table._client._sequence_set(self._table._table_id, self._key, value)
        
        def next(self):
            return self._table._client._sequence_next(self._table._table_id, self._key)
        
    # =============================================================================================
    #
    # Submitter
    #
    # =============================================================================================

    class Submitter:
        def __init__(self, client):
            self._client = client
                
        def __enter__(self):
            return self

        def __exit__(self, type, value, traceback):
            self._client.submit()

    # =============================================================================================
    #
    # Client
    #
    # =============================================================================================

    def __init__(self, nodes):
        self._cptr = SDBP_Create()
        self._result = None
        if isinstance(nodes, list):
            node_params = SDBP_NodeParams(len(nodes))
            for node in nodes:
                node_params.AddNode(node)
            SDBP_Init(self._cptr, node_params)
            node_params.Close()
        elif isinstance(nodes, str):
            node_params = SDBP_NodeParams(1)
            node_params.AddNode(nodes)
            SDBP_Init(self._cptr, node_params)
            node_params.Close()<commit_msg>Changed Python client to work with comma separated controllers string.<commit_after>from scaliendb_client import *
import time
from datetime import datetime
from datetime import date

SDBP_SUCCESS = 0
SDBP_API_ERROR = -1

SDBP_PARTIAL = -101
SDBP_FAILURE = -102

SDBP_NOMASTER = -201
SDBP_NOCONNECTION = -202
SDBP_NOPRIMARY = -203

SDBP_MASTER_TIMEOUT = -301
SDBP_GLOBAL_TIMEOUT = -302
SDBP_PRIMARY_TIMEOUT = -303

SDBP_NOSERVICE = -401
SDBP_FAILED = -402
SDBP_BADSCHEMA = -403

SDBP_CONSISTENCY_ANY = 0
SDBP_CONSISTENCY_RYW = 1
SDBP_CONSISTENCY_STRICT = 2

SDBP_BATCH_DEFAULT = 0
SDBP_BATCH_NOAUTOSUBMIT	= 1
SDBP_BATCH_SINGLE = 2

def composite(*args):
    c = ""
    for a in args:
        i = Client._typemap(a)
        c = "%s/%s" %  (c, i)
    return c
    	        
def str_status(status):
    if status == SDBP_SUCCESS:
        return "SDBP_SUCCESS"
    elif status == SDBP_API_ERROR:
        return "SDBP_API_ERROR"
    elif status == SDBP_PARTIAL:
        return "SDBP_PARTIAL"
    elif status == SDBP_FAILURE:
        return "SDBP_FAILURE"
    elif status == SDBP_NOMASTER:
        return "SDBP_NOMASTER"
    elif status == SDBP_NOCONNECTION:
        return "SDBP_NOCONNECTION"
    elif status == SDBP_NOPRIMARY:
        return "SDBP_NOPRIMARY"
    elif status == SDBP_MASTER_TIMEOUT:
        return "SDBP_MASTER_TIMEOUT"
    elif status == SDBP_GLOBAL_TIMEOUT:
        return "SDBP_GLOBAL_TIMEOUT"
    elif status == SDBP_PRIMARY_TIMEOUT:
        return "SDBP_PRIMARY_TIMEOUT"
    elif status == SDBP_NOSERVICE:
        return "SDBP_NOSERVICE"
    elif status == SDBP_FAILED:
        return "SDBP_FAILED"
    elif status == SDBP_BADSCHEMA:
        return "SDBP_BADSCHEMA"
    return "<UNKNOWN>"

# =============================================================================================
#
# Error
#
# =============================================================================================

class Error(Exception):
    def __init__(self, status, strerror=None):
        self.status = status
        self.strerror = strerror
    
    def __str__(self):
        if self.strerror != None:
            return str_status(self.status) + ": " + self.strerror
        return str_status(self.status)

# =============================================================================================
#
# Client
#
# =============================================================================================

class Client:

    # =========================================================================================
    #
    # Result
    #
    # =========================================================================================

    class Result:
        def __init__(self, cptr):
            self._cptr = cptr

        def __del__(self):
            self.close()
        
        def __iter__(self):
            self.begin()
            return self
        
        def close(self):
            SDBP_ResultClose(self._cptr)
            self._cptr = None
        
        def key(self):
            return SDBP_ResultKey(self._cptr)
        
        def value(self):
            return SDBP_ResultValue(self._cptr)

        def signed_number(self):
            return SDBP_ResultSignedNumber(self._cptr)
        
        def number(self):
            return SDBP_ResultNumber(self._cptr)
        
        def begin(self):
            return SDBP_ResultBegin(self._cptr)
        
        def is_end(self):
            return SDBP_ResultIsEnd(self._cptr)
        
        def is_finished(self):
            return SDBP_ResultIsFinished(self._cptr)
                
        def next(self):
            if self.is_end():
                raise StopIteration
            SDBP_ResultNext(self._cptr)
            return self
        
        def command_status(self):
            return SDBP_ResultCommandStatus(self._cptr)
        
        def transport_status(self):
            return SDBP_ResultTransportStatus(self._cptr)
        
        def connectivity_status(self):
            return SDBP_ResultConnectivityStatus(self._cptr)
        
        def timeout_status(self):
            return SDBP_ResultTimeoutStatus(self._cptr)
        
        def num_nodes(self):
            return SDBP_ResultNumNodes(self._cptr)
        
        def node_id(self, node=0):
            return SDBP_ResultNodeID(self._cptr, node)
        
        def elapsed_time(self):
            return SDBP_ResultElapsedTime(self._cptr)
        
        def keys(self):
            keys = []
            self.begin()
            while not self.is_end():
                keys.append(self.key())
                self.next()
            return keys
        
        def key_values(self):
            kv = {}
            self.begin()
            while not self.is_end():
                kv[self.key()] = self.value()
                self.next()
            return kv
    
    # =========================================================================================
    #
    # Quorum
    #
    # =========================================================================================

    class Quorum:
        def __init__(self, client, quorum_id, name):
            self._client = client
            self._quorum_id = quorum_id
            self.name = name

    # =========================================================================================
    #
    # Database
    #
    # =========================================================================================

    class Database:
        def __init__(self, client, database_id, name):
            self._client = client
            self._database_id = database_id
            self.name = name

        def get_table(self, name):
            tables = self.get_tables()
            for table in tables:
                if table.name == name:
                    return table;
            return None

        def get_tables(self):
            num_tables = SDBP_GetNumTables(self._client._cptr, self._database_id)
            tables = []
            for i in xrange(num_tables):
                tableID = SDBP_GetTableIDAt(self._client._cptr, self._database_id, i)
                name = SDBP_GetTableNameAt(self._client._cptr, self._database_id, i)
                tables.append(Client.Table(self._client, self, tableID, name))
            return tables
        
        def create_table(self, name, quorum=None):
            if quorum is None:
                quorums = self._client.get_quorums()
                if len(quorums) < 1:
                    raise Error(SDBP_BADSCHEMA, "No quorums")
                quorum = quorums[0]
            status = SDBP_CreateTable(self._client._cptr, self._database_id, quorum._quorum_id, name)
            self._client._result = Client.Result(SDBP_GetResult(self._client._cptr))
            self._client._check_status(status)
            return self.get_table(name)
        
        def rename_database(self, name):
            status = SDBP_RenameDatabase(self._client._cptr, self._database_id, name)
            self._client._check_status(status)
            self.name = name

        def delete_database(self):
            status = SDBP_DeleteDatabase(self._client._cptr, self._database_id)
            self._client._check_status(status)


    # =========================================================================================
    #
    # Table
    #
    # =========================================================================================

    class Table:
        def __init__(self, client, database, table_id, name):
            self._client = client
            self.database = database
            self._table_id = table_id
            self.name = name

        def rename_table(self, name):
            status = SDBP_RenameTable(self._client._cptr, self._table_id, name)
            self._client._check_status(status)
            self.name = name
        
        def delete_table(self):
            status = SDBP_DeleteTable(self._client._cptr, self._table_id)
            self._client._check_status(status)
        
        def truncate_table(self):
            status = SDBP_TruncateTable(self._client._cptr, self._table_id)
            self._client._check_status(status)
                        
        def get(self, key):
            return self._client._get(self._table_id, key)

        def set(self, key, value):
            return self._client._set(self._table_id, key, value)

        def add(self, key, number):
            return self._client._add(self._table_id, key, number)
        
        def delete(self, key):
            return self._client._delete(self._table_id, key)

        def count(self, prefix="", start_key="", end_key="", direction="forward"):
            return self._client._count(self._table_id, prefix, start_key, end_key, direction)
        
        def get_key_iterator(self, prefix="", start_key="", end_key="", count=-1, direction="forward"):
            return self._client.Iterator(self, prefix, start_key, end_key, count, direction, False)
    
        def get_key_value_iterator(self, prefix="", start_key="", end_key="", count=-1, direction="forward"):
            return self._client.Iterator(self, prefix, start_key, end_key, count, direction, True)
            
        def get_sequence(self, key):
            return self._client.Sequence(self, key)
            

    # =========================================================================================
    #
    # Iterator
    #
    # =========================================================================================

    class Iterator:
        def __init__(self, table, prefix, start_key, end_key, count, direction, values):
            self._table = table
            self._start_key = start_key
            self._end_key = end_key
            self._prefix = prefix
            self._count = count
            self._direction = direction
            self._values = values
            self._gran = 100
            self._result = []
            self._pos = 0
            self._len = 0

        def _query(self, skip):
            num = self._gran
            if self._count > 0 and self._count < self._gran:
                num = self._count
            if self._values:
                self._result = self._table._client._list_key_values(self._table._table_id, self._prefix, self._start_key, self._end_key, num, self._direction, skip).items()
            else:
                self._result = self._table._client._list_keys(self._table._table_id, self._prefix, self._start_key, self._end_key, num, self._direction, skip)
            #self._result.sort()
            self._len = len(self._result)
            self._pos = 0
        
        def __iter__(self):
            return self

        def next(self):
            if self._count == 0:
                raise StopIteration
            if self._len == 0:
                self._query(False)
            else:
                self._pos += 1
                if self._pos == self._len:
                    if self._len < self._gran:
                        raise StopIteration
                    if self._values:
                        self._start_key = self._result[self._len-1][0]
                    else:
                        self._start_key = self._result[self._len-1]
                    self._query(True)                        
            if self._len == 0:
                raise StopIteration
            self._count -= 1
            return self._result[self._pos]

    # =============================================================================================
    #
    # Sequence
    #
    # =============================================================================================

    class Sequence:
        def __init__(self, table, key):
            self._table = table
            self._key = key
        
        def set_granularity(self, gran):
            pass
        
        def reset(self):
            self._table._client._sequence_set(self._table._table_id, self._key, 1)

        def set(self, value):
            self._table._client._sequence_set(self._table._table_id, self._key, value)
        
        def next(self):
            return self._table._client._sequence_next(self._table._table_id, self._key)
        
    # =============================================================================================
    #
    # Submitter
    #
    # =============================================================================================

    class Submitter:
        def __init__(self, client):
            self._client = client
                
        def __enter__(self):
            return self

        def __exit__(self, type, value, traceback):
            self._client.submit()

    # =============================================================================================
    #
    # Client
    #
    # =============================================================================================

    def __init__(self, nodes):
        self._cptr = SDBP_Create()
        self._result = None
        if isinstance(nodes, list):
            node_params = SDBP_NodeParams(len(nodes))
            for node in nodes:
                node_params.AddNode(node)
            SDBP_Init(self._cptr, node_params)
            node_params.Close()
        elif isinstance(nodes, str):
            nodes = nodes.split(",")
            node_params = SDBP_NodeParams(len(nodes))
            for node in nodes:
                node_params.AddNode(node)
            SDBP_Init(self._cptr, node_params)
            node_params.Close()
{'fraction_non_alphanumeric': 0.21239495020547183, 'fraction_numerical': 0.0046277442523416385, 'mean_word_length': 2.1016190148122633, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3389100', 'n_tokens_mistral': 7439, 'n_tokens_neox': 6918, 'n_words': 1394}