public class TypeLayer extends Object
This layer was intentionally loosely coupled with the basic API. This allows other convenience layers for Fluo to build directly on the basic API w/o having to consider the particulars of this layer. Also its expected that integration with other languages may only use the basic API.
A TypeLayer is created with a certain encoder that is used for converting from bytes to primitives and visa versa. In order to ensure that all of your code uses the same encoder, its probably best to centralize the choice of an encoder within your project. There are many ways do to this, below is an example of one way to centralize and use.
public class MyTypeLayer extends TypeLayer {
public MyTypeLayer() {
super(new MyEncoder());
}
}
public class MyObserver extends TypedObserver {
MyObserver(){
super(new MyTypeLayer());
}
public abstract void process(TypedTransaction tx, Bytes row, Column col){
//do something w/ typed transaction
}
}
public class MyUtil {
//A little util to print out some stuff
public void printStuff(Snapshot snap, byte[] row){
TypedSnapshot tsnap = new MytTypeLayer().wrap(snap);
System.out.println(tsnap.get().row(row).fam("b90000").qual(137).toString("NP"));
}
}
The following example code shows using the basic fluo API with different types.
void process(Transaction tx, byte[] row, byte[] cf, int cq, long val){
tx.set(Bytes.of(row), new Column(Bytes.of(cf), Bytes.of(Integer.toString(cq))),
Bytes.of(Long.toString(val));
}
Alternatively, the same thing can be written using a TypedTransactionBase in the
following way. Because row(), fam(), qual(), and set() each take many different types, this
enables many different permutations that would not be achievable with overloading.
void process(TypedTransaction tx, byte[] r, byte[] cf, int cq, long v){
tx.mutate().row(r).fam(cf).qual(cq).set(v);
}
The following example code shows using the basic fluo API to read a value and default to zero if it does not exist.
void add(Transaction tx, byte[] row, Column col, long amount){
long balance = 0;
Bytes bval = tx.get(Bytes.of(row), col);
if(bval != null)
balance = Long.parseLong(bval.toString());
balance += amount;
tx.set(Bytes.of(row), col, Bytes.of(Long.toString(amount)));
}
Alternatively, the same thing can be written using a TypedTransactionBase in the
following way. This code avoids the null check by supplying a default value of zero.
void add(TypedTransaction tx, byte[] r, Column c, long amount){
long balance = tx.get().row(r).col(c).toLong(0);
balance += amount;
tx.mutate().row(r).col(c).set(balance);
}
For this particular case, shorter code can be written by using the increment method.
void add(TypedTransaction tx, byte[] r, Column c, long amount){
tx.mutate().row(r).col(c).increment(amount);
}
When using the basic API, you must ensure the return type is not null before converting a string or long.
void process(Transaction tx, byte[] row, Column col, long amount) {
Bytes val = tx.get(Bytes.of(row), col);
if(val == null)
return;
long balance = Long.parseLong(val.toString());
}
With TypedTransactionBase if no default value is supplied, then the null is passed
through.
void process(TypedTransaction tx, byte[] r, Column c, long amount){
Long balance = tx.get().row(r).col(c).toLong();
if(balance == null)
return;
}
The operations that return maps, return defaulted maps which make it easy to specify defaults and avoid null.
// pretend this method has curly braces. javadoc has issues with less than.
void process(TypedTransaction tx, byte[] r, Column c1, Column c2, Column c3, long amount)
Map<Column, Value> columns = tx.get().row(r).columns(c1,c2,c3);
// If c1 does not exist in map, a Value that wraps null will be returned.
// When c1 does not exist val1 will be set to null and no NPE will be thrown.
String val1 = columns.get(c1).toString();
// If c2 does not exist in map, then val2 will be set to empty string.
String val2 = columns.get(c2).toString("");
// If c3 does not exist in map, then val9 will be set to 9.
Long val3 = columns.get(c3).toLong(9);
This also applies to getting sets of rows.
// pretend this method has curly braces. javadoc has issues with less than.
void process(TypedTransaction tx, List<String> rows, Column c1, Column c2, Column c3,
long amount)
Map<String,Map<Column,Value>> rowCols =
tx.get().rowsString(rows).columns(c1,c2,c3).toStringMap();
// this will set val1 to null if row does not exist in map and/or column does not
// exist in child map
String val1 = rowCols.get("row1").get(c1).toString();
| Modifier and Type | Class and Description |
|---|---|
class |
TypeLayer.CFB |
class |
TypeLayer.CQB |
class |
TypeLayer.FamilyMethods<R1,R2> |
class |
TypeLayer.QualifierMethods<R> |
class |
TypeLayer.RowMethods<R> |
class |
TypeLayer.SimpleFamilyMethods<R1> |
static class |
TypeLayer.VisibilityMethods |
| Modifier and Type | Method and Description |
|---|---|
TypeLayer.CFB |
bc()
Initiates the chain of calls needed to build a column.
|
TypedSnapshot |
wrap(org.apache.fluo.api.client.Snapshot snap) |
TypedTransaction |
wrap(org.apache.fluo.api.client.Transaction tx) |
TypedTransactionBase |
wrap(org.apache.fluo.api.client.TransactionBase tx) |
public TypeLayer(Encoder encoder)
public TypeLayer.CFB bc()
public TypedSnapshot wrap(org.apache.fluo.api.client.Snapshot snap)
public TypedTransactionBase wrap(org.apache.fluo.api.client.TransactionBase tx)
public TypedTransaction wrap(org.apache.fluo.api.client.Transaction tx)
Copyright © 2016–2018 The Apache Software Foundation. All rights reserved.