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

import me.prettyprint.cassandra.model.AllOneConsistencyLevelPolicy;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.Serializer;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.beans.Row;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.hector.api.query.ColumnQuery;
import me.prettyprint.hector.api.query.MultigetSliceQuery;
import me.prettyprint.hector.api.query.SliceQuery;

public class TestHector {
    private static Serializer<String> sser = StringSerializer.get();
    public static void insert(int f1, String f2, boolean f3, Mutator<String> mut) {
        if(f2 != null) {
            mut.addInsertion(Integer.toString(f1), "T1", HFactory.createColumn("F2", f2, sser, sser));
        }
        mut.addInsertion(Integer.toString(f1), "T1", HFactory.createColumn("F3", Boolean.toString(f3), sser, sser));
        mut.execute();
        System.out.println("insert - OK");
    }
    public static void getOne(int f1, String f, ColumnQuery<String, String, String> q)  {
        q.setColumnFamily("T1");
        q.setKey(Integer.toString(f1));
        q.setName(f);
        HColumn<String, String>  res = q.execute().get();
        System.out.println("getOne - " + res.getName() + "=" + res.getValue());
    }
    public static void getAll(int f1, SliceQuery<String, String, String> q) {
        q.setColumnFamily("T1");
        q.setKey(Integer.toString(f1));
        q.setRange("", "", false, 1000); // q.setColumnNames("F2", "F3");
        System.out.print("getAll -");
        for(HColumn<String, String> res : q.execute().get().getColumns()) {
            System.out.print(" " + res.getName() + "=" + res.getValue());
        }
        System.out.println();
    }
    public static void getRangeAll(int f1start, int f1end, MultigetSliceQuery<String, String, String>  q) {
        q.setColumnFamily("T1");
        List<String> k = new ArrayList<String>();
        for(int f1 = f1start; f1 <= f1end; f1++) k.add(Integer.toString(f1));
        q.setKeys(k);
        q.setRange("", "", false, 1000); // q.setColumnNames("F2", "F3");
        System.out.print("getRangeAll -");
        for(Row<String, String, String> row : q.execute().get()) {
            System.out.print(" F1=" + row.getKey() + ":");
            for(HColumn<String, String> res : row.getColumnSlice().getColumns()) {
                System.out.print(" " + res.getName() + "=" + res.getValue());
            }
        }
        System.out.println();
    }
    public static void main(String[] args) {
        Cluster clu = HFactory.getOrCreateCluster("Test Cluster", "localhost:9160");
        Keyspace ks = HFactory.createKeyspace("Test", clu, new AllOneConsistencyLevelPolicy());
        Mutator<String> mut = HFactory.createMutator(ks, StringSerializer.get());
        insert(1, "A", true, mut);
        insert(2, "BB", false, mut);
        insert(3, "CCC", true, mut);
        insert(4, null, true, mut);
        ColumnQuery<String, String, String>  q1 = HFactory.createColumnQuery(ks, sser, sser, sser);
        SliceQuery<String, String, String> q2 = HFactory.createSliceQuery(ks, sser, sser, sser);
        MultigetSliceQuery<String, String, String> q3 = HFactory.createMultigetSliceQuery(ks, sser, sser, sser);
        getOne(1, "F2", q1);
        getOne(1, "F3", q1);
        getOne(2, "F2", q1);
        getOne(2, "F3", q1);
        getAll(3, q2);
        getAll(4, q2);
        getRangeAll(2, 3, q3);
        System.exit(0); // something hangs
    }
}
