static int __batcher_commit(batch_monitor_t* m, batch_queue_t* bq) { if (0 == heap_count(bq->queue)) return 0; MDB_txn *txn; int e = mdb_txn_begin(sv->db_env, NULL, 0, &txn); if (0 != e) mdb_fatal(e); while (0 < heap_count(bq->queue)) { batch_item_t* item = heap_poll(bq->queue); e = mdb_put(txn, sv->docs, &item->key, &item->val, item->flags); switch (e) { case 0: break; case MDB_MAP_FULL: { mdb_txn_abort(txn); while ((item = heap_poll(bq->queue))) ; snprintf(batcher_error, BATCHER_ERROR_LEN, "NOT ENOUGH SPACE"); return -1; } case MDB_KEYEXIST: item->flags = WOULD_OVERWRITE; break; default: mdb_fatal(e); } } e = mdb_txn_commit(txn); if (0 != e) mdb_fatal(e); return 0; }
int bt_rarestfirst_selector_poll_best_piece( void *r, const void *peer ) { rarestfirst_t *rf = r; heap_t *hp; peer_t *pr; void *p; int piece_idx; hashmap_iterator_t iter; piece_t *pce; if (!(pr = hashmap_get(rf->peers, peer))) { return -1; } /* if we have no pieces, fail */ if (0 == hashmap_count(pr->have_pieces)) { return -1; } hp = heap_new(__cmp_piece, rf); /* add to priority queue */ for (hashmap_iterator(rf->pieces, &iter); (p = hashmap_iterator_next(rf->pieces, &iter));) { /* only add if peer has it */ if (hashmap_get(pr->have_pieces, p)) { pce = hashmap_get(rf->pieces, p); heap_offer(hp, pce); } } /* queue best from priority queue */ if ((pce = heap_poll(hp))) { piece_idx = pce->idx; hashmap_remove(rf->pieces, (void *) (long) pce->idx); hashmap_put(rf->pieces_polled, (void *) (long) pce->idx, pce); } else { piece_idx = -1; } heap_free(hp); return piece_idx; }
/** * Find a slot that is free on this VBO * @return a new item slot on this VBO */ int ren_vbosquare_new_itemslot(const int id) { vbo_t *vb = __get_vbo_from_id(id); /* if the heap is empty just use the most latest slot */ if (0 == heap_count(vb->heap)) { assert(vb->n_length_inuse < vb->n_elem); return vb->n_length_inuse++; } else { unsigned long slot; slot = (unsigned long) heap_poll(vb->heap); return slot; } }
/** * Find a slot that is free on this VBO * @return a new item slot on this VBO */ int ren_vbom_new_itemslot( const int id ) { vbo_t *vb = __get_vbo_from_id(id); if (0 == heap_count(vb->heap)) { assert(vb->n_length_inuse < vb->n_elem); return vb->n_length_inuse++; } else { unsigned long slot; slot = (unsigned long) heap_poll(vb->heap); return slot; } }
QueryResult *Query_Execute(Query *query) { //__queryStage_Print(query->root, 0); QueryResult *res = malloc(sizeof(QueryResult)); res->error = 0; res->errorString = NULL; res->totalResults = 0; res->ids = NULL; res->numIds = 0; int num = query->offset + query->limit; heap_t *pq = malloc(heap_sizeof(num)); heap_init(pq, cmpHits, NULL, num); // start lazy evaluation of all query steps IndexIterator *it = NULL; if (query->root != NULL) { it = Query_EvalStage(query, query->root); } // no query evaluation plan? if (query->root == NULL || it == NULL) { res->error = QUERY_ERROR_INTERNAL; res->errorString = QUERY_ERROR_INTERNAL_STR; return res; } IndexHit *pooledHit = NULL; // iterate the root iterator and push everything to the PQ while (1) { // TODO - Use static allocation if (pooledHit == NULL) { pooledHit = malloc(sizeof(IndexHit)); } IndexHit *h = pooledHit; IndexHit_Init(h); int rc = it->Read(it->ctx, h); if (rc == INDEXREAD_EOF) { break; } else if (rc == INDEXREAD_NOTFOUND) { continue; } h->totalFreq = processHitScore(h, query->docTable); ++res->totalResults; if (heap_count(pq) < heap_size(pq)) { heap_offerx(pq, h); pooledHit = NULL; } else { IndexHit *qh = heap_peek(pq); if (qh->totalFreq < h->totalFreq) { pooledHit = heap_poll(pq); heap_offerx(pq, h); // IndexHit_Terminate(pooledHit); } else { pooledHit = h; // IndexHit_Terminate(pooledHit); } } } if (pooledHit) { free(pooledHit); } it->Free(it); // Reverse the results into the final result size_t n = MIN(heap_count(pq), query->limit); res->numIds = n; res->ids = calloc(n, sizeof(RedisModuleString *)); for (int i = 0; i < n; ++i) { IndexHit *h = heap_poll(pq); LG_DEBUG("Popping %d freq %f", h->docId, h->totalFreq); res->ids[n - i - 1] = Redis_GetDocKey(query->ctx, h->docId); free(h); } // if we still have something in the heap (meaning offset > 0), we need to // poll... while (heap_count(pq) > 0) { IndexHit *h = heap_poll(pq); free(h); } heap_free(pq); return res; }