use of org.teiid.core.types.Streamable in project teiid by teiid.
the class SizeUtility method getSize.
/**
* Get size of object
* @return Size in bytes
*/
public static long getSize(Object obj, boolean accountForValueCache) {
if (obj == null) {
return 0;
}
Class<? extends Object> clazz = obj.getClass();
if (clazz == DataTypeManager.DefaultDataClasses.STRING) {
int length = ((String) obj).length();
if (length > 0) {
return alignMemory(40 + (2 * length));
}
return 40;
} else if (clazz == DataTypeManager.DefaultDataClasses.VARBINARY) {
int length = ((BinaryType) obj).getLength();
if (length > 0) {
return alignMemory(16 + length);
}
return 16;
} else if (clazz == DataTypeManager.DefaultDataClasses.BIG_DECIMAL) {
int bitLength = ((BigDecimal) obj).unscaledValue().bitLength();
// TODO: this does not account for the possibility of a cached string
long result = 88 + alignMemory(4 + (bitLength >> 3));
return result;
} else if (clazz == DataTypeManager.DefaultDataClasses.BIG_INTEGER) {
int bitLength = ((BigInteger) obj).bitLength();
long result = 40 + alignMemory(4 + (bitLength >> 3));
return result;
} else if (obj instanceof Iterable<?>) {
Iterable<?> i = (Iterable<?>) obj;
long total = 16;
for (Object object : i) {
total += getSize(object, false) + REFERENCE_SIZE;
}
return total;
} else if (clazz.isArray() || obj instanceof ArrayImpl) {
int overhead = 0;
if (obj instanceof ArrayImpl) {
obj = ((ArrayImpl) obj).getValues();
clazz = obj.getClass();
overhead += 2 * REFERENCE_SIZE;
}
Class<?> componentType = clazz.getComponentType();
if (!componentType.isPrimitive()) {
Object[] rows = (Object[]) obj;
// Array overhead
long total = overhead + 16 + alignMemory(rows.length * REFERENCE_SIZE);
for (int i = 0; i < rows.length; i++) {
total += getSize(rows[i], false);
}
return total;
}
int length = Array.getLength(obj);
int primitiveSize = 8;
if (componentType == boolean.class) {
primitiveSize = 4;
} else if (componentType == byte.class) {
primitiveSize = 1;
} else if (componentType == short.class) {
primitiveSize = 2;
} else if (componentType == int.class || componentType == float.class) {
primitiveSize = 4;
}
return overhead + alignMemory(length * primitiveSize) + 16;
} else if (obj instanceof Streamable<?>) {
try {
Streamable<?> s = (Streamable) obj;
Object o = s.getReference();
if (o instanceof BaseLob) {
InputStreamFactory isf = ((BaseLob) o).getStreamFactory();
if (isf.getStorageMode() == StorageMode.MEMORY) {
long length = isf.getLength();
if (length >= 0) {
return 40 + alignMemory(length);
}
} else if (isf.getStorageMode() == StorageMode.PERSISTENT) {
long length = isf.getLength();
return 40 + alignMemory(Math.min(DataTypeManager.MAX_LOB_MEMORY_BYTES, length));
}
}
} catch (Exception e) {
}
} else {
if (SIZE_ESTIMATES.containsKey(clazz)) {
return getSize(accountForValueCache, clazz);
}
// assume we can get a plausable estimate from the serialized size
if (obj instanceof Serializable) {
// we're ignoring classloader differences here
ClassStats stats = objectEstimates.get(clazz.getName());
if (stats == null) {
stats = new ClassStats();
objectEstimates.put(clazz.getName(), stats);
}
int samples = stats.samples.getAndIncrement();
if (samples < 1000 || (samples & 1023) == 1023) {
try {
DummyOutputStream os = new DummyOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os) {
@Override
protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException {
}
@Override
protected void writeStreamHeader() throws IOException {
}
};
oos.writeObject(obj);
oos.close();
int result = (int) alignMemory(os.getBytes() * 3);
if (result > stats.averageSize) {
stats.averageSize = (stats.averageSize + result * 2) / 3;
} else {
stats.averageSize = (stats.averageSize + result) / 2;
}
return result;
} catch (Exception e) {
}
}
return stats.averageSize;
}
}
return getSize(accountForValueCache, clazz);
}
use of org.teiid.core.types.Streamable in project teiid by teiid.
the class LobManager method updateReferences.
@SuppressWarnings("unchecked")
public void updateReferences(List<?> tuple, ReferenceMode mode) throws TeiidComponentException {
for (int i = 0; i < lobIndexes.length; i++) {
Object anObj = tuple.get(lobIndexes[i]);
if (!(anObj instanceof Streamable<?>)) {
continue;
}
Streamable lob = (Streamable) anObj;
String id = lob.getReferenceStreamId();
LobHolder lobHolder = this.lobReferences.get(id);
switch(mode) {
case REMOVE:
if (lobHolder != null) {
lobHolder.referenceCount--;
if (lobHolder.referenceCount < 1) {
this.lobReferences.remove(id);
}
}
break;
case ATTACH:
if (lob.getReference() == null) {
if (lobHolder == null) {
throw new TeiidComponentException(QueryPlugin.Event.TEIID30033, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30033));
}
lob.setReference(lobHolder.lob.getReference());
}
break;
case CREATE:
try {
StorageMode storageMode = InputStreamFactory.getStorageMode(lob);
if (lob.getReferenceStreamId() == null || (inlineLobs && (storageMode == StorageMode.MEMORY || (storageMode != StorageMode.FREE && lob.length() * (lob instanceof ClobType ? 2 : 1) <= maxMemoryBytes)))) {
lob.setReferenceStreamId(null);
// since this is untracked at this point, we must detach if possible
if (inlineLobs && storageMode == StorageMode.OTHER) {
persistLob(lob, null, null, true, maxMemoryBytes);
}
continue;
}
} catch (SQLException e) {
// presumably the lob is bad, but let it slide for now
}
if (lob.getReference() == null) {
throw new TeiidComponentException(QueryPlugin.Event.TEIID30034, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30034));
}
if (lobHolder == null) {
this.lobReferences.put(id, new LobHolder(lob));
} else {
lobHolder.referenceCount++;
}
break;
}
}
}
use of org.teiid.core.types.Streamable in project teiid by teiid.
the class ResultSetImpl method getObjectDirect.
/**
* Get the value of the current row at the column index specified.
* @param column Column index
* @return Value at column, which may be null
* @throws SQLException if this result set has an exception
*/
public Object getObjectDirect(int column) throws SQLException {
checkClosed();
if (column < 1 || column > columnCount) {
// $NON-NLS-1$
throw new IllegalArgumentException(JDBCPlugin.Util.getString("ResultsImpl.Invalid_col_index", column));
}
List<?> cursorRow = batchResults.getCurrentRow();
if (cursorRow == null) {
// $NON-NLS-1$
throw new TeiidSQLException(JDBCPlugin.Util.getString("ResultsImpl.The_cursor_is_not_on_a_valid_row._1"));
}
// defect 13539 - set the currentValue (defined in MMResultSet) so that wasNull() accurately returns whether this value was null
currentValue = cursorRow.get(column - 1);
if (currentValue instanceof Streamable<?>) {
Object reference = ((Streamable<?>) currentValue).getReference();
if (reference != null) {
return reference;
}
if (currentValue instanceof ClobType) {
return new ClobImpl(createInputStreamFactory((ClobType) currentValue), ((ClobType) currentValue).getLength());
} else if (currentValue instanceof BlobType) {
InputStreamFactory isf = createInputStreamFactory((BlobType) currentValue);
isf.setLength(((BlobType) currentValue).getLength());
return new BlobImpl(isf);
} else if (currentValue instanceof XMLType) {
XMLType val = (XMLType) currentValue;
SQLXMLImpl impl = new SQLXMLImpl(createInputStreamFactory(val));
impl.setEncoding(val.getEncoding());
return impl;
}
} else if (currentValue instanceof java.util.Date) {
return TimestampWithTimezone.create((java.util.Date) currentValue, serverTimeZone, getDefaultCalendar(), currentValue.getClass());
} else if (maxFieldSize > 0 && currentValue instanceof String) {
String val = (String) currentValue;
return val.substring(0, Math.min(maxFieldSize / 2, val.length()));
} else if (currentValue instanceof BinaryType) {
BinaryType val = (BinaryType) currentValue;
return val.getBytesDirect();
}
return currentValue;
}
Aggregations