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

com.netflix.astyanax.recipes.functions.TraceFunction Maven / Gradle / Ivy

There is a newer version: 3.10.2
Show newest version
package com.netflix.astyanax.recipes.functions;

import java.io.IOException;
import java.io.OutputStream;

import com.google.common.base.Function;
import com.netflix.astyanax.model.Column;
import com.netflix.astyanax.model.ColumnFamily;
import com.netflix.astyanax.model.Row;

/**
 * Simple function to trace the contents
 * @author elandau
 *
 * @param 
 * @param 
 */
public class TraceFunction implements Function, Boolean> {

    public static class Builder {
        private OutputStream out = System.out;
        private boolean showColumns = false;
        
        public Builder(ColumnFamily columnFamily) {
        }
        
        public Builder withOutputStream(OutputStream out) {
            this.out = out;
            return this;
        }
        
        public Builder withShowColumns(boolean showColumns) {
            this.showColumns = showColumns;
            return this;
        }
        
        public TraceFunction build() {
            return new TraceFunction(this);
        }
    }

    public static  Builder builder(ColumnFamily columnFamily) {
        return new Builder(columnFamily);
    }
    
    private final OutputStream out;
    private final boolean showColumns;
    
    private TraceFunction(Builder builder) {
        this.out                    = builder.out;
        this.showColumns            = builder.showColumns;
    }
    
    @Override
    public synchronized Boolean apply(Row row) {
        long size = 0;
        for (Column column : row.getColumns()) {
            size += column.getRawName().limit() + column.getByteBufferValue().limit();
        }
        
        StringBuilder sb = new StringBuilder();
        
        sb.append(String.format("- row: '%s' size: '%dl' count: '%dl' \n", row.getKey(), size, row.getColumns().size()));
        if (showColumns) {
            for (Column column : row.getColumns()) {
                sb.append(String.format("  '%s' (ts='%dl', ttl='%d')\n", column.getName(), column.getTimestamp(), column.getTtl()));
            }
        }
        try {
            out.write(sb.toString().getBytes());
        } catch (IOException e) {
        }
        return true;
    }
    

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy