/* * Delete operation for the nbtr does the following. Delete in the arr or nbtr * based on state of anbtr * * Parameter: ibtr : Btree of key * acol : Secondary index key * apk : value (primary key to be inserted) * * Returns: * AS_SINDEX_OK : In case of success * AS_SINDEX_ERR : In case of failure * AS_SINDEX_KEY_NOTFOUND : If key does not exist */ static int reduced_iRem(bt *ibtr, ai_obj *acol, ai_obj *apk) { ai_nbtr *anbtr = (ai_nbtr *)btIndFind(ibtr, acol); ulong ba = 0, aa = 0; if (!anbtr) { return AS_SINDEX_KEY_NOTFOUND; } if (anbtr->is_btree) { if (!anbtr->u.nbtr) return AS_SINDEX_ERR; // Remove from nbtr if found bt *nbtr = anbtr->u.nbtr; if (!btIndNodeExist(nbtr, apk)) { return AS_SINDEX_KEY_NOTFOUND; } ba = nbtr->msize; // TODO - Needs to be cleaner, type convert from signed // to unsigned. Should be 64 bit !! int nkeys_before = nbtr->numkeys; int nkeys_after = btIndNodeDelete(nbtr, apk, NULL); aa = nbtr->msize; if (nkeys_after == nkeys_before) { return AS_SINDEX_KEY_NOTFOUND; } // remove from ibtr if (nkeys_after == 0) { btIndDelete(ibtr, acol); aa = 0; bt_destroy(nbtr); ba += sizeof(ai_nbtr); cf_free(anbtr); } } else { if (!anbtr->u.arr) return AS_SINDEX_ERR; // Remove from arr if found bool notfound = false; ba = ai_arr_size(anbtr->u.arr); anbtr->u.arr = ai_arr_delete(anbtr->u.arr, (cf_digest *)&apk->y, ¬found); if (notfound) return AS_SINDEX_KEY_NOTFOUND; aa = ai_arr_size(anbtr->u.arr); // Remove from ibtr if (anbtr->u.arr->used == 0) { btIndDelete(ibtr, acol); aa = 0; ai_arr_destroy(anbtr->u.arr); ba += sizeof(ai_nbtr); cf_free(anbtr); } } ibtr->nsize -= (ba - aa); return AS_SINDEX_OK; }
void bt_destroy (BTree* a) { int i; if (!isleaf(a)) { for (i = 0; i <= a->n; ++i) bt_destroy(a->p[i]); } free(a); }
/* * Delete operation for the nbtr does the following. Delete in the arr or nbtr * based on state of anbtr * * Parameter: ibtr : Btree of key * acol : Secondary index key * apk : value (primary key to be inserted) * * Returns: * AS_SINDEX_OK : In case of success * AS_SINDEX_ERR : In case of failure * AS_SINDEX_KEY_NOTFOUND : If key does not exist */ static int reduced_iRem(bt *ibtr, ai_obj *acol, ai_obj *apk) { ai_nbtr *anbtr = (ai_nbtr *)btIndFind(ibtr, acol); ulong ba = 0, aa = 0; if (!anbtr) { return AS_SINDEX_ERR; } if (anbtr->is_btree) { if (!anbtr->u.nbtr) return AS_SINDEX_ERR; // Remove from nbtr if found bt *nbtr = anbtr->u.nbtr; if (!btIndNodeExist(nbtr, apk)) { return AS_SINDEX_KEY_NOTFOUND; } ba = nbtr->msize; int nkeys = btIndNodeDelete(nbtr, apk, NULL); aa = nbtr->msize; // remove from ibtr if (!nkeys) { btIndDelete(ibtr, acol); aa = 0; bt_destroy(nbtr); ba += sizeof(ai_nbtr); cf_free(anbtr); } } else { if (!anbtr->u.arr) return AS_SINDEX_ERR; // Remove from arr if found bool notfound = false; ba = ai_arr_size(anbtr->u.arr); anbtr->u.arr = ai_arr_delete(anbtr->u.arr, (cf_digest *)&apk->y, ¬found); if (notfound) return AS_SINDEX_KEY_NOTFOUND; aa = ai_arr_size(anbtr->u.arr); // Remove from ibtr if (anbtr->u.arr->used == 0) { btIndDelete(ibtr, acol); aa = 0; ai_arr_destroy(anbtr->u.arr); ba += sizeof(ai_nbtr); cf_free(anbtr); } } ibtr->nsize -= (ba - aa); return AS_SINDEX_OK; }
static void destroy_index(bt *ibtr, bt_n *n) { if (! n->leaf) { for (int i = 0; i <= n->n; i++) { destroy_index(ibtr, NODES(ibtr, n)[i]); } } for (int i = 0; i < n->n; i++) { void *be = KEYS(ibtr, n, i); ai_nbtr *anbtr = (ai_nbtr *) parseStream(be, ibtr); if (anbtr) { if (anbtr->is_btree) { bt_destroy(anbtr->u.nbtr); } else { ai_arr_destroy(anbtr->u.arr); } cf_free(anbtr); } } }
int cdx_calcfiltlist(ClipMachine * ClipMachineMemory, RDD_DATA * rd, RDD_ORDER * ro, RDD_FILTER * fp, const char *__PROC__) { int i, er; BTREE *bt; void *key = malloc(sizeof(unsigned int) + ro->bufsize); ClipVar vv, *vp; if (fp->list) { bt = bt_create(0, fp->listlen, sizeof(unsigned int) + ro->bufsize, _cdx_compare); for (i = 0; i < fp->listlen; i++) { if ((er = rd->vtbl->rawgo(ClipMachineMemory, rd, fp->list[i], 0, __PROC__))) return er; if (ro->simpexpr) { if ((er = rd->vtbl->getvalue(ClipMachineMemory, rd, ro->simpfno, &vv, __PROC__))) return er; vp = &vv; } else { if ((er = rdd_calc(ClipMachineMemory, rd->area, &ro->block, &vv, 0))) return er; vp = _clip_vptr(&vv); } memcpy(key, &rd->recno, sizeof(unsigned int)); if ((er = cdx_formatkey(ClipMachineMemory, ro, vp, key + sizeof(unsigned int), __PROC__))) return er; _clip_destroy(ClipMachineMemory, &vv); bt_add(bt, ro, key); } free(fp->list); } else { #if 1 unsigned int bytes = ((fp->size + 1) >> 5) + 1; int i, b, bb, t, tt; fp->listlen = 0; for (i = 0; i < bytes; i++) { if (fp->rmap[i]) { for (b = (i << 2), bb = 0; bb < 4; b++, bb++) { if (((char *) fp->rmap)[b]) { for (t = (b << 3) + 1, tt = 0; tt < 8; t++, tt++) { if (_rm_getbit(fp->rmap, fp->size, t)) fp->listlen++; } } } } } if (fp->listlen > 100) { free(key); return 0; } #else fp->listlen = 0; for (i = 1; i <= fp->size; i++) if (_rm_getbit(fp->rmap, fp->size, i)) fp->listlen++; #endif bt = bt_create(0, fp->listlen, sizeof(unsigned int) + ro->bufsize, _cdx_compare); #if 1 for (i = 0; i < bytes; i++) { if (fp->rmap[i]) { for (b = (i << 2), bb = 0; bb < 4; b++, bb++) { if (((char *) fp->rmap)[b]) { for (t = (b << 3) + 1, tt = 0; tt < 8; t++, tt++) { if (_rm_getbit(fp->rmap, fp->size, t)) { if ((er = rd->vtbl->rawgo(ClipMachineMemory, rd, t, 0, __PROC__))) return er; if (ro->simpexpr) { if ((er = rd->vtbl->getvalue(ClipMachineMemory, rd, ro->simpfno, &vv, __PROC__))) return er; vp = &vv; } else { if ((er = rdd_calc(ClipMachineMemory, rd->area, &ro->block, &vv, 0))) return er; vp = _clip_vptr(&vv); } memcpy(key, &rd->recno, sizeof(unsigned int)); if ((er = cdx_formatkey(ClipMachineMemory, ro, vp, key + sizeof(unsigned int), __PROC__))) return er; _clip_destroy(ClipMachineMemory, &vv); bt_add(bt, ro, key); } } } } } } #else for (i = 1; i <= fp->size; i++) { if (_rm_getbit(fp->rmap, fp->size, i)) { if ((er = rd->vtbl->rawgo(ClipMachineMemory, rd, i, 0, __PROC__))) return er; if (ro->simpexpr) { if ((er = rd->vtbl->getvalue(ClipMachineMemory, rd, ro->simpfno, &vv, __PROC__))) return er; vp = &vv; } else { if ((er = rdd_calc(ClipMachineMemory, rd->area, &ro->block, &vv, 0))) return er; vp = _clip_vptr(&vv); } memcpy(key, &rd->recno, sizeof(unsigned int)); if ((er = cdx_formatkey(ClipMachineMemory, ro, vp, key + sizeof(unsigned int), __PROC__))) return er; _clip_destroy(ClipMachineMemory, &vv); bt_add(bt, ro, key); } } #endif } fp->list = malloc(sizeof(unsigned int) * (fp->listlen + 1)); if (!ro->descend) { bt_first(bt); if (bt_key(bt)) { i = 0; fp->list[i] = *(unsigned int *) bt_key(bt); while (!bt_next(bt)) { i++; fp->list[i] = *(unsigned int *) bt_key(bt); } } } else { bt_last(bt); if (bt_key(bt)) { i = 0; fp->list[i] = *(unsigned int *) bt_key(bt); while (!bt_prev(bt)) { i++; fp->list[i] = *(unsigned int *) bt_key(bt); } } } bt_destroy(bt); free(key); return 0; }