|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2023 Eclipse RDF4J contributors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Distribution License v1.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: BSD-3-Clause |
| 10 | + ******************************************************************************/ |
| 11 | + |
| 12 | +package org.eclipse.rdf4j.sail.extensiblestore.impl; |
| 13 | + |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.Comparator; |
| 16 | +import java.util.EnumSet; |
| 17 | +import java.util.Set; |
| 18 | +import java.util.concurrent.ConcurrentHashMap; |
| 19 | + |
| 20 | +import org.eclipse.rdf4j.common.iteration.CloseableIteration; |
| 21 | +import org.eclipse.rdf4j.common.iteration.CloseableIteratorIteration; |
| 22 | +import org.eclipse.rdf4j.common.iteration.EmptyIteration; |
| 23 | +import org.eclipse.rdf4j.common.order.StatementOrder; |
| 24 | +import org.eclipse.rdf4j.model.IRI; |
| 25 | +import org.eclipse.rdf4j.model.Resource; |
| 26 | +import org.eclipse.rdf4j.model.Value; |
| 27 | +import org.eclipse.rdf4j.sail.extensiblestore.DataStructureInterface; |
| 28 | +import org.eclipse.rdf4j.sail.extensiblestore.FilteringIteration; |
| 29 | +import org.eclipse.rdf4j.sail.extensiblestore.SortedIteration; |
| 30 | +import org.eclipse.rdf4j.sail.extensiblestore.valuefactory.ExtensibleStatement; |
| 31 | + |
| 32 | +class OrderedDataStructure implements DataStructureInterface { |
| 33 | + |
| 34 | + private static final EmptyIteration<ExtensibleStatement> EMPTY_ITERATION = new EmptyIteration<>(); |
| 35 | + |
| 36 | + Set<ExtensibleStatement> statements = Collections.newSetFromMap(new ConcurrentHashMap<>()); |
| 37 | + |
| 38 | + @Override |
| 39 | + synchronized public void addStatement(ExtensibleStatement statement) { |
| 40 | + statements.add(statement); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + synchronized public void removeStatement(ExtensibleStatement statement) { |
| 45 | + statements.remove(statement); |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + synchronized public CloseableIteration<? extends ExtensibleStatement> getStatements(Resource subject, |
| 51 | + IRI predicate, |
| 52 | + Value object, boolean inferred, Resource... context) { |
| 53 | + return new FilteringIteration<>( |
| 54 | + new CloseableIteratorIteration<>(statements.iterator()), subject, predicate, object, inferred, context); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public CloseableIteration<? extends ExtensibleStatement> getStatements(StatementOrder statementOrder, |
| 59 | + Resource subject, IRI predicate, Value object, boolean inferred, Resource... contexts) { |
| 60 | + if (statements.isEmpty()) { |
| 61 | + return EMPTY_ITERATION; |
| 62 | + } |
| 63 | + if (inferred) { |
| 64 | + boolean containsInferred = statements.stream().anyMatch(ExtensibleStatement::isInferred); |
| 65 | + if (!containsInferred) |
| 66 | + return EMPTY_ITERATION; |
| 67 | + } |
| 68 | + return new SortedIteration<>(new FilteringIteration<>(new CloseableIteratorIteration<>(statements.iterator()), |
| 69 | + subject, predicate, object, inferred, contexts), statementOrder); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void flushForReading() { |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void init() { |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void flushForCommit() { |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public long getEstimatedSize() { |
| 89 | + return statements.size(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public Set<StatementOrder> getSupportedOrders(Resource subj, IRI pred, Value obj, boolean inferred, |
| 94 | + Resource... contexts) { |
| 95 | + return EnumSet.of(StatementOrder.S, StatementOrder.P, StatementOrder.O, StatementOrder.C); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public Comparator<Value> getComparator() { |
| 100 | + return Comparator.comparing(Value::stringValue); |
| 101 | + } |
| 102 | +} |
0 commit comments