{
MemoryContext oldCtx = CurrentMemoryContext;
int i;
+ int j;
int *entryIndexes;
ItemPointerSetMin(&key->curItem);
* entries can form a match, without any items from the required set. The
* rest go to the additional set.
*/
- key->requiredEntries = palloc(key->nentries * sizeof(GinScanEntry));
- key->additionalEntries = palloc(key->nentries * sizeof(GinScanEntry));
- key->nrequired = key->nadditional = 0;
-
if (key->nentries > 1)
{
MemoryContextSwitchTo(so->tempCtx);
entryIndexes = (int *) palloc(sizeof(int) * key->nentries);
for (i = 0; i < key->nentries; i++)
entryIndexes[i] = i;
-
qsort_arg(entryIndexes, key->nentries, sizeof(int),
entryIndexByFrequencyCmp, key);
- for (i = 0; i < key->nentries; i++)
- key->entryRes[i] = GIN_MAYBE;
-
- for (i = 0; i < key->nentries; i++)
+ for (i = 0; i < key->nentries - 1; i++)
{
- key->requiredEntries[key->nrequired++] = key->scanEntry[entryIndexes[i]];
- key->entryRes[entryIndexes[i]] = GIN_FALSE;
+ /* Pass all entries <= i as FALSE, and the rest as MAYBE */
+ for (j = 0; j <= i; j++)
+ key->entryRes[entryIndexes[j]] = GIN_FALSE;
+ for (j = i + 1; j < key->nentries; j++)
+ key->entryRes[entryIndexes[j]] = GIN_MAYBE;
if (key->triConsistentFn(key) == GIN_FALSE)
break;
}
- for (i = i + 1; i < key->nentries; i++)
- key->additionalEntries[key->nadditional++] = key->scanEntry[entryIndexes[i]];
+ /* i is now the last required entry. */
- /* clean up after consistentFn calls (also frees entryIndexes) */
MemoryContextSwitchTo(oldCtx);
+
+ key->nrequired = i + 1;
+ key->nadditional = key->nentries - key->nrequired;
+ key->requiredEntries = palloc(key->nrequired * sizeof(GinScanEntry));
+ key->additionalEntries = palloc(key->nadditional * sizeof(GinScanEntry));
+
+ j = 0;
+ for (i = 0; i < key->nrequired; i++)
+ key->requiredEntries[i] = key->scanEntry[entryIndexes[j++]];
+ for (i = 0; i < key->nadditional; i++)
+ key->additionalEntries[i] = key->scanEntry[entryIndexes[j++]];
+
+ /* clean up after consistentFn calls (also frees entryIndexes) */
MemoryContextReset(so->tempCtx);
}
else
{
- key->requiredEntries[key->nrequired++] = key->scanEntry[0];
+ key->nrequired = 1;
+ key->nadditional = 0;
+ key->requiredEntries = palloc(1 * sizeof(GinScanEntry));
+ key->requiredEntries[0] = key->scanEntry[0];
}
}
* combination, that's the overall result. Otherwise, return MAYBE. Testing
* every combination is O(n^2), so this is only feasible for a small number of
* MAYBE inputs.
+ *
+ * NB: This function modifies the key->entryRes array!
*/
static GinLogicValue
shimTriConsistentFn(GinScanKey key)
int maybeEntries[MAX_MAYBE_ENTRIES];
int i;
bool boolResult;
- bool recheck = 0;
+ bool recheck = false;
GinLogicValue curResult;
/*
if (nmaybe == 0)
return normalBoolConsistentFn(key);
- /* Try the consistent function with the maybe-inputs set both ways */
+ /* First call consistent function with all the maybe-inputs set FALSE */
for (i = 0; i < nmaybe; i++)
key->entryRes[maybeEntries[i]] = GIN_FALSE;
curResult = normalBoolConsistentFn(key);