Search in sources :

Example 11 with OBlob

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;
}
Also used : OBlob(com.orientechnologies.orient.core.record.impl.OBlob) BufferedInputStream(java.io.BufferedInputStream) ORecordBytes(com.orientechnologies.orient.core.record.impl.ORecordBytes) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 12 with OBlob

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;
}
Also used : OBlob(com.orientechnologies.orient.core.record.impl.OBlob) BufferedInputStream(java.io.BufferedInputStream) ORecordBytes(com.orientechnologies.orient.core.record.impl.ORecordBytes) ArrayList(java.util.ArrayList) ORID(com.orientechnologies.orient.core.id.ORID) File(java.io.File) FileInputStream(java.io.FileInputStream) OIntentMassiveInsert(com.orientechnologies.orient.core.intent.OIntentMassiveInsert)

Example 13 with OBlob

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);
            }
        }
    }
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) OBlob(com.orientechnologies.orient.core.record.impl.OBlob) OType(com.orientechnologies.orient.core.metadata.schema.OType) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 14 with OBlob

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);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OBlob(com.orientechnologies.orient.core.record.impl.OBlob) ORecordBytes(com.orientechnologies.orient.core.record.impl.ORecordBytes) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ORID(com.orientechnologies.orient.core.id.ORID) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) Test(org.testng.annotations.Test)

Example 15 with OBlob

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;
}
Also used : OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) Entry(java.util.Map.Entry) OBlob(com.orientechnologies.orient.core.record.impl.OBlob) ORecord(com.orientechnologies.orient.core.record.ORecord) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OBlob (com.orientechnologies.orient.core.record.impl.OBlob)32 ORecordBytes (com.orientechnologies.orient.core.record.impl.ORecordBytes)20 Test (org.testng.annotations.Test)19 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)12 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)8 ORID (com.orientechnologies.orient.core.id.ORID)4 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)4 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)3 IOException (java.io.IOException)3 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 OAfterDeserialization (com.orientechnologies.orient.core.annotation.OAfterDeserialization)1 OBeforeSerialization (com.orientechnologies.orient.core.annotation.OBeforeSerialization)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)1