/* * (c) Copyright 2008 Hewlett-Packard Development Company, LP * All rights reserved. * [See end of file] */ package dev.g2; import java.util.Iterator ; import org.openjena.atlas.iterator.Iter ; import org.openjena.atlas.iterator.Transform ; import org.openjena.atlas.lib.ColumnMap ; import org.openjena.atlas.lib.Tuple ; import com.hp.hpl.jena.graph.Node ; import com.hp.hpl.jena.graph.Triple ; import com.hp.hpl.jena.graph.TripleMatch ; import com.hp.hpl.jena.graph.query.QueryHandler ; import com.hp.hpl.jena.shared.PrefixMapping ; import com.hp.hpl.jena.shared.impl.PrefixMappingImpl ; import com.hp.hpl.jena.sparql.graph.GraphBase2 ; import com.hp.hpl.jena.util.iterator.ExtendedIterator ; import com.hp.hpl.jena.util.iterator.NiceIterator ; public class Graph2 extends GraphBase2 { //TupleDex TupleDex indexes[] = { new TupleDex(new ColumnMap("SPO", "SPO")) , new TupleDex(new ColumnMap("SPO", "POS")) , new TupleDex(new ColumnMap("SPO", "OSP")) } ; @Override protected PrefixMapping createPrefixMapping() { return new PrefixMappingImpl() ; } @Override protected ExtendedIterator graphBaseFind(TripleMatch m) { Node s = m.getMatchSubject() ; Node p = m.getMatchPredicate() ; Node o = m.getMatchObject() ; // A bit mixed between tuples and S,P,O - solve with Tuple>Triple Tuple tuple = Tuple.create(s,p,o) ; int indexNumSlots = -1 ; int index = -1 ; TupleDex tupleIndex = null ; for ( int i = 0 ; i < indexes.length ; i++ ) { TupleDex idx = indexes[i] ; if ( idx != null ) { int w = idx.weight(tuple) ; if ( w > indexNumSlots ) { indexNumSlots = w ; tupleIndex = idx ; } } } if ( tupleIndex == null ) // Scan tupleIndex = indexes[0] ; Iterator iter = Iter.map(tupleIndex.find(tuple), transform) ; return new MapperIteratorTriples(iter) ; } // Convert from Iterator to ExtendedIterator static class MapperIteratorTriples extends NiceIterator { private final Iterator iter ; MapperIteratorTriples(Iterator iter) { this.iter = iter ; } @Override public boolean hasNext() { return iter.hasNext() ; } @Override public Triple next() { return iter.next(); } @Override public void remove() { iter.remove(); } } @Override public QueryHandler queryHandler() { return null ; } @Override public void performAdd( Triple t ) { Tuple tuple = Tuple.create(t.getSubject(), t.getPredicate(), t.getObject()) ; for ( TupleDex index : indexes ) index.add(tuple) ; } @Override public void performDelete( Triple t ) { Tuple tuple = Tuple.create(t.getSubject(), t.getPredicate(), t.getObject()) ; for ( TupleDex index : indexes ) index.remove(tuple) ; } static Transform, Triple> transform = new Transform, Triple>() { @Override public Triple convert(Tuple tuple) { return new Triple(tuple.get(0), tuple.get(1), tuple.get(2)) ; }} ; } /* * (c) Copyright 2008 Hewlett-Packard Development Company, LP * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */