Search in sources :

Example 96 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class OJDBCExtractor method next.

@Override
public OExtractedItem next() {
    try {
        if (!didNext) {
            if (!rs.next())
                throw new NoSuchElementException("[JDBC extractor] previous position was " + current);
        }
        didNext = false;
        final ODocument doc = new ODocument();
        for (int i = 0; i < rsColumns; i++) {
            // final OType fieldType = columnTypes != null ? columnTypes.get(i) : null;
            Object fieldValue = rs.getObject(i + 1);
            doc.field(columnNames.get(i), fieldValue);
        }
        return new OExtractedItem(current++, doc);
    } catch (SQLException e) {
        throw new OExtractorException("[JDBC extractor] error on moving forward in resultset of query '" + query + "'. Previous position was " + current, e);
    }
}
Also used : SQLException(java.sql.SQLException) OExtractedItem(com.orientechnologies.orient.etl.OExtractedItem) NoSuchElementException(java.util.NoSuchElementException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 97 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class OXmlExtractor method xml2doc.

private Object xml2doc(final Node xmlDocument, final String iPath, final int iLevel) {
    final ODocument doc = new ODocument();
    Object result = doc;
    final NamedNodeMap attrs = xmlDocument.getAttributes();
    if (attrs != null)
        for (int i = 0; i < attrs.getLength(); ++i) {
            final Node item = attrs.item(i);
            switch(item.getNodeType()) {
                case Node.ATTRIBUTE_NODE:
                    {
                        final Attr attr = (Attr) item;
                        doc.field(attr.getName(), attr.getValue());
                        break;
                    }
            }
        }
    final NodeList children = xmlDocument.getChildNodes();
    if (children != null) {
        for (int i = 0; i < children.getLength(); ++i) {
            final Node child = children.item(i);
            switch(child.getNodeType()) {
                case Node.ELEMENT_NODE:
                    {
                        final Element element = (Element) child;
                        final String path = iPath.isEmpty() ? element.getNodeName() : iPath + "." + element.getNodeName();
                        if (tagsAsAttribute.contains(iPath)) {
                            final NodeList subChildren = element.getChildNodes();
                            if (subChildren.getLength() > 0) {
                                final Node fieldContent = subChildren.item(0);
                                doc.field(element.getNodeName(), fieldContent.getTextContent());
                            }
                        } else {
                            final Object sub = xml2doc(element, path, iLevel + 1);
                            final Object previous = doc.field(element.getNodeName());
                            if (previous != null) {
                                List list;
                                if (previous instanceof List) {
                                    list = (List) previous;
                                } else {
                                    // TRANSFORM IN A LIST
                                    list = new ArrayList();
                                    list.add(previous);
                                    doc.field(element.getNodeName(), list, OType.EMBEDDEDLIST);
                                }
                                list.add(sub);
                            } else
                                doc.field(element.getNodeName(), sub, OType.EMBEDDED);
                            if (rootNode != null && rootNode.startsWith(path))
                                // SKIP
                                result = doc.field(element.getNodeName());
                        }
                        break;
                    }
            }
        }
    }
    return result;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) List(java.util.List) Attr(org.w3c.dom.Attr) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 98 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class SimulateOperationsAgainstServer method updateDocument.

protected void updateDocument(final int threadId, final int iCycle, final String dbUrl, final String className, final int iSkip) {
    final ODatabaseDocumentTx db = getDatabase(dbUrl);
    for (int retry = 0; retry < MAX_RETRY; ++retry) {
        ODocument doc = null;
        try {
            List<OIdentifiable> result = db.query(new OSQLSynchQuery<Object>("select from " + className + " skip " + iSkip + " limit 1"));
            if (result == null || result.isEmpty())
                log(threadId, iCycle, dbUrl, " update no item " + iSkip + " because out of range");
            else {
                doc = (ODocument) result.get(0);
                doc.field("updated", "" + (doc.getVersion() + 1));
                doc.save();
                log(threadId, iCycle, dbUrl, " updated item " + iSkip + " RID=" + result.get(0));
            }
            // OK
            break;
        } catch (OConcurrentModificationException e) {
            log(threadId, iCycle, dbUrl, " concurrent update against record " + doc + ", reload it and retry " + retry + "/" + MAX_RETRY + "...");
            if (doc != null)
                doc.reload(null, true);
        } catch (ORecordNotFoundException e) {
            log(threadId, iCycle, dbUrl, " update no item " + iSkip + " because not found");
            break;
        } finally {
            db.close();
        }
    }
}
Also used : ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 99 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class SimulateOperationsAgainstServer method createDocument.

protected void createDocument(final int threadId, final int iCycle, final String dbUrl, final String className, final int iProperties) {
    final ODatabaseDocumentTx db = getDatabase(dbUrl);
    try {
        log(threadId, iCycle, dbUrl, " creating document: class=" + className);
        ODocument doc = new ODocument(className);
        for (int i = 0; i < iProperties; ++i) {
            doc.field("prop" + i, "propValue" + i);
        }
        doc.save();
    } finally {
        db.close();
    }
}
Also used : ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 100 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class TestDistributeConfigSerialization method executeTest.

@Override
protected void executeTest() throws Exception {
    ODatabaseDocumentTx db = new ODatabaseDocumentTx("plocal:target/server0/databases/" + getDatabaseName());
    try {
        db.setSerializer(new ORecordSerializerSchemaAware2CSV());
        db.create();
        db.getMetadata().getSchema().createClass("TestMessaging");
        db.activateOnCurrentThread();
        db.save(new ODocument("TestMessaging").field("test", "test"));
        db.query(new OSQLSynchQuery("select from TestMessaging "));
        db.close();
    } catch (OException e) {
        e.printStackTrace();
        Assert.fail("error on creating a csv database in distributed environment");
    }
    try {
        db = new ODatabaseDocumentTx("remote:localhost/" + getDatabaseName());
        db.open("admin", "admin");
        db.query(new OSQLSynchQuery("select from TestMessaging "));
        db.close();
    } catch (OException e) {
        e.printStackTrace();
        Assert.fail("error on creating a csv database in distributed environment");
    }
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OException(com.orientechnologies.common.exception.OException) ORecordSerializerSchemaAware2CSV(com.orientechnologies.orient.core.serialization.serializer.record.string.ORecordSerializerSchemaAware2CSV) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2200 Test (org.testng.annotations.Test)651 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)426 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)422 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)277 Test (org.junit.Test)267 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)257 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)244 ORID (com.orientechnologies.orient.core.id.ORID)196 ORecordId (com.orientechnologies.orient.core.id.ORecordId)139 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)122 ArrayList (java.util.ArrayList)118 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)103 HashMap (java.util.HashMap)103 HashSet (java.util.HashSet)96 ORecord (com.orientechnologies.orient.core.record.ORecord)80 Set (java.util.Set)76 OETLBaseTest (com.orientechnologies.orient.etl.OETLBaseTest)75 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)68 Collection (java.util.Collection)55