Search in sources :

Example 1 with GraphObject

use of org.structr.core.GraphObject in project structr by structr.

the class PerformanceTest method testPerformanceOfNodeDeletion.

@Test
public void testPerformanceOfNodeDeletion() {
    final App app = StructrApp.getInstance(setup());
    final List<TestOne> nodes = new LinkedList<>();
    final int number = 1000;
    try {
        try (final Tx tx = app.tx()) {
            nodes.addAll(createNodes(app, TestOne.class, number));
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
    // start measuring
    final long t0 = System.currentTimeMillis();
    try {
        final BulkDeleteCommand cmd = app.command(BulkDeleteCommand.class);
        try (final Tx tx = app.tx()) {
            final Iterator<GraphObject> iterator = (Iterator) nodes.iterator();
            cmd.bulkDelete(iterator);
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
    final long t1 = System.currentTimeMillis();
    DecimalFormat decimalFormat = new DecimalFormat("0.000000000", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
    Double time = (t1 - t0) / 1000.0;
    Double rate = number / ((t1 - t0) / 1000.0);
    logger.info("Deleted {} nodes in {} seconds ({} per s)", number, decimalFormat.format(time), decimalFormat.format(rate));
    assertTrue("Deletion rate of nodes too low, expected > 100, was " + rate, rate > 50);
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) BulkDeleteCommand(org.structr.core.graph.BulkDeleteCommand) DecimalFormat(java.text.DecimalFormat) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) Iterator(java.util.Iterator) TestOne(org.structr.web.entity.TestOne) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 2 with GraphObject

use of org.structr.core.GraphObject in project structr by structr.

the class DOMNodeChildrenCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final DOMNode node = getDOMNode(webSocketData.getId());
    if (node == null) {
        return;
    }
    final List<GraphObject> result = new LinkedList<>();
    DOMNode currentNode = (DOMNode) node.getFirstChild();
    while (currentNode != null) {
        result.add(currentNode);
        currentNode = (DOMNode) currentNode.getNextSibling();
    }
    webSocketData.setView(PropertyView.Ui);
    webSocketData.setResult(result);
    // send only over local connection
    getWebSocket().send(webSocketData, true);
}
Also used : DOMNode(org.structr.web.entity.dom.DOMNode) GraphObject(org.structr.core.GraphObject)

Example 3 with GraphObject

use of org.structr.core.GraphObject in project structr by structr.

the class SyncCommand method exportToFile.

// ----- static methods -----
/**
 * Exports the whole structr database to a file with the given name.
 *
 * @param graphDb
 * @param fileName
 * @param includeFiles
 * @throws FrameworkException
 */
public static void exportToFile(final DatabaseService graphDb, final String fileName, final String query, final boolean includeFiles) throws FrameworkException {
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        final NodeFactory nodeFactory = new NodeFactory(SecurityContext.getSuperUserInstance());
        final RelationshipFactory relFactory = new RelationshipFactory(SecurityContext.getSuperUserInstance());
        final Set<AbstractNode> nodes = new HashSet<>();
        final Set<AbstractRelationship> rels = new HashSet<>();
        boolean conditionalIncludeFiles = includeFiles;
        if (query != null) {
            logger.info("Using Cypher query {} to determine export set, disabling export of files", query);
            conditionalIncludeFiles = false;
            final List<GraphObject> result = StructrApp.getInstance().cypher(query, null);
            for (final GraphObject obj : result) {
                if (obj.isNode()) {
                    nodes.add((AbstractNode) obj.getSyncNode());
                } else {
                    rels.add((AbstractRelationship) obj.getSyncRelationship());
                }
            }
            logger.info("Query returned {} nodes and {} relationships.", new Object[] { nodes.size(), rels.size() });
        } else {
            nodes.addAll(nodeFactory.bulkInstantiate(graphDb.getAllNodes()));
            rels.addAll(relFactory.bulkInstantiate(graphDb.getAllRelationships()));
        }
        try (final FileOutputStream fos = new FileOutputStream(fileName)) {
            exportToStream(fos, nodes, rels, null, conditionalIncludeFiles);
        }
        tx.success();
    } catch (Throwable t) {
        logger.warn("", t);
        throw new FrameworkException(500, t.getMessage());
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) AbstractRelationship(org.structr.core.entity.AbstractRelationship) GraphObject(org.structr.core.GraphObject) FileOutputStream(java.io.FileOutputStream) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with GraphObject

use of org.structr.core.GraphObject in project structr by structr.

the class CypherQueryCommand method handleObject.

final Object handleObject(final NodeFactory nodeFactory, final RelationshipFactory relFactory, final String key, final Object value, boolean includeHiddenAndDeleted, boolean publicOnly, int level) throws FrameworkException {
    GraphObject graphObject = null;
    if (value instanceof Node) {
        graphObject = nodeFactory.instantiate((Node) value, includeHiddenAndDeleted, publicOnly);
    } else if (value instanceof Relationship) {
        graphObject = relFactory.instantiate((Relationship) value);
    } else if (value instanceof Map) {
        final Map<String, Object> valueMap = (Map<String, Object>) value;
        graphObject = new GraphObjectMap();
        for (final Entry<String, Object> valueEntry : valueMap.entrySet()) {
            final String valueKey = valueEntry.getKey();
            final Object valueValue = valueEntry.getValue();
            graphObject.setProperty(new GenericProperty(valueKey), handleObject(nodeFactory, relFactory, valueKey, valueValue, includeHiddenAndDeleted, publicOnly, level + 1));
        }
    } else if (value instanceof Collection) {
        final Collection<Object> valueCollection = (Collection<Object>) value;
        final List<Object> collection = new LinkedList<>();
        for (final Object valueEntry : valueCollection) {
            collection.add(handleObject(nodeFactory, relFactory, null, valueEntry, includeHiddenAndDeleted, publicOnly, level + 1));
        }
        return collection;
    } else if (level == 0) {
        graphObject = new GraphObjectMap();
        graphObject.setProperty(new GenericProperty(key), value);
    } else {
        return value;
    }
    return graphObject;
}
Also used : Node(org.structr.api.graph.Node) GraphObject(org.structr.core.GraphObject) Relationship(org.structr.api.graph.Relationship) GraphObjectMap(org.structr.core.GraphObjectMap) GenericProperty(org.structr.core.property.GenericProperty) Collection(java.util.Collection) GraphObject(org.structr.core.GraphObject) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap)

Example 5 with GraphObject

use of org.structr.core.GraphObject in project structr by structr.

the class ValuesFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    // if (arrayHasMinLengthAndAllElementsNotNull(sources, 2) && sources[0] instanceof GraphObject) {
    if (sources.length >= 2 && sources[0] instanceof GraphObject) {
        final Set<Object> values = new LinkedHashSet<>();
        final GraphObject source = (GraphObject) sources[0];
        for (final PropertyKey key : source.getPropertyKeys(sources[1].toString())) {
            values.add(source.getProperty(key));
        }
        return new LinkedList<>(values);
    // } else if (arrayHasMinLengthAndAllElementsNotNull(sources, 1) && sources[0] instanceof GraphObjectMap) {
    } else if (sources.length >= 1) {
        if (sources[0] instanceof GraphObjectMap) {
            return new LinkedList<>(((GraphObjectMap) sources[0]).values());
        } else if (sources[0] instanceof Map) {
            return new LinkedList<>(((Map) sources[0]).values());
        }
    }
    logParameterError(caller, sources, ctx.isJavaScriptContext());
    return "";
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GraphObjectMap(org.structr.core.GraphObjectMap) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) PropertyKey(org.structr.core.property.PropertyKey) LinkedList(java.util.LinkedList)

Aggregations

GraphObject (org.structr.core.GraphObject)151 FrameworkException (org.structr.common.error.FrameworkException)58 PropertyKey (org.structr.core.property.PropertyKey)39 LinkedList (java.util.LinkedList)35 SecurityContext (org.structr.common.SecurityContext)25 App (org.structr.core.app.App)25 StructrApp (org.structr.core.app.StructrApp)24 Tx (org.structr.core.graph.Tx)23 List (java.util.List)22 PropertyConverter (org.structr.core.converter.PropertyConverter)22 AbstractNode (org.structr.core.entity.AbstractNode)18 GraphObjectMap (org.structr.core.GraphObjectMap)17 NodeInterface (org.structr.core.graph.NodeInterface)17 LinkedHashSet (java.util.LinkedHashSet)15 PropertyMap (org.structr.core.property.PropertyMap)13 Map (java.util.Map)12 RestMethodResult (org.structr.rest.RestMethodResult)11 ConfigurationProvider (org.structr.schema.ConfigurationProvider)10 Result (org.structr.core.Result)9 ArrayList (java.util.ArrayList)8