package org.apache.cassandra.streaming; /* * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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. * */ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang.StringUtils; import org.apache.cassandra.io.ICompactSerializer; import org.apache.cassandra.io.sstable.Descriptor; import org.apache.cassandra.io.sstable.SSTable; import org.apache.cassandra.utils.Pair; /** * Represents portions of a file to be streamed between nodes. */ public class PendingFile { private static PendingFileSerializer serializer_ = new PendingFileSerializer(); public static PendingFileSerializer serializer() { return serializer_; } // NB: this reference prevents garbage collection of the sstable on the source node private final SSTable sstable; public final Descriptor desc; public final String component; public final List<Pair<Long,Long>> sections; public final long size; public long progress; public PendingFile(Descriptor desc, PendingFile pf) { this(null, desc, pf.component, pf.sections); } public PendingFile(SSTable sstable, Descriptor desc, String component, List<Pair<Long,Long>> sections) { this.sstable = sstable; this.desc = desc; this.component = component; this.sections = sections; long tempSize = 0; for(Pair<Long,Long> section : sections) { tempSize += section.right - section.left; } size = tempSize; } public String getFilename() { return desc.filenameFor(component); } public boolean equals(Object o) { if ( !(o instanceof PendingFile) ) return false; PendingFile rhs = (PendingFile)o; return getFilename().equals(rhs.getFilename()); } public int hashCode() { return getFilename().hashCode(); } public String toString() { return getFilename() + "/" + StringUtils.join(sections, ",") + "\n\t progress=" + progress + "/" + size + " - " + progress*100/size + "%"; } public static class PendingFileSerializer implements ICompactSerializer<PendingFile> { public void serialize(PendingFile sc, DataOutputStream dos) throws IOException { if (sc == null) { dos.writeUTF(""); return; } dos.writeUTF(sc.desc.filenameFor(sc.component)); dos.writeUTF(sc.component); dos.writeInt(sc.sections.size()); for (Pair<Long,Long> section : sc.sections) { dos.writeLong(section.left); dos.writeLong(section.right); } } public PendingFile deserialize(DataInputStream dis) throws IOException { String filename = dis.readUTF(); if (filename.isEmpty()) return null; Descriptor desc = Descriptor.fromFilename(filename); String component = dis.readUTF(); int count = dis.readInt(); List<Pair<Long,Long>> sections = new ArrayList<Pair<Long,Long>>(count); for (int i = 0; i < count; i++) sections.add(new Pair<Long,Long>(Long.valueOf(dis.readLong()), Long.valueOf(dis.readLong()))); return new PendingFile(null, desc, component, sections); } } }