void as_namespace_get_bins_info(as_namespace *ns, cf_dyn_buf *db, bool show_ns) { if (show_ns) { cf_dyn_buf_append_string(db, ns->name); cf_dyn_buf_append_char(db, ':'); } if (ns->single_bin) { cf_dyn_buf_append_string(db, "[single-bin]"); } else { uint32_t bin_count = cf_vmapx_count(ns->p_bin_name_vmap); cf_dyn_buf_append_string(db, "num-bin-names="); cf_dyn_buf_append_uint32(db, bin_count); cf_dyn_buf_append_string(db, ",bin-names-quota="); cf_dyn_buf_append_uint32(db, BIN_NAMES_QUOTA); for (uint32_t i = 0; i < bin_count; i++) { cf_dyn_buf_append_char(db, ','); cf_dyn_buf_append_string(db, as_bin_get_name_from_id(ns, (uint16_t)i)); } } if (show_ns) { cf_dyn_buf_append_char(db, ';'); } }
// TODO - old pickle - remove in "six months". // Flatten record's bins into "pickle" format for fabric. uint8_t * as_record_pickle(as_storage_rd *rd, size_t *len_r) { as_namespace *ns = rd->ns; uint32_t sz = 2; // always 2 bytes for number of bins uint16_t n_bins_in_use; for (n_bins_in_use = 0; n_bins_in_use < rd->n_bins; n_bins_in_use++) { as_bin *b = &rd->bins[n_bins_in_use]; if (! as_bin_inuse(b)) { break; } sz += 1; // for bin name length sz += ns->single_bin ? 0 : strlen(as_bin_get_name_from_id(ns, b->id)); // for bin name sz += 1; // was for version - currently not used sz += as_bin_particle_pickled_size(b); } uint8_t *pickle = cf_malloc(sz); uint8_t *buf = pickle; (*(uint16_t *)buf) = cf_swap_to_be16(n_bins_in_use); // number of bins buf += 2; for (uint16_t i = 0; i < n_bins_in_use; i++) { as_bin *b = &rd->bins[i]; // Copy bin name, skipping a byte for name length. uint8_t name_len = (uint8_t)as_bin_memcpy_name(ns, buf + 1, b); *buf++ = name_len; // fill in bin name length buf += name_len; // skip past bin name *buf++ = 0; // was version - currently not used buf += as_bin_particle_to_pickled(b, buf); } *len_r = sz; return pickle; }
static int udf_record_bin_names(const as_rec *rec, as_rec_bin_names_callback callback, void * udata) { int ret = udf_record_param_check(rec, UDF_BIN_NONAME, __FILE__, __LINE__); if (ret) { return 1; } udf_record *urecord = (udf_record *)as_rec_source(rec); char * bin_names = NULL; if (urecord && (urecord->flag & UDF_RECORD_FLAG_STORAGE_OPEN)) { uint16_t nbins; if (urecord->rd->ns->single_bin) { nbins = 1; bin_names = alloca(1); *bin_names = 0; } else { nbins = urecord->rd->n_bins; bin_names = alloca(nbins * AS_ID_BIN_SZ); for (uint16_t i = 0; i < nbins; i++) { as_bin *b = &urecord->rd->bins[i]; if (! as_bin_inuse(b)) { nbins = i; break; } const char * name = as_bin_get_name_from_id(urecord->rd->ns, b->id); strcpy(bin_names + (i * AS_ID_BIN_SZ), name); } } callback(bin_names, nbins, AS_ID_BIN_SZ, udata); return 0; } else { cf_warning(AS_UDF, "Error in getting bin names: no record found"); bin_names = alloca(1); *bin_names = 0; callback(bin_names, 1, AS_ID_BIN_SZ, udata); return -1; } }
int as_msg_make_response_bufbuilder(as_record *r, as_storage_rd *rd, cf_buf_builder **bb_r, bool nobindata, char *nsname, bool use_sets, bool include_key, cf_vector *binlist) { // Sanity checks. Either rd should be there or nobindata and nsname should be present. if (!(rd || (nobindata && nsname))) { cf_detail(AS_PROTO, "Neither storage record nor nobindata is set. Skipping the record."); return 0; } // figure out the size of the entire buffer int set_name_len = 0; const char *set_name = NULL; int ns_len = rd ? strlen(rd->ns->name) : strlen(nsname); if (use_sets && as_index_get_set_id(r) != INVALID_SET_ID) { as_namespace *ns = NULL; if (rd) { ns = rd->ns; } else if (nsname) { ns = as_namespace_get_byname(nsname); } if (!ns) { cf_info(AS_PROTO, "Cannot get namespace, needed to get set information. Skipping record."); return -1; } set_name = as_index_get_set_name(r, ns); if (set_name) { set_name_len = strlen(set_name); } } uint8_t* key = NULL; uint32_t key_size = 0; if (include_key && as_index_is_flag_set(r, AS_INDEX_FLAG_KEY_STORED)) { if (! as_storage_record_get_key(rd)) { cf_info(AS_PROTO, "can't get key - skipping record"); return -1; } key = rd->key; key_size = rd->key_size; } uint16_t n_fields = 2; int msg_sz = sizeof(as_msg); msg_sz += sizeof(as_msg_field) + sizeof(cf_digest); msg_sz += sizeof(as_msg_field) + ns_len; if (set_name) { n_fields++; msg_sz += sizeof(as_msg_field) + set_name_len; } if (key) { n_fields++; msg_sz += sizeof(as_msg_field) + key_size; } int list_bins = 0; int in_use_bins = 0; if (rd) { in_use_bins = as_bin_inuse_count(rd); } if (nobindata == false) { if(binlist) { int binlist_sz = cf_vector_size(binlist); for(uint16_t i = 0; i < binlist_sz; i++) { char binname[AS_ID_BIN_SZ]; cf_vector_get(binlist, i, (void*)&binname); cf_debug(AS_PROTO, " Binname projected inside is |%s| \n", binname); as_bin *p_bin = as_bin_get (rd, (uint8_t*)binname, strlen(binname)); if (!p_bin) { cf_debug(AS_PROTO, "To be projected bin |%s| not found \n", binname); continue; } cf_debug(AS_PROTO, "Adding bin |%s| to projected bins |%s| \n", binname); list_bins++; msg_sz += sizeof(as_msg_op); msg_sz += rd->ns->single_bin ? 0 : strlen(binname); uint32_t psz; if (as_bin_is_hidden(p_bin)) { psz = 0; } else { as_particle_tobuf(p_bin, 0, &psz); // get size } msg_sz += psz; } } else { msg_sz += sizeof(as_msg_op) * in_use_bins; // the bin headers for (uint16_t i = 0; i < in_use_bins; i++) { as_bin *p_bin = &rd->bins[i]; msg_sz += rd->ns->single_bin ? 0 : strlen(as_bin_get_name_from_id(rd->ns, p_bin->id)); uint32_t psz; if (as_bin_is_hidden(p_bin)) { psz = 0; } else { as_particle_tobuf(p_bin, 0, &psz); // get size } msg_sz += psz; } } } uint8_t *b; cf_buf_builder_reserve(bb_r, msg_sz, &b); // set up the header uint8_t *buf = b; as_msg *msgp = (as_msg *) buf; msgp->header_sz = sizeof(as_msg); msgp->info1 = (nobindata ? AS_MSG_INFO1_GET_NOBINDATA : 0); msgp->info2 = 0; msgp->info3 = 0; msgp->unused = 0; msgp->result_code = 0; msgp->generation = r->generation; msgp->record_ttl = r->void_time; msgp->transaction_ttl = 0; msgp->n_fields = n_fields; if (rd) { if (binlist) msgp->n_ops = list_bins; else msgp->n_ops = in_use_bins; } else { msgp->n_ops = 0; } as_msg_swap_header(msgp); buf += sizeof(as_msg); as_msg_field *mf = (as_msg_field *) buf; mf->field_sz = sizeof(cf_digest) + 1; mf->type = AS_MSG_FIELD_TYPE_DIGEST_RIPE; if (rd) { memcpy(mf->data, &rd->keyd, sizeof(cf_digest)); } else { memcpy(mf->data, &r->key, sizeof(cf_digest)); } as_msg_swap_field(mf); buf += sizeof(as_msg_field) + sizeof(cf_digest); mf = (as_msg_field *) buf; mf->field_sz = ns_len + 1; mf->type = AS_MSG_FIELD_TYPE_NAMESPACE; if (rd) { memcpy(mf->data, rd->ns->name, ns_len); } else { memcpy(mf->data, nsname, ns_len); } as_msg_swap_field(mf); buf += sizeof(as_msg_field) + ns_len; if (set_name) { mf = (as_msg_field *) buf; mf->field_sz = set_name_len + 1; mf->type = AS_MSG_FIELD_TYPE_SET; memcpy(mf->data, set_name, set_name_len); as_msg_swap_field(mf); buf += sizeof(as_msg_field) + set_name_len; } if (key) { mf = (as_msg_field *) buf; mf->field_sz = key_size + 1; mf->type = AS_MSG_FIELD_TYPE_KEY; memcpy(mf->data, key, key_size); as_msg_swap_field(mf); buf += sizeof(as_msg_field) + key_size; } if (nobindata) { goto Out; } if(binlist) { int binlist_sz = cf_vector_size(binlist); for(uint16_t i = 0; i < binlist_sz; i++) { char binname[AS_ID_BIN_SZ]; cf_vector_get(binlist, i, (void*)&binname); cf_debug(AS_PROTO, " Binname projected inside is |%s| \n", binname); as_bin *p_bin = as_bin_get (rd, (uint8_t*)binname, strlen(binname)); if (!p_bin) // should it be checked before ??? continue; as_msg_op *op = (as_msg_op *)buf; buf += sizeof(as_msg_op); op->op = AS_MSG_OP_READ; op->name_sz = as_bin_memcpy_name(rd->ns, op->name, p_bin); buf += op->name_sz; // Since there are two variable bits, the size is everything after // the data bytes - and this is only the head, we're patching up // the rest in a minute. op->op_sz = 4 + op->name_sz; if (as_bin_inuse(p_bin)) { op->particle_type = as_particle_type_convert(as_bin_get_particle_type(p_bin)); op->version = as_bin_get_version(p_bin, rd->ns->single_bin); uint32_t psz = msg_sz - (buf - b); // size remaining in buffer, for safety if (as_bin_is_hidden(p_bin)) { op->particle_type = AS_PARTICLE_TYPE_NULL; psz = 0; } else { if (0 != as_particle_tobuf(p_bin, buf, &psz)) { cf_warning(AS_PROTO, "particle to buf: could not copy data!"); } } buf += psz; op->op_sz += psz; } else { cf_debug(AS_PROTO, "Whoops !! bin not in use"); op->particle_type = AS_PARTICLE_TYPE_NULL; } as_msg_swap_op(op); } } else { // over all bins, copy into the buffer for (uint16_t i = 0; i < in_use_bins; i++) { as_msg_op *op = (as_msg_op *)buf; buf += sizeof(as_msg_op); op->op = AS_MSG_OP_READ; op->name_sz = as_bin_memcpy_name(rd->ns, op->name, &rd->bins[i]); buf += op->name_sz; // Since there are two variable bits, the size is everything after // the data bytes - and this is only the head, we're patching up // the rest in a minute. op->op_sz = 4 + op->name_sz; if (as_bin_inuse(&rd->bins[i])) { op->particle_type = as_particle_type_convert(as_bin_get_particle_type(&rd->bins[i])); op->version = as_bin_get_version(&rd->bins[i], rd->ns->single_bin); uint32_t psz = msg_sz - (buf - b); // size remaining in buffer, for safety if (as_bin_is_hidden(&rd->bins[i])) { op->particle_type = AS_PARTICLE_TYPE_NULL; psz = 0; } else { if (0 != as_particle_tobuf(&rd->bins[i], buf, &psz)) { cf_warning(AS_PROTO, "particle to buf: could not copy data!"); } } buf += psz; op->op_sz += psz; } else { op->particle_type = AS_PARTICLE_TYPE_NULL; } as_msg_swap_op(op); } } Out: return(0); }
// NB: this uses the same logic as the bufbuild function // as_msg_make_response_bufbuilder() but does not build a buffer and simply // returns sizing information. This is required for query runtime memory // accounting. // returns -1 in case of error // otherwise returns resize value size_t as_msg_response_msgsize(as_record *r, as_storage_rd *rd, bool nobindata, char *nsname, bool use_sets, cf_vector *binlist) { // Sanity checks. Either rd should be there or nobindata and nsname should be present. if (!(rd || (nobindata && nsname))) { cf_detail(AS_PROTO, "Neither storage record nor nobindata is set. Skipping the record."); return -1; } // figure out the size of the entire buffer int set_name_len = 0; const char *set_name = NULL; int ns_len = rd ? strlen(rd->ns->name) : strlen(nsname); if (use_sets && as_index_get_set_id(r) != INVALID_SET_ID) { as_namespace *ns = NULL; if (rd) { ns = rd->ns; } else if (nsname) { ns = as_namespace_get_byname(nsname); } if (!ns) { cf_info(AS_PROTO, "Cannot get namespace, needed to get set information. Skipping record."); return -1; } set_name = as_index_get_set_name(r, ns); if (set_name) { set_name_len = strlen(set_name); } } int msg_sz = sizeof(as_msg); msg_sz += sizeof(as_msg_field) + sizeof(cf_digest); msg_sz += sizeof(as_msg_field) + ns_len; if (set_name) { msg_sz += sizeof(as_msg_field) + set_name_len; } int in_use_bins = as_bin_inuse_count(rd); int list_bins = 0; if (nobindata == false) { if(binlist) { int binlist_sz = cf_vector_size(binlist); for(uint16_t i = 0; i < binlist_sz; i++) { char binname[AS_ID_BIN_SZ]; cf_vector_get(binlist, i, (void*)&binname); cf_debug(AS_PROTO, " Binname projected inside is |%s| \n", binname); as_bin *p_bin = as_bin_get (rd, (uint8_t*)binname, strlen(binname)); if (!p_bin) { cf_debug(AS_PROTO, "To be projected bin |%s| not found \n", binname); continue; } cf_debug(AS_PROTO, "Adding bin |%s| to projected bins |%s| \n", binname); list_bins++; msg_sz += sizeof(as_msg_op); msg_sz += rd->ns->single_bin ? 0 : strlen(binname); uint32_t psz; as_particle_tobuf(p_bin, 0, &psz); // get size msg_sz += psz; } } else { msg_sz += sizeof(as_msg_op) * in_use_bins; // the bin headers for (uint16_t i = 0; i < in_use_bins; i++) { as_bin *p_bin = &rd->bins[i]; msg_sz += rd->ns->single_bin ? 0 : strlen(as_bin_get_name_from_id(rd->ns, p_bin->id)); uint32_t psz; as_particle_tobuf(p_bin, 0, &psz); // get size msg_sz += psz; } } } return msg_sz; }
cl_msg * as_msg_make_response_msg( uint32_t result_code, uint32_t generation, uint32_t void_time, as_msg_op **ops, as_bin **bins, uint16_t bin_count, as_namespace *ns, cl_msg *msgp_in, size_t *msg_sz_in, uint64_t trid, const char *setname) { int setname_len = 0; // figure out the size of the entire buffer int msg_sz = sizeof(cl_msg); msg_sz += sizeof(as_msg_op) * bin_count; // the bin headers for (uint16_t i = 0; i < bin_count; i++) { if (bins[i]) { msg_sz += ns->single_bin ? 0 : strlen(as_bin_get_name_from_id(ns, bins[i]->id)); uint32_t psz; if (as_bin_is_hidden(bins[i])) { psz = 0; } else { bool tojson = (as_bin_get_particle_type(bins[i]) == AS_PARTICLE_TYPE_LUA_BLOB); _as_particle_tobuf(bins[i], 0, &psz, tojson); // get size } msg_sz += psz; } else if (ops[i]) // no bin, only op, no particle size msg_sz += ops[i]->name_sz; else cf_warning(AS_PROTO, "internal error!"); } //If a transaction-id is sent by the client, we should send it back in a field if (trid != 0) { msg_sz += (sizeof(as_msg_field) + sizeof(trid)); } // If setname is present, we will send it as a field. Account for its space overhead. if (setname != 0) { setname_len = strlen(setname); msg_sz += (sizeof(as_msg_field) + setname_len); } // most cases are small messages - try to stack alloc if we can byte *b; if ((0 == msgp_in) || (*msg_sz_in < msg_sz)) { b = cf_malloc(msg_sz); if (!b) return(0); } else { b = (byte *) msgp_in; } *msg_sz_in = msg_sz; // set up the header byte *buf = b; // current buffer pointer cl_msg *msgp = (cl_msg *) buf; msgp->proto.version = PROTO_VERSION; msgp->proto.type = PROTO_TYPE_AS_MSG; msgp->proto.sz = msg_sz - sizeof(as_proto); as_proto_swap(&msgp->proto); as_msg *m = &msgp->msg; m->header_sz = sizeof(as_msg); m->info1 = 0; m->info2 = 0; m->info3 = 0; m->unused = 0; m->result_code = result_code; m->generation = generation; m->record_ttl = void_time; m->transaction_ttl = 0; m->n_ops = bin_count; m->n_fields = 0; // Count the number of fields that we are going to send back if (trid != 0) { m->n_fields++; } if (setname != NULL) { m->n_fields++; } as_msg_swap_header(m); buf += sizeof(cl_msg); //If we have to send back the transaction-id, we have fields to send back if (trid != 0) { as_msg_field *trfield = (as_msg_field *) buf; //Allow space for the message field header buf += sizeof(as_msg_field); //Fill the field header trfield->type = AS_MSG_FIELD_TYPE_TRID; //Copy the transaction-id as field data in network byte order (big-endian) uint64_t trid_nbo = __cpu_to_be64(trid); trfield->field_sz = sizeof(trid_nbo); memcpy(trfield->data, &trid_nbo, sizeof(trid_nbo)); as_msg_swap_field(trfield); //Allow space for the message field data buf += sizeof(trid_nbo); } // If we have to send back the setname, we have fields to send back if (setname != NULL) { as_msg_field *trfield = (as_msg_field *) buf; // Allow space for the message field header buf += sizeof(as_msg_field); // Fill the field header trfield->type = AS_MSG_FIELD_TYPE_SET; trfield->field_sz = setname_len + 1; memcpy(trfield->data, setname, setname_len); as_msg_swap_field(trfield); // Allow space for the message field data buf += setname_len; } // over all bins, copy into the buffer for (uint16_t i = 0; i < bin_count; i++) { as_msg_op *op = (as_msg_op *)buf; buf += sizeof(as_msg_op); op->op = AS_MSG_OP_READ; if (bins[i]) { op->version = as_bin_get_version(bins[i], ns->single_bin); op->name_sz = as_bin_memcpy_name(ns, op->name, bins[i]); } else { op->version = 0; memcpy(op->name, ops[i]->name, ops[i]->name_sz); op->name_sz = ops[i]->name_sz; } buf += op->name_sz; // cf_detail(AS_PROTO, "make response: bin %d %s : version %d",i,bins[i]->name,op->version); // Since there are two variable bits, the size is everything after the // data bytes - and this is only the head, we're patching up the rest // in a minute. op->op_sz = 4 + op->name_sz; if (bins[i] && as_bin_inuse(bins[i])) { op->particle_type = as_particle_type_convert(as_bin_get_particle_type(bins[i])); uint32_t psz = msg_sz - (buf - b); // size remaining in buffer, for safety if (as_bin_is_hidden(bins[i])) { op->particle_type = AS_PARTICLE_TYPE_NULL; psz = 0; // packet of size NULL } else { bool tojson = (as_bin_get_particle_type(bins[i]) == AS_PARTICLE_TYPE_LUA_BLOB); if (0 != _as_particle_tobuf(bins[i], buf, &psz, tojson)) { cf_warning(AS_PROTO, "particle to buf: could not copy data!"); } } buf += psz; op->op_sz += psz; } else { op->particle_type = AS_PARTICLE_TYPE_NULL; } as_msg_swap_op(op); } return((cl_msg *) b); }
int as_msg_make_response_bufbuilder(as_record *r, as_storage_rd *rd, cf_buf_builder **bb_r, bool nobindata, char *nsname, bool include_ldt_data, bool include_key, bool skip_empty_records, cf_vector *binlist) { // Sanity checks. Either rd should be there or nobindata and nsname should be present. if (!(rd || (nobindata && nsname))) { cf_detail(AS_PROTO, "Neither storage record nor nobindata is set. Skipping the record."); return 0; } // figure out the size of the entire buffer int set_name_len = 0; const char *set_name = NULL; int ns_len = rd ? strlen(rd->ns->name) : strlen(nsname); if (as_index_get_set_id(r) != INVALID_SET_ID) { as_namespace *ns = NULL; if (rd) { ns = rd->ns; } else if (nsname) { ns = as_namespace_get_byname(nsname); } if (!ns) { cf_info(AS_PROTO, "Cannot get namespace, needed to get set information. Skipping record."); return -1; } set_name = as_index_get_set_name(r, ns); if (set_name) { set_name_len = strlen(set_name); } } uint8_t* key = NULL; uint32_t key_size = 0; if (include_key && as_index_is_flag_set(r, AS_INDEX_FLAG_KEY_STORED)) { if (! as_storage_record_get_key(rd)) { cf_info(AS_PROTO, "can't get key - skipping record"); return -1; } key = rd->key; key_size = rd->key_size; } uint16_t n_fields = 2; int msg_sz = sizeof(as_msg); msg_sz += sizeof(as_msg_field) + sizeof(cf_digest); msg_sz += sizeof(as_msg_field) + ns_len; if (set_name) { n_fields++; msg_sz += sizeof(as_msg_field) + set_name_len; } if (key) { n_fields++; msg_sz += sizeof(as_msg_field) + key_size; } int list_bins = 0; int in_use_bins = rd ? (int)as_bin_inuse_count(rd) : 0; as_val *ldt_bin_vals[in_use_bins]; if (! nobindata) { if (binlist) { int binlist_sz = cf_vector_size(binlist); for (uint16_t i = 0; i < binlist_sz; i++) { char binname[AS_ID_BIN_SZ]; cf_vector_get(binlist, i, (void*)&binname); as_bin *p_bin = as_bin_get(rd, binname); if (! p_bin) { continue; } msg_sz += sizeof(as_msg_op); msg_sz += rd->ns->single_bin ? 0 : strlen(binname); if (as_bin_is_hidden(p_bin)) { if (include_ldt_data) { msg_sz += (int)as_ldt_particle_client_value_size(rd, p_bin, &ldt_bin_vals[list_bins]); } else { ldt_bin_vals[list_bins] = NULL; } } else { msg_sz += (int)as_bin_particle_client_value_size(p_bin); } list_bins++; } // Don't return an empty record. if (skip_empty_records && list_bins == 0) { return 0; } } else { msg_sz += sizeof(as_msg_op) * in_use_bins; for (uint16_t i = 0; i < in_use_bins; i++) { as_bin *p_bin = &rd->bins[i]; msg_sz += rd->ns->single_bin ? 0 : strlen(as_bin_get_name_from_id(rd->ns, p_bin->id)); if (as_bin_is_hidden(p_bin)) { if (include_ldt_data) { msg_sz += (int)as_ldt_particle_client_value_size(rd, p_bin, &ldt_bin_vals[i]); } else { ldt_bin_vals[i] = NULL; } } else { msg_sz += (int)as_bin_particle_client_value_size(p_bin); } } } } uint8_t *b; cf_buf_builder_reserve(bb_r, msg_sz, &b); // set up the header uint8_t *buf = b; as_msg *msgp = (as_msg *) buf; msgp->header_sz = sizeof(as_msg); msgp->info1 = (nobindata ? AS_MSG_INFO1_GET_NOBINDATA : 0); msgp->info2 = 0; msgp->info3 = 0; msgp->unused = 0; msgp->result_code = 0; msgp->generation = r->generation; msgp->record_ttl = r->void_time; msgp->transaction_ttl = 0; msgp->n_fields = n_fields; if (rd) { if (binlist) msgp->n_ops = list_bins; else msgp->n_ops = in_use_bins; } else { msgp->n_ops = 0; } as_msg_swap_header(msgp); buf += sizeof(as_msg); as_msg_field *mf = (as_msg_field *) buf; mf->field_sz = sizeof(cf_digest) + 1; mf->type = AS_MSG_FIELD_TYPE_DIGEST_RIPE; if (rd) { memcpy(mf->data, &rd->keyd, sizeof(cf_digest)); } else { memcpy(mf->data, &r->key, sizeof(cf_digest)); } as_msg_swap_field(mf); buf += sizeof(as_msg_field) + sizeof(cf_digest); mf = (as_msg_field *) buf; mf->field_sz = ns_len + 1; mf->type = AS_MSG_FIELD_TYPE_NAMESPACE; if (rd) { memcpy(mf->data, rd->ns->name, ns_len); } else { memcpy(mf->data, nsname, ns_len); } as_msg_swap_field(mf); buf += sizeof(as_msg_field) + ns_len; if (set_name) { mf = (as_msg_field *) buf; mf->field_sz = set_name_len + 1; mf->type = AS_MSG_FIELD_TYPE_SET; memcpy(mf->data, set_name, set_name_len); as_msg_swap_field(mf); buf += sizeof(as_msg_field) + set_name_len; } if (key) { mf = (as_msg_field *) buf; mf->field_sz = key_size + 1; mf->type = AS_MSG_FIELD_TYPE_KEY; memcpy(mf->data, key, key_size); as_msg_swap_field(mf); buf += sizeof(as_msg_field) + key_size; } if (nobindata) { return 0; } if (binlist) { list_bins = 0; int binlist_sz = cf_vector_size(binlist); for (uint16_t i = 0; i < binlist_sz; i++) { char binname[AS_ID_BIN_SZ]; cf_vector_get(binlist, i, (void*)&binname); as_bin *p_bin = as_bin_get(rd, binname); if (! p_bin) { continue; } as_msg_op *op = (as_msg_op *)buf; op->op = AS_MSG_OP_READ; op->version = 0; op->name_sz = as_bin_memcpy_name(rd->ns, op->name, p_bin); op->op_sz = 4 + op->name_sz; buf += sizeof(as_msg_op) + op->name_sz; if (as_bin_is_hidden(p_bin)) { buf += as_ldt_particle_to_client(ldt_bin_vals[list_bins], op); } else { buf += as_bin_particle_to_client(p_bin, op); } list_bins++; as_msg_swap_op(op); } } else { for (uint16_t i = 0; i < in_use_bins; i++) { as_msg_op *op = (as_msg_op *)buf; op->op = AS_MSG_OP_READ; op->version = 0; op->name_sz = as_bin_memcpy_name(rd->ns, op->name, &rd->bins[i]); op->op_sz = 4 + op->name_sz; buf += sizeof(as_msg_op) + op->name_sz; if (as_bin_is_hidden(&rd->bins[i])) { buf += as_ldt_particle_to_client(ldt_bin_vals[i], op); } else { buf += as_bin_particle_to_client(&rd->bins[i], op); } as_msg_swap_op(op); } } return 0; }
cl_msg * as_msg_make_response_msg(uint32_t result_code, uint32_t generation, uint32_t void_time, as_msg_op **ops, as_bin **bins, uint16_t bin_count, as_namespace *ns, cl_msg *msgp_in, size_t *msg_sz_in, uint64_t trid, const char *setname) { size_t msg_sz = sizeof(cl_msg); msg_sz += sizeof(as_msg_op) * bin_count; for (uint16_t i = 0; i < bin_count; i++) { if (ops) { msg_sz += ops[i]->name_sz; } else if (bins[i]) { msg_sz += ns->single_bin ? 0 : strlen(as_bin_get_name_from_id(ns, bins[i]->id)); } else { cf_crash(AS_PROTO, "making response message with null bin and op"); } if (bins[i]) { msg_sz += as_bin_particle_client_value_size(bins[i]); } } if (trid != 0) { msg_sz += sizeof(as_msg_field) + sizeof(trid); } uint32_t setname_len = 0; if (setname) { setname_len = strlen(setname); msg_sz += sizeof(as_msg_field) + setname_len; } uint8_t *b; if (! msgp_in || *msg_sz_in < msg_sz) { b = cf_malloc(msg_sz); if (! b) { return NULL; } } else { b = (uint8_t *)msgp_in; } *msg_sz_in = msg_sz; uint8_t *buf = b; cl_msg *msgp = (cl_msg *)buf; msgp->proto.version = PROTO_VERSION; msgp->proto.type = PROTO_TYPE_AS_MSG; msgp->proto.sz = msg_sz - sizeof(as_proto); as_proto_swap(&msgp->proto); as_msg *m = &msgp->msg; m->header_sz = sizeof(as_msg); m->info1 = 0; m->info2 = 0; m->info3 = 0; m->unused = 0; m->result_code = result_code; m->generation = generation; m->record_ttl = void_time; m->transaction_ttl = 0; m->n_ops = bin_count; m->n_fields = 0; buf += sizeof(cl_msg); if (trid != 0) { m->n_fields++; as_msg_field *trfield = (as_msg_field *)buf; trfield->field_sz = 1 + sizeof(uint64_t); trfield->type = AS_MSG_FIELD_TYPE_TRID; *(uint64_t *)trfield->data = cf_swap_to_be64(trid); buf += sizeof(as_msg_field) + sizeof(uint64_t); as_msg_swap_field(trfield); } if (setname) { m->n_fields++; as_msg_field *trfield = (as_msg_field *)buf; trfield->field_sz = 1 + setname_len; trfield->type = AS_MSG_FIELD_TYPE_SET; memcpy(trfield->data, setname, setname_len); buf += sizeof(as_msg_field) + setname_len; as_msg_swap_field(trfield); } as_msg_swap_header(m); for (uint16_t i = 0; i < bin_count; i++) { as_msg_op *op = (as_msg_op *)buf; op->version = 0; if (ops) { op->op = ops[i]->op; memcpy(op->name, ops[i]->name, ops[i]->name_sz); op->name_sz = ops[i]->name_sz; } else { op->op = AS_MSG_OP_READ; op->name_sz = as_bin_memcpy_name(ns, op->name, bins[i]); } op->op_sz = 4 + op->name_sz; buf += sizeof(as_msg_op) + op->name_sz; buf += as_bin_particle_to_client(bins[i], op); as_msg_swap_op(op); } return (cl_msg *)b; }