Exemple #1
0
int
main(int argc, char* argv[])
{
	// Parse command line arguments.
	if (! example_get_opts(argc, argv, EXAMPLE_BASIC_OPTS)) {
		exit(-1);
	}

	// Connect to the aerospike database cluster.
	aerospike as;
	example_connect_to_aerospike(&as);

	// Start clean.
	example_remove_test_record(&as);

	// Create a large map object to use. No need to destroy lmap if using
	// as_ldt_init() on stack object.
	as_ldt lmap;
	if (! as_ldt_init(&lmap, "mylmap", AS_LDT_LMAP, NULL)) {
		LOG("unable to initialize ldt");
		example_cleanup(&as);
		exit(-1);
	}

	as_error err;
	as_boolean ldt_exists;
	as_boolean_init(&ldt_exists, false);

	// Verify that the LDT is not already there.
	if (aerospike_lmap_ldt_exists(&as, &err, NULL, &g_key, &lmap,
			&ldt_exists) != AEROSPIKE_OK) {
		int rc = example_handle_udf_error(&err, "first aerospike_lmap_ldt_exists()");
		example_cleanup(&as);
		exit(rc);
	}

	if (as_boolean_get(&ldt_exists)) {
		LOG("found ldt that should not be present");
		example_cleanup(&as);
		exit(-1);
	}

	LOG("verified that lmap ldt is not present");

	// No need to destroy ikey if using as_integer_init() on stack object.
	as_integer ikey;
	as_integer_init(&ikey, 12345);

	// No need to destroy sval if using as_string_init() on stack object with
	// free parameter false.
	as_string sval;
	as_string_init(&sval, "lmap value", false);

	// Put a string entry to the lmap.
	if (aerospike_lmap_put(&as, &err, NULL, &g_key, &lmap,
			(const as_val*)&ikey, (as_val *)&sval) != AEROSPIKE_OK) {
		LOG("first aerospike_lmap_put() returned %d - %s", err.code,
				err.message);
		example_cleanup(&as);
		exit(-1);
	}

	// Ok to reuse.
	as_integer_init(&ikey, 345);

	as_integer ival;
	as_integer_init(&ival, 1000);

	// Put an integer entry to the lmap.
	if (aerospike_lmap_put(&as, &err, NULL, &g_key, &lmap,
			(const as_val*)&ikey, (as_val*)&ival) != AEROSPIKE_OK) {
		LOG("second aerospike_lmap_put() returned %d - %s", err.code,
				err.message);
		example_cleanup(&as);
		exit(-1);
	}

	LOG("2 entries added to map");

	uint32_t n_elements = 0;

	// See how many elements we have in the lmap now.
	if (aerospike_lmap_size(&as, &err, NULL, &g_key, &lmap, &n_elements) !=
			AEROSPIKE_OK) {
		LOG("aerospike_lmap_size() returned %d - %s", err.code, err.message);
		example_cleanup(&as);
		exit(-1);
	}

	if (n_elements != 2) {
		LOG("unexpected lmap size %u", n_elements);
		example_cleanup(&as);
		exit(-1);
	}

	LOG("lmap size confirmed to be %u", n_elements);

	as_boolean_init(&ldt_exists, false);

	// Verify that the LDT is now present.
	if (aerospike_lmap_ldt_exists(&as, &err, NULL, &g_key, &lmap,
			&ldt_exists) != AEROSPIKE_OK) {
		LOG("first aerospike_lmap_ldt_exists() returned %d - %s", err.code,
				err.message);
		example_cleanup(&as);
		exit(-1);
	}

	if (! as_boolean_get(&ldt_exists)) {
		LOG("did not find ldt that should be be present");
		example_cleanup(&as);
		exit(-1);
	}

	LOG("verified that lmap ldt is present");

	as_map* p_map = NULL;

	// Get all the entries back.
	if (aerospike_lmap_get_all(&as, &err, NULL, &g_key, &lmap, &p_map) !=
			AEROSPIKE_OK) {
		LOG("aerospike_lmap_filter() returned %d - %s", err.code, err.message);
		example_cleanup(&as);
		exit(-1);
	}

	as_hashmap_iterator it;
	as_hashmap_iterator_init(&it, (const as_hashmap*)p_map);

	// See if the elements match what we expect.
	while (as_hashmap_iterator_has_next(&it)) {
		const as_val* p_val = as_hashmap_iterator_next(&it);
		char* p_str = as_val_tostring(p_val);

		LOG("   element type %d, value %s", as_val_type(p_val), p_str);
		free(p_str);
	}

	as_map_destroy(p_map);
	p_map = NULL;

	as_integer_init(&ikey, 12345);

	// Remove an entry from the map.
	if (aerospike_lmap_remove(&as, &err, NULL, &g_key, &lmap,
			(const as_val*)&ikey) != AEROSPIKE_OK) {
		LOG("aerospike_lmap_remove() returned %d - %s", err.code, err.message);
		example_cleanup(&as);
		exit(-1);
	}

	as_val* p_val = NULL;

	// Make sure we can't get the value any more.
	as_status result = aerospike_lmap_get(&as, &err, NULL, &g_key, &lmap,
			(const as_val*)&ikey, &p_val);

	if (result == AEROSPIKE_OK) {
		// Server version >= 3.4.1 returns empty map if element doesn't exist.
		if (p_val && (as_val_type(p_val) != AS_MAP ||
				as_map_size((as_map*)p_val) != 0)) {
			char* p_str = as_val_tostring(p_val);

			LOG("entry was not successfully removed");
			LOG("   element type %d, value %s", as_val_type(p_val), p_str);
			free(p_str);

			as_val_destroy(p_val);
			example_cleanup(&as);
			exit(-1);
		}
	}
	else if (result != AEROSPIKE_ERR_LARGE_ITEM_NOT_FOUND) {
		LOG("aerospike_lmap_get() returned %d - %s", err.code, err.message);
		example_cleanup(&as);
		exit(-1);
	}

	LOG("entry successfully removed");

	// Destroy the lmap.
	if (aerospike_lmap_destroy(&as, &err, NULL, &g_key, &lmap) !=
			AEROSPIKE_OK) {
		LOG("aerospike_lmap_destroy() returned %d - %s", err.code, err.message);
		example_cleanup(&as);
		exit(-1);
	}

	n_elements = 0;

	// See if we can still do any lmap operations.
	if (aerospike_lmap_size(&as, &err, NULL, &g_key, &lmap, &n_elements) ==
			AEROSPIKE_OK) {
		LOG("aerospike_lmap_size() did not return error");
		example_cleanup(&as);
		exit(-1);
	}

	LOG("lmap successfully destroyed");

	// Cleanup and disconnect from the database cluster.
	example_cleanup(&as);

	LOG("lmap example successfully completed");

	return 0;
}
/**
 ********************************************************************************************************
 * Add an object 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(AerospikeLMap * self, PyObject * args, PyObject * kwds)
{
	PyObject* py_map_key = NULL;
	PyObject* py_map_value = NULL;
	PyObject* py_policy = NULL;
	as_policy_apply apply_policy;
	as_policy_apply* apply_policy_p = NULL;
	as_val * map_key = NULL;
	as_val * map_value = NULL;

	as_static_pool static_pool;
	memset(&static_pool, 0, sizeof(static_pool));

	as_error err;
	as_error_init(&err);

	static char * kwlist[] = {"key", "value", "policy", NULL};

	// Python Function Argument Parsing
	if ( PyArg_ParseTupleAndKeywords(args, kwds, "OO|O:put", kwlist, 
				&py_map_key, &py_map_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(&err, py_map_key, &map_key, &static_pool, SERIALIZER_PYTHON);
	if (err.code != AEROSPIKE_OK) {
		goto CLEANUP;
	}

	pyobject_to_val(&err, py_map_value, &map_value, &static_pool, SERIALIZER_PYTHON);
	if (err.code != AEROSPIKE_OK) {
		goto CLEANUP;
	}

	aerospike_lmap_put(self->client->as, &err, apply_policy_p, &self->key,
			&self->lmap, map_key, map_value);
	if(err.code != AEROSPIKE_OK) {
		as_error_update(&err, err.code, NULL);
	}

CLEANUP:

	if (map_key) {
		as_val_destroy(map_key);
	}

	if (map_value) {
		as_val_destroy(map_value);
	}

	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 = PyString_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);
}
int
main(int argc, char* argv[])
{
    // Parse command line arguments.
    if (! example_get_opts(argc, argv, EXAMPLE_BASIC_OPTS)) {
        exit(-1);
    }

    // Connect to the aerospike database cluster.
    aerospike as;
    example_connect_to_aerospike(&as);

    // Start clean.
    example_remove_test_record(&as);

    as_ldt lmap;

    // Create a lmap bin to use. No need to destroy as_ldt if using
    // as_ldt_init() on stack object.
    if (! as_ldt_init(&lmap, "mylmap", AS_LDT_LMAP, NULL)) {
        LOG("unable to initialize ldt");
        example_cleanup(&as);
        exit(-1);
    }

    as_error err;

    // No need to destroy as_integer if using as_integer_init() on stack object.
    as_integer ikey;
    as_integer_init(&ikey, 12345);

    // No need to destroy as_string if using as_string_init() on stack object
    // with free parameter false.
    as_string sval;
    as_string_init(&sval, "lmap value", false);

    // Put a string entry to the lmap.
    if (aerospike_lmap_put(&as, &err, NULL, &g_key, &lmap,
                           (const as_val*)&ikey, (as_val *)&sval) != AEROSPIKE_OK) {
        LOG("first aerospike_lmap_put() returned %d - %s", err.code,
            err.message);
        example_cleanup(&as);
        exit(-1);
    }

    as_integer ival;
    as_integer_init(&ival, 1000);

    // It's ok to reset the as_integer.
    as_integer_init(&ikey, 345);

    // Put an integer entry to the lmap.
    if (aerospike_lmap_put(&as, &err, NULL, &g_key, &lmap,
                           (const as_val*)&ikey, (as_val*)&ival) != AEROSPIKE_OK) {
        LOG("second aerospike_lmap_put() returned %d - %s", err.code,
            err.message);
        example_cleanup(&as);
        exit(-1);
    }

    LOG("2 entries added to map");

    uint32_t n_elements = 0;

    // See how many elements we have in the lmap now.
    if (aerospike_lmap_size(&as, &err, NULL, &g_key, &lmap, &n_elements) !=
            AEROSPIKE_OK) {
        LOG("aerospike_lmap_size() returned %d - %s", err.code, err.message);
        example_cleanup(&as);
        exit(-1);
    }

    if (n_elements != 2) {
        LOG("unexpected lmap size %u", n_elements);
        example_cleanup(&as);
        exit(-1);
    }

    LOG("lmap size confirmed to be %u", n_elements);

    as_ldt lmap2;
    as_ldt_init(&lmap2, "mylmap", AS_LDT_LMAP, NULL);

    as_map* p_map = NULL;

    // Get all the entries back.
    if (aerospike_lmap_get_all(&as, &err, NULL, &g_key, &lmap, &p_map) !=
            AEROSPIKE_OK) {
        LOG("aerospike_lmap_filter() returned %d - %s", err.code, err.message);
        example_cleanup(&as);
        exit(-1);
    }

    // See if the elements match what we expect.
    as_hashmap_iterator it;
    as_hashmap_iterator_init(&it, (const as_hashmap*)p_map);

    while (as_hashmap_iterator_has_next(&it)) {
        const as_val* p_val = as_hashmap_iterator_next(&it);
        char* p_str = as_val_tostring(p_val);

        LOG("   element - type = %d, value = %s", as_val_type(p_val), p_str);
        free(p_str);
    }

    as_map_destroy(p_map);
    p_map = NULL;

    as_integer_init(&ikey, 345);
    as_integer_init(&ival, 2000);

    // Remove an entry from the map.
    as_integer_init(&ikey, 12345);
    if (aerospike_lmap_remove(&as, &err, NULL, &g_key, &lmap,
                              (const as_val*)&ikey) != AEROSPIKE_OK) {
        LOG("aerospike_lmap_remove() returned %d - %s", err.code,
            err.message);
        example_cleanup(&as);
        exit(-1);
    }

    as_val* p_val = NULL;

    // Make sure we cannot get the value any more.
    if (aerospike_lmap_get(&as, &err, NULL, &g_key, &lmap,
                           (const as_val*)&ikey, &p_val) == AEROSPIKE_OK) {
        LOG("unexpected success getting a removed entry");
        as_val_destroy(p_val);
        example_cleanup(&as);
        exit(-1);
    }

    LOG("entry successfully removed");

    // Destroy the lmap.
    if (aerospike_lmap_destroy(&as, &err, NULL, &g_key, &lmap) !=
            AEROSPIKE_OK) {
        LOG("aerospike_lmap_destroy() returned %d - %s", err.code, err.message);
        example_cleanup(&as);
        exit(-1);
    }

    n_elements = 0;

    // See if we can still do any lmap operations.
    if (aerospike_lmap_size(&as, &err, NULL, &g_key, &lmap, &n_elements) ==
            AEROSPIKE_OK) {
        LOG("aerospike_lmap_size() did not return error");
        example_cleanup(&as);
        exit(-1);
    }

    LOG("lmap successfully destroyed");

    // Cleanup and disconnect from the database cluster.
    example_cleanup(&as);

    LOG("lmap example successfully completed");

    return 0;
}