/** ******************************************************************************************************* * This function converts PyObject key to as_key object, Also converts PyObject * policy to as_policy_operate object. * * @param err The as_error to be populated by the function * with the encountered error if any. * @param py_key The PyObject key. * @param py_policy The PyObject policy. * @param key_p The C client's as_key that identifies the record. * @param operate_policy_p The as_policy_operate type pointer. * @param operate_policy_pp The as_policy_operate type pointer to pointer. ******************************************************************************************************* */ static PyObject * AerospikeClient_convert_pythonObj_to_asType( AerospikeClient * self, as_error *err, PyObject* py_key, PyObject* py_policy, as_key* key_p, as_policy_operate* operate_policy_p, as_policy_operate** operate_policy_pp) { pyobject_to_key(err, py_key, key_p); if ( err->code != AEROSPIKE_OK ) { goto CLEANUP; } if (py_policy) { pyobject_to_policy_operate(err, py_policy, operate_policy_p, operate_policy_pp, &self->as->config.policies.operate); } 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 PyLong_FromLong(0); }
static int AerospikeLList_Type_Init(AerospikeLList * self, PyObject * args, PyObject * kwds) { PyObject * py_key = NULL; char* bin_name = NULL; char* module = NULL; static char * kwlist[] = {"key", "bin", "module", NULL}; if ( PyArg_ParseTupleAndKeywords(args, kwds, "Os|s:llist", kwlist, &py_key, &bin_name, &module) == false ) { return -1; } /* * Convert pyobject to as_key type. */ as_error error; as_error_init(&error); pyobject_to_key(&error, py_key, &self->key); if (error.code != AEROSPIKE_OK) { return -1; } int bin_name_len = strlen(bin_name); if ((bin_name_len == 0) || (bin_name_len > AS_BIN_NAME_MAX_LEN)) { return -1; } strcpy(self->bin_name, bin_name); /* * LDT Initialization */ initialize_ldt(&error, &self->llist, self->bin_name, AS_LDT_LLIST, module); if (error.code != AEROSPIKE_OK) { return -1; } return 0; }
/** ****************************************************************************************************** * Removes a bin from a record. * * @param self AerospikeClient object * @prama py_key The key for the record. * @pram py_binList The name of the bins to be removed from the record. * @param py_policy The optional policies. * @param err The C client's as_error to be set to the encountered error. * * Returns an integer status. 0(Zero) is success value. * In case of error,appropriate exceptions will be raised. ******************************************************************************************************* */ static PyObject * AerospikeClient_RemoveBin_Invoke( AerospikeClient * self, PyObject * py_key,PyObject* py_binList ,PyObject * py_policy, PyObject * py_meta, as_error *err) { // Aerospike Client Arguments as_policy_write write_policy; as_policy_write * write_policy_p = NULL; as_key key; as_record rec; char* binName = NULL; int count = 0; PyObject * py_ustr = NULL; // Get the bin list size; Py_ssize_t size = PyList_Size(py_binList); // Initialize record as_record_inita(&rec, size); // Convert python key object to as_key pyobject_to_key(err, py_key, &key); if ( err->code != AEROSPIKE_OK ) { goto CLEANUP; } // Convert python policy object to as_policy_write pyobject_to_policy_write(err, py_policy, &write_policy, &write_policy_p, &self->as->config.policies.write); if ( err->code != AEROSPIKE_OK ) { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Incorrect policy"); goto CLEANUP; } // Invoke operation for ( count = 0; count < size; count++ ) { PyObject * py_val = PyList_GetItem(py_binList, count); if( PyUnicode_Check(py_val) ){ py_ustr = PyUnicode_AsUTF8String(py_val); binName = PyStr_AsString(py_ustr); } else if( PyStr_Check(py_val) ) { binName = PyStr_AsString(py_val); } else { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Invalid bin name, bin name should be a string or unicode string") goto CLEANUP; } if (!as_record_set_nil(&rec, binName)){ goto CLEANUP; } if (py_ustr) { Py_DECREF(py_ustr); py_ustr = NULL; } } if ( py_meta && PyDict_Check(py_meta) ) { PyObject * py_gen = PyDict_GetItemString(py_meta, "gen"); PyObject * py_ttl = PyDict_GetItemString(py_meta, "ttl"); if( py_ttl != NULL ){ if ( PyInt_Check(py_ttl) ) { rec.ttl = (uint32_t) PyInt_AsLong(py_ttl); } else if ( PyLong_Check(py_ttl) ) { rec.ttl = (uint32_t) PyLong_AsLongLong(py_ttl); if((uint32_t)-1 == rec.ttl) { as_error_update(err, AEROSPIKE_ERR_PARAM, "integer value for ttl exceeds sys.maxsize"); goto CLEANUP; } } else { as_error_update(err, AEROSPIKE_ERR_PARAM, "Ttl should be an int or long"); goto CLEANUP; } } if( py_gen != NULL ){ if ( PyInt_Check(py_gen) ) { rec.gen = (uint16_t) PyInt_AsLong(py_gen); } else if ( PyLong_Check(py_gen) ) { rec.gen = (uint16_t) PyLong_AsLongLong(py_gen); if((uint16_t)-1 == rec.gen) { as_error_update(err, AEROSPIKE_ERR_PARAM, "integer value for gen exceeds sys.maxsize"); goto CLEANUP; } } else { as_error_update(err, AEROSPIKE_ERR_PARAM, "Generation should be an int or long"); goto CLEANUP; } } } Py_BEGIN_ALLOW_THREADS aerospike_key_put(self->as, err, write_policy_p, &key, &rec); Py_END_ALLOW_THREADS if (err->code != AEROSPIKE_OK) { as_error_update(err, err->code, NULL); goto CLEANUP; } CLEANUP: as_record_destroy(&rec); if ( err->code != AEROSPIKE_OK ) { PyObject * py_err = NULL; error_to_pyobject(err, &py_err); PyObject *exception_type = raise_exception(err); if(PyObject_HasAttrString(exception_type, "key")) { PyObject_SetAttrString(exception_type, "key", py_key); } if(PyObject_HasAttrString(exception_type, "bin")) { PyObject_SetAttrString(exception_type, "bin", Py_None); } PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return PyLong_FromLong(0); }
/** ******************************************************************************************************* * This function invokes csdk's API to remove particular record. * * @param self AerospikeClient object * @param py_key The key under which to store the record * @param generation The generation value * @param py_policy The optional policy parameters * * Returns 0 on success. * In case of error,appropriate exceptions will be raised. ******************************************************************************************************* */ PyObject * AerospikeClient_Remove_Invoke( AerospikeClient * self, PyObject * py_key, PyObject * py_meta, PyObject * py_policy) { // Aerospike Client Arguments as_error err; as_policy_remove remove_policy; as_policy_remove * remove_policy_p = NULL; as_key key; // Initialisation flags bool key_initialised = false; // Initialize error 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; } // Convert python key object to as_key pyobject_to_key(&err, py_key, &key); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } // Key is initialised successfully key_initialised = true; // Convert python policy object to as_policy_exists if (py_policy) { pyobject_to_policy_remove(&err, py_policy, &remove_policy, &remove_policy_p, &self->as->config.policies.remove); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } else { if ( py_meta && PyDict_Check(py_meta) ) { PyObject * py_gen = PyDict_GetItemString(py_meta, "gen"); if( py_gen != NULL ){ if ( PyInt_Check(py_gen) ) { remove_policy_p->generation = (uint16_t) PyInt_AsLong(py_gen); } else if ( PyLong_Check(py_gen) ) { remove_policy_p->generation = (uint16_t) PyLong_AsLongLong(py_gen); if((uint16_t)-1 == remove_policy_p->generation) { as_error_update(&err, AEROSPIKE_ERR_PARAM, "integer value for gen exceeds sys.maxsize"); goto CLEANUP; } } else { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Generation should be an int or long"); } } } } } // Invoke operation Py_BEGIN_ALLOW_THREADS aerospike_key_remove(self->as, &err, remove_policy_p, &key); Py_END_ALLOW_THREADS if(err.code != AEROSPIKE_OK) { as_error_update(&err, err.code, NULL); } CLEANUP: if (key_initialised == true){ // Destroy the key if it is initialised successfully. as_key_destroy(&key); } if ( err.code != AEROSPIKE_OK ) { PyObject * py_err = NULL; error_to_pyobject(&err, &py_err); PyObject *exception_type = raise_exception(&err); if(PyObject_HasAttrString(exception_type, "key")) { PyObject_SetAttrString(exception_type, "key", py_key); } if(PyObject_HasAttrString(exception_type, "bin")) { PyObject_SetAttrString(exception_type, "bin", Py_None); } PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return PyLong_FromLong(0); }
/** ******************************************************************************************************* * This function applies a registered udf module on a particular record. * * @param self AerospikeClient object * @param py_key The key under which to store the record. * @param py_module The module name. * @param py_function The UDF function to be applied on a record. * @param py_arglist The arguments to the UDF function * @param py_policy The optional policy parameters * * Returns the result of UDF function. ******************************************************************************************************* */ PyObject * AerospikeClient_Apply_Invoke( AerospikeClient * self, PyObject * py_key, PyObject * py_module, PyObject * py_function, PyObject * py_arglist, PyObject * py_policy) { // Python Return Value PyObject * py_result = NULL; // Aerospike Client Arguments as_error err; as_policy_apply apply_policy; as_policy_apply * apply_policy_p = NULL; as_key key; char * module = NULL; char * function = NULL; as_list * arglist = NULL; as_val * result = NULL; PyObject * py_umodule = NULL; PyObject * py_ufunction = NULL; // Initialisation flags bool key_initialised = false; // Initialize error as_error_init(&err); if( !PyList_Check(py_arglist) ) { PyErr_SetString(PyExc_TypeError, "expected UDF method arguments in a 'list'"); return NULL; } 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; } // Convert python key object to as_key pyobject_to_key(&err, py_key, &key); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } // Key is initialiased successfully key_initialised = true; // Convert python list to as_list pyobject_to_list(self, &err, py_arglist, &arglist, NULL, -1); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } // Convert python policy object to as_policy_apply pyobject_to_policy_apply(&err, py_policy, &apply_policy, &apply_policy_p, &self->as->config.policies.apply); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } if ( PyUnicode_Check(py_module) ) { py_umodule = PyUnicode_AsUTF8String(py_module); module = PyStr_AsString(py_umodule); } else if ( PyStr_Check(py_module) ) { module = PyStr_AsString(py_module); } else { as_error_update(&err, AEROSPIKE_ERR_CLIENT, "udf module argument must be a string or unicode string"); goto CLEANUP; } if ( PyUnicode_Check(py_function) ) { py_ufunction = PyUnicode_AsUTF8String(py_function); function = PyStr_AsString(py_ufunction); } else if ( PyStr_Check(py_function) ) { function = PyStr_AsString(py_function); } else { as_error_update(&err, AEROSPIKE_ERR_CLIENT, "function name must be a string or unicode string"); goto CLEANUP; } // Invoke operation aerospike_key_apply(self->as, &err, apply_policy_p, &key, module, function, arglist, &result); if ( err.code == AEROSPIKE_OK ) { val_to_pyobject(&err, result, &py_result); } else { as_error_update(&err, err.code, NULL); } CLEANUP: if (py_umodule) { Py_DECREF(py_umodule); } if (py_ufunction) { Py_DECREF(py_ufunction); } if (key_initialised == true) { // Destroy the key if it is initialised successfully. as_key_destroy(&key); } as_list_destroy(arglist); as_val_destroy(result); if ( err.code != AEROSPIKE_OK ) { PyObject * py_err = NULL; error_to_pyobject(&err, &py_err); PyObject *exception_type = raise_exception(&err); if(PyObject_HasAttrString(exception_type, "key")) { PyObject_SetAttrString(exception_type, "key", py_key); } if(PyObject_HasAttrString(exception_type, "bin")) { PyObject_SetAttrString(exception_type, "bin", Py_None); } if(PyObject_HasAttrString(exception_type, "module")) { PyObject_SetAttrString(exception_type, "module", py_module); } if(PyObject_HasAttrString(exception_type, "func")) { PyObject_SetAttrString(exception_type, "func", py_function); } PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return py_result; }
PyObject * AerospikeClient_Exists_Invoke( AerospikeClient * self, PyObject * py_key, PyObject * py_policy) { // Python Return Value PyObject * py_result = NULL; // Aerospike Client Arguments as_error err; as_policy_read policy; as_policy_read * policy_p = NULL; as_key key; as_record * rec = NULL; // Initialize error as_error_init(&err); // Convert python key object to as_key pyobject_to_key(&err, py_key, &key); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } // Convert python policy object to as_policy_exists pyobject_to_policy_read(&err, py_policy, &policy, &policy_p); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } // Invoke operation aerospike_key_exists(self->as, &err, policy_p, &key, &rec); if ( err.code == AEROSPIKE_OK ) { PyObject * py_result_key = NULL; PyObject * py_result_meta = NULL; key_to_pyobject(&err, &key, &py_result_key); metadata_to_pyobject(&err, rec, &py_result_meta); py_result = PyTuple_New(2); PyTuple_SetItem(py_result, 0, py_result_key); PyTuple_SetItem(py_result, 1, py_result_meta); } else if ( err.code == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { as_error_reset(&err); PyObject * py_result_key = NULL; PyObject * py_result_meta = Py_None; key_to_pyobject(&err, &key, &py_result_key); py_result = PyTuple_New(2); PyTuple_SetItem(py_result, 0, py_result_key); PyTuple_SetItem(py_result, 1, py_result_meta); Py_INCREF(py_result_meta); } CLEANUP: as_record_destroy(rec); if ( err.code != AEROSPIKE_OK ) { PyObject * py_err = NULL; error_to_pyobject(&err, &py_err); PyErr_SetObject(PyExc_Exception, py_err); return NULL; } return py_result; }
/** ******************************************************************************************************* * This function will get a batch of records from the Aeropike DB. * * @param err as_error object * @param self AerospikeClient object * @param py_keys The list of keys * @param batch_policy_p as_policy_batch object * * Returns the record if key exists otherwise NULL. ******************************************************************************************************* */ static PyObject * batch_select_aerospike_batch_get(as_error *err, AerospikeClient * self, PyObject *py_keys, as_policy_batch * batch_policy_p, char **filter_bins, Py_ssize_t bins_size) { PyObject * py_recs = NULL; as_batch batch; bool batch_initialised = false; LocalData data; data.client = self; // Convert python keys list to as_key ** and add it to as_batch.keys // keys can be specified in PyList or PyTuple if ( py_keys != NULL && PyList_Check(py_keys) ) { Py_ssize_t size = PyList_Size(py_keys); py_recs = PyList_New(size); data.py_recs = py_recs; as_batch_init(&batch, size); // Batch object initialised batch_initialised = true; for ( int i = 0; i < size; i++ ) { PyObject * py_key = PyList_GetItem(py_keys, i); if ( !PyTuple_Check(py_key) ){ as_error_update(err, AEROSPIKE_ERR_PARAM, "Key should be a tuple."); goto CLEANUP; } pyobject_to_key(err, py_key, as_batch_keyat(&batch, i)); if ( err->code != AEROSPIKE_OK ) { goto CLEANUP; } } } else if ( py_keys != NULL && PyTuple_Check(py_keys) ) { Py_ssize_t size = PyTuple_Size(py_keys); py_recs = PyList_New(size); data.py_recs = py_recs; as_batch_init(&batch, size); // Batch object initialised batch_initialised = true; for ( int i = 0; i < size; i++ ) { PyObject * py_key = PyTuple_GetItem(py_keys, i); if ( !PyTuple_Check(py_key) ){ as_error_update(err, AEROSPIKE_ERR_PARAM, "Key should be a tuple."); goto CLEANUP; } pyobject_to_key(err, py_key, as_batch_keyat(&batch, i)); if ( err->code != AEROSPIKE_OK ) { goto CLEANUP; } } } else { as_error_update(err, AEROSPIKE_ERR_PARAM, "Keys should be specified as a list or tuple."); goto CLEANUP; } // Invoke C-client API Py_BEGIN_ALLOW_THREADS aerospike_batch_get_bins(self->as, err, batch_policy_p, &batch, (const char **) filter_bins, bins_size, (aerospike_batch_read_callback) batch_select_cb, &data); Py_END_ALLOW_THREADS CLEANUP: if (batch_initialised == true){ // We should destroy batch object as we are using 'as_batch_init' for initialisation // Also, pyobject_to_key is soing strdup() in case of Unicode. So, object destruction // is necessary. as_batch_destroy(&batch); } return py_recs; }
/** ******************************************************************************************************* * This function will get a batch of records from the Aeropike DB. * * @param err as_error object * @param self AerospikeClient object * @param py_keys The list of keys * @param batch_policy_p as_policy_batch object * * Returns the record if key exists otherwise NULL. ******************************************************************************************************* */ static PyObject * batch_select_aerospike_batch_read(as_error *err, AerospikeClient * self, PyObject *py_keys, as_policy_batch * batch_policy_p, char** filter_bins, Py_ssize_t bins_size) { PyObject * py_recs = NULL; as_batch_read_records records; as_batch_read_record* record = NULL; bool batch_initialised = false; // Convert python keys list to as_key ** and add it to as_batch.keys // keys can be specified in PyList or PyTuple if ( py_keys != NULL && PyList_Check(py_keys) ) { Py_ssize_t size = PyList_Size(py_keys); py_recs = PyList_New(size); as_batch_read_inita(&records, size); // Batch object initialised batch_initialised = true; for ( int i = 0; i < size; i++ ) { PyObject * py_key = PyList_GetItem(py_keys, i); if ( !PyTuple_Check(py_key) ){ as_error_update(err, AEROSPIKE_ERR_PARAM, "Key should be a tuple."); goto CLEANUP; } record = as_batch_read_reserve(&records); pyobject_to_key(err, py_key, &record->key); if (bins_size) { record->bin_names = filter_bins; record->n_bin_names = bins_size; } else { record->read_all_bins = true; } if ( err->code != AEROSPIKE_OK ) { goto CLEANUP; } } } else if ( py_keys != NULL && PyTuple_Check(py_keys) ) { Py_ssize_t size = PyTuple_Size(py_keys); py_recs = PyList_New(size); as_batch_read_inita(&records, size); // Batch object initialised batch_initialised = true; for ( int i = 0; i < size; i++ ) { PyObject * py_key = PyTuple_GetItem(py_keys, i); if ( !PyTuple_Check(py_key) ){ as_error_update(err, AEROSPIKE_ERR_PARAM, "Key should be a tuple."); goto CLEANUP; } record = as_batch_read_reserve(&records); pyobject_to_key(err, py_key, &record->key); if (bins_size) { record->bin_names = filter_bins; record->n_bin_names = bins_size; } else { record->read_all_bins = true; } if ( err->code != AEROSPIKE_OK ) { goto CLEANUP; } } } else { as_error_update(err, AEROSPIKE_ERR_PARAM, "Keys should be specified as a list or tuple."); goto CLEANUP; } // Invoke C-client API Py_BEGIN_ALLOW_THREADS aerospike_batch_read(self->as, err, batch_policy_p, &records); Py_END_ALLOW_THREADS if (err->code != AEROSPIKE_OK) { goto CLEANUP; } batch_select_recs(self, err, &records, &py_recs); CLEANUP: if (batch_initialised == true){ // We should destroy batch object as we are using 'as_batch_init' for initialisation // Also, pyobject_to_key is soing strdup() in case of Unicode. So, object destruction // is necessary. as_batch_read_destroy(&records); } return py_recs; }
/** ******************************************************************************************************* * This function read a record with a given key, and return the record. * * @param self AerospikeClient object * @param py_key The key under which to store the record. * @param py_policy The dictionary of policies to be given while * reading a record. * * Returns the record on success. * In case of error,appropriate exceptions will be raised. ******************************************************************************************************* */ PyObject * AerospikeClient_Get_Invoke( AerospikeClient * self, PyObject * py_key, PyObject * py_policy) { // Python Return Value PyObject * py_rec = NULL; // Aerospike Client Arguments as_error err; as_policy_read read_policy; as_policy_read * read_policy_p = NULL; as_key key; as_record * rec = NULL; // Initialised flags bool key_initialised = false; bool record_initialised = false; // Initialize error 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; } // Convert python key object to as_key pyobject_to_key(&err, py_key, &key); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } // Key is successfully initialised. key_initialised = true; // Convert python policy object to as_policy_exists pyobject_to_policy_read(&err, py_policy, &read_policy, &read_policy_p, &self->as->config.policies.read); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } // Initialize record as_record_init(rec, 0); // Record initialised successfully. record_initialised = true; // Invoke operation aerospike_key_get(self->as, &err, read_policy_p, &key, &rec); if ( err.code == AEROSPIKE_OK ) { record_to_pyobject(&err, rec, &key, &py_rec); if ( read_policy_p == NULL || ( read_policy_p != NULL && read_policy_p->key == AS_POLICY_KEY_DIGEST)){ // This is a special case. // C-client returns NULL key, so to the user // response will be (<ns>, <set>, None, <digest>) // Using the same input key, just making primary key part to be None // Only in case of POLICY_KEY_DIGEST or no policy specified PyObject * p_key = PyTuple_GetItem( py_rec, 0 ); Py_INCREF(Py_None); PyTuple_SetItem(p_key, 2, Py_None); } } else if( err.code == AEROSPIKE_ERR_RECORD_NOT_FOUND ) { as_error_reset(&err); PyObject * py_rec_key = NULL; PyObject * py_rec_meta = Py_None; PyObject * py_rec_bins = Py_None; key_to_pyobject(&err, &key, &py_rec_key); py_rec = PyTuple_New(3); PyTuple_SetItem(py_rec, 0, py_rec_key); PyTuple_SetItem(py_rec, 1, py_rec_meta); PyTuple_SetItem(py_rec, 2, py_rec_bins); Py_INCREF(py_rec_meta); Py_INCREF(py_rec_bins); } else { as_error_update(&err, err.code, NULL); } CLEANUP: if (key_initialised == true){ // Destroy key only if it is initialised. as_key_destroy(&key); } if (record_initialised == true){ // Destroy record only if it is initialised. as_record_destroy(rec); } if ( err.code != AEROSPIKE_OK ) { PyObject * py_err = NULL; error_to_pyobject(&err, &py_err); PyObject *exception_type = raise_exception(&err); if(PyObject_HasAttrString(exception_type, "key")) { PyObject_SetAttrString(exception_type, "key", py_key); } if(PyObject_HasAttrString(exception_type, "bin")) { PyObject_SetAttrString(exception_type, "bin", Py_None); } PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return py_rec; }
/** ********************************************************************* * This function will invoke aerospike_batch_get_bins to get filtered * bins from all the records in a batches. * * @param self AerospikeClient object * @param py_keys List of keys passed on by user * @param py_bins List of filter bins passed on by user * @param py_policy User specified Policy dictionary * ********************************************************************* **/ static PyObject * AerospikeClient_Select_Many_Invoke( AerospikeClient * self, PyObject * py_keys, PyObject * py_bins, PyObject * py_policy) { // Python Return Value PyObject * py_recs = PyDict_New(); // Aerospike Client Arguments as_error err; as_batch batch; as_policy_batch policy; as_policy_batch * batch_policy_p = NULL; Py_ssize_t bins_size = 0; char **filter_bins = NULL; // Unicode object's pool UnicodePyObjects u_objs; u_objs.size = 0; int i = 0; // Initialisation flags bool batch_initialised = false; // Initialize error 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; } // Convert python keys list to as_key ** and add it to as_batch.keys // keys can be specified in PyList or PyTuple if ( py_keys != NULL && PyList_Check(py_keys) ) { Py_ssize_t size = PyList_Size(py_keys); as_batch_init(&batch, size); // Batch object initialised batch_initialised = true; for ( i = 0; i < size; i++ ) { PyObject * py_key = PyList_GetItem(py_keys, i); if ( !PyTuple_Check(py_key) ){ as_error_update(&err, AEROSPIKE_ERR_PARAM, "Key should be a tuple."); goto CLEANUP; } pyobject_to_key(&err, py_key, as_batch_keyat(&batch, i)); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } } } else if ( py_keys != NULL && PyTuple_Check(py_keys) ) { Py_ssize_t size = PyTuple_Size(py_keys); as_batch_init(&batch, size); // Batch object initialised. batch_initialised = true; for ( i = 0; i < size; i++ ) { PyObject * py_key = PyTuple_GetItem(py_keys, i); if ( !PyTuple_Check(py_key) ){ as_error_update(&err, AEROSPIKE_ERR_PARAM, "Key should be a tuple."); goto CLEANUP; } pyobject_to_key(&err, py_key, as_batch_keyat(&batch, i)); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } } } else { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Keys should be specified as a list or tuple."); goto CLEANUP; } // Check the type of bins and get it's size // i.e. number of bins provided if (py_bins != NULL && PyList_Check(py_bins)){ bins_size = PyList_Size(py_bins); } else if (py_bins != NULL && PyTuple_Check(py_bins)){ bins_size = PyTuple_Size(py_bins); } else { as_error_update(&err, AEROSPIKE_ERR_PARAM, "Filter bins should be specified as a list or tuple."); goto CLEANUP; } filter_bins = (char **)malloc(sizeof(long int) * bins_size); for (i = 0; i < bins_size; i++){ PyObject *py_bin = NULL; if(PyList_Check(py_bins)){ py_bin = PyList_GetItem(py_bins, i); } if(PyTuple_Check(py_bins)){ py_bin = PyTuple_GetItem(py_bins, i); } if (PyUnicode_Check(py_bin)){ // Store the unicode object into a pool // It is DECREFed at later stages // So, no need of DECREF here. filter_bins[i] = PyString_AsString( store_unicode_bins(&u_objs, PyUnicode_AsUTF8String(py_bin))); } else if (PyString_Check(py_bin)){ filter_bins[i] = PyString_AsString(py_bin); } else{ as_error_update(&err, AEROSPIKE_ERR_PARAM, "Bin name should be a string or unicode string."); goto CLEANUP; } } // Convert python policy object to as_policy_batch pyobject_to_policy_batch(&err, py_policy, &policy, &batch_policy_p, &self->as->config.policies.batch); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } // Invoke C-client API aerospike_batch_get_bins(self->as, &err, batch_policy_p, &batch, (const char **) filter_bins, bins_size, (aerospike_batch_read_callback) batch_select_cb, py_recs); CLEANUP: if (filter_bins != NULL){ free(filter_bins); } // DECREFed all the unicode objects stored in Pool for ( i = 0; i< u_objs.size; i++){ Py_DECREF(u_objs.ob[i]); } if (batch_initialised == true){ // We should destroy batch object as we are using 'as_batch_init' for initialisation // Also, pyobject_to_key is soing strdup() in case of Unicode. So, object destruction // is necessary. as_batch_destroy(&batch); } if ( err.code != AEROSPIKE_OK ) { PyObject * py_err = NULL; error_to_pyobject(&err, &py_err); PyObject *exception_type = raise_exception(&err); if(PyObject_HasAttrString(exception_type, "key")) { PyObject_SetAttrString(exception_type, "key", py_keys); } if(PyObject_HasAttrString(exception_type, "bin")) { PyObject_SetAttrString(exception_type, "bin", Py_None); } PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return py_recs; }
/** ******************************************************************************************************* * This function will put record to the Aerospike DB. * * @param self AerospikeClient object * @param py_key The key under which to store the record. * @param py_bins The data to write to the Aerospike DB. * @param py_meta The meatadata for the record. * @param py_policy The dictionary of policies to be given while * reading a record. * * Returns an integer status. 0(Zero) is success value. * In case of error,appropriate exceptions will be raised. ******************************************************************************************************* */ PyObject * AerospikeClient_Put_Invoke( AerospikeClient * self, PyObject * py_key, PyObject * py_bins, PyObject * py_meta, PyObject * py_policy, long serializer_option) { // Aerospike Client Arguments as_error err; as_policy_write write_policy; as_policy_write * write_policy_p = NULL; as_key key; as_record rec; // Initialisation flags bool key_initialised = false; bool record_initialised = false; // Initialize record as_record_init(&rec, 0); record_initialised = true; as_static_pool static_pool; memset(&static_pool, 0, sizeof(static_pool)); // Initialize error 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; } // Convert python key object to as_key pyobject_to_key(&err, py_key, &key); if (err.code != AEROSPIKE_OK) { goto CLEANUP; } // Key is initialised successfully. key_initialised = true; // Convert python bins and metadata objects to as_record pyobject_to_record(self, &err, py_bins, py_meta, &rec, serializer_option, &static_pool); if (err.code != AEROSPIKE_OK) { goto CLEANUP; } // Convert python policy object to as_policy_write pyobject_to_policy_write(&err, py_policy, &write_policy, &write_policy_p, &self->as->config.policies.write); if (err.code != AEROSPIKE_OK) { goto CLEANUP; } // Invoke operation Py_BEGIN_ALLOW_THREADS aerospike_key_put(self->as, &err, write_policy_p, &key, &rec); Py_END_ALLOW_THREADS if (err.code != AEROSPIKE_OK) { as_error_update(&err, err.code, NULL); } CLEANUP: POOL_DESTROY(&static_pool); if (key_initialised == true) { // Destroy the key if it is initialised. as_key_destroy(&key); } if (record_initialised == true) { // Destroy the record if it is initialised. as_record_destroy(&rec); } // If an error occurred, tell Python. if (err.code != AEROSPIKE_OK) { PyObject * py_err = NULL; error_to_pyobject(&err, &py_err); PyObject *exception_type = raise_exception(&err); if (PyObject_HasAttrString(exception_type, "key")) { PyObject_SetAttrString(exception_type, "key", py_key); } if (PyObject_HasAttrString(exception_type, "bin")) { PyObject_SetAttrString(exception_type, "bin", py_bins); } PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); return NULL; } return PyLong_FromLong(0); }
/** ******************************************************************************************************* * This function applies a registered udf module on a particular record. * * @param self AerospikeClient object * @param py_key The key under which the record is stored. * @param py_policy The dictionary of policies * * Returns a tuple of record having key and meta sequentially. ******************************************************************************************************* */ extern PyObject * AerospikeClient_Exists_Invoke( AerospikeClient * self, PyObject * py_key, PyObject * py_policy) { // Python Return Value PyObject * py_result = NULL; // Aerospike Client Arguments as_error err; as_policy_read read_policy; as_policy_read * read_policy_p = NULL; as_key key; as_record * rec = NULL; // Initialisation flags bool key_initialised = false; // Initialize error 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; } // Convert python key object to as_key pyobject_to_key(&err, py_key, &key); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } // key is initialised successfully key_initialised = true; // Convert python policy object to as_policy_exists pyobject_to_policy_read(&err, py_policy, &read_policy, &read_policy_p, &self->as->config.policies.read); if ( err.code != AEROSPIKE_OK ) { goto CLEANUP; } // Invoke operation Py_BEGIN_ALLOW_THREADS aerospike_key_exists(self->as, &err, read_policy_p, &key, &rec); Py_END_ALLOW_THREADS if ( err.code == AEROSPIKE_OK ) { PyObject * py_result_key = NULL; PyObject * py_result_meta = NULL; key_to_pyobject(&err, &key, &py_result_key); metadata_to_pyobject(&err, rec, &py_result_meta); py_result = PyTuple_New(2); PyTuple_SetItem(py_result, 0, py_result_key); PyTuple_SetItem(py_result, 1, py_result_meta); } else { as_error_update(&err, err.code, NULL); } CLEANUP: if (key_initialised == true){ // Destroy the key if it is initialised successfully. as_key_destroy(&key); } as_record_destroy(rec); if ( err.code != AEROSPIKE_OK ) { PyObject * py_err = NULL; error_to_pyobject(&err, &py_err); PyObject *exception_type = raise_exception(&err); if(PyObject_HasAttrString(exception_type, "key")) { PyObject_SetAttrString(exception_type, "key", py_key); } if(PyObject_HasAttrString(exception_type, "bin")) { PyObject_SetAttrString(exception_type, "bin", Py_None); } PyErr_SetObject(exception_type, py_err); Py_DECREF(py_err); } return py_result; }