001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.kernel.modeshape.rdf.impl;
019
020import static java.util.stream.Stream.of;
021import static org.apache.jena.graph.NodeFactory.createURI;
022import static org.apache.jena.graph.Triple.create;
023import static org.apache.jena.rdf.model.ResourceFactory.createTypedLiteral;
024import static org.apache.jena.vocabulary.RDF.type;
025import static org.fcrepo.kernel.api.RdfLexicon.HAS_FIXITY_CHECK_COUNT;
026import static org.fcrepo.kernel.api.RdfLexicon.HAS_FIXITY_ERROR_COUNT;
027import static org.fcrepo.kernel.api.RdfLexicon.HAS_FIXITY_REPAIRED_COUNT;
028import static org.fcrepo.kernel.api.RdfLexicon.REPOSITORY_NAMESPACE;
029import static org.fcrepo.kernel.modeshape.FedoraJcrConstants.ROOT;
030import static org.slf4j.LoggerFactory.getLogger;
031
032import org.apache.jena.rdf.model.Resource;
033import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
034import org.fcrepo.kernel.api.models.FedoraResource;
035import org.fcrepo.metrics.RegistryService;
036
037import java.util.Map;
038import java.util.stream.Stream;
039import org.slf4j.Logger;
040
041import com.codahale.metrics.Counter;
042import org.apache.jena.graph.Triple;
043
044/**
045 * Assemble {@link Triple}s derived from the root of a repository.
046 *
047 * @author ajs6f
048 * @since Oct 18, 2013
049 */
050public class RootRdfContext extends NodeRdfContext {
051
052    private static final String PREFIX = "org.fcrepo.services.";
053    private static final String FIXITY_REPAIRED_COUNTER = "LowLevelStorageService.fixity-repaired-counter";
054    private static final String FIXITY_ERROR_COUNTER = "LowLevelStorageService.fixity-error-counter";
055    private static final String FIXITY_CHECK_COUNTER = "LowLevelStorageService.fixity-check-counter";
056    private static final Logger LOGGER = getLogger(RootRdfContext.class);
057    static final RegistryService registryService = RegistryService.getInstance();
058
059    /**
060     * Ordinary constructor.
061     *
062     * @param resource the resource
063     * @param idTranslator the id translator
064     */
065    public RootRdfContext(final FedoraResource resource,
066                          final IdentifierConverter<Resource, FedoraResource> idTranslator) {
067        super(resource, idTranslator);
068
069        if (resource().hasType(ROOT)) {
070            concat(getRepositoryTriples());
071        }
072    }
073
074    private Stream<Triple> getRepositoryTriples() {
075        LOGGER.trace("Creating RDF triples for repository description");
076
077        final Stream.Builder<Triple> b = Stream.builder();
078
079        of("RepositoryRoot", "Resource", "Container").forEach(x ->
080            b.accept(create(subject(), type.asNode(), createURI(REPOSITORY_NAMESPACE + x))));
081
082        /*
083            FIXME: removing due to performance problems, esp. w/ many files on federated filesystem
084            see: https://www.pivotaltracker.com/story/show/78647248
085
086            final Repository repository = session().getRepository();
087
088            b.add(create(subject(), HAS_OBJECT_COUNT.asNode(), createLiteral(String
089                    .valueOf(getRepositoryCount(repository)))));
090            b.add(create(subject(), HAS_OBJECT_SIZE.asNode(), createLiteral(String
091                    .valueOf(getRepositorySize(repository)))));
092        */
093
094        // retrieve the metrics from the service
095        final Map<String, Counter> counters = registryService.getMetrics().getCounters();
096        // and add the repository metrics to the RDF model
097        if (counters.containsKey(FIXITY_CHECK_COUNTER)) {
098            b.accept(create(subject(), HAS_FIXITY_CHECK_COUNT.asNode(),
099                    createTypedLiteral(counters.get(PREFIX + FIXITY_CHECK_COUNTER).getCount()).asNode()));
100        }
101
102        if (counters.containsKey(FIXITY_ERROR_COUNTER)) {
103            b.accept(create(subject(), HAS_FIXITY_ERROR_COUNT.asNode(),
104                    createTypedLiteral(counters.get(PREFIX + FIXITY_ERROR_COUNTER).getCount()).asNode()));
105        }
106
107        if (counters.containsKey(FIXITY_REPAIRED_COUNTER)) {
108            b.accept(create(subject(), HAS_FIXITY_REPAIRED_COUNT.asNode(),
109                    createTypedLiteral(counters.get(PREFIX + FIXITY_REPAIRED_COUNTER).getCount()).asNode()));
110        }
111
112        // offer all these accumulated triples
113        return b.build();
114    }
115}