Datum spg_text_choose(PG_FUNCTION_ARGS) { spgChooseIn *in = (spgChooseIn *) PG_GETARG_POINTER(0); spgChooseOut *out = (spgChooseOut *) PG_GETARG_POINTER(1); text *inText = DatumGetTextPP(in->datum); char *inStr = VARDATA_ANY(inText); int inSize = VARSIZE_ANY_EXHDR(inText); uint8 nodeChar = '\0'; int i = 0; int commonLen = 0; /* Check for prefix match, set nodeChar to first byte after prefix */ if (in->hasPrefix) { text *prefixText = DatumGetTextPP(in->prefixDatum); char *prefixStr = VARDATA_ANY(prefixText); int prefixSize = VARSIZE_ANY_EXHDR(prefixText); commonLen = commonPrefix(inStr + in->level, prefixStr, inSize - in->level, prefixSize); if (commonLen == prefixSize) { if (inSize - in->level > commonLen) nodeChar = *(uint8 *) (inStr + in->level + commonLen); else nodeChar = '\0'; } else { /* Must split tuple because incoming value doesn't match prefix */ out->resultType = spgSplitTuple; if (commonLen == 0) { out->result.splitTuple.prefixHasPrefix = false; } else { out->result.splitTuple.prefixHasPrefix = true; out->result.splitTuple.prefixPrefixDatum = formTextDatum(prefixStr, commonLen); } out->result.splitTuple.nodeLabel = UInt8GetDatum(*(prefixStr + commonLen)); if (prefixSize - commonLen == 1) { out->result.splitTuple.postfixHasPrefix = false; } else { out->result.splitTuple.postfixHasPrefix = true; out->result.splitTuple.postfixPrefixDatum = formTextDatum(prefixStr + commonLen + 1, prefixSize - commonLen - 1); } PG_RETURN_VOID(); } } else if (inSize > in->level) { nodeChar = *(uint8 *) (inStr + in->level); } else { nodeChar = '\0'; } /* Look up nodeChar in the node label array */ if (searchChar(in->nodeLabels, in->nNodes, nodeChar, &i)) { /* * Descend to existing node. (If in->allTheSame, the core code will * ignore our nodeN specification here, but that's OK. We still * have to provide the correct levelAdd and restDatum values, and * those are the same regardless of which node gets chosen by core.) */ out->resultType = spgMatchNode; out->result.matchNode.nodeN = i; out->result.matchNode.levelAdd = commonLen + 1; if (inSize - in->level - commonLen - 1 > 0) out->result.matchNode.restDatum = formTextDatum(inStr + in->level + commonLen + 1, inSize - in->level - commonLen - 1); else out->result.matchNode.restDatum = formTextDatum(NULL, 0); } else if (in->allTheSame) { /* * Can't use AddNode action, so split the tuple. The upper tuple * has the same prefix as before and uses an empty node label for * the lower tuple. The lower tuple has no prefix and the same * node labels as the original tuple. */ out->resultType = spgSplitTuple; out->result.splitTuple.prefixHasPrefix = in->hasPrefix; out->result.splitTuple.prefixPrefixDatum = in->prefixDatum; out->result.splitTuple.nodeLabel = UInt8GetDatum('\0'); out->result.splitTuple.postfixHasPrefix = false; } else { /* Add a node for the not-previously-seen nodeChar value */ out->resultType = spgAddNode; out->result.addNode.nodeLabel = UInt8GetDatum(nodeChar); out->result.addNode.nodeN = i; } PG_RETURN_VOID(); }
Datum spg_text_picksplit(PG_FUNCTION_ARGS) { spgPickSplitIn *in = (spgPickSplitIn *) PG_GETARG_POINTER(0); spgPickSplitOut *out = (spgPickSplitOut *) PG_GETARG_POINTER(1); text *text0 = DatumGetTextPP(in->datums[0]); int i, commonLen; spgNodePtr *nodes; /* Identify longest common prefix, if any */ commonLen = VARSIZE_ANY_EXHDR(text0); for (i = 1; i < in->nTuples && commonLen > 0; i++) { text *texti = DatumGetTextPP(in->datums[i]); int tmp = commonPrefix(VARDATA_ANY(text0), VARDATA_ANY(texti), VARSIZE_ANY_EXHDR(text0), VARSIZE_ANY_EXHDR(texti)); if (tmp < commonLen) commonLen = tmp; } /* * Limit the prefix length, if necessary, to ensure that the resulting * inner tuple will fit on a page. */ commonLen = Min(commonLen, SPGIST_MAX_PREFIX_LENGTH); /* Set node prefix to be that string, if it's not empty */ if (commonLen == 0) { out->hasPrefix = false; } else { out->hasPrefix = true; out->prefixDatum = formTextDatum(VARDATA_ANY(text0), commonLen); } /* Extract the node label (first non-common byte) from each value */ nodes = (spgNodePtr *) palloc(sizeof(spgNodePtr) * in->nTuples); for (i = 0; i < in->nTuples; i++) { text *texti = DatumGetTextPP(in->datums[i]); if (commonLen < VARSIZE_ANY_EXHDR(texti)) nodes[i].c = *(uint8 *) (VARDATA_ANY(texti) + commonLen); else nodes[i].c = '\0'; /* use \0 if string is all common */ nodes[i].i = i; nodes[i].d = in->datums[i]; } /* * Sort by label bytes so that we can group the values into nodes. This * also ensures that the nodes are ordered by label value, allowing the * use of binary search in searchChar. */ qsort(nodes, in->nTuples, sizeof(*nodes), cmpNodePtr); /* And emit results */ out->nNodes = 0; out->nodeLabels = (Datum *) palloc(sizeof(Datum) * in->nTuples); out->mapTuplesToNodes = (int *) palloc(sizeof(int) * in->nTuples); out->leafTupleDatums = (Datum *) palloc(sizeof(Datum) * in->nTuples); for (i = 0; i < in->nTuples; i++) { text *texti = DatumGetTextPP(nodes[i].d); Datum leafD; if (i == 0 || nodes[i].c != nodes[i - 1].c) { out->nodeLabels[out->nNodes] = UInt8GetDatum(nodes[i].c); out->nNodes++; } if (commonLen < VARSIZE_ANY_EXHDR(texti)) leafD = formTextDatum(VARDATA_ANY(texti) + commonLen + 1, VARSIZE_ANY_EXHDR(texti) - commonLen - 1); else leafD = formTextDatum(NULL, 0); out->leafTupleDatums[nodes[i].i] = leafD; out->mapTuplesToNodes[nodes[i].i] = out->nNodes - 1; } PG_RETURN_VOID(); }
Datum spg_text_choose(PG_FUNCTION_ARGS) { spgChooseIn *in = (spgChooseIn *) PG_GETARG_POINTER(0); spgChooseOut *out = (spgChooseOut *) PG_GETARG_POINTER(1); text *inText = DatumGetTextPP(in->datum); char *inStr = VARDATA_ANY(inText); int inSize = VARSIZE_ANY_EXHDR(inText); char *prefixStr = NULL; int prefixSize = 0; int commonLen = 0; int16 nodeChar = 0; int i = 0; /* Check for prefix match, set nodeChar to first byte after prefix */ if (in->hasPrefix) { text *prefixText = DatumGetTextPP(in->prefixDatum); prefixStr = VARDATA_ANY(prefixText); prefixSize = VARSIZE_ANY_EXHDR(prefixText); commonLen = commonPrefix(inStr + in->level, prefixStr, inSize - in->level, prefixSize); if (commonLen == prefixSize) { if (inSize - in->level > commonLen) nodeChar = *(unsigned char *) (inStr + in->level + commonLen); else nodeChar = -1; } else { /* Must split tuple because incoming value doesn't match prefix */ out->resultType = spgSplitTuple; if (commonLen == 0) { out->result.splitTuple.prefixHasPrefix = false; } else { out->result.splitTuple.prefixHasPrefix = true; out->result.splitTuple.prefixPrefixDatum = formTextDatum(prefixStr, commonLen); } out->result.splitTuple.nodeLabel = Int16GetDatum(*(unsigned char *) (prefixStr + commonLen)); if (prefixSize - commonLen == 1) { out->result.splitTuple.postfixHasPrefix = false; } else { out->result.splitTuple.postfixHasPrefix = true; out->result.splitTuple.postfixPrefixDatum = formTextDatum(prefixStr + commonLen + 1, prefixSize - commonLen - 1); } PG_RETURN_VOID(); } } else if (inSize > in->level) { nodeChar = *(unsigned char *) (inStr + in->level); } else { nodeChar = -1; } /* Look up nodeChar in the node label array */ if (searchChar(in->nodeLabels, in->nNodes, nodeChar, &i)) { /* * Descend to existing node. (If in->allTheSame, the core code will * ignore our nodeN specification here, but that's OK. We still have * to provide the correct levelAdd and restDatum values, and those are * the same regardless of which node gets chosen by core.) */ int levelAdd; out->resultType = spgMatchNode; out->result.matchNode.nodeN = i; levelAdd = commonLen; if (nodeChar >= 0) levelAdd++; out->result.matchNode.levelAdd = levelAdd; if (inSize - in->level - levelAdd > 0) out->result.matchNode.restDatum = formTextDatum(inStr + in->level + levelAdd, inSize - in->level - levelAdd); else out->result.matchNode.restDatum = formTextDatum(NULL, 0); } else if (in->allTheSame) { /* * Can't use AddNode action, so split the tuple. The upper tuple has * the same prefix as before and uses a dummy node label -2 for the * lower tuple. The lower tuple has no prefix and the same node * labels as the original tuple. * * Note: it might seem tempting to shorten the upper tuple's prefix, * if it has one, then use its last byte as label for the lower tuple. * But that doesn't win since we know the incoming value matches the * whole prefix: we'd just end up splitting the lower tuple again. */ out->resultType = spgSplitTuple; out->result.splitTuple.prefixHasPrefix = in->hasPrefix; out->result.splitTuple.prefixPrefixDatum = in->prefixDatum; out->result.splitTuple.nodeLabel = Int16GetDatum(-2); out->result.splitTuple.postfixHasPrefix = false; } else { /* Add a node for the not-previously-seen nodeChar value */ out->resultType = spgAddNode; out->result.addNode.nodeLabel = Int16GetDatum(nodeChar); out->result.addNode.nodeN = i; } PG_RETURN_VOID(); }