Example #1
0
as_status
as_key_set_digest(as_error* err, as_key* key)
{
	if (key->digest.init) {
		return AEROSPIKE_OK;
	}
	
	size_t set_len = strlen(key->set);
	size_t size;
	
	as_val* val = (as_val*)key->valuep;
	uint8_t* buf;
	
	switch (val->type) {
		case AS_INTEGER: {
			as_integer* v = as_integer_fromval(val);
			size = 9;
			buf = alloca(size);
			buf[0] = AS_BYTES_INTEGER;
			*(uint64_t*)&buf[1] = cf_swap_to_be64(v->value);
			break;
		}
		case AS_DOUBLE: {
			as_double* v = as_double_fromval(val);
			size = 9;
			buf = alloca(size);
			buf[0] = AS_BYTES_DOUBLE;
			*(double*)&buf[1] = cf_swap_to_big_float64(v->value);
			break;
		}
		case AS_STRING: {
			as_string* v = as_string_fromval(val);
			size_t len = as_string_len(v);
			size = len + 1;
			buf = alloca(size);
			buf[0] = AS_BYTES_STRING;
			memcpy(&buf[1], v->value, len);
			break;
		}
		case AS_BYTES: {
			as_bytes* v = as_bytes_fromval(val);
			size = v->size + 1;
			buf = alloca(size);
			// Note: v->type must be a blob type (AS_BYTES_BLOB, AS_BYTES_JAVA, AS_BYTES_PYTHON ...).
			// Otherwise, the particle type will be reassigned to a non-blob which causes a
			// mismatch between type and value.
			buf[0] = v->type;
			memcpy(&buf[1], v->value, v->size);
			break;
		}
		default: {
			return as_error_update(err, AEROSPIKE_ERR_PARAM, "Invalid key type: %d", val->type);
		}
	}
		
	cf_digest_compute2(key->set, set_len, buf, size, (cf_digest*)key->digest.value);
	key->digest.init = true;
	return AEROSPIKE_OK;
}
Example #2
0
static uint8_t*
as_command_write_user_key(uint8_t* begin, const as_key* key)
{
    uint8_t* p = begin + AS_FIELD_HEADER_SIZE;
    as_val* val = (as_val*)key->valuep;
    uint32_t len;

    // Key must not be list or map.
    switch (val->type) {
    default:
    case AS_NIL: {
        *p++ = AS_BYTES_UNDEF;
        len = 0;
        break;
    }
    case AS_INTEGER: {
        as_integer* v = as_integer_fromval(val);
        *p++ = AS_BYTES_INTEGER;
        *(uint64_t*)p = cf_swap_to_be64(v->value);
        p += 8;
        len = 8;
        break;
    }
    case AS_DOUBLE: {
        as_double* v = as_double_fromval(val);
        *p++ = AS_BYTES_DOUBLE;
        *(double*)p = cf_swap_to_big_float64(v->value);
        p += 8;
        len = 8;
        break;
    }
    case AS_STRING: {
        as_string* v = as_string_fromval(val);
        *p++ = AS_BYTES_STRING;
        // v->len should have been already set when calculating the digest.
        memcpy(p, v->value, v->len);
        p += v->len;
        len = (uint32_t)v->len;
        break;
    }
    case AS_BYTES: {
        as_bytes* v = as_bytes_fromval(val);
        // Note: v->type must be a blob type (AS_BYTES_BLOB, AS_BYTES_JAVA, AS_BYTES_PYTHON ...).
        // Otherwise, the particle type will be reassigned to a non-blob which causes a
        // mismatch between type and value.
        *p++ = v->type;
        memcpy(p, v->value, v->size);
        p += v->size;
        len = v->size;
        break;
    }
    }
    as_command_write_field_header(begin, AS_FIELD_KEY, ++len);
    return p;
}
Example #3
0
uint8_t*
as_command_write_bin(uint8_t* begin, uint8_t operation_type, const as_bin* bin, as_buffer* buffer)
{
    uint8_t* p = begin + AS_OPERATION_HEADER_SIZE;
    const char* name = bin->name;

    // Copy string, but do not transfer null byte.
    while (*name) {
        *p++ = *name++;
    }
    uint8_t name_len = p - begin - AS_OPERATION_HEADER_SIZE;
    as_val* val = (as_val*)bin->valuep;
    uint32_t val_len;
    uint8_t val_type;

    switch (val->type) {
    default:
    case AS_NIL: {
        val_len = 0;
        val_type = AS_BYTES_UNDEF;
        break;
    }
    case AS_INTEGER: {
        as_integer* v = as_integer_fromval(val);
        *(uint64_t*)p = cf_swap_to_be64(v->value);
        p += 8;
        val_len = 8;
        val_type = AS_BYTES_INTEGER;
        break;
    }
    case AS_DOUBLE: {
        as_double* v = as_double_fromval(val);
        *(double*)p = cf_swap_to_big_float64(v->value);
        p += 8;
        val_len = 8;
        val_type = AS_BYTES_DOUBLE;
        break;
    }
    case AS_STRING: {
        as_string* v = as_string_fromval(val);
        // v->len should have been already set by as_command_value_size().
        memcpy(p, v->value, v->len);
        p += v->len;
        val_len = (uint32_t)v->len;
        val_type = AS_BYTES_STRING;
        break;
    }
    case AS_BYTES: {
        as_bytes* v = as_bytes_fromval(val);
        memcpy(p, v->value, v->size);
        p += v->size;
        val_len = v->size;
        // Note: v->type must be a blob type (AS_BYTES_BLOB, AS_BYTES_JAVA, AS_BYTES_PYTHON ...).
        // Otherwise, the particle type will be reassigned to a non-blob which causes a
        // mismatch between type and value.
        val_type = v->type;
        break;
    }
    case AS_LIST: {
        memcpy(p, buffer->data, buffer->size);
        p += buffer->size;
        val_len = buffer->size;
        val_type = AS_BYTES_LIST;
        break;
    }
    case AS_MAP: {
        memcpy(p, buffer->data, buffer->size);
        p += buffer->size;
        val_len = buffer->size;
        val_type = AS_BYTES_MAP;
        break;
    }
    }
    *(uint32_t*)begin = cf_swap_to_be32(name_len + val_len + 4);
    begin += 4;
    *begin++ = operation_type;
    *begin++ = val_type;
    *begin++ = 0;
    *begin++ = name_len;
    return p;
}
Example #4
0
static int get(lua_State *L){
	//printf("-get-\n");
	aerospike* as = lua_touserdata(L, 1);
	const char* nameSpace = luaL_checkstring(L, 2);
	const char* set = luaL_checkstring(L, 3);
	const char* keyString = luaL_checkstring(L, 4);
	//printf("key-:%s\n", keyString);
	as_record* rec = NULL;
	as_key key;
	as_error err;
	as_key_init(&key, nameSpace, set, keyString);

	// Read the test record from the database.
	aerospike_key_get(as, &err, NULL, &key, &rec);

	// Push the error code
	lua_pushnumber(L, err.code);

	// Push the error message
	lua_pushstring(L, err.message);

	// Create an new table and push it
	if ( err.code == AEROSPIKE_OK){
        
		lua_newtable(L); /* create table to hold Bins read */
		/*
		 * iterate through bin and add the bin name
		 * and value to the table
		 */
		as_record_iterator it;
		as_record_iterator_init(&it, rec);

		while (as_record_iterator_has_next(&it)) {
		    as_bin *bin        = as_record_iterator_next(&it);
		    as_val *value      = (as_val*)as_bin_get_value(bin);
            char * binName = as_bin_get_name(bin);
            
		    int bin_type = as_val_type(value); //Bin Type

		    switch (bin_type){
		    case AS_INTEGER:
                   
		    	//printf("--integer-%s-\n", binName);
			    lua_pushstring(L, binName); //Bin name
		    	lua_pushnumber(L, as_integer_get(as_integer_fromval(value)));
		    	//printf("--integer-end-\n");
		    	break;
		    case AS_DOUBLE:
                   
		    	//printf("--double-%s-\n", binName);
			    lua_pushstring(L, binName); //Bin name
		    	lua_pushnumber(L, as_double_get(as_double_fromval(value)));
		    	//printf("--double-end-\n");
		    	break;
		    case AS_STRING:
		    	//printf("--string-%s-\n", binName);
			    lua_pushstring(L, binName); //Bin name
		    	lua_pushstring(L, as_val_tostring(value));
		    	//printf("--string-end-\n");
		    	break;
		    case AS_LIST:
		    	//printf("--list-%s-\n", binName);
			    lua_pushstring(L, binName); //Bin name
		    	// Iterate through arraylist populating table
		    	as_list* p_list = as_list_fromval(value);
		    	as_arraylist_iterator it;
		    	as_arraylist_iterator_init(&it, (const as_arraylist*)p_list);
                    
                // create a Lua inner table table for the "List"
		    	lua_newtable(L);
                    
		    	int count = 0;
		    	// See if the elements match what we expect.
		    	while (as_arraylist_iterator_has_next(&it)) {
		    		const as_val* p_val = as_arraylist_iterator_next(&it);
		    		//Assume string
		    		char* p_str = as_val_tostring(p_val);
                    lua_pushnumber(L, count); // table[i]
			    	lua_pushstring(L, p_str); //Value
                    //printf("%d => %s\n", count, p_str);
			    	count++;
			    	lua_settable(L, -3);
		    	}
                //printf("--list-end-\n");
                break;
		    }
		    //printf("--settable-\n");
		    lua_settable(L, -3);
		    //printf("--settable-end-\n");
		}
	}
	as_record_destroy(rec);
	as_key_destroy(&key);
	//printf("-get-end-\n");
	return 3;
}
Example #5
0
as_double * as_record_get_as_double(const as_record * rec, const as_bin_name name)
{
	return as_double_fromval((as_val *) as_record_get(rec, name));
}
Example #6
0
double as_record_get_double(const as_record * rec, const as_bin_name name, double fallback)
{
	as_double * val = as_double_fromval((as_val *) as_record_get(rec, name));
	return val ? val->value : fallback;
}
Example #7
0
uint8_t*
as_command_write_bin(uint8_t* begin, uint8_t operation_type, const as_bin* bin, as_buffer* buffer)
{
	uint8_t* p = begin + AS_OPERATION_HEADER_SIZE;
	const char* name = bin->name;

	// Copy string, but do not transfer null byte.
	while (*name) {
		*p++ = *name++;
	}
	uint8_t name_len = p - begin - AS_OPERATION_HEADER_SIZE;
	as_val* val = (as_val*)bin->valuep;
	uint32_t val_len;
	uint8_t val_type;
	
	switch (val->type) {
		default:
		case AS_NIL: {
			val_len = 0;
			val_type = AS_BYTES_UNDEF;
			break;
		}
		case AS_INTEGER: {
			as_integer* v = as_integer_fromval(val);
			*(uint64_t*)p = cf_swap_to_be64(v->value);
			p += 8;
			val_len = 8;
			val_type = AS_BYTES_INTEGER;
			break;
		}
		case AS_DOUBLE: {
			as_double* v = as_double_fromval(val);
			*(double*)p = cf_swap_to_big_float64(v->value);
			p += 8;
			val_len = 8;
			val_type = AS_BYTES_DOUBLE;
			break;
		}
		case AS_STRING: {
			as_string* v = as_string_fromval(val);
			// v->len should have been already set by as_command_value_size().
			memcpy(p, v->value, v->len);
			p += v->len;
			val_len = (uint32_t)v->len;
			val_type = AS_BYTES_STRING;
			break;
		}
		case AS_GEOJSON: {
			// We send a cellid placeholder so we can fill in points
			// in place on the server w/o changing object size.

			as_geojson* v = as_geojson_fromval(val);
			// v->len should have been already set by as_command_value_size().

			// as_particle_geojson_mem::flags
			*p++ = 0;

			// as_particle_geojson_mem::ncells
			*(uint16_t *) p = cf_swap_to_be16(0);
			p += sizeof(uint16_t);
			
			// placeholder cellid
			// THIS LOOP EXECUTES 0 TIMES (still, it belongs here ...)
			for (int ii = 0; ii < 0; ++ii) {
				*(uint64_t *) p = cf_swap_to_be64(0);
				p += sizeof(uint64_t);
			}

			// json data itself
			memcpy(p, v->value, v->len);
			p += v->len;

			val_len = (uint32_t)(1 + 2 + (0 * 8) + v->len);
			val_type = AS_BYTES_GEOJSON;
			break;
		}
		case AS_BYTES: {
			as_bytes* v = as_bytes_fromval(val);
			memcpy(p, v->value, v->size);
			p += v->size;
			val_len = v->size;
			// Note: v->type must be a blob type (AS_BYTES_BLOB, AS_BYTES_JAVA, AS_BYTES_PYTHON ...).
			// Otherwise, the particle type will be reassigned to a non-blob which causes a
			// mismatch between type and value.
			val_type = v->type;
			break;
		}
		case AS_LIST: {
			memcpy(p, buffer->data, buffer->size);
			p += buffer->size;
			val_len = buffer->size;
			val_type = AS_BYTES_LIST;
			cf_free(buffer->data);
			break;
		}
		case AS_MAP: {
			memcpy(p, buffer->data, buffer->size);
			p += buffer->size;
			val_len = buffer->size;
			val_type = AS_BYTES_MAP;
			cf_free(buffer->data);
			break;
		}
	}
	*(uint32_t*)begin = cf_swap_to_be32(name_len + val_len + 4);
	begin += 4;
	*begin++ = operation_type;
	*begin++ = val_type;
	*begin++ = 0;
	*begin++ = name_len;
	return p;
}