// 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.util.List;
import java.util.Map.Entry;

import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.KeyRange;
import org.apache.cassandra.thrift.SlicePredicate;
import org.scale7.cassandra.pelops.Bytes;
import org.scale7.cassandra.pelops.Cluster;
import org.scale7.cassandra.pelops.Mutator;
import org.scale7.cassandra.pelops.Pelops;
import org.scale7.cassandra.pelops.Selector;

public class TestPelops {
    private static String sval(byte[] b) throws UnsupportedEncodingException {
        return new String(b, "UTF-8");
    }
    public static void insert(int f1, String f2, boolean f3, Mutator mut) {
        if(f2 != null) {
            mut.writeColumn("T1", Integer.toString(f1), mut.newColumn("F2", f2));
        }
        mut.writeColumn("T1", Integer.toString(f1), mut.newColumn("F3", Boolean.toString(f3)));
        mut.execute(ConsistencyLevel.ONE);
        System.out.println("insert - OK");
    }
    public static void getOne(int f1, String f, Selector sel) throws UnsupportedEncodingException  {
        Column res = sel.getColumnFromRow("T1", Integer.toString(f1), f, ConsistencyLevel.ONE);
        System.out.println("getOne - " + sval(res.getName()) + "=" + sval(res.getValue()));
    }
    public static void getAll(int f1, Selector sel) throws UnsupportedEncodingException {
        System.out.print("getAll -");
        for(Column res : sel.getColumnsFromRow("T1", Integer.toString(f1), false, ConsistencyLevel.ONE)) {
            System.out.print(" " + sval(res.getName()) + "=" + sval(res.getValue()));
        }
        System.out.println();
    }
    public static void getRangeAll(int f1start, int f1end, Selector sel) throws UnsupportedEncodingException {
        KeyRange kr = Selector.newKeyRange(Integer.toString(f1start), Integer.toString(f1end), 1000);
        SlicePredicate pred = Selector.newColumnsPredicateAll(false);
        System.out.print("getRangeAll -");
        for(Entry<Bytes, List<Column>> row : sel.getColumnsFromRows("T1", kr, pred, ConsistencyLevel.ONE).entrySet()) {
            System.out.print(" F1=" + row.getKey().toUTF8() + ":");
            for(Column res : row.getValue()) {
                System.out.print(" " + sval(res.getName()) + "=" + sval(res.getValue()));
            }
        }
        System.out.println();
    }
    public static void main(String[] args) throws UnsupportedEncodingException {
        Pelops.addPool("TestPool", new Cluster("localhost", 9160), "Test");
        Mutator mut = Pelops.createMutator("TestPool");
        Selector sel = Pelops.createSelector("TestPool");
        insert(1, "A", true, mut);
        insert(2, "BB", false, mut);
        insert(3, "CCC", true, mut);
        insert(4, null, true, mut);
        getOne(1, "F2", sel);
        getOne(1, "F3", sel);
        getOne(2, "F2", sel);
        getOne(2, "F3", sel);
        getAll(3, sel);
        getAll(4, sel);
        // f1start <> f1end does not work with  RandomPartitioner - necessary to emulate with getAll
        getRangeAll(2, 2, sel);
        Pelops.removePool("TestPool");
    }
}
