

package galileo.comm;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import galileo.dht.NodeInfo;
import galileo.event.Event;
import galileo.serialization.SerializationException;
import galileo.serialization.SerializationInputStream;
import galileo.serialization.SerializationOutputStream;

/**
 * Represents a "Query Preamble" -- a set of information sent back to a client
 * before the query results start being transmitted.
 *
 * @author malensek
 */
public class QueryPreamble implements Event {
    private String id;
    private String filesystem;
    private List<NodeInfo> nodesInvolved = new ArrayList<>();

    public QueryPreamble(String id, String filesystem,
            List<NodeInfo> nodesInvolved) {
        this.id = id;
        this.filesystem = filesystem;
        this.nodesInvolved = nodesInvolved;
    }

    public String getFilesystem() {
        return this.filesystem;
    }

    public String getQueryId() {
        return id;
    }

    /**
     * Retrieves the list of StorageNodes involved in servicing a QueryRequest.
     * Each node in the list should reply with resulting metadata.
     */
    public List<NodeInfo> getNodesInvolved() {
        return nodesInvolved;
    }

    @Deserialize
    public QueryPreamble(SerializationInputStream in)
    throws IOException, SerializationException {
        id = in.readString();
        filesystem = in.readString();
        in.readSerializableCollection(NodeInfo.class, nodesInvolved);
    }

    @Override
    public void serialize(SerializationOutputStream out)
    throws IOException {
        out.writeString(id);
        out.writeString(filesystem);
        out.writeSerializableCollection(nodesInvolved);
    }
}
