Java tutorial
/******************************************************************************* * Copyright 2012 Apigee Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package org.usergrid.persistence.query.ir.result; import java.util.LinkedHashSet; import java.util.Set; import java.util.UUID; import org.usergrid.persistence.cassandra.CursorCache; import com.google.common.collect.Sets; /** * An iterator that unions 1 or more subsets. It makes the assuming that sub * iterators iterate from min(uuid) to max(uuid) * * @author tnine * */ public class IntersectionIterator extends MultiIterator { /** * */ public IntersectionIterator(int pageSize) { super(pageSize); } /* * (non-Javadoc) * * @see org.usergrid.persistence.query.ir.result.ResultIterator#reset() */ @Override public void doReset() { for (ResultIterator itr : iterators) { itr.reset(); } } /* * (non-Javadoc) * * @see org.usergrid.persistence.query.ir.result.MergeIterator#advance() */ @Override protected Set<UUID> advance() { /** * Advance our sub iterators until the UUID's all line up */ int size = iterators.size(); if (size == 0) { return null; } // edge case with only 1 iterator if (size == 1) { ResultIterator itr = iterators.get(0); if (!itr.hasNext()) { return null; } return itr.next(); } // begin our tree merge of the iterators return merge(); } private Set<UUID> merge() { Set<UUID> results = new LinkedHashSet<UUID>(); ResultIterator rootIterator = iterators.get(0); //we've matched to the end if (!rootIterator.hasNext()) { return null; } //purposely check size first, that way we avoid another round trip if we can while (results.size() < pageSize && rootIterator.hasNext()) { Set<UUID> intersection = rootIterator.next(); for (int i = 1; i < iterators.size(); i++) { ResultIterator joinIterator = iterators.get(i); intersection = merge(intersection, joinIterator); //nothing left short circuit, there is no point in advancing to further join iterators if (intersection.size() == 0) { break; } } //now add the intermediate results and continue results.addAll(intersection); } return results; } private Set<UUID> merge(Set<UUID> current, ResultIterator child) { Set<UUID> results = new LinkedHashSet<UUID>(pageSize); while (results.size() < pageSize) { if (!child.hasNext()) { // we've iterated to the end, reset for next pass child.reset(); return results; } results.addAll(Sets.intersection(current, child.next())); } return results; } /* * (non-Javadoc) * * @see * org.usergrid.persistence.query.ir.result.ResultIterator#finalizeCursor( * org.usergrid.persistence.cassandra.CursorCache) */ @Override public void finalizeCursor(CursorCache cache, UUID lastLoaded) { ResultIterator itr = iterators.get(0); //We can only create a cursor on our root level value in the intersection iterator. if (itr != null) { itr.finalizeCursor(cache, lastLoaded); } } }