All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.marklogic.spark.writer.rdf.GraphWriter Maven / Gradle / Ivy

There is a newer version: 2.4.2
Show newest version
/*
 * Copyright © 2024 MarkLogic Corporation. All Rights Reserved.
 */
package com.marklogic.spark.writer.rdf;

import com.marklogic.client.DatabaseClient;
import com.marklogic.client.eval.ServerEvaluationCall;
import com.marklogic.client.io.DocumentMetadataHandle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Set;

/**
 * Knows how to use the non-public "sem:create-graph-document(iri, permissions)" function to write sem:graph
 * documents.
 */
public class GraphWriter {

    private static final Logger logger = LoggerFactory.getLogger(GraphWriter.class);

    private final DatabaseClient databaseClient;
    private final String permissions;

    public GraphWriter(DatabaseClient databaseClient, String permissionsString) {
        this.databaseClient = databaseClient;
        this.permissions = permissionsString != null && permissionsString.trim().length() > 0 ?
            parsePermissions(permissionsString) :
            "xdmp:default-permissions()";
    }

    public void createGraphs(Set graphs) {
        for (String graph : graphs) {
            StringBuilder query = new StringBuilder("declare variable $GRAPH external; ");
            query.append(String.format(
                "if (fn:doc-available($GRAPH)) then () else sem:create-graph-document(sem:iri($GRAPH), %s)",
                permissions)
            );

            if (logger.isDebugEnabled()) {
                logger.debug("Writing graph {} if it does not yet exist.", graph);
            }
            ServerEvaluationCall call = databaseClient.newServerEval().xquery(query.toString());
            call.addVariable("GRAPH", graph);
            call.evalAs(String.class);
        }
    }

    /**
     * We know the permissions string is valid at this point, as if it weren't, the writing process would have failed
     * before the connector gets to here.
     *
     * @param permissions
     * @return
     */
    private String parsePermissions(final String permissions) {
        DocumentMetadataHandle metadata = new DocumentMetadataHandle();
        metadata.getPermissions().addFromDelimitedString(permissions);
        StringBuilder permissionsString = new StringBuilder("(");
        boolean firstOne = true;
        for (String role : metadata.getPermissions().keySet()) {
            for (DocumentMetadataHandle.Capability cap : metadata.getPermissions().get(role)) {
                if (!firstOne) {
                    permissionsString.append(", ");
                }
                permissionsString.append(String.format("xdmp:permission('%s', '%s')", role, cap.toString().toLowerCase()));
                firstOne = false;
            }
        }
        return permissionsString.append(")").toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy