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