// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.ColumnParent;
import org.apache.cassandra.thrift.ColumnPath;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.KeyRange;
import org.apache.cassandra.thrift.KeySlice;
import org.apache.cassandra.thrift.NotFoundException;
import org.apache.cassandra.thrift.SlicePredicate;
import org.apache.cassandra.thrift.SliceRange;
import org.apache.cassandra.thrift.TimedOutException;
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
public class TestThrift {
    private static ByteBuffer key(int k) throws UnsupportedEncodingException {
        return ByteBuffer.wrap(Integer.toString(k).getBytes("UTF-8"));
    }
    private static String sval(byte[] b) throws UnsupportedEncodingException {
        return new String(b, "UTF-8");
    }
    private static byte[] bval(String s) throws UnsupportedEncodingException {
        return s.getBytes("UTF-8");
    }
    public static void insert(int f1, String f2, boolean f3, Cassandra.Client cli) throws UnsupportedEncodingException, InvalidRequestException, UnavailableException, TimedOutException, TException {
        long t = System.currentTimeMillis();
        ColumnParent p = new ColumnParent("T1");
        if(f2 != null) {
            Column xf2 = new Column();
            xf2.setName(bval("F2"));
            xf2.setValue(bval(f2));
            xf2.setTimestamp(t);
            cli.insert(key(f1), p, xf2, ConsistencyLevel.ONE);
        }
        Column xf3 = new Column();
        xf3.setName(bval("F3"));
        xf3.setValue(bval(Boolean.toString(f3)));
        xf3.setTimestamp(t);
        cli.insert(key(f1), p, xf3, ConsistencyLevel.ONE);
        System.out.println("insert - OK");
    }
    public static void getOne(int f1, String f, Cassandra.Client cli) throws UnsupportedEncodingException, InvalidRequestException, NotFoundException, UnavailableException, TimedOutException, TException {
        ColumnPath p = new ColumnPath("T1");
        p.setColumn(bval(f));
        ColumnOrSuperColumn res = cli.get(key(f1), p, ConsistencyLevel.ONE);
        System.out.println("getOne - " + sval(res.getColumn().getName()) + "=" + sval(res.getColumn().getValue()));
    }
    public static void getAll(int f1, Cassandra.Client cli) throws UnsupportedEncodingException, InvalidRequestException, NotFoundException, UnavailableException, TimedOutException, TException {
        ColumnParent p = new ColumnParent("T1");
        SliceRange all = new SliceRange();
        all.setStart(new byte[0]);
        all.setFinish(new byte[0]);
        SlicePredicate pred = new SlicePredicate();
        pred.setSlice_range(all);
        System.out.print("getAll -");
        for(ColumnOrSuperColumn res : cli.get_slice(key(f1), p, pred, ConsistencyLevel.ONE)) {
            System.out.print(" " + sval(res.getColumn().getName()) + "=" + sval(res.getColumn().getValue()));
        }
        System.out.println();
    }
    public static void getRangeAll(int f1start, int f1end, Cassandra.Client cli) throws UnsupportedEncodingException, InvalidRequestException, NotFoundException, UnavailableException, TimedOutException, TException {
        KeyRange rang = new KeyRange();
        rang.setStart_key(key(f1start));
        rang.setEnd_key(key(f1end));
        ColumnParent p = new ColumnParent("T1");
        SliceRange all = new SliceRange();
        all.setStart(new byte[0]);
        all.setFinish(new byte[0]);
        SlicePredicate pred = new SlicePredicate();
        pred.setSlice_range(all);
        System.out.print("getRangeAll -");
        for(KeySlice row : cli.get_range_slices(p, pred, rang, ConsistencyLevel.ONE)) {
            System.out.print(" F1=" + sval(row.getKey()) + ":");
            for(ColumnOrSuperColumn res : row.getColumns()) {
                System.out.print(" " + sval(res.getColumn().getName()) + "=" + sval(res.getColumn().getValue()));
            }
        }
        System.out.println();
    }
    public static void main(String[] args) throws InvalidRequestException, TException, UnsupportedEncodingException, UnavailableException, TimedOutException, NotFoundException {
        TTransport tra = new TFramedTransport(new TSocket("localhost", 9160));
        TProtocol pro = new TBinaryProtocol(tra);
        Cassandra.Client cli = new Cassandra.Client(pro);
        tra.open();
        cli.set_keyspace("Test");
        insert(1, "A", true, cli);
        insert(2, "BB", false, cli);
        insert(3, "CCC", true, cli);
        insert(4, null, true, cli);
        getOne(1, "F2", cli);
        getOne(1, "F3", cli);
        getOne(2, "F2", cli);
        getOne(2, "F3", cli);
        getAll(3, cli);
        getAll(4, cli);
        // f1start <> f1end does not work with  RandomPartitioner - necessary to emulate with getAll
        getRangeAll(2, 2, cli);
        tra.close();
    }
}
