Skip to content

Instantly share code, notes, and snippets.

@sbcd90
Last active May 21, 2017 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbcd90/26e3277f55912f3f05c5ffe5df73b711 to your computer and use it in GitHub Desktop.
Save sbcd90/26e3277f55912f3f05c5ffe5df73b711 to your computer and use it in GitHub Desktop.
Graph Aware expiry module test
package org.neo4j.test;
import com.graphaware.neo4j.expire.ExpirationModule;
import com.graphaware.neo4j.expire.config.ExpirationConfiguration;
import com.graphaware.runtime.GraphAwareRuntime;
import com.graphaware.runtime.GraphAwareRuntimeFactory;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseBuilder;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import java.io.File;
import java.util.function.Consumer;
public class GraphAwareExpiryTest {
public static void main(String[] args) throws Exception {
GraphDatabaseBuilder graphDatabaseBuilder = new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder(new File(System.getProperty("java.io.tmpdir") + File.separator + "neo4j11"));
GraphDatabaseService graphDatabaseService = graphDatabaseBuilder.newGraphDatabase();
GraphAwareRuntime graphAwareRuntime = GraphAwareRuntimeFactory.createRuntime(graphDatabaseService);
ExpirationConfiguration configuration = ExpirationConfiguration.defaultConfiguration().withNodeExpirationProperty("expire");
graphAwareRuntime.registerModule(new ExpirationModule("EXP", graphDatabaseService, configuration));
graphAwareRuntime.start();
graphAwareRuntime.waitUntilStarted();
long now = System.currentTimeMillis();
long twentySecondsFromNow = now + 20 * 1000;
long fiftySecondsFromNow = now + 50 * 1000;
try(Transaction tx = graphDatabaseService.beginTx()) {
Node s1 = graphDatabaseService.createNode(Label.label("State1"));
s1.setProperty("name", "Cloudy");
s1.setProperty("expire", twentySecondsFromNow);
Node s2 = graphDatabaseService.createNode(Label.label("State1"));
s2.setProperty("name", "Windy");
s2.setProperty("expire", fiftySecondsFromNow);
tx.success();
}
System.out.println("1st print");
try(Transaction tx = graphDatabaseService.beginTx()) {
graphDatabaseService.findNodes(Label.label("State1")).forEachRemaining(new Consumer<Node>() {
@Override
public void accept(Node node) {
System.out.println(node.getProperty("expire"));
}
});
tx.success();
}
Thread.sleep(25000 - (System.currentTimeMillis() - now));
System.out.println("2nd print");
try(Transaction tx = graphDatabaseService.beginTx()) {
graphDatabaseService.findNodes(Label.label("State1")).forEachRemaining(new Consumer<Node>() {
@Override
public void accept(Node node) {
System.out.println(node.getProperty("expire"));
}
});
tx.success();
}
Thread.sleep(55000 - (System.currentTimeMillis() - now));
System.out.println("3rd print");
try(Transaction tx = graphDatabaseService.beginTx()) {
graphDatabaseService.findNodes(Label.label("State1")).forEachRemaining(new Consumer<Node>() {
@Override
public void accept(Node node) {
System.out.println(node.getProperty("expire"));
}
});
tx.success();
}
graphDatabaseService.shutdown();
}
}
@bachmanm
Copy link

You're actually doing the whole thing in a single tx without realising it. tx.success() alone does not commit the tx. What you need to do is either call tx.close() afterwards, or even better put it in a try-with-resources block. Something like:

public class GraphAwareExpiryTest {

public static void main(String[] args) throws Exception {
    GraphDatabaseBuilder graphDatabaseBuilder = new GraphDatabaseFactory()
            .newEmbeddedDatabaseBuilder(new File(System.getProperty("java.io.tmpdir") + File.separator + "neo4j9"));
    GraphDatabaseService graphDatabaseService = graphDatabaseBuilder.newGraphDatabase();
    GraphAwareRuntime graphAwareRuntime = GraphAwareRuntimeFactory.createRuntime(graphDatabaseService);

    ExpirationConfiguration configuration = ExpirationConfiguration.defaultConfiguration().withNodeExpirationProperty("expire");

    graphAwareRuntime.registerModule(new ExpirationModule("EXP", graphDatabaseService, configuration));

    graphAwareRuntime.start();
    graphAwareRuntime.waitUntilStarted();

    long now = System.currentTimeMillis();
    long twentySecondsFromNow = now + 20 * 1000;
    long fiftySecondsFromNow = now + 50 * 1000;

    try (Transaction tx = graphDatabaseService.beginTx()) {
        Node s1 = graphDatabaseService.createNode(Label.label("State1"));
        s1.setProperty("name", "Cloudy");
        s1.setProperty("expire", twentySecondsFromNow);

        Node s2 = graphDatabaseService.createNode(Label.label("State1"));
        s2.setProperty("name", "Windy");
        s2.setProperty("expire", fiftySecondsFromNow);

        tx.success();
    }

    System.out.println("1st print");

    try (Transaction tx = graphDatabaseService.beginTx()) {
        graphDatabaseService.findNodes(Label.label("State1")).forEachRemaining(new Consumer<Node>() {
            @Override
            public void accept(Node node) {
                System.out.println(node.getProperty("expire"));
            }
        });

        tx.success();
    }

    Thread.sleep(25000 - (System.currentTimeMillis() - now));

    System.out.println("2nd print");

    try (Transaction tx = graphDatabaseService.beginTx()) {
        graphDatabaseService.findNodes(Label.label("State1")).forEachRemaining(new Consumer<Node>() {
            @Override
            public void accept(Node node) {
                System.out.println(node.getProperty("expire"));
            }
        });

        tx.success();
    }

    Thread.sleep(55000 - (System.currentTimeMillis() - now));

    System.out.println("3rd print");

    try (Transaction tx = graphDatabaseService.beginTx()) {
        graphDatabaseService.findNodes(Label.label("State1")).forEachRemaining(new Consumer<Node>() {
            @Override
            public void accept(Node node) {
                System.out.println(node.getProperty("expire"));
                System.out.println(System.currentTimeMillis() + " is now");
            }
        });

        tx.success();
    }

    graphDatabaseService.shutdown();
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment