STATIC couchstore_error_t encode_spatial_key(const sized_mbb_t *mbb, char *key, size_t nkey) { raw_16 num = encode_raw16(mbb->num); memcpy(key, &num, 2); key += 2; assert(mbb->num * sizeof(double) <= nkey); memcpy(key, mbb->mbb, mbb->num * sizeof(double)); return COUCHSTORE_SUCCESS; }
static couchstore_error_t write_header(Db *db) { sized_buf writebuf; size_t seqrootsize = 0, idrootsize = 0, localrootsize = 0; if (db->header.by_seq_root) { seqrootsize = ROOT_BASE_SIZE + db->header.by_seq_root->reduce_value.size; } if (db->header.by_id_root) { idrootsize = ROOT_BASE_SIZE + db->header.by_id_root->reduce_value.size; } if (db->header.local_docs_root) { localrootsize = ROOT_BASE_SIZE + db->header.local_docs_root->reduce_value.size; } writebuf.size = sizeof(raw_file_header) + seqrootsize + idrootsize + localrootsize; writebuf.buf = (char *) calloc(1, writebuf.size); raw_file_header* header = (raw_file_header*)writebuf.buf; header->version = encode_raw08(COUCH_DISK_VERSION); header->update_seq = encode_raw48(db->header.update_seq); header->purge_seq = encode_raw48(db->header.purge_seq); header->purge_ptr = encode_raw48(db->header.purge_ptr); header->seqrootsize = encode_raw16((uint16_t)seqrootsize); header->idrootsize = encode_raw16((uint16_t)idrootsize); header->localrootsize = encode_raw16((uint16_t)localrootsize); uint8_t *root = (uint8_t*)(header + 1); encode_root(root, db->header.by_seq_root); root += seqrootsize; encode_root(root, db->header.by_id_root); root += idrootsize; encode_root(root, db->header.local_docs_root); cs_off_t pos; couchstore_error_t errcode = db_write_header(&db->file, &writebuf, &pos); if (errcode == COUCHSTORE_SUCCESS) { db->header.position = pos; } free(writebuf.buf); return errcode; }
static nodelist *encode_pointer(arena* a, node_pointer *ptr) { nodelist *pel = make_nodelist(a, sizeof(raw_node_pointer) + ptr->reduce_value.size); if (!pel) { return NULL; } raw_node_pointer *raw = (raw_node_pointer*)pel->data.buf; raw->pointer = encode_raw48(ptr->pointer); raw->subtreesize = encode_raw48(ptr->subtreesize); raw->reduce_value_size = encode_raw16((uint16_t)ptr->reduce_value.size); memcpy(raw + 1, ptr->reduce_value.buf, ptr->reduce_value.size); pel->pointer = ptr; pel->key = ptr->key; return pel; }
static void test_raw_16(uint16_t value) { raw_16 raw; raw = encode_raw16(value); assert(decode_raw16(raw) == value); }
static void enc_uint16(uint16_t u, char **buf) { raw_16 r = encode_raw16(u); memcpy(*buf, &r, 2); *buf += 2; }