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; }
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; }
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; }
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; }