Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2012 Jan Kotek
*
* 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.mapdb20;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Data Pump moves data from one source to other.
* It can be used to import data from text file, or copy store from memory to disk.
*/
public final class Pump {
private static final Logger LOG = Logger.getLogger(Pump.class.getName());
/**
* Sorts large data set by given {@code Comparator}. Data are sorted with in-memory cache and temporary files.
*
* @param source iterator over unsorted data
* @param mergeDuplicates should be duplicate keys merged into single one?
* @param batchSize how much items can fit into heap memory
* @param comparator used to sort data
* @param serializer used to store data in temporary files
* @return iterator over sorted data set
*/
public static Iterator sort(Iterator source, boolean mergeDuplicates, final int batchSize,
Comparator comparator, final Serializer serializer, Executor executor){
if(batchSize<=0) throw new IllegalArgumentException();
if(comparator==null)
comparator=Fun.comparator();
if(source==null)
source = Fun.emptyIterator();
int counter = 0;
final Object[] presort = new Object[batchSize];
final List presortFiles = new ArrayList();
final List presortCount2 = new ArrayList();
try{
while(source.hasNext()){
presort[counter]=source.next();
counter++;
if(counter>=batchSize){
//sort all items
arraySort(presort, presort.length, comparator ,executor);
//flush presort into temporary file
File f = File.createTempFile("mapdb","sort");
f.deleteOnExit();
presortFiles.add(f);
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
for(Object e:presort){
serializer.serialize(out,(E)e);
}
out.close();
presortCount2.add(counter);
Arrays.fill(presort,0);
counter = 0;
}
}
//now all records from source are fetch
if(presortFiles.isEmpty()){
//no presort files were created, so on-heap sorting is enough
arraySort(presort, counter, comparator, executor);
return arrayIterator(presort,0, counter);
}
final int[] presortCount = new int[presortFiles.size()];
for(int i=0;i0;
}
@Override public Object next() {
try {
Object ret = serializer.deserialize(ins[pos],-1);
if(--presortCount[pos]==0){
ins[pos].close();
presortFiles.get(pos).delete();
}
return ret;
} catch (IOException e) {
throw new IOError(e);
}
}
@Override public void remove() {
//ignored
}
};
}
//and add iterator over data on-heap
arraySort(presort, counter, comparator, executor);
iterators[iterators.length-1] = arrayIterator(presort,0,counter);
//and finally sort presorted iterators and return iterators over them
return sort(comparator, mergeDuplicates, iterators);
}catch(IOException e){
throw new IOError(e);
}finally{
for(File f:presortFiles) f.delete();
}
}
/**
* Reflection method {@link Arrays#parallelSort(Object[], int, int, Comparator)}.
* Is not invoked directly to keep compatibility with java8
*/
static private Method parallelSortMethod;
static{
try {
parallelSortMethod = Arrays.class.getMethod("parallelSort", Object[].class, int.class, int.class, Comparator.class);
} catch (NoSuchMethodException e) {
//java 6 & 7
parallelSortMethod = null;
}
}
protected static void arraySort(Object[] array, int arrayLen, Comparator comparator, Executor executor) {
//if executor is specified, try to use parallel method in java 8
if(executor!=null && parallelSortMethod!=null){
//TODO this uses common pool, but perhaps we should use Executor instead
try {
parallelSortMethod.invoke(null, array, 0, arrayLen, comparator);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e); //TODO exception hierarchy here?
}
}
Arrays.sort(array, 0, arrayLen, comparator);
}
/**
* Merge presorted iterators into single sorted iterator.
*
* @param comparator used to compare data
* @param mergeDuplicates if duplicate keys should be merged into single one
* @param iterators array of already sorted iterators
* @return sorted iterator
*/
public static Iterator sort(Comparator comparator, final boolean mergeDuplicates, final Iterator... iterators) {
final Comparator comparator2 = comparator==null?Fun.COMPARATOR:comparator;
return new Iterator(){
final NavigableSet