typedb-docs icon indicating copy to clipboard operation
typedb-docs copied to clipboard

Document how to delete while iterating over concepts

Open lolski opened this issue 4 years ago • 1 comments

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();
                }
            }
        }

lolski avatar Dec 01 '20 14:12 lolski

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

haikalpribadi avatar Feb 03 '21 11:02 haikalpribadi