Search in sources :

Example 11 with ResizeFactor

use of com.yahoo.sketches.ResizeFactor in project sketches-core by DataSketches.

the class PreambleUtil method sketchPreambleToString.

private static String sketchPreambleToString(final Memory mem, final Family family, final int preLongs) {
    final ResizeFactor rf = ResizeFactor.getRF(extractResizeFactor(mem));
    final int serVer = extractSerVer(mem);
    // Flags
    final int flags = extractFlags(mem);
    final String flagsStr = zeroPad(Integer.toBinaryString(flags), 8) + ", " + (flags);
    //final boolean bigEndian = (flags & BIG_ENDIAN_FLAG_MASK) > 0;
    //final String nativeOrder = ByteOrder.nativeOrder().toString();
    //final boolean readOnly = (flags & READ_ONLY_FLAG_MASK) > 0;
    final boolean isEmpty = (flags & EMPTY_FLAG_MASK) > 0;
    final boolean isGadget = (flags & GADGET_FLAG_MASK) > 0;
    final int k;
    if (serVer == 1) {
        final short encK = extractEncodedReservoirSize(mem);
        k = ReservoirSize.decodeValue(encK);
    } else {
        k = extractK(mem);
    }
    long n = 0;
    if (!isEmpty) {
        n = extractN(mem);
    }
    final long dataBytes = mem.getCapacity() - (preLongs << 3);
    final StringBuilder sb = new StringBuilder();
    sb.append(LS).append("### END ").append(family.getFamilyName().toUpperCase()).append(" PREAMBLE SUMMARY").append(LS).append("Byte  0: Preamble Longs       : ").append(preLongs).append(LS).append("Byte  0: ResizeFactor         : ").append(rf.toString()).append(LS).append("Byte  1: Serialization Version: ").append(serVer).append(LS).append("Byte  2: Family               : ").append(family.toString()).append(LS).append("Byte  3: Flags Field          : ").append(flagsStr).append(LS).append("  EMPTY                       : ").append(isEmpty).append(LS);
    if (family == Family.VAROPT) {
        sb.append("  GADGET                      : ").append(isGadget).append(LS);
    }
    sb.append("Bytes  4-7: Sketch Size (k)   : ").append(k).append(LS);
    if (!isEmpty) {
        sb.append("Bytes 8-15: Items Seen (n)    : ").append(n).append(LS);
    }
    if (family == Family.VAROPT && !isEmpty) {
        final int hCount = extractHRegionItemCount(mem);
        final int rCount = extractRRegionItemCount(mem);
        final double totalRWeight = extractTotalRWeight(mem);
        sb.append("Bytes 16-19: H region count   : ").append(hCount).append(LS).append("Bytes 20-23: R region count   : ").append(rCount).append(LS);
        if (rCount > 0) {
            sb.append("Bytes 24-31: R region weight  : ").append(totalRWeight).append(LS);
        }
    }
    sb.append("TOTAL Sketch Bytes            : ").append(mem.getCapacity()).append(LS).append("  Preamble Bytes              : ").append(preLongs << 3).append(LS).append("  Data Bytes                  : ").append(dataBytes).append(LS).append("### END ").append(family.getFamilyName().toUpperCase()).append(" PREAMBLE SUMMARY").append(LS);
    return sb.toString();
}
Also used : ResizeFactor(com.yahoo.sketches.ResizeFactor)

Example 12 with ResizeFactor

use of com.yahoo.sketches.ResizeFactor in project sketches-core by DataSketches.

the class SetOperationTest method checkBuilder.

@Test
public void checkBuilder() {
    int k = 2048;
    long seed = 1021;
    UpdateSketch usk1 = UpdateSketch.builder().setSeed(seed).setNominalEntries(k).build();
    UpdateSketch usk2 = UpdateSketch.builder().setSeed(seed).setNominalEntries(k).build();
    for (int i = 0; i < (k / 2); i++) {
        //256
        usk1.update(i);
    }
    for (int i = k / 2; i < k; i++) {
        //256 no overlap
        usk2.update(i);
    }
    ResizeFactor rf = X4;
    //use default size
    Union union = SetOperation.builder().setSeed(seed).setResizeFactor(rf).buildUnion();
    union.update(usk1);
    union.update(usk2);
    double exactUnionAnswer = k;
    //ordered: false
    CompactSketch comp1 = union.getResult(false, null);
    double compEst = comp1.getEstimate();
    assertEquals(compEst, exactUnionAnswer, 0.0);
}
Also used : ResizeFactor(com.yahoo.sketches.ResizeFactor) Test(org.testng.annotations.Test)

Example 13 with ResizeFactor

use of com.yahoo.sketches.ResizeFactor in project sketches-core by DataSketches.

the class UpdateSketchTest method checkBuilder.

@Test
public void checkBuilder() {
    UpdateSketchBuilder bldr = UpdateSketch.builder();
    long seed = 12345L;
    bldr.setSeed(seed);
    assertEquals(seed, bldr.getSeed());
    float p = (float) 0.5;
    bldr.setP(p);
    assertEquals(p, bldr.getP());
    ResizeFactor rf = ResizeFactor.X4;
    bldr.setResizeFactor(rf);
    assertEquals(rf, bldr.getResizeFactor());
    Family fam = Family.ALPHA;
    bldr.setFamily(fam);
    assertEquals(fam, bldr.getFamily());
    int lgK = 10;
    int k = 1 << lgK;
    bldr.setNominalEntries(k);
    assertEquals(lgK, bldr.getLgNominalEntries());
    println(bldr.toString());
}
Also used : Family(com.yahoo.sketches.Family) ResizeFactor(com.yahoo.sketches.ResizeFactor) Test(org.testng.annotations.Test)

Example 14 with ResizeFactor

use of com.yahoo.sketches.ResizeFactor in project sketches-core by DataSketches.

the class SketchTest method checkBuilderResizeFactor.

@Test
public void checkBuilderResizeFactor() {
    ResizeFactor rf;
    rf = X1;
    assertEquals(rf.getValue(), 1);
    assertEquals(rf.lg(), 0);
    assertEquals(ResizeFactor.getRF(0), X1);
    rf = X2;
    assertEquals(rf.getValue(), 2);
    assertEquals(rf.lg(), 1);
    assertEquals(ResizeFactor.getRF(1), X2);
    rf = X4;
    assertEquals(rf.getValue(), 4);
    assertEquals(rf.lg(), 2);
    assertEquals(ResizeFactor.getRF(2), X4);
    rf = X8;
    assertEquals(rf.getValue(), 8);
    assertEquals(rf.lg(), 3);
    assertEquals(ResizeFactor.getRF(3), X8);
}
Also used : ResizeFactor(com.yahoo.sketches.ResizeFactor) Test(org.testng.annotations.Test)

Example 15 with ResizeFactor

use of com.yahoo.sketches.ResizeFactor in project sketches-core by DataSketches.

the class SketchTest method checkBuilder.

@Test
public void checkBuilder() {
    int k = 2048;
    int lgK = Integer.numberOfTrailingZeros(k);
    long seed = 1021;
    float p = (float) 0.5;
    ResizeFactor rf = X4;
    Family fam = Family.ALPHA;
    UpdateSketch sk1 = UpdateSketch.builder().setSeed(seed).setP(p).setResizeFactor(rf).setFamily(fam).setNominalEntries(k).build();
    String nameS1 = sk1.getClass().getSimpleName();
    assertEquals(nameS1, "HeapAlphaSketch");
    assertEquals(sk1.getLgNomLongs(), lgK);
    assertEquals(sk1.getSeed(), seed);
    assertEquals(sk1.getP(), p);
    //check reset of defaults
    sk1 = UpdateSketch.builder().build();
    nameS1 = sk1.getClass().getSimpleName();
    assertEquals(nameS1, "HeapQuickSelectSketch");
    assertEquals(sk1.getLgNomLongs(), Integer.numberOfTrailingZeros(DEFAULT_NOMINAL_ENTRIES));
    assertEquals(sk1.getSeed(), DEFAULT_UPDATE_SEED);
    assertEquals(sk1.getP(), (float) 1.0);
    assertEquals(sk1.getResizeFactor(), ResizeFactor.X8);
}
Also used : Family(com.yahoo.sketches.Family) Family.objectToFamily(com.yahoo.sketches.Family.objectToFamily) ResizeFactor(com.yahoo.sketches.ResizeFactor) Test(org.testng.annotations.Test)

Aggregations

ResizeFactor (com.yahoo.sketches.ResizeFactor)22 Test (org.testng.annotations.Test)11 SketchesArgumentException (com.yahoo.sketches.SketchesArgumentException)6 Family (com.yahoo.sketches.Family)5 PreambleUtil.extractResizeFactor (com.yahoo.sketches.sampling.PreambleUtil.extractResizeFactor)3 ArrayList (java.util.ArrayList)3 SketchesException (com.yahoo.sketches.SketchesException)2 WritableMemory (com.yahoo.memory.WritableMemory)1 ArrayOfBooleansSerDe (com.yahoo.sketches.ArrayOfBooleansSerDe)1 Family.objectToFamily (com.yahoo.sketches.Family.objectToFamily)1