Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to delete while iterating over concepts #466

Open
lolski opened this issue Dec 1, 2020 · 1 comment
Open

Document how to delete while iterating over concepts #466

lolski opened this issue Dec 1, 2020 · 1 comment

Comments

@lolski
Copy link
Member

lolski commented Dec 1, 2020

Trying to delete concepts as you iterate over them will fail with a ConcurrentModificationException:

try (Grakn.Session session = grakn.session(database, Arguments.Session.Type.DATA)) {
                try (Grakn.Transaction tx = session.transaction(Arguments.Transaction.Type.WRITE)) {
                    tx.query().insert(Graql.parseQuery("insert $x true isa property;"));
                    tx.query().insert(Graql.parseQuery("insert $x false isa property;"));
                    Stream<? extends Attribute> properties = tx.concepts().getAttributeType("property").getInstances().limit(10);
                    properties.forEach(e -> { // the 2nd iteration will fail because the original collection has been modified by the delete() from the 1st iteration
                        e.delete();
                    });
                    tx.commit();
                }
            }

Instead, the user should collect the concepts onto a separate list and iterate over them:

        try (Grakn grakn = RocksGrakn.open(directory)) {
            grakn.databases().create(database);

            try (Grakn.Session session = grakn.session(database, Arguments.Session.Type.SCHEMA)) {
                try (Grakn.Transaction tx = session.transaction(Arguments.Transaction.Type.WRITE)) {
                    tx.query().define(Graql.parseQuery("define property sub attribute, value boolean;"));
                    tx.commit();
                }
            }

            try (Grakn.Session session = grakn.session(database, Arguments.Session.Type.DATA)) {
                try (Grakn.Transaction tx = session.transaction(Arguments.Transaction.Type.WRITE)) {
                    tx.query().insert(Graql.parseQuery("insert $x true isa property;"));
                    tx.query().insert(Graql.parseQuery("insert $x false isa property;"));
                    List<? extends Attribute> properties = tx.concepts().getAttributeType("property").getInstances().limit(10).collect(Collectors.toList());
                    for (Attribute t: properties) {
                        t.delete();
                    }
                    tx.commit();
                }
            }
        }
@haikalpribadi
Copy link
Member

actually, the right solution is to just use Graql Delete query. But yes we should explain this in the documentation.

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

No branches or pull requests

4 participants