/* * (c) Copyright 2010 Talis Systems Ltd. * All rights reserved. * [See end of file] */ package algebra2; import java.util.Iterator ; import java.util.List ; import org.openjena.atlas.lib.Tuple ; import com.hp.hpl.jena.sparql.core.Var ; import com.hp.hpl.jena.tdb.store.NodeId ; public class JoinEngine { static int compare(Tuple t1, Tuple t2, List vars, List indexes) { return 0 ; } static void mergeJoin(Iterator> iter1, Iterator> iter2, List indexes) { /* static void SortMergeJoin(List> people, List> purchases) { // first we sort both lists people.Sort(Compare); purchases.Sort(Compare); // then we do an interleaved linear scan to merge the sorted lists int i = 0, j = 0; while(i purchases[j].Key) j++; else // must be a match { Console.WriteLine("Name index {0}, {1}, bought some {2} ", people[i].Key, people[i].Value, purchases[j].Value); j++; } } } static int Compare(KeyValuePair a, KeyValuePair b) { if (a.Key > b.Key) return 1; if (a.Key == b.Key) return 0; return -1; } -- public class MergeJoin { // Assume that left and right are already sorted public static Relation Sort(Relation left, Relation right) { Relation output = new Relation(); while (!left.IsPastEnd() && !right.IsPastEnd()) { if (left.Key == right.Key) { output.Add(left.Key); left.Advance(); right.Advance(); } else if (left.Key < right.Key) left.Advance(); else //(left.Key > right.Key) right.Advance(); } return output; } } */ // Sort must be total, not just on common vars if ( ! iter1.hasNext() ) return ; if ( ! iter2.hasNext() ) return ; Tuple slot1 = iter1.next() ; Tuple slot2 = iter1.next() ; while(true) { int x = compare(slot1, slot2, null, indexes) ; if ( x > 0 ) { // LHS > RHS : advance LHS if ( ! iter1.hasNext() ) return ; slot1 = iter1.next() ; continue ; } if ( x < 0 ) { // LHS < RHS : advance RHS if ( ! iter2.hasNext() ) return ; slot2 = iter2.next() ; continue ; } // Compares. // Advance each to count the number completely the same. int N1 = 0 ; int N2 = 0 ; Tuple x1 = slot1 ; Tuple x2 = slot2 ; yield(slot1, slot2, N1*N2) ; // Now what? } } private static void yield(Tuple t1, Tuple t2, int number) {} } /* * (c) Copyright 2010 Talis Systems Ltd. * 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. */