Search in sources :

Example 6 with DocumentVisibility

use of org.apache.rya.mongodb.document.visibility.DocumentVisibility in project incubator-rya by apache.

the class MongoDBRyaDAOIT method testAdd.

@Test
public void testAdd() throws RyaDAOException, MongoException, IOException {
    final MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();
        final RyaStatementBuilder builder = new RyaStatementBuilder();
        builder.setPredicate(new RyaURI("http://temp.com"));
        builder.setSubject(new RyaURI("http://subject.com"));
        builder.setObject(new RyaURI("http://object.com"));
        builder.setColumnVisibility(new DocumentVisibility("B").flatten());
        final MongoDatabase db = conf.getMongoClient().getDatabase(conf.get(MongoDBRdfConfiguration.MONGO_DB_NAME));
        final MongoCollection<Document> coll = db.getCollection(conf.getTriplesCollectionName());
        dao.add(builder.build());
        assertEquals(coll.count(), 1);
        final Document dbo = coll.find().first();
        assertTrue(dbo.containsKey(DOCUMENT_VISIBILITY));
        assertTrue(dbo.containsKey(TIMESTAMP));
    } finally {
        dao.destroy();
    }
}
Also used : RyaURI(org.apache.rya.api.domain.RyaURI) RyaStatementBuilder(org.apache.rya.api.domain.RyaStatement.RyaStatementBuilder) DocumentVisibility(org.apache.rya.mongodb.document.visibility.DocumentVisibility) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

Example 7 with DocumentVisibility

use of org.apache.rya.mongodb.document.visibility.DocumentVisibility in project incubator-rya by apache.

the class MongoDBRyaDAOIT method testDelete.

@Test
public void testDelete() throws RyaDAOException, MongoException, IOException {
    final MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();
        final RyaStatementBuilder builder = new RyaStatementBuilder();
        builder.setPredicate(new RyaURI("http://temp.com"));
        builder.setSubject(new RyaURI("http://subject.com"));
        builder.setObject(new RyaURI("http://object.com"));
        builder.setColumnVisibility(new DocumentVisibility("C").flatten());
        final RyaStatement statement = builder.build();
        final MongoDatabase db = conf.getMongoClient().getDatabase(conf.get(MongoDBRdfConfiguration.MONGO_DB_NAME));
        final MongoCollection<Document> coll = db.getCollection(conf.getTriplesCollectionName());
        dao.add(statement);
        assertEquals(1, coll.count());
        dao.delete(statement, conf);
        assertEquals(0, coll.count());
    } finally {
        dao.destroy();
    }
}
Also used : RyaURI(org.apache.rya.api.domain.RyaURI) RyaStatementBuilder(org.apache.rya.api.domain.RyaStatement.RyaStatementBuilder) RyaStatement(org.apache.rya.api.domain.RyaStatement) DocumentVisibility(org.apache.rya.mongodb.document.visibility.DocumentVisibility) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

Example 8 with DocumentVisibility

use of org.apache.rya.mongodb.document.visibility.DocumentVisibility in project incubator-rya by apache.

the class MongoDBRyaDAOIT method testDeleteWildcardSubjectWithContext.

@Test
public void testDeleteWildcardSubjectWithContext() throws RyaDAOException, MongoException, IOException {
    final MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();
        final RyaStatementBuilder builder = new RyaStatementBuilder();
        builder.setPredicate(new RyaURI("http://temp.com"));
        builder.setSubject(new RyaURI("http://subject.com"));
        builder.setObject(new RyaURI("http://object.com"));
        builder.setContext(new RyaURI("http://context.com"));
        builder.setColumnVisibility(new DocumentVisibility("A&B&C").flatten());
        final RyaStatement statement = builder.build();
        final MongoDatabase db = conf.getMongoClient().getDatabase(conf.get(MongoDBRdfConfiguration.MONGO_DB_NAME));
        final MongoCollection<Document> coll = db.getCollection(conf.getTriplesCollectionName());
        dao.add(statement);
        assertEquals(1, coll.count());
        final RyaStatementBuilder builder2 = new RyaStatementBuilder();
        builder2.setPredicate(new RyaURI("http://temp.com"));
        builder2.setObject(new RyaURI("http://object.com"));
        builder2.setContext(new RyaURI("http://context3.com"));
        final RyaStatement query = builder2.build();
        dao.delete(query, conf);
        assertEquals(1, coll.count());
    } finally {
        dao.destroy();
    }
}
Also used : RyaURI(org.apache.rya.api.domain.RyaURI) RyaStatementBuilder(org.apache.rya.api.domain.RyaStatement.RyaStatementBuilder) RyaStatement(org.apache.rya.api.domain.RyaStatement) DocumentVisibility(org.apache.rya.mongodb.document.visibility.DocumentVisibility) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

Example 9 with DocumentVisibility

use of org.apache.rya.mongodb.document.visibility.DocumentVisibility in project incubator-rya by apache.

the class DocumentVisibilityUtilTest method testGoodExpressions.

@Test
public void testGoodExpressions() throws DocumentVisibilityConversionException {
    int count = 1;
    for (final Pair<String, String> pair : INPUT_AND_EXPECTED_BOOLEAN_EXPRESSIONS) {
        final String booleanExpression = pair.getLeft();
        final String expected = pair.getRight();
        log.info("Valid Test: " + count);
        log.info("Original: " + booleanExpression);
        // Convert to multidimensional array
        final DocumentVisibility dv = new DocumentVisibility(booleanExpression);
        final Object[] multidimensionalArray = DocumentVisibilityUtil.toMultidimensionalArray(dv);
        log.info("Array   : " + Arrays.deepToString(multidimensionalArray));
        // Convert multidimensional array back to string
        final String booleanStringResult = DocumentVisibilityUtil.multidimensionalArrayToBooleanString(multidimensionalArray);
        log.info("Result  : " + booleanStringResult);
        // Compare results
        assertEquals(expected, booleanStringResult);
        log.info("===========================");
        count++;
    }
}
Also used : DocumentVisibility(org.apache.rya.mongodb.document.visibility.DocumentVisibility) Test(org.junit.Test)

Example 10 with DocumentVisibility

use of org.apache.rya.mongodb.document.visibility.DocumentVisibility in project incubator-rya by apache.

the class DisjunctiveNormalFormConverter method convertToDisjunctiveNormalForm.

/**
 * Creates a document visibility boolean expression string into Disjunctive
 * Normal Form (DNF).  Expressions use this format in DNF:<pre>
 * (P1 & P2 & P3 ... Pn) | (Q1 & Q2 ... Qm) ...
 * </pre>
 * @param documentVisibility the {@link DocumentVisibility}.
 * @return a new {@link DocumentVisibility} with its expression in DNF.
 */
public static DocumentVisibility convertToDisjunctiveNormalForm(final DocumentVisibility documentVisibility) {
    // Find all the terms used in the expression
    final List<String> terms = findNodeTerms(documentVisibility.getParseTree(), documentVisibility.getExpression());
    // Create an appropriately sized truth table that has the correct 0's
    // and 1's in place based on the number of terms.
    // This size should be [numberOfTerms][2 ^ numberOfTerms].
    final byte[][] truthTable = createTruthTableInputs(terms);
    // Go through each row in the truth table.
    // If the row has a 1 for the term then create an Authorization for it
    // and test if it works.
    // If the row passes then that means all the terms that were a 1 and
    // were used can be AND'ed together to pass the expression.
    // All the rows that pass can be OR'd together.
    // Disjunction Normal Form: (P1 & P2 & P3 ... Pn) | (Q1 & Q2 ... Qm) ...
    final List<List<String>> termRowsThatPass = new ArrayList<>();
    for (final byte[] row : truthTable) {
        final List<String> termRowToCheck = new ArrayList<>();
        // term that it matches.
        for (int i = 0; i < row.length; i++) {
            final byte entry = row[i];
            if (entry == 1) {
                termRowToCheck.add(terms.get(i));
            }
        }
        final List<String> authList = new ArrayList<>();
        for (final String auth : termRowToCheck) {
            String formattedAuth = auth;
            formattedAuth = StringUtils.removeStart(formattedAuth, "\"");
            formattedAuth = StringUtils.removeEnd(formattedAuth, "\"");
            authList.add(formattedAuth);
        }
        final Authorizations auths = new Authorizations(authList.toArray(new String[0]));
        final boolean hasAccess = DocumentVisibilityUtil.doesUserHaveDocumentAccess(auths, documentVisibility, false);
        if (hasAccess) {
            boolean alreadyCoveredBySimplerTerms = false;
            // (it's a subset)
            for (final List<String> existingTermRowThatPassed : termRowsThatPass) {
                alreadyCoveredBySimplerTerms = termRowToCheck.containsAll(existingTermRowThatPassed);
                if (alreadyCoveredBySimplerTerms) {
                    break;
                }
            }
            if (!alreadyCoveredBySimplerTerms) {
                termRowsThatPass.add(termRowToCheck);
            }
        }
    }
    // Rebuild the term rows that passed as a document visibility boolean
    // expression string.
    final StringBuilder sb = new StringBuilder();
    boolean isFirst = true;
    final boolean hasMultipleGroups = termRowsThatPass.size() > 1;
    for (final List<String> termRowThatPassed : termRowsThatPass) {
        if (isFirst) {
            isFirst = false;
        } else {
            sb.append("|");
        }
        if (hasMultipleGroups && termRowThatPassed.size() > 1) {
            sb.append("(");
        }
        sb.append(Joiner.on("&").join(termRowThatPassed));
        if (hasMultipleGroups && termRowThatPassed.size() > 1) {
            sb.append(")");
        }
    }
    log.trace(sb.toString());
    final DocumentVisibility dnfDv = new DocumentVisibility(sb.toString());
    return dnfDv;
}
Also used : Authorizations(org.apache.accumulo.core.security.Authorizations) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) DocumentVisibility(org.apache.rya.mongodb.document.visibility.DocumentVisibility)

Aggregations

DocumentVisibility (org.apache.rya.mongodb.document.visibility.DocumentVisibility)18 Test (org.junit.Test)12 RyaURI (org.apache.rya.api.domain.RyaURI)7 RyaStatementBuilder (org.apache.rya.api.domain.RyaStatement.RyaStatementBuilder)6 MongoDatabase (com.mongodb.client.MongoDatabase)5 Document (org.bson.Document)5 RyaStatement (org.apache.rya.api.domain.RyaStatement)3 ArrayList (java.util.ArrayList)2 Node (org.apache.accumulo.core.security.ColumnVisibility.Node)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 List (java.util.List)1 Authorizations (org.apache.accumulo.core.security.Authorizations)1 RyaType (org.apache.rya.api.domain.RyaType)1 StatementMetadata (org.apache.rya.api.domain.StatementMetadata)1 MalformedDocumentVisibilityException (org.apache.rya.mongodb.document.visibility.DocumentVisibilityAdapter.MalformedDocumentVisibilityException)1