node_pointer *modify_btree(couchfile_modify_request *rq, node_pointer *root, couchstore_error_t *errcode) { arena* a = new_arena(0); node_pointer *ret_ptr = root; couchfile_modify_result *root_result = make_modres(a, rq); if (!root_result) { delete_arena(a); *errcode = COUCHSTORE_ERROR_ALLOC_FAIL; return root; } root_result->node_type = KP_NODE; *errcode = modify_node(rq, root, 0, rq->num_actions, root_result); if (*errcode < 0) { delete_arena(a); return NULL; } if (root_result->values_end->pointer == root) { //If we got the root pointer back, remove it from the list //so we don't try to free it. root_result->values_end->pointer = NULL; } if (root_result->modified) { if (root_result->count > 1 || root_result->pointers != root_result->pointers_end) { //The root was split //Write it to disk and return the pointer to it. ret_ptr = finish_root(rq, root_result, errcode); if (*errcode < 0) { ret_ptr = NULL; } } else { ret_ptr = root_result->values_end->pointer; } } if (ret_ptr != root) { ret_ptr = copy_node_pointer(ret_ptr); } delete_arena(a); return ret_ptr; }
void user_dialog(int *stop){ int i = 0, classnum = 0; char choice[100]; char *choicep = choice; printf( "\n __________________________________ \n" ": Command list below: :\n" ": -------------------------------- :\n" ": New Arena -> 1 :\n" ": Delete Arena -> 2 :\n" ": View Latest -> 3 :\n" ": View Most Wins -> 4 :\n" ": Quit -> 5 :\n" ": -------------------------------- :\n" ": Enter class name to get specific :\n" ": class statistics or 'all' :\n" ": 'Hunter' or 'Warrior' or 'Mage' :\n" ":__________________________________:\n\n"); scanf("%s",&choice); system("cls"); if(strcmp(choicep, "1") == 0 || strcmp(choicep, "2") == 0 || strcmp(choicep, "3") == 0 || strcmp(choicep, "4") == 0 || strcmp(choicep, "5") == 0){ i = atoi(choicep); switch(i){ case 1: new_arena(); break; case 2: delete_arena(); break; case 3: view_late(); break; case 4: view_m_win(); break; default: *stop = TRUE; break; } } else{ classnum = class_number(choice); commandlist(classnum); } }
static void test_equalities(uint32_t n) { uint32_t k, l; arena_t m; init_arena(&m); for (k=0; k<n; k++) { arena_push(&m); for (l=0; l<n; l++) { show_label(k); if (k != l) show_label(l); test_eq(&m, pos_occ(k), pos_occ(l)); test_eq(&m, pos_occ(k), neg_occ(l)); test_eq(&m, neg_occ(k), pos_occ(l)); test_eq(&m, neg_occ(k), neg_occ(l)); printf("\n"); } arena_pop(&m); } delete_arena(&m); }
static void test_disjunctions(uint32_t n) { uint32_t t1, t2, t3; arena_t m; init_arena(&m); for (t1=0; t1<n; t1++) { arena_push(&m); for (t2=0; t2<n; t2++) { for (t3=0; t3<n; t3++) { show_label(t1); if (t1 != t2) show_label(t2); if (t1 != t3 && t2 != t3) show_label(t3); test_or3(&m, pos_occ(t1), pos_occ(t2), pos_occ(t3)); printf("\n"); } } arena_pop(&m); } delete_arena(&m); }
couchstore_error_t TreeWriterWrite(TreeWriter* writer, tree_file* treefile, node_pointer** out_root) { couchstore_error_t errcode = COUCHSTORE_SUCCESS; arena* transient_arena = new_arena(0); arena* persistent_arena = new_arena(0); error_unless(transient_arena && persistent_arena, COUCHSTORE_ERROR_ALLOC_FAIL); rewind(writer->file); // Create the structure to write the tree to the db: compare_info idcmp; sized_buf tmp; idcmp.compare = writer->key_compare; idcmp.arg = &tmp; couchfile_modify_result* target_mr = new_btree_modres(persistent_arena, transient_arena, treefile, &idcmp, writer->reduce, writer->rereduce, DB_CHUNK_THRESHOLD, DB_CHUNK_THRESHOLD); if(target_mr == NULL) { error_pass(COUCHSTORE_ERROR_ALLOC_FAIL); } // Read all the key/value pairs from the file and add them to the tree: uint16_t klen; uint32_t vlen; sized_buf k, v; while(1) { if(fread(&klen, sizeof(klen), 1, writer->file) != 1) { break; } if(fread(&vlen, sizeof(vlen), 1, writer->file) != 1) { break; } k.size = ntohs(klen); k.buf = arena_alloc(transient_arena, k.size); v.size = ntohl(vlen); v.buf = arena_alloc(transient_arena, v.size); if(fread(k.buf, k.size, 1, writer->file) != 1) { error_pass(COUCHSTORE_ERROR_READ); } if(fread(v.buf, v.size, 1, writer->file) != 1) { error_pass(COUCHSTORE_ERROR_READ); } //printf("K: '%.*s'\n", k.size, k.buf); mr_push_item(&k, &v, target_mr); if(target_mr->count == 0) { /* No items queued, we must have just flushed. We can safely rewind the transient arena. */ arena_free_all(transient_arena); } } // Check for file error: int readerr = ferror(writer->file); if(readerr != 0 && readerr != EOF) { error_pass(COUCHSTORE_ERROR_READ); } // Finish up the tree: *out_root = complete_new_btree(target_mr, &errcode); cleanup: delete_arena(transient_arena); delete_arena(persistent_arena); return errcode; }
static couchstore_error_t build_btree(const char *source_file, tree_file *dest_file, compare_info *cmp, reduce_fn reduce_fun, reduce_fn rereduce_fun, void *reduce_ctx, node_pointer **out_root) { couchstore_error_t ret = COUCHSTORE_SUCCESS; arena *transient_arena = new_arena(0); arena *persistent_arena = new_arena(0); couchfile_modify_result *mr; FILE *f = NULL; if (transient_arena == NULL || persistent_arena == NULL) { ret = COUCHSTORE_ERROR_ALLOC_FAIL; goto out; } mr = new_btree_modres(persistent_arena, transient_arena, dest_file, cmp, reduce_fun, rereduce_fun, reduce_ctx, VIEW_KV_CHUNK_THRESHOLD + (VIEW_KV_CHUNK_THRESHOLD / 3), VIEW_KP_CHUNK_THRESHOLD + (VIEW_KP_CHUNK_THRESHOLD / 3)); if (mr == NULL) { ret = COUCHSTORE_ERROR_ALLOC_FAIL; goto out; } f = fopen(source_file, "rb"); if (f == NULL) { ret = COUCHSTORE_ERROR_OPEN_FILE; goto out; } while (1) { sized_buf k, v; int read_ret; read_ret = read_record(f, transient_arena, &k, &v); if (read_ret == 0) { break; } else if (read_ret < 0) { ret = (couchstore_error_t) read_ret; goto out; } ret = mr_push_item(&k, &v, mr); if (ret != COUCHSTORE_SUCCESS) { goto out; } if (mr->count == 0) { arena_free_all(transient_arena); } } *out_root = complete_new_btree(mr, &ret); if (ret != COUCHSTORE_SUCCESS) { goto out; } /* Don't care about success/failure. Erlang side will eventually delete it. */ remove(source_file); out: if (f != NULL) { fclose(f); } if (transient_arena != NULL) { delete_arena(transient_arena); } if (persistent_arena != NULL) { delete_arena(persistent_arena); } return ret; }