Search in sources :

Example 1 with GrowableArray

use of org.apache.hyracks.data.std.util.GrowableArray in project asterixdb by apache.

the class ListItemBinaryHashFunctionFactory method createBinaryHashFunction.

public IBinaryHashFunction createBinaryHashFunction(final ATypeTag itemTypeTag, final boolean ignoreCase) {
    return new IBinaryHashFunction() {

        private IBinaryHashFunction lowerCaseStringHash = new PointableBinaryHashFunctionFactory(UTF8StringLowercasePointable.FACTORY).createBinaryHashFunction();

        private IBinaryHashFunction genericBinaryHash = MurmurHash3BinaryHashFunctionFamily.INSTANCE.createBinaryHashFunction(0);

        private GrowableArray taggedBytes = new GrowableArray();

        @Override
        public int hash(byte[] bytes, int offset, int length) throws HyracksDataException {
            ATypeTag tag = itemTypeTag;
            int skip = 0;
            if (itemTypeTag == ATypeTag.ANY) {
                tag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(bytes[offset]);
                skip = 1;
            }
            switch(tag) {
                case STRING:
                    {
                        if (ignoreCase) {
                            return lowerCaseStringHash.hash(bytes, offset + skip, length - skip);
                        }
                    }
                default:
                    {
                        if (itemTypeTag != ATypeTag.ANY) {
                            // add the itemTypeTag in front of the data
                            try {
                                resetTaggedBytes(bytes, offset, length);
                                return genericBinaryHash.hash(taggedBytes.getByteArray(), 0, length + 1);
                            } catch (IOException e) {
                                throw new HyracksDataException(e);
                            }
                        } else {
                            return genericBinaryHash.hash(bytes, offset, length);
                        }
                    }
            }
        }

        private void resetTaggedBytes(byte[] data, int offset, int length) throws IOException {
            taggedBytes.reset();
            taggedBytes.getDataOutput().writeByte(itemTypeTag.serialize());
            taggedBytes.getDataOutput().write(data, offset, length);
        }
    };
}
Also used : IBinaryHashFunction(org.apache.hyracks.api.dataflow.value.IBinaryHashFunction) ATypeTag(org.apache.asterix.om.types.ATypeTag) GrowableArray(org.apache.hyracks.data.std.util.GrowableArray) PointableBinaryHashFunctionFactory(org.apache.hyracks.data.std.accessors.PointableBinaryHashFunctionFactory) IOException(java.io.IOException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 2 with GrowableArray

use of org.apache.hyracks.data.std.util.GrowableArray in project asterixdb by apache.

the class SubstringAfterDescriptor method createEvaluatorFactory.

@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
    return new IScalarEvaluatorFactory() {

        private static final long serialVersionUID = 1L;

        @Override
        public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
            return new IScalarEvaluator() {

                private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                private IPointable array0 = new VoidPointable();

                private IPointable array1 = new VoidPointable();

                private IScalarEvaluator evalString = args[0].createScalarEvaluator(ctx);

                private IScalarEvaluator evalPattern = args[1].createScalarEvaluator(ctx);

                private final GrowableArray array = new GrowableArray();

                private final UTF8StringBuilder builder = new UTF8StringBuilder();

                private final UTF8StringPointable stringPtr = new UTF8StringPointable();

                private final UTF8StringPointable patternPtr = new UTF8StringPointable();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    evalString.evaluate(tuple, array0);
                    byte[] src = array0.getByteArray();
                    int srcOffset = array0.getStartOffset();
                    int srcLen = array0.getLength();
                    evalPattern.evaluate(tuple, array1);
                    byte[] pattern = array1.getByteArray();
                    int patternOffset = array1.getStartOffset();
                    int patternLen = array1.getLength();
                    if (src[srcOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 0, src[srcOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                    }
                    if (pattern[patternOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 1, pattern[patternOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                    }
                    try {
                        stringPtr.set(src, srcOffset + 1, srcLen - 1);
                        patternPtr.set(pattern, patternOffset + 1, patternLen - 1);
                        array.reset();
                        UTF8StringPointable.substrAfter(stringPtr, patternPtr, builder, array);
                        out.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        out.write(array.getByteArray(), 0, array.getLength());
                    } catch (IOException e) {
                        throw new HyracksDataException(e);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) GrowableArray(org.apache.hyracks.data.std.util.GrowableArray) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) UTF8StringBuilder(org.apache.hyracks.data.std.util.UTF8StringBuilder) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)

Example 3 with GrowableArray

use of org.apache.hyracks.data.std.util.GrowableArray in project asterixdb by apache.

the class UTF8StringPointableTest method testTrimWithPattern.

@Test
public void testTrimWithPattern() throws Exception {
    UTF8StringBuilder builder = new UTF8StringBuilder();
    GrowableArray storage = new GrowableArray();
    UTF8StringPointable result = new UTF8StringPointable();
    UTF8StringPointable input = generateUTF8Pointable("  this is it.i am;here.  ");
    // Trims both sides.
    input.trim(builder, storage, true, true, CharSet.getInstance(" hert."));
    result.set(storage.getByteArray(), 0, storage.getLength());
    UTF8StringPointable expected = generateUTF8Pointable("is is it.i am;");
    assertEquals(0, expected.compareTo(result));
    // Only trims the right side.
    storage.reset();
    input.trim(builder, storage, false, true, CharSet.getInstance(" hert."));
    result.set(storage.getByteArray(), 0, storage.getLength());
    expected = generateUTF8Pointable("  this is it.i am;");
    assertEquals(0, expected.compareTo(result));
    // Only trims the left side.
    storage.reset();
    input.trim(builder, storage, true, false, CharSet.getInstance(" hert."));
    result.set(storage.getByteArray(), 0, storage.getLength());
    expected = generateUTF8Pointable("is is it.i am;here.  ");
    assertEquals(0, expected.compareTo(result));
}
Also used : GrowableArray(org.apache.hyracks.data.std.util.GrowableArray) UTF8StringBuilder(org.apache.hyracks.data.std.util.UTF8StringBuilder) Test(org.junit.Test)

Example 4 with GrowableArray

use of org.apache.hyracks.data.std.util.GrowableArray in project asterixdb by apache.

the class UTF8StringPointableTest method testInitCap.

@Test
public void testInitCap() throws Exception {
    UTF8StringBuilder builder = new UTF8StringBuilder();
    GrowableArray storage = new GrowableArray();
    UTF8StringPointable result = new UTF8StringPointable();
    UTF8StringPointable input = generateUTF8Pointable("this is it.i am;here.");
    input.initCap(builder, storage);
    result.set(storage.getByteArray(), 0, storage.getLength());
    UTF8StringPointable expected = generateUTF8Pointable("This Is It.I Am;Here.");
    assertEquals(0, expected.compareTo(result));
}
Also used : GrowableArray(org.apache.hyracks.data.std.util.GrowableArray) UTF8StringBuilder(org.apache.hyracks.data.std.util.UTF8StringBuilder) Test(org.junit.Test)

Example 5 with GrowableArray

use of org.apache.hyracks.data.std.util.GrowableArray in project asterixdb by apache.

the class UTF8StringPointableTest method testTrim.

@Test
public void testTrim() throws Exception {
    UTF8StringBuilder builder = new UTF8StringBuilder();
    GrowableArray storage = new GrowableArray();
    UTF8StringPointable result = new UTF8StringPointable();
    UTF8StringPointable input = generateUTF8Pointable("  this is it.i am;here.  ");
    // Trims both sides.
    input.trim(builder, storage, true, true, CharSet.getInstance(" "));
    result.set(storage.getByteArray(), 0, storage.getLength());
    UTF8StringPointable expected = generateUTF8Pointable("this is it.i am;here.");
    assertEquals(0, expected.compareTo(result));
    // Only trims the right side.
    storage.reset();
    input.trim(builder, storage, false, true, CharSet.getInstance(" "));
    result.set(storage.getByteArray(), 0, storage.getLength());
    expected = generateUTF8Pointable("  this is it.i am;here.");
    assertEquals(0, expected.compareTo(result));
    // Only trims the left side.
    storage.reset();
    input.trim(builder, storage, true, false, CharSet.getInstance(" "));
    result.set(storage.getByteArray(), 0, storage.getLength());
    expected = generateUTF8Pointable("this is it.i am;here.  ");
    assertEquals(0, expected.compareTo(result));
}
Also used : GrowableArray(org.apache.hyracks.data.std.util.GrowableArray) UTF8StringBuilder(org.apache.hyracks.data.std.util.UTF8StringBuilder) Test(org.junit.Test)

Aggregations

GrowableArray (org.apache.hyracks.data.std.util.GrowableArray)23 UTF8StringBuilder (org.apache.hyracks.data.std.util.UTF8StringBuilder)14 Test (org.junit.Test)12 ByteArrayInputStream (java.io.ByteArrayInputStream)7 DataInput (java.io.DataInput)7 DataInputStream (java.io.DataInputStream)7 IOException (java.io.IOException)6 DataOutput (java.io.DataOutput)5 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)5 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)5 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)5 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)5 IPointable (org.apache.hyracks.data.std.api.IPointable)5 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)5 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)5 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)5 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)4 UTF8StringPointable (org.apache.hyracks.data.std.primitive.UTF8StringPointable)4 ArrayList (java.util.ArrayList)3 RuntimeDataException (org.apache.asterix.common.exceptions.RuntimeDataException)2