Spaces:
Running
Running
File size: 4,088 Bytes
78c921d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
/* istanbul ignore file */
import { Visitor } from '../visitor.mjs';
import { TimeUnit, UnionMode } from '../enum.mjs';
/** @ignore */ const sum = (x, y) => x + y;
/** @ignore */
export class GetByteLengthVisitor extends Visitor {
visitNull(____, _) {
return 0;
}
visitInt(data, _) {
return data.type.bitWidth / 8;
}
visitFloat(data, _) {
return data.type.ArrayType.BYTES_PER_ELEMENT;
}
visitBool(____, _) {
return 1 / 8;
}
visitDecimal(data, _) {
return data.type.bitWidth / 8;
}
visitDate(data, _) {
return (data.type.unit + 1) * 4;
}
visitTime(data, _) {
return data.type.bitWidth / 8;
}
visitTimestamp(data, _) {
return data.type.unit === TimeUnit.SECOND ? 4 : 8;
}
visitInterval(data, _) {
return (data.type.unit + 1) * 4;
}
visitStruct(data, i) {
return data.children.reduce((total, child) => total + instance.visit(child, i), 0);
}
visitFixedSizeBinary(data, _) {
return data.type.byteWidth;
}
visitMap(data, i) {
// 4 + 4 for the indices
return 8 + data.children.reduce((total, child) => total + instance.visit(child, i), 0);
}
visitDictionary(data, i) {
var _a;
return (data.type.indices.bitWidth / 8) + (((_a = data.dictionary) === null || _a === void 0 ? void 0 : _a.getByteLength(data.values[i])) || 0);
}
}
/** @ignore */
const getUtf8ByteLength = ({ valueOffsets }, index) => {
// 4 + 4 for the indices, `end - start` for the data bytes
return 8 + (valueOffsets[index + 1] - valueOffsets[index]);
};
/** @ignore */
const getBinaryByteLength = ({ valueOffsets }, index) => {
// 4 + 4 for the indices, `end - start` for the data bytes
return 8 + (valueOffsets[index + 1] - valueOffsets[index]);
};
/** @ignore */
const getListByteLength = ({ valueOffsets, stride, children }, index) => {
const child = children[0];
const { [index * stride]: start } = valueOffsets;
const { [index * stride + 1]: end } = valueOffsets;
const visit = instance.getVisitFn(child.type);
const slice = child.slice(start, end - start);
let size = 8; // 4 + 4 for the indices
for (let idx = -1, len = end - start; ++idx < len;) {
size += visit(slice, idx);
}
return size;
};
/** @ignore */
const getFixedSizeListByteLength = ({ stride, children }, index) => {
const child = children[0];
const slice = child.slice(index * stride, stride);
const visit = instance.getVisitFn(child.type);
let size = 0;
for (let idx = -1, len = slice.length; ++idx < len;) {
size += visit(slice, idx);
}
return size;
};
/* istanbul ignore next */
/** @ignore */
const getUnionByteLength = (data, index) => {
return data.type.mode === UnionMode.Dense ?
getDenseUnionByteLength(data, index) :
getSparseUnionByteLength(data, index);
};
/** @ignore */
const getDenseUnionByteLength = ({ type, children, typeIds, valueOffsets }, index) => {
const childIndex = type.typeIdToChildIndex[typeIds[index]];
// 4 for the typeId, 4 for the valueOffsets, then the child at the offset
return 8 + instance.visit(children[childIndex], valueOffsets[index]);
};
/** @ignore */
const getSparseUnionByteLength = ({ children }, index) => {
// 4 for the typeId, then once each for the children at this index
return 4 + instance.visitMany(children, children.map(() => index)).reduce(sum, 0);
};
GetByteLengthVisitor.prototype.visitUtf8 = getUtf8ByteLength;
GetByteLengthVisitor.prototype.visitBinary = getBinaryByteLength;
GetByteLengthVisitor.prototype.visitList = getListByteLength;
GetByteLengthVisitor.prototype.visitFixedSizeList = getFixedSizeListByteLength;
GetByteLengthVisitor.prototype.visitUnion = getUnionByteLength;
GetByteLengthVisitor.prototype.visitDenseUnion = getDenseUnionByteLength;
GetByteLengthVisitor.prototype.visitSparseUnion = getSparseUnionByteLength;
/** @ignore */
export const instance = new GetByteLengthVisitor();
//# sourceMappingURL=bytelength.mjs.map
|