| 1 | |
|
| 2 | |
|
| 3 | |
package org.galagosearch.core.types; |
| 4 | |
|
| 5 | |
import org.galagosearch.tupleflow.Utility; |
| 6 | |
import org.galagosearch.tupleflow.ArrayInput; |
| 7 | |
import org.galagosearch.tupleflow.ArrayOutput; |
| 8 | |
import org.galagosearch.tupleflow.Order; |
| 9 | |
import org.galagosearch.tupleflow.OrderedWriter; |
| 10 | |
import org.galagosearch.tupleflow.Type; |
| 11 | |
import org.galagosearch.tupleflow.TypeReader; |
| 12 | |
import org.galagosearch.tupleflow.Step; |
| 13 | |
import org.galagosearch.tupleflow.IncompatibleProcessorException; |
| 14 | |
import org.galagosearch.tupleflow.ReaderSource; |
| 15 | |
import java.io.IOException; |
| 16 | |
import java.io.EOFException; |
| 17 | |
import java.io.UnsupportedEncodingException; |
| 18 | |
import java.util.ArrayList; |
| 19 | |
import java.util.Arrays; |
| 20 | |
import java.util.Comparator; |
| 21 | |
import java.util.PriorityQueue; |
| 22 | |
import java.util.Collection; |
| 23 | |
|
| 24 | |
public class NumberedValuedExtent implements Type<NumberedValuedExtent> { |
| 25 | |
public byte[] extentName; |
| 26 | |
public long number; |
| 27 | |
public int begin; |
| 28 | |
public int end; |
| 29 | |
public long value; |
| 30 | |
|
| 31 | 0 | public NumberedValuedExtent() {} |
| 32 | 0 | public NumberedValuedExtent(byte[] extentName, long number, int begin, int end, long value) { |
| 33 | 0 | this.extentName = extentName; |
| 34 | 0 | this.number = number; |
| 35 | 0 | this.begin = begin; |
| 36 | 0 | this.end = end; |
| 37 | 0 | this.value = value; |
| 38 | 0 | } |
| 39 | |
|
| 40 | |
public String toString() { |
| 41 | |
try { |
| 42 | 0 | return String.format("%s,%d,%d,%d,%d", |
| 43 | |
new String(extentName, "UTF-8"), number, begin, end, value); |
| 44 | 0 | } catch(UnsupportedEncodingException e) { |
| 45 | 0 | throw new RuntimeException("Couldn't convert string to UTF-8."); |
| 46 | |
} |
| 47 | |
} |
| 48 | |
|
| 49 | |
public Order<NumberedValuedExtent> getOrder(String... spec) { |
| 50 | 0 | if (Arrays.equals(spec, new String[] { "+extentName", "+number", "+begin" })) { |
| 51 | 0 | return new ExtentNameNumberBeginOrder(); |
| 52 | |
} |
| 53 | 0 | return null; |
| 54 | |
} |
| 55 | |
|
| 56 | |
public interface Processor extends Step, org.galagosearch.tupleflow.Processor<NumberedValuedExtent> { |
| 57 | |
public void process(NumberedValuedExtent object) throws IOException; |
| 58 | |
public void close() throws IOException; |
| 59 | |
} |
| 60 | |
public interface Source extends Step { |
| 61 | |
} |
| 62 | 4 | public static class ExtentNameNumberBeginOrder implements Order<NumberedValuedExtent> { |
| 63 | |
public int hash(NumberedValuedExtent object) { |
| 64 | 0 | int h = 0; |
| 65 | 0 | h += Utility.hash(object.extentName); |
| 66 | 0 | h += Utility.hash(object.number); |
| 67 | 0 | h += Utility.hash(object.begin); |
| 68 | 0 | return h; |
| 69 | |
} |
| 70 | |
public Comparator<NumberedValuedExtent> greaterThan() { |
| 71 | 0 | return new Comparator<NumberedValuedExtent>() { |
| 72 | 0 | public int compare(NumberedValuedExtent one, NumberedValuedExtent two) { |
| 73 | 0 | int result = 0; |
| 74 | |
do { |
| 75 | 0 | result = + Utility.compare(one.extentName, two.extentName); |
| 76 | 0 | if(result != 0) break; |
| 77 | 0 | result = + Utility.compare(one.number, two.number); |
| 78 | 0 | if(result != 0) break; |
| 79 | 0 | result = + Utility.compare(one.begin, two.begin); |
| 80 | 0 | if(result != 0) break; |
| 81 | |
} while (false); |
| 82 | 0 | return -result; |
| 83 | |
} |
| 84 | |
}; |
| 85 | |
} |
| 86 | |
public Comparator<NumberedValuedExtent> lessThan() { |
| 87 | 0 | return new Comparator<NumberedValuedExtent>() { |
| 88 | 0 | public int compare(NumberedValuedExtent one, NumberedValuedExtent two) { |
| 89 | 0 | int result = 0; |
| 90 | |
do { |
| 91 | 0 | result = + Utility.compare(one.extentName, two.extentName); |
| 92 | 0 | if(result != 0) break; |
| 93 | 0 | result = + Utility.compare(one.number, two.number); |
| 94 | 0 | if(result != 0) break; |
| 95 | 0 | result = + Utility.compare(one.begin, two.begin); |
| 96 | 0 | if(result != 0) break; |
| 97 | |
} while (false); |
| 98 | 0 | return result; |
| 99 | |
} |
| 100 | |
}; |
| 101 | |
} |
| 102 | |
public TypeReader<NumberedValuedExtent> orderedReader(ArrayInput _input) { |
| 103 | 0 | return new ShreddedReader(_input); |
| 104 | |
} |
| 105 | |
|
| 106 | |
public TypeReader<NumberedValuedExtent> orderedReader(ArrayInput _input, int bufferSize) { |
| 107 | 0 | return new ShreddedReader(_input, bufferSize); |
| 108 | |
} |
| 109 | |
public OrderedWriter<NumberedValuedExtent> orderedWriter(ArrayOutput _output) { |
| 110 | 0 | ShreddedWriter w = new ShreddedWriter(_output); |
| 111 | 0 | return new OrderedWriterClass(w); |
| 112 | |
} |
| 113 | 0 | public static class OrderedWriterClass extends OrderedWriter< NumberedValuedExtent > { |
| 114 | 0 | NumberedValuedExtent last = null; |
| 115 | 0 | ShreddedWriter shreddedWriter = null; |
| 116 | |
|
| 117 | 0 | public OrderedWriterClass(ShreddedWriter s) { |
| 118 | 0 | this.shreddedWriter = s; |
| 119 | 0 | } |
| 120 | |
|
| 121 | |
public void process(NumberedValuedExtent object) throws IOException { |
| 122 | 0 | boolean processAll = false; |
| 123 | 0 | if (processAll || last == null || 0 != Utility.compare(object.extentName, last.extentName)) { processAll = true; shreddedWriter.processExtentName(object.extentName); } |
| 124 | 0 | if (processAll || last == null || 0 != Utility.compare(object.number, last.number)) { processAll = true; shreddedWriter.processNumber(object.number); } |
| 125 | 0 | if (processAll || last == null || 0 != Utility.compare(object.begin, last.begin)) { processAll = true; shreddedWriter.processBegin(object.begin); } |
| 126 | 0 | shreddedWriter.processTuple(object.end, object.value); |
| 127 | 0 | last = object; |
| 128 | 0 | } |
| 129 | |
|
| 130 | |
public void close() throws IOException { |
| 131 | 0 | shreddedWriter.close(); |
| 132 | 0 | } |
| 133 | |
|
| 134 | |
public Class<NumberedValuedExtent> getInputClass() { |
| 135 | 0 | return NumberedValuedExtent.class; |
| 136 | |
} |
| 137 | |
} |
| 138 | |
public ReaderSource<NumberedValuedExtent> orderedCombiner(Collection<TypeReader<NumberedValuedExtent>> readers, boolean closeOnExit) { |
| 139 | 0 | ArrayList<ShreddedReader> shreddedReaders = new ArrayList(); |
| 140 | |
|
| 141 | 0 | for (TypeReader<NumberedValuedExtent> reader : readers) { |
| 142 | 0 | shreddedReaders.add((ShreddedReader)reader); |
| 143 | |
} |
| 144 | |
|
| 145 | 0 | return new ShreddedCombiner(shreddedReaders, closeOnExit); |
| 146 | |
} |
| 147 | |
public NumberedValuedExtent clone(NumberedValuedExtent object) { |
| 148 | 0 | NumberedValuedExtent result = new NumberedValuedExtent(); |
| 149 | 0 | if (object == null) return result; |
| 150 | 0 | result.extentName = object.extentName; |
| 151 | 0 | result.number = object.number; |
| 152 | 0 | result.begin = object.begin; |
| 153 | 0 | result.end = object.end; |
| 154 | 0 | result.value = object.value; |
| 155 | 0 | return result; |
| 156 | |
} |
| 157 | |
public Class<NumberedValuedExtent> getOrderedClass() { |
| 158 | 4 | return NumberedValuedExtent.class; |
| 159 | |
} |
| 160 | |
public String[] getOrderSpec() { |
| 161 | 4 | return new String[] {"+extentName", "+number", "+begin"}; |
| 162 | |
} |
| 163 | |
|
| 164 | |
public static String getSpecString() { |
| 165 | 0 | return "+extentName +number +begin"; |
| 166 | |
} |
| 167 | |
|
| 168 | |
public interface ShreddedProcessor extends Step { |
| 169 | |
public void processExtentName(byte[] extentName) throws IOException; |
| 170 | |
public void processNumber(long number) throws IOException; |
| 171 | |
public void processBegin(int begin) throws IOException; |
| 172 | |
public void processTuple(int end, long value) throws IOException; |
| 173 | |
public void close() throws IOException; |
| 174 | |
} |
| 175 | |
public interface ShreddedSource extends Step { |
| 176 | |
} |
| 177 | |
|
| 178 | 0 | public static class ShreddedWriter implements ShreddedProcessor { |
| 179 | |
ArrayOutput output; |
| 180 | 0 | ShreddedBuffer buffer = new ShreddedBuffer(); |
| 181 | |
byte[] lastExtentName; |
| 182 | |
long lastNumber; |
| 183 | |
int lastBegin; |
| 184 | 0 | boolean lastFlush = false; |
| 185 | |
|
| 186 | 0 | public ShreddedWriter(ArrayOutput output) { |
| 187 | 0 | this.output = output; |
| 188 | 0 | } |
| 189 | |
|
| 190 | |
public void close() throws IOException { |
| 191 | 0 | flush(); |
| 192 | 0 | } |
| 193 | |
|
| 194 | |
public void processExtentName(byte[] extentName) { |
| 195 | 0 | lastExtentName = extentName; |
| 196 | 0 | buffer.processExtentName(extentName); |
| 197 | 0 | } |
| 198 | |
public void processNumber(long number) { |
| 199 | 0 | lastNumber = number; |
| 200 | 0 | buffer.processNumber(number); |
| 201 | 0 | } |
| 202 | |
public void processBegin(int begin) { |
| 203 | 0 | lastBegin = begin; |
| 204 | 0 | buffer.processBegin(begin); |
| 205 | 0 | } |
| 206 | |
public final void processTuple(int end, long value) throws IOException { |
| 207 | 0 | if (lastFlush) { |
| 208 | 0 | if(buffer.extentNames.size() == 0) buffer.processExtentName(lastExtentName); |
| 209 | 0 | if(buffer.numbers.size() == 0) buffer.processNumber(lastNumber); |
| 210 | 0 | if(buffer.begins.size() == 0) buffer.processBegin(lastBegin); |
| 211 | 0 | lastFlush = false; |
| 212 | |
} |
| 213 | 0 | buffer.processTuple(end, value); |
| 214 | 0 | if (buffer.isFull()) |
| 215 | 0 | flush(); |
| 216 | 0 | } |
| 217 | |
public final void flushTuples(int pauseIndex) throws IOException { |
| 218 | |
|
| 219 | 0 | while (buffer.getReadIndex() < pauseIndex) { |
| 220 | |
|
| 221 | 0 | output.writeInt(buffer.getEnd()); |
| 222 | 0 | output.writeLong(buffer.getValue()); |
| 223 | 0 | buffer.incrementTuple(); |
| 224 | |
} |
| 225 | 0 | } |
| 226 | |
public final void flushExtentName(int pauseIndex) throws IOException { |
| 227 | 0 | while (buffer.getReadIndex() < pauseIndex) { |
| 228 | 0 | int nextPause = buffer.getExtentNameEndIndex(); |
| 229 | 0 | int count = nextPause - buffer.getReadIndex(); |
| 230 | |
|
| 231 | 0 | output.writeBytes(buffer.getExtentName()); |
| 232 | 0 | output.writeInt(count); |
| 233 | 0 | buffer.incrementExtentName(); |
| 234 | |
|
| 235 | 0 | flushNumber(nextPause); |
| 236 | 0 | assert nextPause == buffer.getReadIndex(); |
| 237 | 0 | } |
| 238 | 0 | } |
| 239 | |
public final void flushNumber(int pauseIndex) throws IOException { |
| 240 | 0 | while (buffer.getReadIndex() < pauseIndex) { |
| 241 | 0 | int nextPause = buffer.getNumberEndIndex(); |
| 242 | 0 | int count = nextPause - buffer.getReadIndex(); |
| 243 | |
|
| 244 | 0 | output.writeLong(buffer.getNumber()); |
| 245 | 0 | output.writeInt(count); |
| 246 | 0 | buffer.incrementNumber(); |
| 247 | |
|
| 248 | 0 | flushBegin(nextPause); |
| 249 | 0 | assert nextPause == buffer.getReadIndex(); |
| 250 | 0 | } |
| 251 | 0 | } |
| 252 | |
public final void flushBegin(int pauseIndex) throws IOException { |
| 253 | 0 | while (buffer.getReadIndex() < pauseIndex) { |
| 254 | 0 | int nextPause = buffer.getBeginEndIndex(); |
| 255 | 0 | int count = nextPause - buffer.getReadIndex(); |
| 256 | |
|
| 257 | 0 | output.writeInt(buffer.getBegin()); |
| 258 | 0 | output.writeInt(count); |
| 259 | 0 | buffer.incrementBegin(); |
| 260 | |
|
| 261 | 0 | flushTuples(nextPause); |
| 262 | 0 | assert nextPause == buffer.getReadIndex(); |
| 263 | 0 | } |
| 264 | 0 | } |
| 265 | |
public void flush() throws IOException { |
| 266 | 0 | flushExtentName(buffer.getWriteIndex()); |
| 267 | 0 | buffer.reset(); |
| 268 | 0 | lastFlush = true; |
| 269 | 0 | } |
| 270 | |
} |
| 271 | 0 | public static class ShreddedBuffer { |
| 272 | 0 | ArrayList<byte[]> extentNames = new ArrayList(); |
| 273 | 0 | ArrayList<Long> numbers = new ArrayList(); |
| 274 | 0 | ArrayList<Integer> begins = new ArrayList(); |
| 275 | 0 | ArrayList<Integer> extentNameTupleIdx = new ArrayList(); |
| 276 | 0 | ArrayList<Integer> numberTupleIdx = new ArrayList(); |
| 277 | 0 | ArrayList<Integer> beginTupleIdx = new ArrayList(); |
| 278 | 0 | int extentNameReadIdx = 0; |
| 279 | 0 | int numberReadIdx = 0; |
| 280 | 0 | int beginReadIdx = 0; |
| 281 | |
|
| 282 | |
int[] ends; |
| 283 | |
long[] values; |
| 284 | 0 | int writeTupleIndex = 0; |
| 285 | 0 | int readTupleIndex = 0; |
| 286 | |
int batchSize; |
| 287 | |
|
| 288 | 0 | public ShreddedBuffer(int batchSize) { |
| 289 | 0 | this.batchSize = batchSize; |
| 290 | |
|
| 291 | 0 | ends = new int[batchSize]; |
| 292 | 0 | values = new long[batchSize]; |
| 293 | 0 | } |
| 294 | |
|
| 295 | |
public ShreddedBuffer() { |
| 296 | 0 | this(10000); |
| 297 | 0 | } |
| 298 | |
|
| 299 | |
public void processExtentName(byte[] extentName) { |
| 300 | 0 | extentNames.add(extentName); |
| 301 | 0 | extentNameTupleIdx.add(writeTupleIndex); |
| 302 | 0 | } |
| 303 | |
public void processNumber(long number) { |
| 304 | 0 | numbers.add(number); |
| 305 | 0 | numberTupleIdx.add(writeTupleIndex); |
| 306 | 0 | } |
| 307 | |
public void processBegin(int begin) { |
| 308 | 0 | begins.add(begin); |
| 309 | 0 | beginTupleIdx.add(writeTupleIndex); |
| 310 | 0 | } |
| 311 | |
public void processTuple(int end, long value) { |
| 312 | 0 | assert extentNames.size() > 0; |
| 313 | 0 | assert numbers.size() > 0; |
| 314 | 0 | assert begins.size() > 0; |
| 315 | 0 | ends[writeTupleIndex] = end; |
| 316 | 0 | values[writeTupleIndex] = value; |
| 317 | 0 | writeTupleIndex++; |
| 318 | 0 | } |
| 319 | |
public void resetData() { |
| 320 | 0 | extentNames.clear(); |
| 321 | 0 | numbers.clear(); |
| 322 | 0 | begins.clear(); |
| 323 | 0 | extentNameTupleIdx.clear(); |
| 324 | 0 | numberTupleIdx.clear(); |
| 325 | 0 | beginTupleIdx.clear(); |
| 326 | 0 | writeTupleIndex = 0; |
| 327 | 0 | } |
| 328 | |
|
| 329 | |
public void resetRead() { |
| 330 | 0 | readTupleIndex = 0; |
| 331 | 0 | extentNameReadIdx = 0; |
| 332 | 0 | numberReadIdx = 0; |
| 333 | 0 | beginReadIdx = 0; |
| 334 | 0 | } |
| 335 | |
|
| 336 | |
public void reset() { |
| 337 | 0 | resetData(); |
| 338 | 0 | resetRead(); |
| 339 | 0 | } |
| 340 | |
public boolean isFull() { |
| 341 | 0 | return writeTupleIndex >= batchSize; |
| 342 | |
} |
| 343 | |
|
| 344 | |
public boolean isEmpty() { |
| 345 | 0 | return writeTupleIndex == 0; |
| 346 | |
} |
| 347 | |
|
| 348 | |
public boolean isAtEnd() { |
| 349 | 0 | return readTupleIndex >= writeTupleIndex; |
| 350 | |
} |
| 351 | |
public void incrementExtentName() { |
| 352 | 0 | extentNameReadIdx++; |
| 353 | 0 | } |
| 354 | |
|
| 355 | |
public void autoIncrementExtentName() { |
| 356 | 0 | while (readTupleIndex >= getExtentNameEndIndex() && readTupleIndex < writeTupleIndex) |
| 357 | 0 | extentNameReadIdx++; |
| 358 | 0 | } |
| 359 | |
public void incrementNumber() { |
| 360 | 0 | numberReadIdx++; |
| 361 | 0 | } |
| 362 | |
|
| 363 | |
public void autoIncrementNumber() { |
| 364 | 0 | while (readTupleIndex >= getNumberEndIndex() && readTupleIndex < writeTupleIndex) |
| 365 | 0 | numberReadIdx++; |
| 366 | 0 | } |
| 367 | |
public void incrementBegin() { |
| 368 | 0 | beginReadIdx++; |
| 369 | 0 | } |
| 370 | |
|
| 371 | |
public void autoIncrementBegin() { |
| 372 | 0 | while (readTupleIndex >= getBeginEndIndex() && readTupleIndex < writeTupleIndex) |
| 373 | 0 | beginReadIdx++; |
| 374 | 0 | } |
| 375 | |
public void incrementTuple() { |
| 376 | 0 | readTupleIndex++; |
| 377 | 0 | } |
| 378 | |
public int getExtentNameEndIndex() { |
| 379 | 0 | if ((extentNameReadIdx+1) >= extentNameTupleIdx.size()) |
| 380 | 0 | return writeTupleIndex; |
| 381 | 0 | return extentNameTupleIdx.get(extentNameReadIdx+1); |
| 382 | |
} |
| 383 | |
|
| 384 | |
public int getNumberEndIndex() { |
| 385 | 0 | if ((numberReadIdx+1) >= numberTupleIdx.size()) |
| 386 | 0 | return writeTupleIndex; |
| 387 | 0 | return numberTupleIdx.get(numberReadIdx+1); |
| 388 | |
} |
| 389 | |
|
| 390 | |
public int getBeginEndIndex() { |
| 391 | 0 | if ((beginReadIdx+1) >= beginTupleIdx.size()) |
| 392 | 0 | return writeTupleIndex; |
| 393 | 0 | return beginTupleIdx.get(beginReadIdx+1); |
| 394 | |
} |
| 395 | |
public int getReadIndex() { |
| 396 | 0 | return readTupleIndex; |
| 397 | |
} |
| 398 | |
|
| 399 | |
public int getWriteIndex() { |
| 400 | 0 | return writeTupleIndex; |
| 401 | |
} |
| 402 | |
public byte[] getExtentName() { |
| 403 | 0 | assert readTupleIndex < writeTupleIndex; |
| 404 | 0 | assert extentNameReadIdx < extentNames.size(); |
| 405 | |
|
| 406 | 0 | return extentNames.get(extentNameReadIdx); |
| 407 | |
} |
| 408 | |
public long getNumber() { |
| 409 | 0 | assert readTupleIndex < writeTupleIndex; |
| 410 | 0 | assert numberReadIdx < numbers.size(); |
| 411 | |
|
| 412 | 0 | return numbers.get(numberReadIdx); |
| 413 | |
} |
| 414 | |
public int getBegin() { |
| 415 | 0 | assert readTupleIndex < writeTupleIndex; |
| 416 | 0 | assert beginReadIdx < begins.size(); |
| 417 | |
|
| 418 | 0 | return begins.get(beginReadIdx); |
| 419 | |
} |
| 420 | |
public int getEnd() { |
| 421 | 0 | assert readTupleIndex < writeTupleIndex; |
| 422 | 0 | return ends[readTupleIndex]; |
| 423 | |
} |
| 424 | |
public long getValue() { |
| 425 | 0 | assert readTupleIndex < writeTupleIndex; |
| 426 | 0 | return values[readTupleIndex]; |
| 427 | |
} |
| 428 | |
public void copyTuples(int endIndex, ShreddedProcessor output) throws IOException { |
| 429 | 0 | while (getReadIndex() < endIndex) { |
| 430 | 0 | output.processTuple(getEnd(), getValue()); |
| 431 | 0 | incrementTuple(); |
| 432 | |
} |
| 433 | 0 | } |
| 434 | |
public void copyUntilIndexExtentName(int endIndex, ShreddedProcessor output) throws IOException { |
| 435 | 0 | while (getReadIndex() < endIndex) { |
| 436 | 0 | output.processExtentName(getExtentName()); |
| 437 | 0 | assert getExtentNameEndIndex() <= endIndex; |
| 438 | 0 | copyUntilIndexNumber(getExtentNameEndIndex(), output); |
| 439 | 0 | incrementExtentName(); |
| 440 | |
} |
| 441 | 0 | } |
| 442 | |
public void copyUntilIndexNumber(int endIndex, ShreddedProcessor output) throws IOException { |
| 443 | 0 | while (getReadIndex() < endIndex) { |
| 444 | 0 | output.processNumber(getNumber()); |
| 445 | 0 | assert getNumberEndIndex() <= endIndex; |
| 446 | 0 | copyUntilIndexBegin(getNumberEndIndex(), output); |
| 447 | 0 | incrementNumber(); |
| 448 | |
} |
| 449 | 0 | } |
| 450 | |
public void copyUntilIndexBegin(int endIndex, ShreddedProcessor output) throws IOException { |
| 451 | 0 | while (getReadIndex() < endIndex) { |
| 452 | 0 | output.processBegin(getBegin()); |
| 453 | 0 | assert getBeginEndIndex() <= endIndex; |
| 454 | 0 | copyTuples(getBeginEndIndex(), output); |
| 455 | 0 | incrementBegin(); |
| 456 | |
} |
| 457 | 0 | } |
| 458 | |
public void copyUntilExtentName(ShreddedBuffer other, ShreddedProcessor output) throws IOException { |
| 459 | 0 | while (!isAtEnd()) { |
| 460 | 0 | if (other != null) { |
| 461 | 0 | assert !other.isAtEnd(); |
| 462 | 0 | int c = + Utility.compare(getExtentName(), other.getExtentName()); |
| 463 | |
|
| 464 | 0 | if (c > 0) { |
| 465 | 0 | break; |
| 466 | |
} |
| 467 | |
|
| 468 | 0 | output.processExtentName(getExtentName()); |
| 469 | |
|
| 470 | 0 | if (c < 0) { |
| 471 | 0 | copyUntilIndexNumber(getExtentNameEndIndex(), output); |
| 472 | 0 | } else if (c == 0) { |
| 473 | 0 | copyUntilNumber(other, output); |
| 474 | 0 | autoIncrementExtentName(); |
| 475 | 0 | break; |
| 476 | |
} |
| 477 | 0 | } else { |
| 478 | 0 | output.processExtentName(getExtentName()); |
| 479 | 0 | copyUntilIndexNumber(getExtentNameEndIndex(), output); |
| 480 | |
} |
| 481 | 0 | incrementExtentName(); |
| 482 | |
|
| 483 | |
|
| 484 | |
} |
| 485 | 0 | } |
| 486 | |
public void copyUntilNumber(ShreddedBuffer other, ShreddedProcessor output) throws IOException { |
| 487 | 0 | while (!isAtEnd()) { |
| 488 | 0 | if (other != null) { |
| 489 | 0 | assert !other.isAtEnd(); |
| 490 | 0 | int c = + Utility.compare(getNumber(), other.getNumber()); |
| 491 | |
|
| 492 | 0 | if (c > 0) { |
| 493 | 0 | break; |
| 494 | |
} |
| 495 | |
|
| 496 | 0 | output.processNumber(getNumber()); |
| 497 | |
|
| 498 | 0 | if (c < 0) { |
| 499 | 0 | copyUntilIndexBegin(getNumberEndIndex(), output); |
| 500 | 0 | } else if (c == 0) { |
| 501 | 0 | copyUntilBegin(other, output); |
| 502 | 0 | autoIncrementNumber(); |
| 503 | 0 | break; |
| 504 | |
} |
| 505 | 0 | } else { |
| 506 | 0 | output.processNumber(getNumber()); |
| 507 | 0 | copyUntilIndexBegin(getNumberEndIndex(), output); |
| 508 | |
} |
| 509 | 0 | incrementNumber(); |
| 510 | |
|
| 511 | 0 | if (getExtentNameEndIndex() <= readTupleIndex) |
| 512 | 0 | break; |
| 513 | |
} |
| 514 | 0 | } |
| 515 | |
public void copyUntilBegin(ShreddedBuffer other, ShreddedProcessor output) throws IOException { |
| 516 | 0 | while (!isAtEnd()) { |
| 517 | 0 | if (other != null) { |
| 518 | 0 | assert !other.isAtEnd(); |
| 519 | 0 | int c = + Utility.compare(getBegin(), other.getBegin()); |
| 520 | |
|
| 521 | 0 | if (c > 0) { |
| 522 | 0 | break; |
| 523 | |
} |
| 524 | |
|
| 525 | 0 | output.processBegin(getBegin()); |
| 526 | |
|
| 527 | 0 | copyTuples(getBeginEndIndex(), output); |
| 528 | 0 | } else { |
| 529 | 0 | output.processBegin(getBegin()); |
| 530 | 0 | copyTuples(getBeginEndIndex(), output); |
| 531 | |
} |
| 532 | 0 | incrementBegin(); |
| 533 | |
|
| 534 | 0 | if (getNumberEndIndex() <= readTupleIndex) |
| 535 | 0 | break; |
| 536 | |
} |
| 537 | 0 | } |
| 538 | |
public void copyUntil(ShreddedBuffer other, ShreddedProcessor output) throws IOException { |
| 539 | 0 | copyUntilExtentName(other, output); |
| 540 | 0 | } |
| 541 | |
|
| 542 | |
} |
| 543 | 0 | public static class ShreddedCombiner implements ReaderSource<NumberedValuedExtent>, ShreddedSource { |
| 544 | |
public ShreddedProcessor processor; |
| 545 | |
Collection<ShreddedReader> readers; |
| 546 | 0 | boolean closeOnExit = false; |
| 547 | 0 | boolean uninitialized = true; |
| 548 | 0 | PriorityQueue<ShreddedReader> queue = new PriorityQueue<ShreddedReader>(); |
| 549 | |
|
| 550 | 0 | public ShreddedCombiner(Collection<ShreddedReader> readers, boolean closeOnExit) { |
| 551 | 0 | this.readers = readers; |
| 552 | 0 | this.closeOnExit = closeOnExit; |
| 553 | 0 | } |
| 554 | |
|
| 555 | |
public void setProcessor(Step processor) throws IncompatibleProcessorException { |
| 556 | 0 | if (processor instanceof ShreddedProcessor) { |
| 557 | 0 | this.processor = new DuplicateEliminator((ShreddedProcessor) processor); |
| 558 | 0 | } else if (processor instanceof NumberedValuedExtent.Processor) { |
| 559 | 0 | this.processor = new DuplicateEliminator(new TupleUnshredder((NumberedValuedExtent.Processor) processor)); |
| 560 | 0 | } else if (processor instanceof org.galagosearch.tupleflow.Processor) { |
| 561 | 0 | this.processor = new DuplicateEliminator(new TupleUnshredder((org.galagosearch.tupleflow.Processor<NumberedValuedExtent>) processor)); |
| 562 | |
} else { |
| 563 | 0 | throw new IncompatibleProcessorException(processor.getClass().getName() + " is not supported by " + this.getClass().getName()); |
| 564 | |
} |
| 565 | 0 | } |
| 566 | |
|
| 567 | |
public Class<NumberedValuedExtent> getOutputClass() { |
| 568 | 0 | return NumberedValuedExtent.class; |
| 569 | |
} |
| 570 | |
|
| 571 | |
public void initialize() throws IOException { |
| 572 | 0 | for (ShreddedReader reader : readers) { |
| 573 | 0 | reader.fill(); |
| 574 | |
|
| 575 | 0 | if (!reader.getBuffer().isAtEnd()) |
| 576 | 0 | queue.add(reader); |
| 577 | |
} |
| 578 | |
|
| 579 | 0 | uninitialized = false; |
| 580 | 0 | } |
| 581 | |
|
| 582 | |
public void run() throws IOException { |
| 583 | 0 | initialize(); |
| 584 | |
|
| 585 | 0 | while (queue.size() > 0) { |
| 586 | 0 | ShreddedReader top = queue.poll(); |
| 587 | 0 | ShreddedReader next = null; |
| 588 | 0 | ShreddedBuffer nextBuffer = null; |
| 589 | |
|
| 590 | 0 | assert !top.getBuffer().isAtEnd(); |
| 591 | |
|
| 592 | 0 | if (queue.size() > 0) { |
| 593 | 0 | next = queue.peek(); |
| 594 | 0 | nextBuffer = next.getBuffer(); |
| 595 | 0 | assert !nextBuffer.isAtEnd(); |
| 596 | |
} |
| 597 | |
|
| 598 | 0 | top.getBuffer().copyUntil(nextBuffer, processor); |
| 599 | 0 | if (top.getBuffer().isAtEnd()) |
| 600 | 0 | top.fill(); |
| 601 | |
|
| 602 | 0 | if (!top.getBuffer().isAtEnd()) |
| 603 | 0 | queue.add(top); |
| 604 | 0 | } |
| 605 | |
|
| 606 | 0 | if (closeOnExit) |
| 607 | 0 | processor.close(); |
| 608 | 0 | } |
| 609 | |
|
| 610 | |
public NumberedValuedExtent read() throws IOException { |
| 611 | 0 | if (uninitialized) |
| 612 | 0 | initialize(); |
| 613 | |
|
| 614 | 0 | NumberedValuedExtent result = null; |
| 615 | |
|
| 616 | 0 | while (queue.size() > 0) { |
| 617 | 0 | ShreddedReader top = queue.poll(); |
| 618 | 0 | result = top.read(); |
| 619 | |
|
| 620 | 0 | if (result != null) { |
| 621 | 0 | if (top.getBuffer().isAtEnd()) |
| 622 | 0 | top.fill(); |
| 623 | |
|
| 624 | 0 | queue.offer(top); |
| 625 | 0 | break; |
| 626 | |
} |
| 627 | 0 | } |
| 628 | |
|
| 629 | 0 | return result; |
| 630 | |
} |
| 631 | |
} |
| 632 | 0 | public static class ShreddedReader implements Step, Comparable<ShreddedReader>, TypeReader<NumberedValuedExtent>, ShreddedSource { |
| 633 | |
public ShreddedProcessor processor; |
| 634 | |
ShreddedBuffer buffer; |
| 635 | 0 | NumberedValuedExtent last = new NumberedValuedExtent(); |
| 636 | 0 | long updateExtentNameCount = -1; |
| 637 | 0 | long updateNumberCount = -1; |
| 638 | 0 | long updateBeginCount = -1; |
| 639 | 0 | long tupleCount = 0; |
| 640 | 0 | long bufferStartCount = 0; |
| 641 | |
ArrayInput input; |
| 642 | |
|
| 643 | 0 | public ShreddedReader(ArrayInput input) { |
| 644 | 0 | this.input = input; |
| 645 | 0 | this.buffer = new ShreddedBuffer(); |
| 646 | 0 | } |
| 647 | |
|
| 648 | 0 | public ShreddedReader(ArrayInput input, int bufferSize) { |
| 649 | 0 | this.input = input; |
| 650 | 0 | this.buffer = new ShreddedBuffer(bufferSize); |
| 651 | 0 | } |
| 652 | |
|
| 653 | |
public final int compareTo(ShreddedReader other) { |
| 654 | 0 | ShreddedBuffer otherBuffer = other.getBuffer(); |
| 655 | |
|
| 656 | 0 | if (buffer.isAtEnd() && otherBuffer.isAtEnd()) { |
| 657 | 0 | return 0; |
| 658 | 0 | } else if (buffer.isAtEnd()) { |
| 659 | 0 | return -1; |
| 660 | 0 | } else if (otherBuffer.isAtEnd()) { |
| 661 | 0 | return 1; |
| 662 | |
} |
| 663 | |
|
| 664 | 0 | int result = 0; |
| 665 | |
do { |
| 666 | 0 | result = + Utility.compare(buffer.getExtentName(), otherBuffer.getExtentName()); |
| 667 | 0 | if(result != 0) break; |
| 668 | 0 | result = + Utility.compare(buffer.getNumber(), otherBuffer.getNumber()); |
| 669 | 0 | if(result != 0) break; |
| 670 | 0 | result = + Utility.compare(buffer.getBegin(), otherBuffer.getBegin()); |
| 671 | 0 | if(result != 0) break; |
| 672 | |
} while (false); |
| 673 | |
|
| 674 | 0 | return result; |
| 675 | |
} |
| 676 | |
|
| 677 | |
public final ShreddedBuffer getBuffer() { |
| 678 | 0 | return buffer; |
| 679 | |
} |
| 680 | |
|
| 681 | |
public final NumberedValuedExtent read() throws IOException { |
| 682 | 0 | if (buffer.isAtEnd()) { |
| 683 | 0 | fill(); |
| 684 | |
|
| 685 | 0 | if (buffer.isAtEnd()) { |
| 686 | 0 | return null; |
| 687 | |
} |
| 688 | |
} |
| 689 | |
|
| 690 | 0 | assert !buffer.isAtEnd(); |
| 691 | 0 | NumberedValuedExtent result = new NumberedValuedExtent(); |
| 692 | |
|
| 693 | 0 | result.extentName = buffer.getExtentName(); |
| 694 | 0 | result.number = buffer.getNumber(); |
| 695 | 0 | result.begin = buffer.getBegin(); |
| 696 | 0 | result.end = buffer.getEnd(); |
| 697 | 0 | result.value = buffer.getValue(); |
| 698 | |
|
| 699 | 0 | buffer.incrementTuple(); |
| 700 | 0 | buffer.autoIncrementExtentName(); |
| 701 | 0 | buffer.autoIncrementNumber(); |
| 702 | 0 | buffer.autoIncrementBegin(); |
| 703 | |
|
| 704 | 0 | return result; |
| 705 | |
} |
| 706 | |
|
| 707 | |
public final void fill() throws IOException { |
| 708 | |
try { |
| 709 | 0 | buffer.reset(); |
| 710 | |
|
| 711 | 0 | if (tupleCount != 0) { |
| 712 | |
|
| 713 | 0 | if(updateExtentNameCount - tupleCount > 0) { |
| 714 | 0 | buffer.extentNames.add(last.extentName); |
| 715 | 0 | buffer.extentNameTupleIdx.add((int) (updateExtentNameCount - tupleCount)); |
| 716 | |
} |
| 717 | 0 | if(updateNumberCount - tupleCount > 0) { |
| 718 | 0 | buffer.numbers.add(last.number); |
| 719 | 0 | buffer.numberTupleIdx.add((int) (updateNumberCount - tupleCount)); |
| 720 | |
} |
| 721 | 0 | if(updateBeginCount - tupleCount > 0) { |
| 722 | 0 | buffer.begins.add(last.begin); |
| 723 | 0 | buffer.beginTupleIdx.add((int) (updateBeginCount - tupleCount)); |
| 724 | |
} |
| 725 | 0 | bufferStartCount = tupleCount; |
| 726 | |
} |
| 727 | |
|
| 728 | 0 | while (!buffer.isFull()) { |
| 729 | 0 | updateBegin(); |
| 730 | 0 | buffer.processTuple(input.readInt(), input.readLong()); |
| 731 | 0 | tupleCount++; |
| 732 | |
} |
| 733 | 0 | } catch(EOFException e) {} |
| 734 | 0 | } |
| 735 | |
|
| 736 | |
public final void updateExtentName() throws IOException { |
| 737 | 0 | if (updateExtentNameCount > tupleCount) |
| 738 | 0 | return; |
| 739 | |
|
| 740 | 0 | last.extentName = input.readBytes(); |
| 741 | 0 | updateExtentNameCount = tupleCount + input.readInt(); |
| 742 | |
|
| 743 | 0 | buffer.processExtentName(last.extentName); |
| 744 | 0 | } |
| 745 | |
public final void updateNumber() throws IOException { |
| 746 | 0 | if (updateNumberCount > tupleCount) |
| 747 | 0 | return; |
| 748 | |
|
| 749 | 0 | updateExtentName(); |
| 750 | 0 | last.number = input.readLong(); |
| 751 | 0 | updateNumberCount = tupleCount + input.readInt(); |
| 752 | |
|
| 753 | 0 | buffer.processNumber(last.number); |
| 754 | 0 | } |
| 755 | |
public final void updateBegin() throws IOException { |
| 756 | 0 | if (updateBeginCount > tupleCount) |
| 757 | 0 | return; |
| 758 | |
|
| 759 | 0 | updateNumber(); |
| 760 | 0 | last.begin = input.readInt(); |
| 761 | 0 | updateBeginCount = tupleCount + input.readInt(); |
| 762 | |
|
| 763 | 0 | buffer.processBegin(last.begin); |
| 764 | 0 | } |
| 765 | |
|
| 766 | |
public void run() throws IOException { |
| 767 | |
while (true) { |
| 768 | 0 | fill(); |
| 769 | |
|
| 770 | 0 | if (buffer.isAtEnd()) |
| 771 | 0 | break; |
| 772 | |
|
| 773 | 0 | buffer.copyUntil(null, processor); |
| 774 | |
} |
| 775 | 0 | processor.close(); |
| 776 | 0 | } |
| 777 | |
|
| 778 | |
public void setProcessor(Step processor) throws IncompatibleProcessorException { |
| 779 | 0 | if (processor instanceof ShreddedProcessor) { |
| 780 | 0 | this.processor = new DuplicateEliminator((ShreddedProcessor) processor); |
| 781 | 0 | } else if (processor instanceof NumberedValuedExtent.Processor) { |
| 782 | 0 | this.processor = new DuplicateEliminator(new TupleUnshredder((NumberedValuedExtent.Processor) processor)); |
| 783 | 0 | } else if (processor instanceof org.galagosearch.tupleflow.Processor) { |
| 784 | 0 | this.processor = new DuplicateEliminator(new TupleUnshredder((org.galagosearch.tupleflow.Processor<NumberedValuedExtent>) processor)); |
| 785 | |
} else { |
| 786 | 0 | throw new IncompatibleProcessorException(processor.getClass().getName() + " is not supported by " + this.getClass().getName()); |
| 787 | |
} |
| 788 | 0 | } |
| 789 | |
|
| 790 | |
public Class<NumberedValuedExtent> getOutputClass() { |
| 791 | 0 | return NumberedValuedExtent.class; |
| 792 | |
} |
| 793 | |
} |
| 794 | |
|
| 795 | |
public static class DuplicateEliminator implements ShreddedProcessor { |
| 796 | |
public ShreddedProcessor processor; |
| 797 | 0 | NumberedValuedExtent last = new NumberedValuedExtent(); |
| 798 | 0 | boolean extentNameProcess = true; |
| 799 | 0 | boolean numberProcess = true; |
| 800 | 0 | boolean beginProcess = true; |
| 801 | |
|
| 802 | 0 | public DuplicateEliminator() {} |
| 803 | 0 | public DuplicateEliminator(ShreddedProcessor processor) { |
| 804 | 0 | this.processor = processor; |
| 805 | 0 | } |
| 806 | |
|
| 807 | |
public void setShreddedProcessor(ShreddedProcessor processor) { |
| 808 | 0 | this.processor = processor; |
| 809 | 0 | } |
| 810 | |
|
| 811 | |
public void processExtentName(byte[] extentName) throws IOException { |
| 812 | 0 | if (extentNameProcess || Utility.compare(extentName, last.extentName) != 0) { |
| 813 | 0 | last.extentName = extentName; |
| 814 | 0 | processor.processExtentName(extentName); |
| 815 | 0 | resetNumber(); |
| 816 | 0 | extentNameProcess = false; |
| 817 | |
} |
| 818 | 0 | } |
| 819 | |
public void processNumber(long number) throws IOException { |
| 820 | 0 | if (numberProcess || Utility.compare(number, last.number) != 0) { |
| 821 | 0 | last.number = number; |
| 822 | 0 | processor.processNumber(number); |
| 823 | 0 | resetBegin(); |
| 824 | 0 | numberProcess = false; |
| 825 | |
} |
| 826 | 0 | } |
| 827 | |
public void processBegin(int begin) throws IOException { |
| 828 | 0 | if (beginProcess || Utility.compare(begin, last.begin) != 0) { |
| 829 | 0 | last.begin = begin; |
| 830 | 0 | processor.processBegin(begin); |
| 831 | 0 | beginProcess = false; |
| 832 | |
} |
| 833 | 0 | } |
| 834 | |
|
| 835 | |
public void resetExtentName() { |
| 836 | 0 | extentNameProcess = true; |
| 837 | 0 | resetNumber(); |
| 838 | 0 | } |
| 839 | |
public void resetNumber() { |
| 840 | 0 | numberProcess = true; |
| 841 | 0 | resetBegin(); |
| 842 | 0 | } |
| 843 | |
public void resetBegin() { |
| 844 | 0 | beginProcess = true; |
| 845 | 0 | } |
| 846 | |
|
| 847 | |
public void processTuple(int end, long value) throws IOException { |
| 848 | 0 | processor.processTuple(end, value); |
| 849 | 0 | } |
| 850 | |
|
| 851 | |
public void close() throws IOException { |
| 852 | 0 | processor.close(); |
| 853 | 0 | } |
| 854 | |
} |
| 855 | |
public static class TupleUnshredder implements ShreddedProcessor { |
| 856 | 0 | NumberedValuedExtent last = new NumberedValuedExtent(); |
| 857 | |
public org.galagosearch.tupleflow.Processor<NumberedValuedExtent> processor; |
| 858 | |
|
| 859 | 0 | public TupleUnshredder(NumberedValuedExtent.Processor processor) { |
| 860 | 0 | this.processor = processor; |
| 861 | 0 | } |
| 862 | |
|
| 863 | 0 | public TupleUnshredder(org.galagosearch.tupleflow.Processor<NumberedValuedExtent> processor) { |
| 864 | 0 | this.processor = processor; |
| 865 | 0 | } |
| 866 | |
|
| 867 | |
public NumberedValuedExtent clone(NumberedValuedExtent object) { |
| 868 | 0 | NumberedValuedExtent result = new NumberedValuedExtent(); |
| 869 | 0 | if (object == null) return result; |
| 870 | 0 | result.extentName = object.extentName; |
| 871 | 0 | result.number = object.number; |
| 872 | 0 | result.begin = object.begin; |
| 873 | 0 | result.end = object.end; |
| 874 | 0 | result.value = object.value; |
| 875 | 0 | return result; |
| 876 | |
} |
| 877 | |
|
| 878 | |
public void processExtentName(byte[] extentName) throws IOException { |
| 879 | 0 | last.extentName = extentName; |
| 880 | 0 | } |
| 881 | |
|
| 882 | |
public void processNumber(long number) throws IOException { |
| 883 | 0 | last.number = number; |
| 884 | 0 | } |
| 885 | |
|
| 886 | |
public void processBegin(int begin) throws IOException { |
| 887 | 0 | last.begin = begin; |
| 888 | 0 | } |
| 889 | |
|
| 890 | |
|
| 891 | |
public void processTuple(int end, long value) throws IOException { |
| 892 | 0 | last.end = end; |
| 893 | 0 | last.value = value; |
| 894 | 0 | processor.process(clone(last)); |
| 895 | 0 | } |
| 896 | |
|
| 897 | |
public void close() throws IOException { |
| 898 | 0 | processor.close(); |
| 899 | 0 | } |
| 900 | |
} |
| 901 | 4 | public static class TupleShredder implements Processor { |
| 902 | 0 | NumberedValuedExtent last = new NumberedValuedExtent(); |
| 903 | |
public ShreddedProcessor processor; |
| 904 | |
|
| 905 | 0 | public TupleShredder(ShreddedProcessor processor) { |
| 906 | 0 | this.processor = processor; |
| 907 | 0 | } |
| 908 | |
|
| 909 | |
public NumberedValuedExtent clone(NumberedValuedExtent object) { |
| 910 | 0 | NumberedValuedExtent result = new NumberedValuedExtent(); |
| 911 | 0 | if (object == null) return result; |
| 912 | 0 | result.extentName = object.extentName; |
| 913 | 0 | result.number = object.number; |
| 914 | 0 | result.begin = object.begin; |
| 915 | 0 | result.end = object.end; |
| 916 | 0 | result.value = object.value; |
| 917 | 0 | return result; |
| 918 | |
} |
| 919 | |
|
| 920 | |
public void process(NumberedValuedExtent object) throws IOException { |
| 921 | 0 | boolean processAll = false; |
| 922 | 0 | if(last == null || Utility.compare(last.extentName, object.extentName) != 0 || processAll) { processor.processExtentName(object.extentName); processAll = true; } |
| 923 | 0 | if(last == null || Utility.compare(last.number, object.number) != 0 || processAll) { processor.processNumber(object.number); processAll = true; } |
| 924 | 0 | if(last == null || Utility.compare(last.begin, object.begin) != 0 || processAll) { processor.processBegin(object.begin); processAll = true; } |
| 925 | 0 | processor.processTuple(object.end, object.value); |
| 926 | 0 | } |
| 927 | |
|
| 928 | |
public Class<NumberedValuedExtent> getInputClass() { |
| 929 | 0 | return NumberedValuedExtent.class; |
| 930 | |
} |
| 931 | |
|
| 932 | |
public void close() throws IOException { |
| 933 | 0 | processor.close(); |
| 934 | 0 | } |
| 935 | |
} |
| 936 | |
} |
| 937 | |
} |