Example usage for com.google.common.primitives UnsignedBytes saturatedCast

List of usage examples for com.google.common.primitives UnsignedBytes saturatedCast

Introduction

In this page you can find the example usage for com.google.common.primitives UnsignedBytes saturatedCast.

Prototype

public static byte saturatedCast(long value) 

Source Link

Document

Returns the byte value that, when treated as unsigned, is nearest in value to value .

Usage

From source file:se.sics.caracaldb.global.DefaultPolicy.java

private Set<Key> generateVNodes(byte[] schemaId, int num) {
    Set<Key> keys = new TreeSet<Key>();
    // boundary nodes
    Key start = new Key(schemaId);
    keys.add(start);//  www .  ja  va 2  s.  c  om
    //virtualHostsPut(end, rset); // this mind end up being override by the next schema, but that's ok since we only need it if there is no next schema
    if (num == 1) { // single vnode needed
        return keys;
    }
    num--; // account for the initial vnode already created (the end-nodes doesn't count)
    if (num <= UnsignedBytes.MAX_VALUE) { // another byte for subkeys needed
        int incr = (int) UnsignedBytes.MAX_VALUE / (int) num;
        int last = 0;
        int ceiling = (int) UnsignedBytes.MAX_VALUE - incr;
        while (last < ceiling) {
            last = last + incr;
            Key k = start.append(new byte[] { UnsignedBytes.saturatedCast(last) }).get();
            keys.add(k);
        }
    } else if (num <= UnsignedInteger.MAX_VALUE.longValue()) { // another 4 bytes for subkeys needed
        long incr = UnsignedInteger.MAX_VALUE.longValue() / num;
        long last = 0;
        long ceiling = UnsignedInteger.MAX_VALUE.longValue() - incr;
        while (last < ceiling) {
            last = last + incr;
            Key k = start.append(new Key(UnsignedInteger.valueOf(last).intValue())).get();
            keys.add(k);
        }
    } else { // another 8 bytes for subkeys needed (don't support more!)
        UnsignedLong incr = UnsignedLong.MAX_VALUE.dividedBy(UnsignedLong.valueOf(num));
        UnsignedLong last = UnsignedLong.ZERO;
        UnsignedLong ceiling = UnsignedLong.MAX_VALUE.minus(incr);
        while (last.compareTo(ceiling) <= 0) {
            last = last.plus(incr);
            Key k = start.append(new Key(last.intValue())).get();
            keys.add(k);
        }
    }
    return keys;
}

From source file:se.sics.caracaldb.global.LookupTable.java

private void generateInitialVirtuals(ByteBuffer schemaId) {
    ImmutableMap<String, String> meta = schemas.metaData.get(schemaId);
    //String rfactorS = meta.getOrDefault("rfactor", "3");
    String rfactorS = J6.orDefault(meta.get("rfactor"), "3");
    Integer rfactor = Integer.parseInt(rfactorS);
    //String vnodesS = meta.getOrDefault("vnodes", "1");
    String vnodesS = J6.orDefault(meta.get("vnodes"), "1");
    long vnodes = Long.parseLong(vnodesS);
    //String forceMasterS = meta.getOrDefault("forceMaster", "false");
    String forceMasterS = J6.orDefault(meta.get("forceMaster"), "false"); // it would look so nice in Java8 -.-
    boolean forceMaster = Boolean.parseBoolean(forceMasterS);

    // boundary nodes
    Key start = new Key(schemaId);
    //Key end = start.inc();
    Integer rset = forceMaster ? 0 : findReplicationSetOfSize(rfactor);
    virtualHostsPut(start, rset);/*from www . ja va2s  .  c  om*/
    //virtualHostsPut(end, rset); // this mind end up being override by the next schema, but that's ok since we only need it if there is no next schema
    if (vnodes == 1) { // single vnode needed
        return;
    }
    vnodes--; // account for the initial vnode already created (the end-nodes doesn't count)
    Set<Key> subkeys = new TreeSet<Key>();
    if (vnodes <= UnsignedBytes.MAX_VALUE) { // another byte for subkeys needed
        int incr = (int) UnsignedBytes.MAX_VALUE / (int) vnodes;
        int last = 0;
        int ceiling = (int) UnsignedBytes.MAX_VALUE - incr;
        while (last < ceiling) {
            last = last + incr;
            Key k = start.append(new byte[] { UnsignedBytes.saturatedCast(last) }).get();
            subkeys.add(k);
        }
    } else if (vnodes <= UnsignedInteger.MAX_VALUE.longValue()) { // another 4 bytes for subkeys needed
        long incr = UnsignedInteger.MAX_VALUE.longValue() / vnodes;
        long last = 0;
        long ceiling = UnsignedInteger.MAX_VALUE.longValue() - incr;
        while (last < ceiling) {
            last = last + incr;
            Key k = start.append(new Key(UnsignedInteger.valueOf(last).intValue())).get();
            subkeys.add(k);
        }
    } else { // another 8 bytes for subkeys needed (don't support more!)
        UnsignedLong incr = UnsignedLong.MAX_VALUE.dividedBy(UnsignedLong.valueOf(vnodes));
        UnsignedLong last = UnsignedLong.ZERO;
        UnsignedLong ceiling = UnsignedLong.MAX_VALUE.minus(incr);
        while (last.compareTo(ceiling) <= 0) {
            last = last.plus(incr);
            Key k = start.append(new Key(last.intValue())).get();
            subkeys.add(k);
        }
    }
    for (Key subkey : subkeys) {
        virtualHostsPut(subkey, forceMaster ? 0 : findReplicationSetOfSize(rfactor));
    }
}