Example usage for org.apache.cassandra.io.sstable Descriptor toString

List of usage examples for org.apache.cassandra.io.sstable Descriptor toString

Introduction

In this page you can find the example usage for org.apache.cassandra.io.sstable Descriptor toString.

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:com.spotify.cassandra.opstools.SSTableTimestampViewer.java

License:Apache License

/**
 * @param args a list of sstables whose metadata we're interested in
 *///from   www  .  ja v  a 2  s .c  om
public static void main(String[] args) throws IOException {
    PrintStream out = System.out;
    if (args.length == 0) {
        out.println("Usage: spcassandra-sstable-timestamp <sstable filenames>");
        System.exit(1);
    }

    List<TimeMetadata> metadata = Lists.newArrayListWithExpectedSize(args.length);
    for (String fname : args) {
        Descriptor descriptor = Descriptor.fromFilename(fname);
        SSTableMetadata md = SSTableMetadata.serializer.deserialize(descriptor).left;
        metadata.add(new TimeMetadata(descriptor.toString(), md.minTimestamp, md.maxTimestamp,
                new java.io.File(descriptor.baseFilename() + "-Data.db").length()));
    }

    Collections.sort(metadata, new Comparator<TimeMetadata>() {
        public int compare(TimeMetadata o1, TimeMetadata o2) {
            return Long.compare(o1.minTimestamp, o2.minTimestamp);
        }
    });

    long[] timespanHistogram = new long[metadata.size() + 1];
    SortedSet<TimeMetadata> currentOverlaps = new TreeSet<>(new Comparator<TimeMetadata>() {
        public int compare(TimeMetadata o1, TimeMetadata o2) {
            return Long.compare(o1.maxTimestamp, o2.maxTimestamp);
        }
    });

    List<Interval<Long, Integer>> intervals = Lists.newArrayList();

    long currentTime = 0;
    boolean wasMax = false;
    for (TimeMetadata md : metadata) {
        while (currentOverlaps.size() > 0 && currentOverlaps.first().maxTimestamp < md.minTimestamp) {
            intervals.add(new Interval<>(currentTime, !wasMax, currentOverlaps.first().maxTimestamp, true,
                    currentOverlaps.size()));
            timespanHistogram[currentOverlaps.size()] += currentOverlaps.first().maxTimestamp - currentTime;
            currentTime = currentOverlaps.first().maxTimestamp;
            wasMax = true;
            currentOverlaps.remove(currentOverlaps.first());
        }
        if (currentTime != 0) {
            intervals.add(new Interval<>(currentTime, !wasMax, md.minTimestamp, false, currentOverlaps.size()));
            timespanHistogram[currentOverlaps.size()] += md.minTimestamp - currentTime;
        }
        currentTime = md.minTimestamp;
        wasMax = false;
        currentOverlaps.add(md);
    }
    while (currentOverlaps.size() > 0) {
        intervals.add(new Interval<>(currentTime, !wasMax, currentOverlaps.first().maxTimestamp, true,
                currentOverlaps.size()));
        timespanHistogram[currentOverlaps.size()] += currentOverlaps.first().maxTimestamp - currentTime;
        currentTime = currentOverlaps.first().maxTimestamp;
        wasMax = true;
        currentOverlaps.remove(currentOverlaps.first());
    }

    for (TimeMetadata md : metadata)
        out.println(md);

    for (Interval<Long, Integer> interval : intervals)
        out.println(interval);
    out.println();

    for (int i = 0; i < timespanHistogram.length; i++)
        out.printf("Total time covered by %s sstables: %s (%.2f%%)%n", i, timespanHistogram[i],
                (double) timespanHistogram[i] / (currentTime - metadata.get(0).minTimestamp) * 100);
}