static PyObject * _pycfunction_module_name(PyCFunctionObject *cfn) { PyObject *obj; PyObject *name; // The __module__ attribute, can be anything obj = cfn->m_module; if (!obj) { // TODO: Is this always correct? name = PyStr_FromString("__builtin__"); } else if (PyStr_Check(obj)) { Py_INCREF(obj); name = obj; } else if (PyModule_Check(obj)) { const char *s = PyModule_GetName(obj); if (!s) { goto error; } name = PyStr_FromString(s); } else { // Something else - str(obj) name = PyObject_Str(obj); } return name; error: PyErr_Clear(); return PyStr_FromString("<unknown>"); }
/** ******************************************************************************************************** * Delete the entire map(LDT Remove). * * @param self AerospikeLMap object * @param args The args is a tuple object containing an argument * list passed from Python to a C function * @param kwds Dictionary of keywords * * Returns an integer status. 0(Zero) is success value. * In case of error,appropriate exceptions will be raised. ******************************************************************************************************** */ PyObject * AerospikeLMap_Destroy(AerospikeLMap * self, PyObject * args, PyObject * kwds) { PyObject* py_policy = NULL; as_policy_apply apply_policy; as_policy_apply* apply_policy_p = NULL; as_error err; as_error_init(&err); static char * kwlist[] = {"policy", NULL}; // Python Function Argument Parsing if ( PyArg_ParseTupleAndKeywords(args, kwds, "|O:destroy", kwlist, &py_policy) == false ) { return NULL; } if (!self || !self->client->as) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object"); goto CLEANUP; } if (!self->client->is_conn_16) { as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster"); goto CLEANUP; } // Convert python policy object to as_policy_apply pyobject_to_policy_apply(&err, py_policy, &apply_policy, &apply_policy_p, &self->client->as->config.policies.apply); if ( err.code != AEROSPIKE_OK ) { as_error_update(&err, err.code, NULL); goto CLEANUP; } aerospike_lmap_destroy(self->client->as, &err, apply_policy_p, &self->key, &self->lmap); CLEANUP: if ( err.code != AEROSPIKE_OK ) { PyObject * py_err = NULL, *py_key = NULL; PyObject *exception_type = raise_exception(&err); error_to_pyobject(&err, &py_err); if(PyObject_HasAttrString(exception_type, "key")) { key_to_pyobject(&err, &self->key, &py_key); PyObject_SetAttrString(exception_type, "key", py_key); Py_DECREF(py_key); } if(PyObject_HasAttrString(exception_type, "bin")) { PyObject *py_bins = PyStr_FromString((char *)&self->bin_name); PyObject_SetAttrString(exception_type, "bin", py_bins); Py_DECREF(py_bins); } PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return PyLong_FromLong(0); }
static _pit * _ccode2pit(void *cco) { PyCFunctionObject *cfn; _hitem *it; PyObject *name; cfn = cco; // Issue #15: // Hashing cfn to the pits table causes different object methods // to be hashed into the same slot. Use cfn->m_ml for hashing the // Python C functions. it = hfind(current_ctx->pits, (uintptr_t)cfn->m_ml); if (!it) { _pit *pit = _create_pit(); if (!pit) return NULL; if (!hadd(current_ctx->pits, (uintptr_t)cfn->m_ml, (uintptr_t)pit)) return NULL; pit->builtin = 1; pit->modname = _pycfunction_module_name(cfn); pit->lineno = 0; // built-in method? if (cfn->m_self != NULL) { name = PyStr_FromString(cfn->m_ml->ml_name); if (name != NULL) { PyObject *obj_type = PyObject_Type(cfn->m_self); PyObject *mo = _PyType_Lookup((PyTypeObject *)obj_type, name); Py_XINCREF(mo); Py_XDECREF(obj_type); Py_DECREF(name); if (mo != NULL) { pit->name = PyObject_Repr(mo); Py_DECREF(mo); return pit; } } PyErr_Clear(); } pit->name = PyStr_FromString(cfn->m_ml->ml_name); return pit; } return ((_pit *)it->val); }
PyObject* raise_exception(as_error *err) { PyObject * py_key = NULL, *py_value = NULL; Py_ssize_t pos = 0; PyObject * py_module_dict = PyModule_GetDict(module); char * err_msg= err->message, *err_code = err->message; char *final_code = NULL; if(err->code == AEROSPIKE_ERR_UDF) { while(strstr(err_code, ": ") != NULL) { err_code++; } while(strstr(err_msg, ":LDT") != NULL) { err_msg++; } final_code = (char *)malloc(err_msg - err_code + 2); if(err_code != err->message && err_msg != err->message) { strncpy(final_code, err_code + 1, err_msg - err_code + 2); err->code = atoi(final_code); strcpy(err->message, err_msg); } free(final_code); } while(PyDict_Next(py_module_dict, &pos, &py_key, &py_value)) { if(PyObject_HasAttrString(py_value, "code")) { PyObject * py_code = PyObject_GetAttrString(py_value, "code"); if(py_code == Py_None) { continue; } if(err->code == PyInt_AsLong(py_code)) { PyObject *py_attr = NULL; py_attr = PyStr_FromString(err->message); PyObject_SetAttrString(py_value, "msg", py_attr); Py_DECREF(py_attr); py_attr = PyStr_FromString(err->file); PyObject_SetAttrString(py_value, "file", py_attr); Py_DECREF(py_attr); py_attr = PyInt_FromLong(err->line); PyObject_SetAttrString(py_value, "line", py_attr); Py_DECREF(py_attr); break; } Py_DECREF(py_code); } } return py_value; }
static PyObject *py_strstr_m(PyObject *self, PyObject *args) { char *s1, *s2, *ret; if (!PyArg_ParseTuple(args, "ss", &s1, &s2)) return NULL; ret = strstr_m(s1, s2); if (!ret) { Py_RETURN_NONE; } return PyStr_FromString(ret); }
static PyObject *py_generate_random_str(PyObject *self, PyObject *args) { int len; PyObject *ret; char *retstr; if (!PyArg_ParseTuple(args, "i", &len)) return NULL; retstr = generate_random_str(NULL, len); ret = PyStr_FromString(retstr); talloc_free(retstr); return ret; }
static PyObject *py_generate_random_password(PyObject *self, PyObject *args) { int min, max; PyObject *ret; char *retstr; if (!PyArg_ParseTuple(args, "ii", &min, &max)) return NULL; retstr = generate_random_password(NULL, min, max); if (retstr == NULL) { return NULL; } ret = PyStr_FromString(retstr); talloc_free(retstr); return ret; }
static PyObject *py_get_gplink_options(PyObject *self, PyObject *args) { int flags; PyObject *py_ret; const char **ret; TALLOC_CTX *mem_ctx; int i; NTSTATUS status; if (!PyArg_ParseTuple(args, "i", &flags)) return NULL; mem_ctx = talloc_new(NULL); if (mem_ctx == NULL) { PyErr_NoMemory(); return NULL; } status = gp_get_gplink_options(mem_ctx, flags, &ret); if (!NT_STATUS_IS_OK(status)) { PyErr_SetNTSTATUS(status); talloc_free(mem_ctx); return NULL; } py_ret = PyList_New(0); for (i = 0; ret[i]; i++) { PyObject *item = PyStr_FromString(ret[i]); if (item == NULL) { talloc_free(mem_ctx); Py_DECREF(py_ret); PyErr_NoMemory(); return NULL; } PyList_Append(py_ret, item); } talloc_free(mem_ctx); return py_ret; }
static PyObject *py_nttime2string(PyObject *self, PyObject *args) { PyObject *ret; NTTIME nt; TALLOC_CTX *tmp_ctx; const char *string; if (!PyArg_ParseTuple(args, "K", &nt)) return NULL; tmp_ctx = talloc_new(NULL); if (tmp_ctx == NULL) { PyErr_NoMemory(); return NULL; } string = nt_time_string(tmp_ctx, nt); ret = PyStr_FromString(string); talloc_free(tmp_ctx); return ret; }
AerospikeLList * AerospikeLList_New(AerospikeClient * client, PyObject * args, PyObject * kwds) { AerospikeLList * self = (AerospikeLList *) AerospikeLList_Type.tp_new(&AerospikeLList_Type, args, kwds); self->client = client; Py_INCREF(client); if (AerospikeLList_Type.tp_init((PyObject *)self, args, kwds) == 0) { return self; } else { as_error err; as_error_init(&err); as_error_update(&err, AEROSPIKE_ERR_PARAM, "Parameters are incorrect"); PyObject * py_err = NULL; PyObject * py_key = NULL; PyObject *exception_type = raise_exception(&err); error_to_pyobject(&err, &py_err); if(PyObject_HasAttrString(exception_type, "key")) { if(&self->key) { key_to_pyobject(&err, &self->key, &py_key); PyObject_SetAttrString(exception_type, "key", py_key); Py_DECREF(py_key); } else { PyObject_SetAttrString(exception_type, "key", Py_None); } } if(PyObject_HasAttrString(exception_type, "bin")) { if(&self->bin_name) { PyObject *py_bins = PyStr_FromString((char *)&self->bin_name); PyObject_SetAttrString(exception_type, "bin", py_bins); Py_DECREF(py_bins); } else { PyObject_SetAttrString(exception_type, "bin", Py_None); } } PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } }
/** ******************************************************************************************************* * This callback will be called with the results with aerospike_batch_exists(). * * @param results An array of n as_batch_read entries * @param n The number of results from the batch request * @param udata The return value to be filled with result of * exists_many() * * Returns boolean value(true or false). ******************************************************************************************************* */ static bool batch_exists_cb(const as_batch_read* results, uint32_t n, void* udata) { // Typecast udata back to PyObject PyObject * py_recs = (PyObject *) udata; // Loop over results array for ( uint32_t i =0; i < n; i++ ){ PyObject * rec = PyDict_New(); PyObject * py_rec = NULL; PyObject * p_key = NULL; py_rec = PyTuple_New(2); p_key = PyTuple_New(4); if ( results[i].key->ns && strlen(results[i].key->ns) > 0 ) { PyTuple_SetItem(p_key, 0, PyStr_FromString(results[i].key->ns)); } if ( results[i].key->set && strlen(results[i].key->set) > 0 ) { PyTuple_SetItem(p_key, 1, PyStr_FromString(results[i].key->set)); } if ( results[i].key->valuep ) { switch(((as_val*)(results[i].key->valuep))->type){ case AS_INTEGER: PyTuple_SetItem(p_key, 2, PyInt_FromLong((long)results[i].key->value.integer.value)); break; case AS_STRING: PyTuple_SetItem(p_key, 2, PyStr_FromString((const char *)results[i].key->value.string.value)); break; default: break; } } else { Py_INCREF(Py_None); PyTuple_SetItem(p_key, 2, Py_None); } if (results[i].key->digest.init) { PyTuple_SetItem(p_key, 3, PyByteArray_FromStringAndSize((char *) results[i].key->digest.value, AS_DIGEST_VALUE_SIZE)); } PyTuple_SetItem(py_rec, 0, p_key); if ( results[i].result == AEROSPIKE_OK ){ PyObject *py_gen = PyInt_FromLong((long)results[i].record.gen); PyDict_SetItemString( rec, "gen", py_gen ); Py_DECREF(py_gen); PyObject *py_ttl = PyInt_FromLong((long)results[i].record.ttl); PyDict_SetItemString( rec, "ttl", py_ttl ); Py_DECREF(py_ttl); PyTuple_SetItem(py_rec, 1, rec); if ( PyList_SetItem( py_recs, i, py_rec ) ){ return false; } } else if (results[i].result == AEROSPIKE_ERR_RECORD_NOT_FOUND){ Py_DECREF(rec); Py_INCREF(Py_None); PyTuple_SetItem(py_rec, 1, Py_None); if( PyList_SetItem( py_recs, i, py_rec)){ return false; } } } return true; }
/** ******************************************************************************************************** * Remove an object from the list. * * @param self AerospikeLList object * @param args The args is a tuple object containing an argument * list passed from Python to a C function * @param kwds Dictionary of keywords * * Returns an integer status. 0(Zero) is success value. * In case of error,appropriate exceptions will be raised. ******************************************************************************************************** */ PyObject * AerospikeLList_Remove(AerospikeLList * self, PyObject * args, PyObject * kwds) { PyObject* py_value = NULL; PyObject* py_policy = NULL; as_policy_apply apply_policy; as_policy_apply* apply_policy_p = NULL; as_val * val = NULL; as_static_pool static_pool; memset(&static_pool, 0, sizeof(static_pool)); as_error err; as_error_init(&err); static char * kwlist[] = {"value", "policy", NULL}; // Python Function Argument Parsing if ( PyArg_ParseTupleAndKeywords(args, kwds, "O|O:remove", kwlist, &py_value, &py_policy) == false ) { return NULL; } if (!self || !self->client->as) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object"); goto CLEANUP; } if (!self->client->is_conn_16) { as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster"); goto CLEANUP; } // Convert python policy object to as_policy_apply pyobject_to_policy_apply(&err, py_policy, &apply_policy, &apply_policy_p, &self->client->as->config.policies.apply); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } pyobject_to_val(self->client, &err, py_value, &val, &static_pool, SERIALIZER_PYTHON); if (err.code != AEROSPIKE_OK) { as_error_update(&err, err.code, NULL); goto CLEANUP; } Py_BEGIN_ALLOW_THREADS aerospike_llist_remove(self->client->as, &err, apply_policy_p, &self->key, &self->llist, val); Py_END_ALLOW_THREADS CLEANUP: if (val) { as_val_destroy(val); } if ( err.code != AEROSPIKE_OK ) { PyObject * py_err = NULL, *py_key = NULL; PyObject *exception_type = raise_exception(&err); error_to_pyobject(&err, &py_err); if(PyObject_HasAttrString(exception_type, "key")) { key_to_pyobject(&err, &self->key, &py_key); PyObject_SetAttrString(exception_type, "key", py_key); Py_DECREF(py_key); } if(PyObject_HasAttrString(exception_type, "bin")) { PyObject * py_bins = PyStr_FromString((char *)&self->bin_name); PyObject_SetAttrString(exception_type, "bin", py_bins); Py_DECREF(py_bins); } PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return PyLong_FromLong(0); }
/** ******************************************************************************************************** * Select values from the end of list up to a maximum count. * * @param self AerospikeLList object * @param args The args is a tuple object containing an argument * list passed from Python to a C function * @param kwds Dictionary of keywords * * Returns a list of ldt contents. * In case of error,appropriate exceptions will be raised. ******************************************************************************************************** */ PyObject * AerospikeLList_Find_Last(AerospikeLList * self, PyObject * args, PyObject * kwds) { PyObject* py_count = NULL; PyObject* py_policy = NULL; PyObject* py_elements_list = NULL; as_policy_apply apply_policy; as_policy_apply* apply_policy_p = NULL; as_list* elements_list = NULL; as_static_pool static_pool; memset(&static_pool, 0, sizeof(static_pool)); as_error err; as_error_init(&err); static char * kwlist[] = {"count", "policy", NULL}; // Python Function Argument Parsing if ( PyArg_ParseTupleAndKeywords(args, kwds, "O|O:find_last", kwlist, &py_count, &py_policy)== false ) { return NULL; } if (!self || !self->client->as) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object"); goto CLEANUP; } if (!self->client->is_conn_16) { as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster"); goto CLEANUP; } // Convert python policy object to as_policy_apply pyobject_to_policy_apply(&err, py_policy, &apply_policy, &apply_policy_p, &self->client->as->config.policies.apply); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } uint32_t count = 0; if ( PyInt_Check(py_count)) { count = PyInt_AsLong(py_count); } else if( PyLong_Check(py_count) ) { count = PyLong_AsLong(py_count); if (count == (uint32_t)-1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "integer value exceeds sys.maxsize"); goto CLEANUP; } } } else { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Count should be an integer or long"); goto CLEANUP; } Py_BEGIN_ALLOW_THREADS aerospike_llist_find_last(self->client->as, &err, apply_policy_p, &self->key, &self->llist, count, &elements_list); Py_END_ALLOW_THREADS if(err.code != AEROSPIKE_OK) { goto CLEANUP; } list_to_pyobject(self->client, &err, elements_list, &py_elements_list); CLEANUP: if (elements_list) { as_list_destroy(elements_list); } if ( err.code != AEROSPIKE_OK ) { PyObject * py_err = NULL, *py_key = NULL; PyObject *exception_type = raise_exception(&err); error_to_pyobject(&err, &py_err); if (PyObject_HasAttrString(exception_type, "key")) { key_to_pyobject(&err, &self->key, &py_key); PyObject_SetAttrString(exception_type, "key", py_key); Py_DECREF(py_key); } if (PyObject_HasAttrString(exception_type, "bin")) { PyObject *py_bins = PyStr_FromString((char *)&self->bin_name); PyObject_SetAttrString(exception_type, "bin", py_bins); Py_DECREF(py_bins); } PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return py_elements_list; }
/* return the list of interface IPs we have configured takes an loadparm context, returns a list of IPs in string form Does not return addresses on 127.0.0.0/8 */ static PyObject *py_interface_ips(PyObject *self, PyObject *args) { PyObject *pylist; int count; TALLOC_CTX *tmp_ctx; PyObject *py_lp_ctx; struct loadparm_context *lp_ctx; struct interface *ifaces; int i, ifcount; int all_interfaces = 1; if (!PyArg_ParseTuple(args, "O|i", &py_lp_ctx, &all_interfaces)) return NULL; tmp_ctx = talloc_new(NULL); if (tmp_ctx == NULL) { PyErr_NoMemory(); return NULL; } lp_ctx = lpcfg_from_py_object(tmp_ctx, py_lp_ctx); if (lp_ctx == NULL) { talloc_free(tmp_ctx); return NULL; } load_interface_list(tmp_ctx, lp_ctx, &ifaces); count = iface_list_count(ifaces); /* first count how many are not loopback addresses */ for (ifcount = i = 0; i<count; i++) { const char *ip = iface_list_n_ip(ifaces, i); if (all_interfaces) { ifcount++; continue; } if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) { continue; } if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) { continue; } if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) { continue; } if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) { continue; } ifcount++; } pylist = PyList_New(ifcount); for (ifcount = i = 0; i<count; i++) { const char *ip = iface_list_n_ip(ifaces, i); if (all_interfaces) { PyList_SetItem(pylist, ifcount, PyStr_FromString(ip)); ifcount++; continue; } if (iface_list_same_net(ip, "127.0.0.1", "255.0.0.0")) { continue; } if (iface_list_same_net(ip, "169.254.0.0", "255.255.0.0")) { continue; } if (iface_list_same_net(ip, "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")) { continue; } if (iface_list_same_net(ip, "fe80::", "ffff:ffff:ffff:ffff::")) { continue; } PyList_SetItem(pylist, ifcount, PyStr_FromString(ip)); ifcount++; } talloc_free(tmp_ctx); return pylist; }
/* ******************************************************************************************************* * Checks serializer_policy. * Serializes Py_Object (value) into as_bytes using serialization logic * based on serializer_policy. * * @param serializer_policy The serializer_policy to be used to handle * the serialization. * @param bytes The as_bytes to be set. * @param value The value to be serialized. * @param error_p The as_error to be populated by the function * with encountered error if any. ******************************************************************************************************* */ extern PyObject * serialize_based_on_serializer_policy(AerospikeClient * self, int32_t serializer_policy, as_bytes **bytes, PyObject *value, as_error *error_p) { uint8_t use_client_serializer = true; PyObject* initresult = NULL; if (self->is_client_put_serializer) { if (serializer_policy == SERIALIZER_USER) { if (!self->user_serializer_call_info.callback) { use_client_serializer = false; } } } else if (self->user_serializer_call_info.callback) { serializer_policy = SERIALIZER_USER; } switch(serializer_policy) { case SERIALIZER_NONE: as_error_update(error_p, AEROSPIKE_ERR_PARAM, "Cannot serialize: SERIALIZER_NONE selected"); goto CLEANUP; case SERIALIZER_PYTHON: { /* * Serialize bytearray as is and store them into database with * type AS_BYTES_BLOB, unlike other values in case of * SERIALIZER_PYTHON. * This is a special case. * Refer: AER-3589 for more details. */ if (PyByteArray_Check(value)) { uint8_t *bytes_array = (uint8_t *) PyByteArray_AsString(value); uint32_t bytes_array_len = (uint32_t) PyByteArray_Size(value); set_as_bytes(bytes, bytes_array, bytes_array_len, AS_BYTES_BLOB, error_p); } else { /* get the sys.modules dictionary */ PyObject* sysmodules = PyImport_GetModuleDict(); PyObject* cpickle_module = NULL; if(PyMapping_HasKeyString(sysmodules, "cPickle")) { cpickle_module = PyMapping_GetItemString(sysmodules, "cPickle"); } else { cpickle_module = PyImport_ImportModule("cPickle"); } if(!cpickle_module) { /* insert error handling here! and exit this function */ as_error_update(error_p, AEROSPIKE_ERR_CLIENT, "Unable to load cpickle module"); goto CLEANUP; } else { PyObject * py_funcname = PyStr_FromString("dumps"); Py_INCREF(cpickle_module); initresult = PyObject_CallMethodObjArgs(cpickle_module, py_funcname, value, NULL); Py_DECREF(cpickle_module); Py_DECREF(py_funcname); if(!initresult) { /* more error handling &c */ as_error_update(error_p, AEROSPIKE_ERR_CLIENT, "Unable to call dumps function"); goto CLEANUP; } else { Py_INCREF(initresult); char *return_value = PyStr_AsString(initresult); Py_ssize_t len = PyBytes_GET_SIZE(initresult); set_as_bytes(bytes, (uint8_t *) return_value, len, AS_BYTES_PYTHON, error_p); Py_DECREF(initresult); } } Py_XDECREF(cpickle_module); } } break; case SERIALIZER_JSON: /* * TODO: * Handle JSON serialization after support for AS_BYTES_JSON * is added in aerospike-client-c */ as_error_update(error_p, AEROSPIKE_ERR, "Unable to serialize using standard json serializer"); goto CLEANUP; case SERIALIZER_USER: if (use_client_serializer) { execute_user_callback(&self->user_serializer_call_info, bytes, &value, true, error_p); if (AEROSPIKE_OK != (error_p->code)) { goto CLEANUP; } } else { if (is_user_serializer_registered) { execute_user_callback(&user_serializer_call_info, bytes, &value, true, error_p); if (AEROSPIKE_OK != (error_p->code)) { goto CLEANUP; } } else if (self->user_serializer_call_info.callback) { execute_user_callback(&self->user_serializer_call_info, bytes, &value, true, error_p); if (AEROSPIKE_OK != (error_p->code)) { goto CLEANUP; } } else { as_error_update(error_p, AEROSPIKE_ERR, "No serializer callback registered"); goto CLEANUP; } } break; default: as_error_update(error_p, AEROSPIKE_ERR, "Unsupported serializer"); goto CLEANUP; } CLEANUP: Py_XDECREF(initresult); if ( error_p->code != AEROSPIKE_OK ) { PyObject * py_err = NULL; error_to_pyobject(error_p, &py_err); PyObject *exception_type = raise_exception(error_p); PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return PyLong_FromLong(0); }
/* ******************************************************************************************************* * Checks as_bytes->type. * Deserializes as_bytes into Py_Object (retval) using deserialization logic * based on as_bytes->type. * * @param bytes The as_bytes to be deserialized. * @param retval The return zval to be populated with the * deserialized value of the input as_bytes. * @param error_p The as_error to be populated by the function * with encountered error if any. ******************************************************************************************************* */ extern PyObject * deserialize_based_on_as_bytes_type(AerospikeClient * self, as_bytes *bytes, PyObject **retval, as_error *error_p) { switch(as_bytes_get_type(bytes)) { case AS_BYTES_PYTHON: { PyObject* sysmodules = PyImport_GetModuleDict(); PyObject* cpickle_module = NULL; if(PyMapping_HasKeyString(sysmodules, "cPickle")) { cpickle_module = PyMapping_GetItemString(sysmodules, "cPickle"); } else { cpickle_module = PyImport_ImportModule("cPickle"); } PyObject* initresult = NULL; if(!cpickle_module) { /* insert error handling here! and exit this function */ as_error_update(error_p, AEROSPIKE_ERR_CLIENT, "Unable to load cpickle module"); goto CLEANUP; } else { char* bytes_val_p = (char*)bytes->value; PyObject *py_value = PyStr_FromStringAndSize(bytes_val_p, as_bytes_size(bytes)); PyObject *py_funcname = PyStr_FromString("loads"); Py_INCREF(cpickle_module); initresult = PyObject_CallMethodObjArgs(cpickle_module, py_funcname, py_value, NULL); Py_DECREF(cpickle_module); Py_DECREF(py_funcname); Py_DECREF(py_value); if(!initresult) { /* more error handling &c */ as_error_update(error_p, AEROSPIKE_ERR_CLIENT, "Unable to call loads function"); goto CLEANUP; } else { *retval = initresult; } } Py_XDECREF(cpickle_module); } break; case AS_BYTES_BLOB: { if (self->user_deserializer_call_info.callback) { execute_user_callback(&self->user_deserializer_call_info, &bytes, retval, false, error_p); if(AEROSPIKE_OK != (error_p->code)) { uint32_t bval_size = as_bytes_size(bytes); PyObject *py_val = PyByteArray_FromStringAndSize((char *) as_bytes_get(bytes), bval_size); if (!py_val) { as_error_update(error_p, AEROSPIKE_ERR_CLIENT, "Unable to deserialize bytes"); goto CLEANUP; } *retval = py_val; as_error_update(error_p, AEROSPIKE_OK, NULL); } } else { if (is_user_deserializer_registered) { execute_user_callback(&user_deserializer_call_info, &bytes, retval, false, error_p); if(AEROSPIKE_OK != (error_p->code)) { uint32_t bval_size = as_bytes_size(bytes); PyObject *py_val = PyByteArray_FromStringAndSize((char *) as_bytes_get(bytes), bval_size); if (!py_val) { as_error_update(error_p, AEROSPIKE_ERR_CLIENT, "Unable to deserialize bytes"); goto CLEANUP; } as_error_update(error_p, AEROSPIKE_OK, NULL); *retval = py_val; } } else { uint32_t bval_size = as_bytes_size(bytes); PyObject *py_val = PyByteArray_FromStringAndSize((char *) as_bytes_get(bytes), bval_size); if (!py_val) { as_error_update(error_p, AEROSPIKE_ERR_CLIENT, "Unable to deserialize bytes"); goto CLEANUP; } *retval = py_val; } } } break; case AS_BYTES_LDT: { Py_INCREF(Py_None); *retval = Py_None; } break; default: as_error_update(error_p, AEROSPIKE_ERR, "Unable to deserialize bytes"); goto CLEANUP; } CLEANUP: if ( error_p->code != AEROSPIKE_OK ) { PyObject * py_err = NULL; error_to_pyobject(error_p, &py_err); PyObject *exception_type = raise_exception(error_p); PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return PyLong_FromLong(0); }
/** ****************************************************************************************************** * Returns data for a particular request string to AerospikeClient_InfoNode * * @param self AerospikeClient object * @param request_str_p Request string sent from the python client * @param py_host Optional host sent from the python client * @param py_policy The policy sent from the python client * * Returns information about a host. ********************************************************************************************************/ static PyObject * AerospikeClient_InfoNode_Invoke( AerospikeClient * self, PyObject * py_request_str, PyObject * py_host, PyObject * py_policy) { PyObject * py_response = NULL; PyObject * py_ustr = NULL; PyObject * py_ustr1 = NULL; as_policy_info info_policy; as_policy_info* info_policy_p = NULL; char* address = (char *) self->as->config.hosts[0].addr; long port_no = self->as->config.hosts[0].port; char* response_p = NULL; as_status status = AEROSPIKE_OK; as_error err; as_error_init(&err); if (!self || !self->as) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object"); goto CLEANUP; } if (!self->is_conn_16) { as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster"); goto CLEANUP; } if (py_policy) { if( PyDict_Check(py_policy) ) { pyobject_to_policy_info(&err, py_policy, &info_policy, &info_policy_p, &self->as->config.policies.info); if (err.code != AEROSPIKE_OK) { goto CLEANUP; } } else { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Policy should be a dictionary"); goto CLEANUP; } } if ( py_host ) { if ( PyTuple_Check(py_host) && PyTuple_Size(py_host) == 2) { PyObject * py_addr = PyTuple_GetItem(py_host,0); PyObject * py_port = PyTuple_GetItem(py_host,1); if ( PyStr_Check(py_addr) ) { address = PyStr_AsString(py_addr); } else if (PyUnicode_Check(py_addr)) { py_ustr = PyUnicode_AsUTF8String(py_addr); address = PyStr_AsString(py_ustr); } if ( PyInt_Check(py_port) ) { port_no = (uint16_t) PyInt_AsLong(py_port); } else if ( PyLong_Check(py_port) ) { port_no = (uint16_t) PyLong_AsLong(py_port); } } else if ( !PyTuple_Check(py_host)){ as_error_update(&err, AEROSPIKE_ERR_PARAM, "Host should be a specified in form of Tuple."); goto CLEANUP; } } char * request_str_p = NULL; if (PyUnicode_Check(py_request_str)) { py_ustr1 = PyUnicode_AsUTF8String(py_request_str); request_str_p = PyStr_AsString(py_ustr1); } else if (PyStr_Check(py_request_str)) { request_str_p = PyStr_AsString(py_request_str); } else { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Request should be of string type"); goto CLEANUP; } Py_BEGIN_ALLOW_THREADS status = aerospike_info_host(self->as, &err, info_policy_p, (const char *) address, (uint16_t) port_no, request_str_p, &response_p); Py_END_ALLOW_THREADS if( err.code == AEROSPIKE_OK ) { if (response_p && status == AEROSPIKE_OK){ py_response = PyStr_FromString(response_p); free(response_p); } else if ( response_p == NULL){ as_error_update(&err, AEROSPIKE_ERR_CLIENT, "Invalid info operation"); goto CLEANUP; } else if ( status != AEROSPIKE_OK ){ as_error_update(&err, status, "Info operation failed"); goto CLEANUP; } } else { as_error_update(&err, err.code, NULL); goto CLEANUP; } CLEANUP: if (py_ustr) { Py_DECREF(py_ustr); } if (py_ustr1) { Py_DECREF(py_ustr1); } if ( err.code != AEROSPIKE_OK ) { PyObject * py_err = NULL; error_to_pyobject(&err, &py_err); PyObject *exception_type = raise_exception(&err); PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return py_response; }
/** ******************************************************************************************************** * Select values from a begin key to end key corresponding to a value up to a maximum count applying a lua filter. * * @param self AerospikeLList object * @param args The args is a tuple object containing an argument * list passed from Python to a C function * @param kwds Dictionary of keywords * * Returns a list of ldt contents. * In case of error,appropriate exceptions will be raised. ******************************************************************************************************** */ PyObject * AerospikeLList_Range_Limit(AerospikeLList * self, PyObject * args, PyObject * kwds) { char* filter_name = NULL; PyObject* py_count = NULL; PyObject * py_args = NULL; PyObject* py_policy = NULL; PyObject* py_from_value = NULL; PyObject* py_end_value = NULL; PyObject* py_elements_list = NULL; PyObject* py_filter_name = NULL; as_policy_apply apply_policy; as_policy_apply* apply_policy_p = NULL; as_list* arg_list = NULL; as_list* elements_list = NULL; as_val* from_val = NULL; as_val* end_val = NULL; as_static_pool static_pool; memset(&static_pool, 0, sizeof(static_pool)); as_error err; as_error_init(&err); static char * kwlist[] = {"start_value", "end_value", "count", "function", "args", "policy", NULL}; // Python Function Argument Parsing if ( PyArg_ParseTupleAndKeywords(args, kwds, "OOO|OOO:range_limit", kwlist, &py_from_value, &py_end_value, &py_count, &py_filter_name, &py_args, &py_policy)== false ) { return NULL; } if (!self || !self->client->as) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object"); goto CLEANUP; } if (!self->client->is_conn_16) { as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster"); goto CLEANUP; } // Convert python policy object to as_policy_apply pyobject_to_policy_apply(&err, py_policy, &apply_policy, &apply_policy_p, &self->client->as->config.policies.apply); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } uint32_t count = 0; if (PyInt_Check(py_count)) { count = PyInt_AsLong(py_count); } else if (PyLong_Check(py_count)) { count = PyLong_AsLong(py_count); if (count == (uint32_t)-1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "integer value exceeds sys.maxsize"); goto CLEANUP; } } } else { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Count should be an integer or long"); goto CLEANUP; } if(py_filter_name) { if(PyStr_Check(py_filter_name)) { filter_name = PyStr_AsString(py_filter_name); } else if(py_filter_name != Py_None) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Filter name should be string or None"); goto CLEANUP; } } if ( py_args && !py_filter_name) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Filter arguments without filter name"); goto CLEANUP; } if ( !PyList_Check(py_args) && (py_args != Py_None)) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid filter argument(type)"); goto CLEANUP; } if (py_args != Py_None) { pyobject_to_list(self->client, &err, py_args, &arg_list, &static_pool, SERIALIZER_PYTHON); } if( py_from_value != Py_None && py_end_value != Py_None ) { pyobject_to_val(self->client, &err, py_from_value, &from_val, &static_pool, SERIALIZER_PYTHON); pyobject_to_val(self->client, &err, py_end_value, &end_val, &static_pool, SERIALIZER_PYTHON); } else { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Begin or end key cannot be None"); goto CLEANUP; } Py_BEGIN_ALLOW_THREADS aerospike_llist_range_limit(self->client->as, &err, apply_policy_p, &self->key, &self->llist, from_val, end_val, count, filter_name, arg_list, &elements_list); Py_END_ALLOW_THREADS if(err.code != AEROSPIKE_OK) { goto CLEANUP; } list_to_pyobject(self->client, &err, elements_list, &py_elements_list); CLEANUP: if (elements_list) { as_list_destroy(elements_list); } if(from_val) { as_val_destroy(from_val); } if(end_val) { as_val_destroy(end_val); } if ( err.code != AEROSPIKE_OK ) { PyObject * py_err = NULL, *py_key = NULL; PyObject *exception_type = raise_exception(&err); error_to_pyobject(&err, &py_err); if (PyObject_HasAttrString(exception_type, "key")) { key_to_pyobject(&err, &self->key, &py_key); PyObject_SetAttrString(exception_type, "key", py_key); Py_DECREF(py_key); } if (PyObject_HasAttrString(exception_type, "bin")) { PyObject *py_bins = PyStr_FromString((char *)&self->bin_name); PyObject_SetAttrString(exception_type, "bin", py_bins); Py_DECREF(py_bins); } PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return py_elements_list; }
/** ******************************************************************************************************* * This callback will be called with the results with aerospike_batch_exists(). * * @param err Error object * @param records An array of as_batch_read_record entries * @param py_recs The pyobject to be filled in with the return * value * * Returns boolean value(true or false). ******************************************************************************************************* */ static void batch_exists_recs(as_error *err, as_batch_read_records* records, PyObject **py_recs) { // Loop over records array as_vector* list = &records->list; for (uint32_t i = 0; i < list->size; i++) { as_batch_read_record* batch = as_vector_get(list, i); PyObject * rec = PyDict_New(); PyObject * p_key = NULL; PyObject * py_rec = NULL; py_rec = PyTuple_New(2); p_key = PyTuple_New(4); if ( batch->key.ns && strlen(batch->key.ns) > 0 ) { PyTuple_SetItem(p_key, 0, PyStr_FromString(batch->key.ns)); } if ( batch->key.set && strlen(batch->key.set) > 0 ) { PyTuple_SetItem(p_key, 1, PyStr_FromString(batch->key.set)); } if ( batch->key.valuep ) { switch(((as_val*)(batch->key.valuep))->type){ case AS_INTEGER: PyTuple_SetItem(p_key, 2, PyInt_FromLong((long)batch->key.value.integer.value)); break; case AS_STRING: PyTuple_SetItem(p_key, 2, PyStr_FromString((const char *)batch->key.value.string.value)); break; default: break; } } else { Py_INCREF(Py_None); PyTuple_SetItem(p_key, 2, Py_None); } if (batch->key.digest.init) { PyTuple_SetItem(p_key, 3, PyByteArray_FromStringAndSize((char *) batch->key.digest.value, AS_DIGEST_VALUE_SIZE)); } PyTuple_SetItem(py_rec, 0, p_key); if ( batch->result == AEROSPIKE_OK ){ PyObject *py_gen = PyInt_FromLong((long)batch->record.gen); PyDict_SetItemString( rec, "gen", py_gen ); Py_DECREF(py_gen); PyObject *py_ttl = PyInt_FromLong((long)batch->record.ttl); PyDict_SetItemString( rec, "ttl", py_ttl ); Py_DECREF(py_ttl); PyTuple_SetItem(py_rec, 1, rec); PyList_SetItem( *py_recs, i, py_rec ); } else if (batch->result == AEROSPIKE_ERR_RECORD_NOT_FOUND){ Py_DECREF(rec); Py_INCREF(Py_None); PyTuple_SetItem(py_rec, 1, Py_None); PyList_SetItem( *py_recs, i, py_rec); } } }
/** *********************************************************************** * * Callback method, invoked by aerospike_batch_get_bins * * ********************************************************************* **/ static bool batch_select_cb(const as_batch_read* results, uint32_t n, void* udata) { // Typecast udata back to PyObject PyObject * py_recs = (PyObject *) udata; // Initialize error object as_error err; as_error_init(&err); // Loop over results array for ( uint32_t i =0; i < n; i++ ) { PyObject * rec = NULL; PyObject * py_rec = NULL; PyObject * p_key = NULL; py_rec = PyTuple_New(3); p_key = PyTuple_New(4); if ( results[i].key->ns && strlen(results[i].key->ns) > 0 ) { PyTuple_SetItem(p_key, 0, PyStr_FromString(results[i].key->ns)); } if ( results[i].key->set && strlen(results[i].key->set) > 0 ) { PyTuple_SetItem(p_key, 1, PyStr_FromString(results[i].key->set)); } if(results[i].key->valuep) { switch(((as_val*)(results[i].key->valuep))->type) { case AS_INTEGER: PyTuple_SetItem(p_key, 2, PyInt_FromLong((long)results[i].key->value.integer.value)); break; case AS_STRING: PyTuple_SetItem(p_key, 2, PyStr_FromString((const char *)results[i].key->value.string.value)); break; default: break; } } else { Py_INCREF(Py_None); PyTuple_SetItem(p_key, 2, Py_None); } if (results[i].key->digest.init) { PyTuple_SetItem(p_key, 3, PyByteArray_FromStringAndSize((char *) results[i].key->digest.value, AS_DIGEST_VALUE_SIZE)); } PyTuple_SetItem(py_rec, 0, p_key); // Check record status if ( results[i].result == AEROSPIKE_OK ) { record_to_pyobject(&err, &results[i].record, results[i].key, &rec); PyObject *py_obj = PyTuple_GetItem(rec, 1); Py_INCREF(py_obj); PyTuple_SetItem(py_rec, 1, py_obj); py_obj = PyTuple_GetItem(rec, 2); Py_INCREF(py_obj); PyTuple_SetItem(py_rec, 2, py_obj); // Set return value in return Dict if ( PyList_SetItem( py_recs, i, py_rec ) ) { return false; } Py_DECREF(rec); } else if( results[i].result == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { Py_INCREF(Py_None); PyTuple_SetItem(py_rec, 1, Py_None); Py_INCREF(Py_None); PyTuple_SetItem(py_rec, 2, Py_None); if ( PyList_SetItem( py_recs, i, py_rec)) { return false; } } } return true; }
/** ******************************************************************************************************** * Scan the map and apply a predicate filter. * * @param self AerospikeLMap object * @param args The args is a tuple object containing an argument * list passed from Python to a C function * @param kwds Dictionary of keywords * * Returns a list of elements from the map after applying predicate. * In case of error,appropriate exceptions will be raised. ******************************************************************************************************** */ PyObject * AerospikeLMap_Filter(AerospikeLMap * self, PyObject * args, PyObject * kwds) { char* filter_name = NULL; PyObject * py_args = NULL; PyObject* py_policy = NULL; as_policy_apply apply_policy; as_policy_apply* apply_policy_p = NULL; as_list* arg_list = NULL; as_map* elements = NULL; as_static_pool static_pool; memset(&static_pool, 0, sizeof(static_pool)); as_error err; as_error_init(&err); static char * kwlist[] = {"udf_function_name", "args", "policy", NULL}; // Python Function Argument Parsing if ( PyArg_ParseTupleAndKeywords(args, kwds, "|sOO:filter", kwlist, &filter_name, &py_args, &py_policy) == false ) { return NULL; } if (!self || !self->client->as) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object"); goto CLEANUP; } if (!self->client->is_conn_16) { as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster"); goto CLEANUP; } // Convert python policy object to as_policy_apply pyobject_to_policy_apply(&err, py_policy, &apply_policy, &apply_policy_p, &self->client->as->config.policies.apply); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } if ( py_args && !filter_name) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Filter arguments without filter name"); goto CLEANUP; } if ( py_args && !PyList_Check(py_args)) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid filter argument(type)"); goto CLEANUP; } if (py_args) { pyobject_to_list(self->client, &err, py_args, &arg_list, &static_pool, SERIALIZER_PYTHON); } aerospike_lmap_filter(self->client->as, &err, apply_policy_p, &self->key, &self->lmap, filter_name, arg_list, &elements); if (err.code != AEROSPIKE_OK) { as_error_update(&err, err.code, NULL); goto CLEANUP; } PyObject* py_map = NULL; map_to_pyobject(&err, elements, &py_map); CLEANUP: if (elements) { as_map_destroy(elements); } if (arg_list) { as_list_destroy(arg_list); } if ( err.code != AEROSPIKE_OK ) { PyObject * py_err = NULL, *py_key = NULL; PyObject *exception_type = raise_exception(&err); error_to_pyobject(&err, &py_err); if(PyObject_HasAttrString(exception_type, "key")) { key_to_pyobject(&err, &self->key, &py_key); PyObject_SetAttrString(exception_type, "key", py_key); Py_DECREF(py_key); } if(PyObject_HasAttrString(exception_type, "bin")) { PyObject *py_bins = PyStr_FromString((char *)&self->bin_name); PyObject_SetAttrString(exception_type, "bin", py_bins); Py_DECREF(py_bins); } PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return py_map; }
/** ******************************************************************************************************** * Add a list of objects to the map. * * @param self AerospikeLMap object * @param args The args is a tuple object containing an argument * list passed from Python to a C function * @param kwds Dictionary of keywords * * Returns an integer status. 0(Zero) is success value. * In case of error,appropriate exceptions will be raised. ******************************************************************************************************** */ PyObject * AerospikeLMap_Put_Many(AerospikeLMap * self, PyObject * args, PyObject * kwds) { PyObject* py_values = NULL; PyObject* py_policy = NULL; as_policy_apply apply_policy; as_policy_apply* apply_policy_p = NULL; as_map* map_values = NULL; as_static_pool static_pool; memset(&static_pool, 0, sizeof(static_pool)); as_error err; as_error_init(&err); static char * kwlist[] = {"values", "policy", NULL}; // Python Function Argument Parsing if ( PyArg_ParseTupleAndKeywords(args, kwds, "O|O:put_many", kwlist, &py_values, &py_policy)== false ) { return NULL; } if (!self || !self->client->as) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object"); goto CLEANUP; } if (!self->client->is_conn_16) { as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster"); goto CLEANUP; } // Convert python policy object to as_policy_apply pyobject_to_policy_apply(&err, py_policy, &apply_policy, &apply_policy_p, &self->client->as->config.policies.apply); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } if (!PyDict_Check(py_values)) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid argument(type)"); goto CLEANUP; } /* * Convert python map to as map */ pyobject_to_map(self->client, &err, py_values, &map_values, &static_pool, SERIALIZER_PYTHON); if (err.code != AEROSPIKE_OK) { map_values = NULL; goto CLEANUP; } aerospike_lmap_put_all(self->client->as, &err, apply_policy_p, &self->key, &self->lmap, map_values); if(err.code != AEROSPIKE_OK) { as_error_update(&err, err.code, NULL); } CLEANUP: if (map_values) { as_map_destroy(map_values); } if ( err.code != AEROSPIKE_OK ) { PyObject * py_err = NULL, *py_key = NULL; PyObject *exception_type = raise_exception(&err); error_to_pyobject(&err, &py_err); if(PyObject_HasAttrString(exception_type, "key")) { key_to_pyobject(&err, &self->key, &py_key); PyObject_SetAttrString(exception_type, "key", py_key); Py_DECREF(py_key); } if(PyObject_HasAttrString(exception_type, "bin")) { PyObject *py_bins = PyStr_FromString((char *)&self->bin_name); PyObject_SetAttrString(exception_type, "bin", py_bins); Py_DECREF(py_bins); } PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return PyLong_FromLong(0); }
/** ******************************************************************************************************* * Callback for as_info_foreach(). * * @param err The as_error to be populated by the function * with the encountered error if any. * @param node The current as_node object for which the * callback is fired by c client. * @param req The info request string. * @param res The info response string for current node. * @pram udata The callback udata containing the host_lookup * array and the return zval to be populated with * an entry for current node's info response with * the node's ID as the key. * * Returns true if callback is successful, Otherwise false. ******************************************************************************************************* */ static bool AerospikeClient_Info_each(as_error * err, const as_node * node, const char * req, char * res, void * udata) { PyObject * py_err = NULL; PyObject * py_ustr = NULL; PyObject * py_out = NULL; foreach_callback_info_udata* udata_ptr = (foreach_callback_info_udata *) udata; struct sockaddr_in* addr = NULL; if ( err && err->code != AEROSPIKE_OK ) { as_error_update(err, err->code, NULL); goto CLEANUP; } else if ( res != NULL ) { char * out = strchr(res,'\t'); if ( out != NULL ) { out++; py_out = PyStr_FromString(out); } else { py_out = PyStr_FromString(res); } } if ( py_err == NULL ) { Py_INCREF(Py_None); py_err = Py_None; } if ( py_out == NULL ) { Py_INCREF(Py_None); py_out = Py_None; } PyObject * py_res = PyTuple_New(2); PyTuple_SetItem(py_res, 0, py_err); PyTuple_SetItem(py_res, 1, py_out); if(udata_ptr->host_lookup_p) { PyObject * py_hosts = (PyObject *)udata_ptr->host_lookup_p; if ( py_hosts && PyList_Check(py_hosts) ) { addr = as_node_get_address((as_node *)node); int size = (int) PyList_Size(py_hosts); for ( int i = 0; i < size && i < AS_CONFIG_HOSTS_SIZE; i++ ) { char * host_addr = NULL; int port = -1; PyObject * py_host = PyList_GetItem(py_hosts, i); if ( PyTuple_Check(py_host) && PyTuple_Size(py_host) == 2 ) { PyObject * py_addr = PyTuple_GetItem(py_host,0); PyObject * py_port = PyTuple_GetItem(py_host,1); if ( PyStr_Check(py_addr) ) { host_addr = PyStr_AsString(py_addr); } else if (PyUnicode_Check(py_addr)) { py_ustr = PyUnicode_AsUTF8String(py_addr); host_addr = PyStr_AsString(py_ustr); } else { as_error_update(&udata_ptr->error, AEROSPIKE_ERR_PARAM, "Host address is of type incorrect"); if (py_res) { Py_DECREF(py_res); } return false; } if ( PyInt_Check(py_port) ) { port = (uint16_t) PyInt_AsLong(py_port); } else if ( PyLong_Check(py_port) ) { port = (uint16_t) PyLong_AsLong(py_port); } else { break; } char ip_port[IP_PORT_MAX_LEN]; inet_ntop(addr->sin_family, &(addr->sin_addr), ip_port, INET_ADDRSTRLEN); if( (!strcmp(host_addr,ip_port)) && (port == ntohs(addr->sin_port))) { PyObject * py_nodes = (PyObject *) udata_ptr->udata_p; PyDict_SetItemString(py_nodes, node->name, py_res); } } } } else if ( !PyList_Check( py_hosts )){ as_error_update(&udata_ptr->error, AEROSPIKE_ERR_PARAM, "Hosts should be specified in a list."); goto CLEANUP; } } else { PyObject * py_nodes = (PyObject *) udata_ptr->udata_p; PyDict_SetItemString(py_nodes, node->name, py_res); } Py_DECREF(py_res); CLEANUP: if ( udata_ptr->error.code != AEROSPIKE_OK ) { PyObject * py_err = NULL; error_to_pyobject( &udata_ptr->error, &py_err); PyObject *exception_type = raise_exception(&udata_ptr->error); PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } if ( err->code != AEROSPIKE_OK ) { PyObject * py_err = NULL; error_to_pyobject(err, &py_err); PyObject *exception_type = raise_exception(err); PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return true; }
/** ****************************************************************************************************** * Iterates over the hosts in the cluster and creates the list to be returned to the python client. * * @param err as_error object * @param command Request string sent from the python client * @param nodes_tuple List containing details of each host * @param return_value List t o be returned back to the python client * @param host_index Index of the list nodes_tuple * @param index Index of the list to be returned. * * Returns information about a host. ********************************************************************************************************/ static PyObject * AerospikeClient_GetNodes_Returnlist(as_error* err, PyObject * command, PyObject * nodes_tuple[], PyObject * return_value, uint32_t host_index, Py_ssize_t index) { char* tok = NULL; char* saved = NULL; PyObject * value_tok = NULL; bool break_flag = false; tok = strtok_r(PyStr_AsString(command), INFO_REQUEST_RESPONSE_DELIMITER, &saved); if (tok == NULL) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Unable to get addr in service"); goto CLEANUP; } while (tok != NULL && (host_index < MAX_HOST_COUNT)) { tok = strtok_r(NULL, IP_PORT_DELIMITER, &saved); #if defined(__APPLE__) if (tok == NULL || saved == NULL) { #else if (tok == NULL || *saved == '\0') { #endif goto CLEANUP; } nodes_tuple[host_index] = PyTuple_New(2); value_tok = PyStr_FromString(tok); PyTuple_SetItem(nodes_tuple[host_index], 0 , value_tok); //Py_DECREF(value_tok); if(strcmp(PyStr_AsString(command),"response_services_p")) { tok = strtok_r(NULL, HOST_DELIMITER, &saved); if (tok == NULL) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Unable to get port"); goto CLEANUP; } if (strstr(tok, INFO_RESPONSE_END)) { tok = strtok_r(tok, INFO_RESPONSE_END, &saved); break_flag = true; } } else { tok = strtok_r(NULL, INFO_RESPONSE_END, &saved); if (tok == NULL) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Unable to get port in service"); goto CLEANUP; } } value_tok = PyInt_FromString(tok, NULL, 10); PyTuple_SetItem(nodes_tuple[host_index], 1 , value_tok); PyList_Insert(return_value, index , nodes_tuple[host_index]); Py_DECREF(nodes_tuple[host_index]); index++; host_index++; if (break_flag == true) { goto CLEANUP; } } CLEANUP: if ( err->code != AEROSPIKE_OK ) { PyObject * py_err = NULL; error_to_pyobject(err, &py_err); PyObject *exception_type = raise_exception(err); PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return return_value; } /** ****************************************************************************************************** * Returns data about the nodes to AerospikeClient_GetNodes. * * @param self AerospikeClient object * * Returns a list containing the details of the nodes. ********************************************************************************************************/ static PyObject * AerospikeClient_GetNodes_Invoke( AerospikeClient * self) { PyObject * response_services_p = NULL; PyObject * response_service_p = NULL; PyObject * nodes_tuple[MAX_HOST_COUNT] = {0}; PyObject * return_value = PyList_New(0); as_error err; as_error_init(&err); if (!self || !self->as) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object"); goto CLEANUP; } if (!self->is_conn_16) { as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster"); goto CLEANUP; } PyObject * py_req_str = NULL; py_req_str = PyStr_FromString("services"); response_services_p = AerospikeClient_InfoNode_Invoke(self, py_req_str, NULL, NULL); Py_DECREF(py_req_str); if(!response_services_p) { as_error_update(&err, AEROSPIKE_ERR_CLIENT, "Services call returned an error"); goto CLEANUP; } py_req_str = PyStr_FromString("service"); response_service_p = AerospikeClient_InfoNode_Invoke(self, py_req_str, NULL, NULL); Py_DECREF(py_req_str); if(!response_service_p) { as_error_update(&err, AEROSPIKE_ERR_CLIENT, "Service call returned an error"); goto CLEANUP; } return_value = AerospikeClient_GetNodes_Returnlist(&err, response_service_p, nodes_tuple, return_value, 0, 0); if( return_value ) return_value = AerospikeClient_GetNodes_Returnlist(&err, response_services_p, nodes_tuple, return_value, 1, 1); CLEANUP: if(response_services_p) { Py_DECREF(response_services_p); } if(response_service_p) { Py_DECREF(response_service_p); } if ( err.code != AEROSPIKE_OK ) { PyObject * py_err = NULL; error_to_pyobject(&err, &py_err); PyObject *exception_type = raise_exception(&err); PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return return_value; }