Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLIENT-3106] Refactor aerospike.Client code #680

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 68 additions & 43 deletions src/main/client/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,6 @@
static int set_rack_aware_config(as_config *conf, PyObject *config_dict);
static int set_use_services_alternate(as_config *conf, PyObject *config_dict);

enum {
INIT_SUCCESS,
INIT_NO_CONFIG_ERR,
INIT_CONFIG_TYPE_ERR,
INIT_LUA_USER_ERR,
INIT_LUA_SYS_ERR,
INIT_HOST_TYPE_ERR,
INIT_EMPTY_HOSTS_ERR,
INIT_INVALID_ADRR_ERR,
INIT_SERIALIZE_ERR,
INIT_DESERIALIZE_ERR,
INIT_COMPRESSION_ERR,
INIT_POLICY_PARAM_ERR,
INIT_INVALID_AUTHMODE_ERR
};

/*******************************************************************************
* PYTHON DOC METHODS
******************************************************************************/
Expand Down Expand Up @@ -545,20 +529,37 @@ static PyObject *AerospikeClient_Type_New(PyTypeObject *type, PyObject *args,
return (PyObject *)self;
}

enum {
INIT_SUCCESS,
INIT_NO_CONFIG_ERR,
INIT_CONFIG_TYPE_ERR,
INIT_LUA_USER_ERR,
INIT_LUA_SYS_ERR,
INIT_HOST_TYPE_ERR,
INIT_EMPTY_HOSTS_ERR,
INIT_INVALID_ADRR_ERR,
INIT_SERIALIZE_ERR,
INIT_DESERIALIZE_ERR,
INIT_COMPRESSION_ERR,
INIT_POLICY_PARAM_ERR,
INIT_INVALID_AUTHMODE_ERR
};

static int AerospikeClient_Type_Init(AerospikeClient *self, PyObject *args,
PyObject *kwds)
{
PyObject *py_config = NULL;
int error_code = 0;
as_error constructor_err;
as_error_init(&constructor_err);
static char *kwlist[] = {"config", NULL};

self->has_connected = false;
self->use_shared_connection = false;
self->as = NULL;
self->send_bool_as = SEND_BOOL_AS_AS_BOOL;

int error_code = 0;

static char *kwlist[] = {"config", NULL};
PyObject *py_config = NULL;
if (PyArg_ParseTupleAndKeywords(args, kwds, "O:client", kwlist,
&py_config) == false) {
error_code = INIT_NO_CONFIG_ERR;
Expand Down Expand Up @@ -1143,39 +1144,63 @@ static int AerospikeClient_Type_Init(AerospikeClient *self, PyObject *args,
return -1;
}

static int set_rack_aware_config(as_config *conf, PyObject *config_dict)
static int set_rack_aware_config(as_config *conf, PyObject *py_config_dict)
{
PyObject *py_config_value;
long rack_id;
py_config_value = PyDict_GetItemString(config_dict, "rack_aware");
if (py_config_value) {
if (PyBool_Check(py_config_value)) {
conf->rack_aware = PyObject_IsTrue(py_config_value);
PyObject *py_rack_aware_key = PyUnicode_FromString("rack_aware");
if (py_rack_aware_key == NULL) {
PyErr_Clear();
return INIT_POLICY_PARAM_ERR;
}
PyObject *py_config_value =
PyDict_GetItemWithError(py_config_dict, py_rack_aware_key);
if (py_config_value == NULL) {
if (PyErr_Occurred()) {
PyErr_Clear();
return INIT_POLICY_PARAM_ERR;
}
else {
return INIT_POLICY_PARAM_ERR; // A non boolean was passed in as the value of rack_aware
// KeyError is ok
}
else if (!PyBool_Check(py_config_value)) {
return INIT_POLICY_PARAM_ERR;
}
else {
int config_value = PyObject_IsTrue(py_config_value);
if (config_value == -1) {
PyErr_Clear();
return INIT_POLICY_PARAM_ERR;
}
}

py_config_value = PyDict_GetItemString(config_dict, "rack_id");
if (py_config_value) {
if (PyLong_Check(py_config_value)) {
rack_id = PyLong_AsLong(py_config_value);
}
else {
return INIT_POLICY_PARAM_ERR; // A non integer passed in.
}
if (rack_id == -1 && PyErr_Occurred()) {
return INIT_POLICY_PARAM_ERR; // We had overflow.
PyObject *py_rack_id_key = PyUnicode_FromString("rack_id");
if (py_rack_id_key == NULL) {
PyErr_Clear();
return INIT_POLICY_PARAM_ERR;
}
long rack_id;
py_config_value = PyDict_GetItemWithError(py_config_dict, py_rack_id_key);
if (py_config_value == NULL) {
if (PyErr_Occurred()) {
PyErr_Clear();
return INIT_POLICY_PARAM_ERR;
}
}
else if (!PyLong_Check(py_config_value)) {
return INIT_POLICY_PARAM_ERR;
}
else {
rack_id = PyLong_AsLong(py_config_value);
}
if (rack_id == -1 && PyErr_Occurred()) {
return INIT_POLICY_PARAM_ERR; // We had overflow.
}

if (rack_id > INT_MAX || rack_id < INT_MIN) {
return INIT_POLICY_PARAM_ERR; // Magnitude too great for an integer in C.
}
conf->rack_id = (int)rack_id;
if (rack_id > INT_MAX || rack_id < INT_MIN) {
return INIT_POLICY_PARAM_ERR; // Magnitude too great for an integer in C.
}
conf->rack_id = (int)rack_id;

PyObject *rack_ids_pylist = PyDict_GetItemString(config_dict, "rack_ids");
PyObject *rack_ids_pylist =
PyDict_GetItemString(py_config_dict, "rack_ids");
if (rack_ids_pylist == NULL) {
return INIT_SUCCESS;
}
Expand Down
Loading