Search in sources :

Example 36 with SketchesArgumentException

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

the class Sketch method wrap.

/**
   * Wrap takes the sketch image in Memory and refers to it directly. There is no data copying onto
   * the java heap.  Only "Direct" Serialization Version 3 (i.e, OpenSource) sketches that have
   * been explicitly stored as direct objects can be wrapped.
   * An attempt to "wrap" earlier version sketches will result in a "heapified", normal
   * Java Heap version of the sketch where all data will be copied to the heap.
   * @param srcMem an image of a Sketch where the image seed hash matches the given seed hash.
   * <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>
   * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See Update Hash Seed</a>.
   * Compact sketches store a 16-bit hash of the seed, but not the seed itself.
   * @return a UpdateSketch backed by the given Memory
   */
public static Sketch wrap(final Memory srcMem, final long seed) {
    final long pre0 = srcMem.getLong(0);
    final int preLongs = srcMem.getByte(PREAMBLE_LONGS_BYTE) & 0X3F;
    final int serVer = srcMem.getByte(SER_VER_BYTE) & 0XFF;
    final int familyID = srcMem.getByte(FAMILY_BYTE) & 0XFF;
    final Family family = Family.idToFamily(familyID);
    switch(family) {
        case QUICKSELECT:
            {
                //Hash Table structure
                if ((serVer == 3) && (preLongs == 3)) {
                    return DirectQuickSelectSketchR.readOnlyWrap(srcMem, seed);
                } else {
                    throw new SketchesArgumentException("Corrupted: " + family + " family image: must have SerVer = 3 and preLongs = 3");
                }
            }
        case COMPACT:
            {
                //serVer 1, 2, or 3, preLongs = 1, 2, or 3
                if (serVer == 1) {
                    return ForwardCompatibility.heapify1to3(srcMem, seed);
                } else if (serVer == 2) {
                    return ForwardCompatibility.heapify2to3(srcMem, seed);
                }
                final int flags = srcMem.getByte(FLAGS_BYTE);
                //used for corruption check
                final boolean compact = (flags & COMPACT_FLAG_MASK) > 0;
                final boolean ordered = (flags & ORDERED_FLAG_MASK) > 0;
                if (compact) {
                    return ordered ? DirectCompactOrderedSketch.wrapInstance(srcMem, pre0, seed) : DirectCompactSketch.wrapInstance(srcMem, pre0, seed);
                }
                throw new SketchesArgumentException("Corrupted: " + family + " family image must have compact flag set");
            }
        default:
            throw new SketchesArgumentException("Sketch cannot wrap family: " + family + " as a Sketch");
    }
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) Family(com.yahoo.sketches.Family) Family.idToFamily(com.yahoo.sketches.Family.idToFamily)

Example 37 with SketchesArgumentException

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

the class Sketch method constructHeapSketch.

/**
   * Instantiates a Heap Sketch from Memory.
   * @param famID the Family ID
   * @param ordered true if the sketch is of the Compact family and ordered
   * @param srcMem <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>
   * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See Update Hash Seed</a>.
   * The seed required to instantiate a non-compact sketch.
   * @return a Sketch
   */
private static final Sketch constructHeapSketch(final byte famID, final boolean ordered, final Memory srcMem, final long seed) {
    final boolean compact = (srcMem.getByte(FLAGS_BYTE) & COMPACT_FLAG_MASK) != 0;
    final Family family = idToFamily(famID);
    switch(family) {
        case ALPHA:
            {
                if (compact) {
                    throw new SketchesArgumentException("Possibly Corrupted " + family + " image: cannot be compact");
                }
                return HeapAlphaSketch.heapifyInstance(srcMem, seed);
            }
        case QUICKSELECT:
            {
                return HeapQuickSelectSketch.heapifyInstance(srcMem, seed);
            }
        case COMPACT:
            {
                if (!compact) {
                    throw new SketchesArgumentException("Possibly Corrupted " + family + " image: must be compact");
                }
                return ordered ? HeapCompactOrderedSketch.heapifyInstance(srcMem, seed) : HeapCompactSketch.heapifyInstance(srcMem, seed);
            }
        default:
            {
                throw new SketchesArgumentException("Sketch cannot heapify family: " + family + " as a Sketch");
            }
    }
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) Family(com.yahoo.sketches.Family) Family.idToFamily(com.yahoo.sketches.Family.idToFamily)

Example 38 with SketchesArgumentException

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

the class UnionImpl method update.

@Override
public void update(final Memory skMem) {
    //UNION Empty Rule: AND the empty states
    if (skMem == null) {
        return;
    }
    final int cap = (int) skMem.getCapacity();
    final int fam = skMem.getByte(FAMILY_BYTE);
    final int serVer = skMem.getByte(SER_VER_BYTE);
    if (serVer == 1) {
        //older SetSketch, which is compact and ordered
        if (fam != 3) {
            //the original SetSketch
            throw new SketchesArgumentException("Family must be old SET_SKETCH: " + Family.idToFamily(fam));
        }
        //empty
        if (cap <= 24) {
            return;
        }
        processVer1(skMem);
    } else if (serVer == 2) {
        //older SetSketch, which is compact and ordered
        if (fam != 3) {
            //the original SetSketch
            throw new SketchesArgumentException("Family must be old SET_SKETCH: " + Family.idToFamily(fam));
        }
        //empty
        if (cap <= 8) {
            return;
        }
        processVer2(skMem);
    } else if (serVer == 3) {
        //The OpenSource sketches
        if ((fam < 1) || (fam > 3)) {
            throw new SketchesArgumentException("Family must be Alpha, QuickSelect, or Compact: " + Family.idToFamily(fam));
        }
        //empty and Theta = 1.0
        if (cap <= 8) {
            return;
        }
        processVer3(skMem);
    } else {
        throw new SketchesArgumentException("SerVer is unknown: " + serVer);
    }
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException)

Example 39 with SketchesArgumentException

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

the class UpdateSketch method wrap.

/**
  * Wrap takes the sketch image in Memory and refers to it directly. There is no data copying onto
  * the java heap. Only "Direct" Serialization Version 3 (i.e, OpenSource) sketches that have
  * been explicitly stored as direct objects can be wrapped.
  * An attempt to "wrap" earlier version sketches will result in a "heapified", normal
  * Java Heap version of the sketch where all data will be copied to the heap.
  * @param srcMem an image of a Sketch where the image seed hash matches the given seed hash.
  * <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>
  * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See Update Hash Seed</a>.
  * Compact sketches store a 16-bit hash of the seed, but not the seed itself.
  * @return a UpdateSketch backed by the given Memory
  */
public static UpdateSketch wrap(final WritableMemory srcMem, final long seed) {
    final int preLongs = srcMem.getByte(PREAMBLE_LONGS_BYTE) & 0X3F;
    final int serVer = srcMem.getByte(SER_VER_BYTE) & 0XFF;
    final int familyID = srcMem.getByte(FAMILY_BYTE) & 0XFF;
    final Family family = Family.idToFamily(familyID);
    if ((serVer == 3) && (preLongs == 3)) {
        return DirectQuickSelectSketch.writableWrap(srcMem, seed);
    } else {
        throw new SketchesArgumentException("Corrupted: " + family + " family image: must have SerVer = 3 and preLongs = 3");
    }
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) Family(com.yahoo.sketches.Family)

Example 40 with SketchesArgumentException

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

the class ArrayOfDoublesUnion method wrapUnionImpl.

static ArrayOfDoublesUnion wrapUnionImpl(final WritableMemory mem, final long seed, final boolean isWritable) {
    final SerializerDeserializer.SketchType type = SerializerDeserializer.getSketchType(mem);
    // compatibility with version 0.9.1 and lower
    if (type == SerializerDeserializer.SketchType.ArrayOfDoublesQuickSelectSketch) {
        final ArrayOfDoublesQuickSelectSketch sketch = isWritable ? new DirectArrayOfDoublesQuickSelectSketch(mem, seed) : new DirectArrayOfDoublesQuickSelectSketchR(mem, seed);
        return isWritable ? new DirectArrayOfDoublesUnion(sketch, mem) : new DirectArrayOfDoublesUnionR(sketch, mem);
    }
    final byte version = mem.getByte(ArrayOfDoublesUnion.SERIAL_VERSION_BYTE);
    if (version != ArrayOfDoublesUnion.serialVersionUID) {
        throw new SketchesArgumentException("Serial version mismatch. Expected: " + ArrayOfDoublesUnion.serialVersionUID + ", actual: " + version);
    }
    SerializerDeserializer.validateFamily(mem.getByte(ArrayOfDoublesUnion.FAMILY_ID_BYTE), mem.getByte(ArrayOfDoublesUnion.PREAMBLE_LONGS_BYTE));
    SerializerDeserializer.validateType(mem.getByte(ArrayOfDoublesUnion.SKETCH_TYPE_BYTE), SerializerDeserializer.SketchType.ArrayOfDoublesUnion);
    final WritableMemory sketchMem = mem.writableRegion(ArrayOfDoublesUnion.PREAMBLE_SIZE_BYTES, mem.getCapacity() - ArrayOfDoublesUnion.PREAMBLE_SIZE_BYTES);
    final ArrayOfDoublesQuickSelectSketch sketch = isWritable ? new DirectArrayOfDoublesQuickSelectSketch(sketchMem, seed) : new DirectArrayOfDoublesQuickSelectSketchR(sketchMem, seed);
    final ArrayOfDoublesUnion union = isWritable ? new DirectArrayOfDoublesUnion(sketch, mem) : new DirectArrayOfDoublesUnionR(sketch, mem);
    final long unionTheta = mem.getLong(ArrayOfDoublesUnion.THETA_LONG);
    union.setThetaLong(unionTheta);
    return union;
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) WritableMemory(com.yahoo.memory.WritableMemory)

Aggregations

SketchesArgumentException (com.yahoo.sketches.SketchesArgumentException)41 WritableMemory (com.yahoo.memory.WritableMemory)22 Test (org.testng.annotations.Test)12 Family (com.yahoo.sketches.Family)11 Memory (com.yahoo.memory.Memory)10 ResizeFactor (com.yahoo.sketches.ResizeFactor)6 Family.idToFamily (com.yahoo.sketches.Family.idToFamily)5 ArrayOfLongsSerDe (com.yahoo.sketches.ArrayOfLongsSerDe)4 PreambleUtil.extractResizeFactor (com.yahoo.sketches.sampling.PreambleUtil.extractResizeFactor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 ArrayOfBooleansSerDe (com.yahoo.sketches.ArrayOfBooleansSerDe)1 ArrayOfNumbersSerDe (com.yahoo.sketches.ArrayOfNumbersSerDe)1 ArrayOfStringsSerDe (com.yahoo.sketches.ArrayOfStringsSerDe)1 Util.checkIsCompactMemory (com.yahoo.sketches.quantiles.Util.checkIsCompactMemory)1 Method (java.lang.reflect.Method)1 BigDecimal (java.math.BigDecimal)1