📄 heaptuple.c
字号:
tp = (char *) tup + tup->t_hoff; /* * now check for any non-fixed length attrs before our attribute */ if (!slow) { if (att[attnum]->attcacheoff != -1) { return (Datum) fetchatt(&(att[attnum]), tp + att[attnum]->attcacheoff); } else if (attnum == 0) return (Datum) fetchatt(&(att[0]), tp); else if (!HeapTupleAllFixed(tuple)) { int j; /* * In for(), we make this <= and not < because we want to test * if we can go past it in initializing offsets. */ for (j = 0; j <= attnum; j++) { if (att[j]->attlen < 1 && !VARLENA_FIXED_SIZE(att[j])) { slow = 1; break; } } } } /* * If slow is zero, and we got here, we know that we have a tuple with * no nulls or varlenas before the target attribute. If possible, we * also want to initialize the remainder of the attribute cached * offset values. */ if (!slow) { int j = 1; long off; /* * need to set cache for some atts */ att[0]->attcacheoff = 0; while (att[j]->attcacheoff > 0) j++; if (!VARLENA_FIXED_SIZE(att[j - 1])) off = att[j - 1]->attcacheoff + att[j - 1]->attlen; else off = att[j - 1]->attcacheoff + att[j - 1]->atttypmod; for (; j <= attnum || /* Can we compute more? We will probably need them */ (j < tup->t_natts && att[j]->attcacheoff == -1 && (HeapTupleNoNulls(tuple) || !att_isnull(j, bp)) && (HeapTupleAllFixed(tuple) || att[j]->attlen > 0 || VARLENA_FIXED_SIZE(att[j]))); j++) { /* * Fix me when going to a machine with more than a four-byte * word! */ off = att_align(off, att[j]->attlen, att[j]->attalign); att[j]->attcacheoff = off; off = att_addlength(off, att[j]->attlen, tp + off); } return (Datum) fetchatt(&(att[attnum]), tp + att[attnum]->attcacheoff); } else { bool usecache = true; int off = 0; int i; /* * Now we know that we have to walk the tuple CAREFULLY. * * Note - This loop is a little tricky. On iteration i we first set * the offset for attribute i and figure out how much the offset * should be incremented. Finally, we need to align the offset * based on the size of attribute i+1 (for which the offset has * been computed). -mer 12 Dec 1991 */ for (i = 0; i < attnum; i++) { if (!HeapTupleNoNulls(tuple)) { if (att_isnull(i, bp)) { usecache = false; continue; } } /* If we know the next offset, we can skip the rest */ if (usecache && att[i]->attcacheoff != -1) off = att[i]->attcacheoff; else { off = att_align(off, att[i]->attlen, att[i]->attalign); if (usecache) att[i]->attcacheoff = off; } off = att_addlength(off, att[i]->attlen, tp + off); if (usecache && att[i]->attlen == -1 && !VARLENA_FIXED_SIZE(att[i])) usecache = false; } off = att_align(off, att[attnum]->attlen, att[attnum]->attalign); return (Datum) fetchatt(&(att[attnum]), tp + off); }}/* ---------------- * heap_copytuple * * returns a copy of an entire tuple * ---------------- */HeapTupleheap_copytuple(HeapTuple tuple){ HeapTuple newTuple; if (!HeapTupleIsValid(tuple) || tuple->t_data == NULL) return NULL; newTuple = (HeapTuple) palloc(HEAPTUPLESIZE + tuple->t_len); newTuple->t_len = tuple->t_len; newTuple->t_self = tuple->t_self; newTuple->t_data = (HeapTupleHeader) ((char *) newTuple + HEAPTUPLESIZE); memmove((char *) newTuple->t_data, (char *) tuple->t_data, (int) tuple->t_len); return newTuple;}/* ---------------- * heap_copytuple_with_tuple * * returns a copy of an tuple->t_data * ---------------- */voidheap_copytuple_with_tuple(HeapTuple src, HeapTuple dest){ if (!HeapTupleIsValid(src) || src->t_data == NULL) { dest->t_data = NULL; return; } dest->t_len = src->t_len; dest->t_self = src->t_self; dest->t_data = (HeapTupleHeader) palloc(src->t_len); memmove((char *) dest->t_data, (char *) src->t_data, (int) src->t_len); return;}#ifdef NOT_USED/* ---------------- * heap_deformtuple * * the inverse of heap_formtuple (see below) * ---------------- */voidheap_deformtuple(HeapTuple tuple, TupleDesc tdesc, Datum *values, char *nulls){ int i; int natts; Assert(HeapTupleIsValid(tuple)); natts = tuple->t_natts; for (i = 0; i < natts; i++) { bool isnull; values[i] = heap_getattr(tuple, i + 1, tdesc, &isnull); if (isnull) nulls[i] = 'n'; else nulls[i] = ' '; }}#endif/* ---------------- * heap_formtuple * * constructs a tuple from the given *value and *null arrays * * old comments * Handles alignment by aligning 2 byte attributes on short boundries * and 3 or 4 byte attributes on long word boundries on a vax; and * aligning non-byte attributes on short boundries on a sun. Does * not properly align fixed length arrays of 1 or 2 byte types (yet). * * Null attributes are indicated by a 'n' in the appropriate byte * of the *null. Non-null attributes are indicated by a ' ' (space). * * Fix me. (Figure that must keep context if debug--allow give oid.) * Assumes in order. * ---------------- */HeapTupleheap_formtuple(TupleDesc tupleDescriptor, Datum *value, char *nulls){ HeapTuple tuple; /* return tuple */ HeapTupleHeader td; /* tuple data */ int bitmaplen; long len; int hoff; bool hasnull = false; int i; int numberOfAttributes = tupleDescriptor->natts; len = offsetof(HeapTupleHeaderData, t_bits); for (i = 0; i < numberOfAttributes && !hasnull; i++) { if (nulls[i] != ' ') hasnull = true; } if (numberOfAttributes > MaxHeapAttributeNumber) elog(ERROR, "heap_formtuple: numberOfAttributes of %d > %d", numberOfAttributes, MaxHeapAttributeNumber); if (hasnull) { bitmaplen = BITMAPLEN(numberOfAttributes); len += bitmaplen; } hoff = len = MAXALIGN(len); /* be conservative here */ len += ComputeDataSize(tupleDescriptor, value, nulls); tuple = (HeapTuple) palloc(HEAPTUPLESIZE + len); td = tuple->t_data = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE); MemSet((char *) td, 0, (int) len); tuple->t_len = len; ItemPointerSetInvalid(&(tuple->t_self)); td->t_natts = numberOfAttributes; td->t_hoff = hoff; DataFill((char *) td + td->t_hoff, tupleDescriptor, value, nulls, &td->t_infomask, (hasnull ? td->t_bits : NULL)); td->t_infomask |= HEAP_XMAX_INVALID; return tuple;}/* ---------------- * heap_modifytuple * * forms a new tuple from an old tuple and a set of replacement values. * returns a new palloc'ed tuple. * ---------------- */HeapTupleheap_modifytuple(HeapTuple tuple, Relation relation, Datum *replValue, char *replNull, char *repl){ int attoff; int numberOfAttributes; Datum *value; char *nulls; bool isNull; HeapTuple newTuple; uint8 infomask; /* ---------------- * sanity checks * ---------------- */ Assert(HeapTupleIsValid(tuple)); Assert(RelationIsValid(relation)); Assert(PointerIsValid(replValue)); Assert(PointerIsValid(replNull)); Assert(PointerIsValid(repl)); numberOfAttributes = RelationGetForm(relation)->relnatts; /* ---------------- * allocate and fill *value and *nulls arrays from either * the tuple or the repl information, as appropriate. * ---------------- */ value = (Datum *) palloc(numberOfAttributes * sizeof *value); nulls = (char *) palloc(numberOfAttributes * sizeof *nulls); for (attoff = 0; attoff < numberOfAttributes; attoff += 1) { if (repl[attoff] == ' ') { value[attoff] = heap_getattr(tuple, AttrOffsetGetAttrNumber(attoff), RelationGetDescr(relation), &isNull); nulls[attoff] = (isNull) ? 'n' : ' '; } else if (repl[attoff] != 'r') elog(ERROR, "heap_modifytuple: repl is \\%3d", repl[attoff]); else { /* == 'r' */ value[attoff] = replValue[attoff]; nulls[attoff] = replNull[attoff]; } } /* ---------------- * create a new tuple from the *values and *nulls arrays * ---------------- */ newTuple = heap_formtuple(RelationGetDescr(relation), value, nulls); /* ---------------- * copy the header except for t_len, t_natts, t_hoff, t_bits, t_infomask * ---------------- */ infomask = newTuple->t_data->t_infomask; memmove((char *) &newTuple->t_data->t_oid, /* XXX */ (char *) &tuple->t_data->t_oid, ((char *) &tuple->t_data->t_hoff - (char *) &tuple->t_data->t_oid)); /* XXX */ newTuple->t_data->t_infomask = infomask; newTuple->t_data->t_natts = numberOfAttributes; newTuple->t_self = tuple->t_self; return newTuple;}/* ---------------------------------------------------------------- * other misc functions * ---------------------------------------------------------------- */HeapTupleheap_addheader(uint32 natts, /* max domain index */ int structlen, /* its length */ char *structure) /* pointer to the struct */{ HeapTuple tuple; HeapTupleHeader td; /* tuple data */ long len; int hoff; AssertArg(natts > 0); len = offsetof(HeapTupleHeaderData, t_bits); hoff = len = MAXALIGN(len); /* be conservative */ len += structlen; tuple = (HeapTuple) palloc(HEAPTUPLESIZE + len); td = tuple->t_data = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE); MemSet((char *) td, 0, (int) len); tuple->t_len = len; ItemPointerSetInvalid(&(tuple->t_self)); td->t_hoff = hoff; td->t_natts = natts; td->t_infomask = 0; td->t_infomask |= HEAP_XMAX_INVALID; memmove((char *) td + hoff, structure, structlen); return tuple;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -