use of com.orientechnologies.orient.core.record.impl.OBlob in project orientdb by orientechnologies.
the class OrientDbCreationHelper method loadFile.
private static OBlob loadFile(ODatabaseDocumentInternal database, String filePath) throws IOException {
final File f = new File(filePath);
if (f.exists()) {
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(f));
OBlob record = new ORecordBytes(database);
record.fromInputStream(inputStream);
return record;
}
return null;
}
use of com.orientechnologies.orient.core.record.impl.OBlob in project orientdb by orientechnologies.
the class OrientDbCreationHelper method loadFile.
private static List<ORID> loadFile(ODatabaseDocumentInternal database, String filePath, int bufferSize) throws IOException {
File binaryFile = new File(filePath);
long binaryFileLength = binaryFile.length();
int numberOfRecords = (int) (binaryFileLength / bufferSize);
int remainder = (int) (binaryFileLength % bufferSize);
if (remainder > 0)
numberOfRecords++;
List<ORID> binaryChuncks = new ArrayList<ORID>(numberOfRecords);
BufferedInputStream binaryStream = new BufferedInputStream(new FileInputStream(binaryFile));
byte[] chunk;
database.declareIntent(new OIntentMassiveInsert());
OBlob recordChunk;
for (int i = 0; i < numberOfRecords; i++) {
if (i == numberOfRecords - 1)
chunk = new byte[remainder];
else
chunk = new byte[bufferSize];
binaryStream.read(chunk);
recordChunk = new ORecordBytes(database, chunk);
database.save(recordChunk);
binaryChuncks.add(recordChunk.getIdentity());
}
database.declareIntent(null);
return binaryChuncks;
}
use of com.orientechnologies.orient.core.record.impl.OBlob in project orientdb by orientechnologies.
the class OrientJdbcResultSetMetaData method getColumnType.
public int getColumnType(final int column) throws SQLException {
final ODocument currentRecord = getCurrentRecord();
final String[] fieldNames = currentRecord.fieldNames();
if (column > fieldNames.length)
return Types.NULL;
String fieldName = fieldNames[column - 1];
// The OClass is not available so attempting to retrieve the OType from
// the schema class
// results in a NullPointerException
// OClass oclass = currentRecord.getSchemaClass();
OType otype = currentRecord.fieldType(fieldName);
if (otype == null) {
Object value = currentRecord.field(fieldName);
if (value == null) {
return Types.NULL;
} else if (value instanceof OBlob) {
// records
return Types.BINARY;
} else if (value instanceof ORecordLazyList) {
ORecordLazyList list = (ORecordLazyList) value;
// check if all the list items are instances of ORecordBytes
ListIterator<OIdentifiable> iterator = list.listIterator();
OIdentifiable listElement;
boolean stop = false;
while (iterator.hasNext() && !stop) {
listElement = iterator.next();
if (!(listElement instanceof OBlob))
stop = true;
}
if (!stop) {
return Types.BLOB;
}
}
return this.getSQLTypeFromJavaClass(value);
} else {
if (otype == OType.EMBEDDED || otype == OType.LINK) {
Object value = currentRecord.field(fieldName);
if (value == null) {
return Types.NULL;
}
// 1. Check if the type is another record or a collection of records
if (value instanceof OBlob) {
return Types.BINARY;
} else {
// the default type
return typesSqlTypes.get(otype);
}
} else {
if (otype == OType.EMBEDDEDLIST || otype == OType.LINKLIST) {
Object value = currentRecord.field(fieldName);
if (value == null) {
return Types.NULL;
}
if (value instanceof ORecordLazyList) {
ORecordLazyList list = (ORecordLazyList) value;
// check if all the list items are instances of ORecordBytes
ListIterator<OIdentifiable> iterator = list.listIterator();
OIdentifiable listElement;
boolean stop = false;
while (iterator.hasNext() && !stop) {
listElement = iterator.next();
if (!(listElement instanceof OBlob))
stop = true;
}
if (stop) {
return typesSqlTypes.get(otype);
} else {
return Types.BLOB;
}
} else {
return typesSqlTypes.get(otype);
// return Types.JAVA_OBJECT;
}
} else {
return typesSqlTypes.get(otype);
}
}
}
}
use of com.orientechnologies.orient.core.record.impl.OBlob in project orientdb by orientechnologies.
the class SQLDeleteTest method testBinaryClusterDelete.
@Test
public void testBinaryClusterDelete() {
database.command(new OCommandSQL("create blob cluster testbinaryclusterdelete")).execute();
database.reload();
OBlob bytes = new ORecordBytes(new byte[] { 1, 2, 3 });
database.save(bytes, "testbinaryclusterdelete");
List<OIdentifiable> result = database.query(new OSQLSynchQuery<OIdentifiable>("select from cluster:testbinaryclusterdelete"));
Assert.assertEquals(result.size(), 1);
ORID rid = result.get(0).getIdentity();
database.command(new OCommandSQL("delete from " + rid)).execute();
result = database.query(new OSQLSynchQuery<OIdentifiable>("select from cluster:testbinaryclusterdelete"));
Assert.assertEquals(result.size(), 0);
}
use of com.orientechnologies.orient.core.record.impl.OBlob in project orientdb by orientechnologies.
the class OTableFormatter method parseColumns.
/**
* Fill the column map computing the maximum size for a field.
*
* @param resultSet
* @param limit
* @return
*/
private Map<String, Integer> parseColumns(final Collection<? extends OIdentifiable> resultSet, final int limit) {
final Map<String, Integer> columns = new LinkedHashMap<String, Integer>();
for (String c : prefixedColumns) columns.put(c, minColumnSize);
boolean tempRids = false;
boolean hasClass = false;
int fetched = 0;
for (OIdentifiable id : resultSet) {
ORecord rec = id.getRecord();
for (String c : prefixedColumns) columns.put(c, getColumnSize(fetched, rec, c, columns.get(c)));
if (rec instanceof ODocument) {
((ODocument) rec).setLazyLoad(false);
// PARSE ALL THE DOCUMENT'S FIELDS
final ODocument doc = (ODocument) rec;
for (String fieldName : doc.fieldNames()) {
columns.put(fieldName, getColumnSize(fetched, doc, fieldName, columns.get(fieldName)));
}
if (!hasClass && doc.getClassName() != null)
hasClass = true;
} else if (rec instanceof OBlob) {
// UNIQUE BINARY FIELD
columns.put("value", maxWidthSize - 15);
}
if (!tempRids && !rec.getIdentity().isPersistent())
tempRids = true;
if (limit > -1 && fetched++ >= limit)
break;
}
if (tempRids)
columns.remove("@RID");
if (!hasClass)
columns.remove("@CLASS");
if (footer != null) {
footer.setLazyLoad(false);
// PARSE ALL THE DOCUMENT'S FIELDS
for (String fieldName : footer.fieldNames()) {
columns.put(fieldName, getColumnSize(fetched, footer, fieldName, columns.get(fieldName)));
}
}
// COMPUTE MAXIMUM WIDTH
int width = 0;
for (Entry<String, Integer> col : columns.entrySet()) width += col.getValue();
if (width > maxWidthSize) {
// SCALE COLUMNS AUTOMATICALLY
final List<Map.Entry<String, Integer>> orderedColumns = new ArrayList<Map.Entry<String, Integer>>();
orderedColumns.addAll(columns.entrySet());
Collections.sort(orderedColumns, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
// START CUTTING THE BIGGEST ONES
Collections.reverse(orderedColumns);
while (width > maxWidthSize) {
int oldWidth = width;
for (Map.Entry<String, Integer> entry : orderedColumns) {
final int redux = entry.getValue() * 10 / 100;
if (entry.getValue() - redux < minColumnSize)
// RESTART FROM THE LARGEST COLUMN
break;
entry.setValue(entry.getValue() - redux);
width -= redux;
if (width <= maxWidthSize)
break;
}
if (width == oldWidth)
// REACHED THE MINIMUM
break;
}
// POPULATE THE COLUMNS WITH THE REDUXED VALUES
columns.clear();
for (String c : prefixedColumns) columns.put(c, minColumnSize);
Collections.reverse(orderedColumns);
for (Entry<String, Integer> col : orderedColumns) // if (!col.getKey().equals("#") && !col.getKey().equals("@RID"))
columns.put(col.getKey(), col.getValue());
}
if (tempRids)
columns.remove("@RID");
if (!hasClass)
columns.remove("@CLASS");
for (String c : columnHidden) columns.remove(c);
return columns;
}
Aggregations