All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bigdata.service.proxy.RemoteChunk Maven / Gradle / Ivy

Go to download

Blazegraph(TM) DB Core Platform. It contains all Blazegraph DB dependencies other than Blueprints.

There is a newer version: 2.1.4
Show newest version
package com.bigdata.service.proxy;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import com.bigdata.io.IStreamSerializer;
import com.bigdata.striterator.IKeyOrder;

/**
 * A chunk of elements materialized from a remote iterator together with some
 * metadata about the state of the remote iterator (whether or not it is
 * exhausted, what its {@link IKeyOrder} is (if any)).
 * 
 * @author Bryan Thompson
 * @version $Id$
 * @param 
 */
public class RemoteChunk implements IRemoteChunk, Externalizable {

    /**
     * 
     */
    private static final long serialVersionUID = -8022644024286873191L;

    /**
     * true iff the source iterator is exhausted.
     */
    private boolean exhausted;

    /**
     * Used to (de-)serialize the chunk of elements.
     */
    private IStreamSerializer serializer;
    
    /**
     * The natural order of those elements (if any).
     */
    private IKeyOrder keyOrder;

    /**
     * The chunk of elements.
     */
    private E[] a;

    /**
     * De-serialization ctor.
     */
    public RemoteChunk() {
        
    }
    
    public RemoteChunk(
            final boolean exhausted,
            final IStreamSerializer serializer,
            final IKeyOrder keyOrder,
            final E[] a
            ) {

        if (serializer == null)
            throw new IllegalArgumentException();
        
        this.exhausted = exhausted;

        this.serializer = serializer;
        
        this.keyOrder = keyOrder; // MAY be null.

        this.a = a; // MUST be null if no elements to be sent.

    }

    public E[] getChunk() {

        return a;

    }

    public IKeyOrder getKeyOrder() {

        return keyOrder;

    }

    public boolean isExhausted() {

        return exhausted;

    }

    /**
     * The initial version.
     */
    private static final transient byte VERSION0 = 0;

    /**
     * The current version.
     */
    private static final transient byte VERSION = VERSION0;

    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        
       final byte version = in.readByte();

        switch (version) {
        case VERSION0:
            break;
        default:
            throw new UnsupportedOperationException("Unknown version: "
                    + version);
        }
        
        // true iff exhausted.
        exhausted = in.readBoolean();

        // true iff an E[] chunk is included.
        final boolean haveChunk = in.readBoolean();
        
        // the natural order of the iterator
        keyOrder = (IKeyOrder)in.readObject();
        
        if(haveChunk) {
            
            // de-serialize the serializer object.
            serializer = (IStreamSerializer) in.readObject();
            
            // de-serialize the chunk using that serializer.
            a = serializer.deserialize(in);
            
        }
        
    }

    public void writeExternal(ObjectOutput out) throws IOException {

        out.writeByte(VERSION);
        
        out.writeBoolean(exhausted);

        // true iff there are any elements in this chunk.
        final boolean haveChunk = a != null;
        out.writeBoolean(haveChunk);

        // the natural order of the elements (if any).
        out.writeObject(keyOrder);

        if (haveChunk) {

            // The (de-)serializer for the chunk.
            out.writeObject(serializer);

            // serialize the chunk.
            serializer.serialize(out,a);
            
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy