@SuppressWarnings({"unchecked", "rawtypes"}) protected void spill() { // Make sure we have something to spill. if (memory.size() > 0) { OutputStream out; try { out = getSpillStream(); } catch (IOException e) { throw new AtlasException(e); } // Sort the tuples // Collections.sort() will copy to an array, sort, and then copy back. Avoid that // extra copy by copying to an array and using Arrays.sort(). Also it lets us use // Collection<E> instead of List<E> as the type for the memory object. Unfortunately // because of Java's crazy generics we have to do it as an Object array. Object[] array = memory.toArray(); Arrays.sort(array, (Comparator) comparator); Sink<E> serializer = serializationFactory.createSerializer(out); try { for (Object tuple : array) { serializer.send((E) tuple); } } finally { serializer.close(); } spilled = true; policy.reset(); memory.clear(); } }
/** * Creates and returns a deep copy of the specified object. * * @param o the object to be copied * @return the copy. */ public static Serializable deepCopy(Serializable o) { Store store = new Store(); StoreArrayInputStream input = new StoreArrayInputStream(store); StoreArrayOutputStream output = new StoreArrayOutputStream(store); final Object oc = o; try { final SerializationInput ser_input = SerializationFactory.createSerializationInput(serialization, input, null); final SerializationOutput ser_output = SerializationFactory.createSerializationOutput(serialization, output, null); Thread writer = new Thread("DeepCopy writer") { public void run() { // System.out.println("Writer started ..."); try { ser_output.writeObject(oc); ser_output.close(); } catch (Exception e) { // Should not happen // System.out.println("Got exception: " + e); // e.printStackTrace(); throw new Error("Got exception: ", e); } } }; writer.start(); return (Serializable) ser_input.readObject(); } catch (Exception e) { // Should not happen throw new Error("Got exception: ", e); } }
protected Iterator<E> getInputIterator(File spillFile) throws FileNotFoundException { InputStream in = getInputStream(spillFile); Iterator<E> deserializer = serializationFactory.createDeserializer(in); return new IteratorResourceClosing<E>(deserializer, in); }
@Override public void initialize(Map<String, Object> config) { kryo = SerializationFactory.getKryo(config); kryoOut = new Output(2000, 2000000000); kryoIn = new Input(1); }
/* (non-Javadoc) * @see org.openjena.atlas.io.ThresholdPolicy#increment(java.lang.Object) */ @Override public void increment(T item) { count++; size += serializerFactory.getEstimatedMemorySize(item); }