int ai_btree_delete(as_sindex_metadata *imd, as_sindex_pmetadata *pimd, as_sindex_key *skey, void *val) { int ret = AS_SINDEX_OK; uint64_t uk = * (uint64_t *) val; uint64_t bv = skey->b[0].u.i64; if (!pimd->ibtr) { SITRACE(imd->si, DML, debug, "AI_BTREE_FAIL: Delete failed no ibtr %d %lu", bv, uk); return AS_SINDEX_KEY_NOTFOUND; } ai_obj ncol; if (C_IS_Y(imd->dtype)) { init_ai_objFromDigest(&ncol, &skey->b[0].digest); } else { init_ai_objLong(&ncol, skey->b[0].u.i64); } ai_obj apk; init_ai_objFromDigest(&apk, (cf_digest *)val); ulong bb = pimd->ibtr->msize + pimd->ibtr->nsize; ret = reduced_iRem(pimd->ibtr, &ncol, &apk); ulong ab = pimd->ibtr->msize + pimd->ibtr->nsize; as_sindex_release_data_memory(imd, (bb - ab)); SITRACE(imd->si, DML, debug, "ai__btree_delete(N): key: %d - %lu", bv, uk); return ret; }
int ai_btree_delete(as_sindex_metadata *imd, as_sindex_pmetadata *pimd, void * skey, cf_digest * value) { int ret = AS_SINDEX_OK; if (!pimd->ibtr) { return AS_SINDEX_KEY_NOTFOUND; } ai_obj ncol; if (C_IS_Y(imd->dtype)) { init_ai_objFromDigest(&ncol, (cf_digest *)skey); } else { init_ai_objLong(&ncol, *(ulong *)skey); } ai_obj apk; init_ai_objFromDigest(&apk, value); ulong bb = pimd->ibtr->msize + pimd->ibtr->nsize; ret = reduced_iRem(pimd->ibtr, &ncol, &apk); ulong ab = pimd->ibtr->msize + pimd->ibtr->nsize; as_sindex_release_data_memory(imd, (bb - ab)); return ret; }
void dump_ai_obj(FILE *fp, ai_obj *a) { char dumpbuf[1024]; if (C_IS_S(a->type) || C_IS_O(a->type)) { if (a->empty) { fprintf(fp, "\tSTRING ai_obj: EMPTY\n"); return; } memcpy_ai_objStoDumpBuf(dumpbuf, a); fprintf(fp, "\tSTRING ai_obj: mt: %d len: %d -> (%s) type: %d\n", a->empty, a->len, dumpbuf, a->type); } else if (C_IS_C(a->type)) { if (a->empty) { fprintf(fp, "\tCNAME ai_obj: EMPTY\n"); return; } memcpy_ai_objStoDumpBuf(dumpbuf, a); fprintf(fp, "\tCNAME ai_obj: mt: %d -> (%s) cmatch: %d", a->empty, dumpbuf, a->i); if (a->ic) { fprintf(fp, " ic: "); dumpIC(fp, a->ic); } else { fprintf(fp, "\n"); } } else if (C_IS_I(a->type) || C_IS_P(a->type)) { char *name = C_IS_I(a->type) ? "INT" : "FUNC"; if (a->enc == COL_TYPE_INT || a->enc == COL_TYPE_FUNC) { fprintf(fp, "\t%s ai_obj: mt: %d val: %u\n", name, a->empty, a->i); } else { memcpy_ai_objStoDumpBuf(dumpbuf, a); fprintf(fp, "\t%s(S) ai_obj: mt: %d val: %s\n", name, a->empty, dumpbuf); } } else if (C_IS_L(a->type)) { if (a->enc == COL_TYPE_LONG) { fprintf(fp, "\tLONG ai_obj: mt: %d val: %lu\n", a->empty, a->l); } else { memcpy_ai_objStoDumpBuf(dumpbuf, a); fprintf(fp, "\tLONG(S) ai_obj: mt: %d val: %s\n", a->empty, dumpbuf); } } else if (C_IS_X(a->type)) { fprintf(fp, "\tU128 ai_obj: mt: %d val: ", a->empty); DEBUG_U128(fp, a->x); fprintf(fp, "\n"); } else if (C_IS_Y(a->type)) { fprintf(fp, "\tU160 ai_obj: mt: %d val: ", a->empty); DEBUG_U160(fp, a->y); fprintf(fp, "\n"); } else if (C_IS_F(a->type)) { if (a->enc == COL_TYPE_INT) { fprintf(fp, "\tFLOAT ai_obj: mt: %d val: %f\n", a->empty, a->f); } else { memcpy_ai_objStoDumpBuf(dumpbuf, a); fprintf(fp, "\tFLOAT(S) ai_obj: mt: %d val: %s\n", a->empty, dumpbuf); } } else { fprintf(fp, "\tUNINITIALISED ai_obj mt: %d\n", a->empty); } }
btEntry *btRangeNext(btSIter *siter, bool asc) { //printf("btRangeNext\n"); //printf("btRangeNext: siter: %p\n", (void *)siter); //if (siter) printf("btRangeNext: empty: %d\n", siter->empty); if (!siter || siter->empty) return NULL; bt_n *x = NULL; int i = -1; uchar *stream = btNext(siter, &x, &i, asc); if (!streamToBTEntry(stream, siter, x, i)) return NULL; if (C_IS_I(siter->ktype) || C_IS_L(siter->ktype) || C_IS_G(siter->ktype)) { long l = C_IS_I(siter->ktype) ? (ulong)(siter->key.i) : siter->key.l; if (l == siter->x.high) siter->x.finished = 1; /* exact match */ if (!asc) { //printf("btRangeNext: DESC: l: %lu dr: %u\n", // l, getDR(siter->x.btr, x, i)); l += getDR(siter->x.btr, x, i); } bool over = asc ? (l > siter->x.high) : (l < siter->x.high); if (over && siter->nim) { siter->missed = 1; } //printf("btRangeNext: over: %d l: %lu high: %lu\n", // over, l, siter->x.high); return over ? NULL : &(siter->be); } else if (C_IS_X(siter->ktype)) { uint128 xx = siter->key.x; if (xx == siter->x.highx) siter->x.finished = 1; /* exact match */ if (!asc) { xx += getDR(siter->x.btr, x, i); } bool over = asc ? (xx > siter->x.highx) : (xx < siter->x.highx); return over ? NULL : &(siter->be); } else if (C_IS_Y(siter->ktype)) { uint160 yy = siter->key.y; int ret = u160Cmp(&yy, &siter->x.highy); if (!ret) siter->x.finished = 1; /* exact match */ if (!asc) { //TODO is ENDIANness of memcpy() correct uint32 low; char *spot = ((char *)&yy) + 12; memcpy(&low, spot, 4); low += getDR(siter->x.btr, x, i); memcpy(spot, &low, 4); } bool over = asc ? (ret > 0) : (ret < 0); return over ? NULL : &(siter->be); } else if (C_IS_F(siter->ktype)) { float f = siter->key.f; if (f == siter->x.highf) siter->x.finished = 1; /* exact match */ return asc ? ((f <= siter->x.highf) ? & (siter->be) : NULL) : ((f >= siter->x.highf) ? & (siter->be) : NULL); } else { // C_IS_S() int r = strncmp(siter->key.s, siter->x.highs, siter->key.len); if (r == 0) siter->x.finished = 1; /* exact match */ return asc ? ((r <= 0) ? & (siter->be) : NULL) : ((r >= 0) ? & (siter->be) : NULL); } }
/* * Return 0 in case of success * -1 in case of failure */ static int btree_addsinglerec(as_sindex_metadata *imd, ai_obj * key, cf_digest *dig, cf_ll *recl, uint64_t *n_bdigs, bool * can_partition_query, bool partitions_pre_reserved) { // The digests which belongs to one of the query-able partitions are elligible to go into recl as_partition_id pid = as_partition_getid(*dig); as_namespace * ns = imd->si->ns; if (partitions_pre_reserved) { if (!can_partition_query[pid]) { return 0; } } else { if (!as_partition_is_queryable_lockfree(ns, &ns->partitions[pid])) { return 0; } } bool create = (cf_ll_size(recl) == 0) ? true : false; as_index_keys_arr * keys_arr = NULL; if (!create) { cf_ll_element * ele = cf_ll_get_tail(recl); keys_arr = ((as_index_keys_ll_element*)ele)->keys_arr; if (keys_arr->num == AS_INDEX_KEYS_PER_ARR) { create = true; } } if (create) { keys_arr = as_index_get_keys_arr(); if (!keys_arr) { cf_warning(AS_SINDEX, "Fail to allocate sindex key value array"); return -1; } as_index_keys_ll_element * node = cf_malloc(sizeof(as_index_keys_ll_element)); node->keys_arr = keys_arr; cf_ll_append(recl, (cf_ll_element *)node); } // Copy the digest (value) memcpy(&keys_arr->pindex_digs[keys_arr->num], dig, CF_DIGEST_KEY_SZ); // Copy the key if (C_IS_Y(imd->dtype)) { memcpy(&keys_arr->sindex_keys[keys_arr->num].key.str_key, &key->y, CF_DIGEST_KEY_SZ); } else { keys_arr->sindex_keys[keys_arr->num].key.int_key = key->l; } keys_arr->num++; *n_bdigs = *n_bdigs + 1; return 0; }
int ai_btree_key_hash(as_sindex_metadata *imd, as_sindex_bin *b) { uint64_t u; if (C_IS_Y(imd->dtype)) { char *x = (char *) &b->digest; // x += 4; u = ((* (uint128 *) x) % imd->nprts); } else { u = (((uint64_t) b->u.i64) % imd->nprts); } return (int) u; }
int ai_btree_key_hash(as_sindex_metadata *imd, void *skey) { uint64_t u; if (C_IS_Y(imd->dtype)) { char *x = (char *) ((cf_digest *)skey); // x += 4; u = ((* (uint128 *) x) % imd->nprts); } else { u = ((*(uint64_t*)skey) % imd->nprts); } return (int) u; }
int ai_btree_query(as_sindex_metadata *imd, as_sindex_range *srange, as_sindex_qctx *qctx) { bool err = 1; if (!srange->isrange) { // EQUALITY LOOKUP ai_obj afk; if (C_IS_Y(imd->dtype)) { init_ai_objFromDigest(&afk, &srange->start.digest); } else { init_ai_objLong(&afk, srange->start.u.i64); } err = get_recl(imd, &afk, qctx); } else { // RANGE LOOKUP err = get_numeric_range_recl(imd, srange->start.u.i64, srange->end.u.i64, qctx); } return (err ? AS_SINDEX_ERR_NO_MEMORY : (qctx->n_bdigs >= qctx->bsize) ? AS_SINDEX_CONTINUE : AS_SINDEX_OK); }
static int ai_objCmp(ai_obj *a, ai_obj *b) { if (C_IS_S(a->type)) { return strncmp(a->s, b->s, a->len); } else if (C_IS_F(a->type)) { float f = a->f - b->f; return (f == 0.0) ? 0 : ((f > 0.0) ? 1 : -1); } else if (C_IS_L(a->type)) { return (a->l == b->l) ? 0 : ((a->l > b->l) ? 1 : -1); } else if (C_IS_X(a->type)) { return (a->x == b->x) ? 0 : ((a->x > b->x) ? 1 : -1); } else if (C_IS_Y(a->type)) { return u160Cmp_string20(&a->y, &b->y); } else if (C_IS_I(a->type) || C_IS_P(a->type)) { return (long) (a->i - b->i); } else { assert(!"ai_objCmp ERROR"); } }
static void setHigh(btSIter *siter, ai_obj *high, uchar ktype) { if (C_IS_S(ktype)) { siter->x.highs = cf_malloc(high->len + 1); /* FREE ME 058 */ memcpy(siter->x.highs, high->s, high->len); siter->x.highs[high->len] = '\0'; } else if (C_IS_I(ktype)) { siter->x.high = high->i; } else if (C_IS_L(ktype) || C_IS_G(ktype)) { siter->x.high = high->l; } else if (C_IS_X(ktype)) { siter->x.highx = high->x; } else if (C_IS_Y(ktype)) { siter->x.highy = high->y; } else if (C_IS_F(ktype)) { siter->x.highf = high->f; } }
int ai_btree_put(as_sindex_metadata *imd, as_sindex_pmetadata *pimd, as_sindex_key *skey, void *value) { int ret = AS_SINDEX_OK; uint64_t uk = *(uint64_t *)value; cf_digest *keyd = (cf_digest *)value; ai_obj ncol; if (C_IS_Y(imd->dtype)) { init_ai_objFromDigest(&ncol, &skey->b[0].digest); } else { init_ai_objLong(&ncol, skey->b[0].u.i64); } ai_obj apk; init_ai_objFromDigest(&apk, keyd); cf_detail(AS_SINDEX, "Insert: %ld %ld %ld", *(uint64_t *) &ncol.y, *(uint64_t *) &skey->b[0].digest, *((uint64_t *) &apk.y)); ulong bb = pimd->ibtr->msize + pimd->ibtr->nsize; ret = reduced_iAdd(pimd->ibtr, &ncol, &apk, COL_TYPE_U160); if (ret == AS_SINDEX_KEY_FOUND) { goto END; } else if (ret != AS_SINDEX_OK) { cf_warning(AS_SINDEX, "Insert into the btree failed"); ret = AS_SINDEX_ERR_NO_MEMORY; goto END; } ulong ab = pimd->ibtr->msize + pimd->ibtr->nsize; if (!as_sindex_reserve_data_memory(imd, (ab - bb))) { reduced_iRem(pimd->ibtr, &ncol, &apk); ret = AS_SINDEX_ERR_NO_MEMORY; goto END; } SITRACE(imd->si, DML, debug, "ai__btree_insert(N): %s key: %d val %lu", imd->iname, skey->b[0].u.i64, uk); END: return ret; }
int ai_btree_put(as_sindex_metadata *imd, as_sindex_pmetadata *pimd, void *skey, cf_digest *value) { int ret = AS_SINDEX_OK; ai_obj ncol; if (C_IS_Y(imd->dtype)) { init_ai_objFromDigest(&ncol, (cf_digest*)skey); } else { init_ai_objLong(&ncol, *(ulong *)skey); } ai_obj apk; init_ai_objFromDigest(&apk, value); ulong bb = pimd->ibtr->msize + pimd->ibtr->nsize; ret = reduced_iAdd(pimd->ibtr, &ncol, &apk, COL_TYPE_U160); if (ret == AS_SINDEX_KEY_FOUND) { goto END; } else if (ret != AS_SINDEX_OK) { cf_warning(AS_SINDEX, "Insert into the btree failed"); ret = AS_SINDEX_ERR_NO_MEMORY; goto END; } ulong ab = pimd->ibtr->msize + pimd->ibtr->nsize; if (!as_sindex_reserve_data_memory(imd, (ab - bb))) { reduced_iRem(pimd->ibtr, &ncol, &apk); ret = AS_SINDEX_ERR_NO_MEMORY; goto END; } END: return ret; }