Search in sources :

Example 1 with IonTimestamp

use of com.amazon.ion.IonTimestamp in project amazon-qldb-dmv-sample-java by aws-samples.

the class RevisionMetadata method fromIon.

public static RevisionMetadata fromIon(final IonStruct ionStruct) {
    if (ionStruct == null) {
        throw new IllegalArgumentException("Metadata cannot be null");
    }
    try {
        IonString id = (IonString) ionStruct.get("id");
        IonInt version = (IonInt) ionStruct.get("version");
        IonTimestamp txTime = (IonTimestamp) ionStruct.get("txTime");
        IonString txId = (IonString) ionStruct.get("txId");
        if (id == null || version == null || txTime == null || txId == null) {
            throw new IllegalArgumentException("Document is missing required fields");
        }
        return new RevisionMetadata(id.stringValue(), version.longValue(), new Date(txTime.getMillis()), txId.stringValue());
    } catch (ClassCastException e) {
        log.error("Failed to parse ion document");
        throw new IllegalArgumentException("Document members are not of the correct type", e);
    }
}
Also used : IonString(com.amazon.ion.IonString) IonTimestamp(com.amazon.ion.IonTimestamp) IonInt(com.amazon.ion.IonInt) Date(java.util.Date)

Example 2 with IonTimestamp

use of com.amazon.ion.IonTimestamp in project ion-java by amzn.

the class IonJavaCli method compare.

private static boolean compare(CompareContext compareContext, int startI, int endI, int startJ, int endJ) throws IOException {
    List<Event> eventsFirst = compareContext.getEventStreamFirst();
    List<Event> eventsSecond = compareContext.getEventStreamSecond();
    int i = startI;
    int j = startJ;
    while (i <= endI && j <= endJ && i < eventsFirst.size() && j < eventsSecond.size()) {
        Event eventFirst = eventsFirst.get(i);
        Event eventSecond = eventsSecond.get(j);
        SymbolToken fieldNameFirst = eventFirst.getFieldName();
        SymbolToken fieldNameSecond = eventSecond.getFieldName();
        SymbolToken[] annotationFirst = eventFirst.getAnnotations();
        SymbolToken[] annotationSecond = eventSecond.getAnnotations();
        EventType eventTypeFirst = eventFirst.getEventType();
        EventType eventTypeSecond = eventSecond.getEventType();
        if (eventTypeFirst != eventTypeSecond) {
            setReportInfo(i, j, "Didn't match event_type", compareContext);
            return false;
        } else if (eventFirst.getDepth() != eventSecond.getDepth()) {
            setReportInfo(i, j, "Didn't match depth", compareContext);
            return false;
        } else if (eventFirst.getIonType() != eventSecond.getIonType()) {
            setReportInfo(i, j, "Didn't match ion_type", compareContext);
            return false;
        } else if (!isSameSymbolToken(fieldNameFirst, fieldNameSecond)) {
            setReportInfo(i, j, "Didn't match field_name", compareContext);
            return false;
        } else if (!isSameSymbolTokenArray(annotationFirst, annotationSecond)) {
            setReportInfo(i, j, "Didn't match annotation", compareContext);
            return false;
        }
        if (eventTypeFirst == EventType.CONTAINER_START && eventFirst.getIonType() == IonType.STRUCT) {
            int iStart = i;
            int jStart = j;
            ContainerContext containerContextFirst = new ContainerContext(i);
            IonStruct structFirst = parseStruct(containerContextFirst, compareContext, endI, true);
            i = containerContextFirst.getIndex();
            ContainerContext containerContextSecond = new ContainerContext(j);
            IonStruct structSecond = parseStruct(containerContextSecond, compareContext, endJ, false);
            j = containerContextSecond.getIndex();
            if (!Equivalence.ionEquals(structFirst, structSecond)) {
                setReportInfo(iStart, jStart, "Did not find matching field for " + structFirst.toString(), compareContext);
                return false;
            }
        } else if (eventTypeFirst == EventType.SCALAR) {
            boolean compareResult;
            if (compareContext.getType() == ComparisonType.EQUIVS_TIMELINE && eventFirst.getIonType() == IonType.TIMESTAMP) {
                IonTimestamp ionTimestampFirst = (IonTimestamp) eventFirst.getValue();
                IonTimestamp ionTimestampSecond = (IonTimestamp) eventSecond.getValue();
                compareResult = ionTimestampFirst.timestampValue().compareTo(ionTimestampSecond.timestampValue()) == 0;
            } else {
                compareResult = Equivalence.ionEquals(eventFirst.getValue(), eventSecond.getValue());
            }
            if (!compareResult) {
                setReportInfo(i, j, eventFirst.getValue() + " vs. " + eventSecond.getValue(), compareContext);
                return false;
            }
        }
        i++;
        j++;
    }
    if (i <= endI || j <= endJ) {
        setReportInfo(i, j, "two event streams have different size", compareContext);
        return false;
    }
    return true;
}
Also used : IonStruct(com.amazon.ion.IonStruct) SymbolToken(com.amazon.ion.SymbolToken) EventType(com.amazon.tools.events.EventType) IonTimestamp(com.amazon.ion.IonTimestamp) Event(com.amazon.tools.events.Event)

Example 3 with IonTimestamp

use of com.amazon.ion.IonTimestamp in project ion-java by amzn.

the class IonSystemLite method newUtcTimestampFromMillis.

public IonTimestamp newUtcTimestampFromMillis(long millis) {
    IonTimestamp result = newNullTimestamp();
    result.setMillisUtc(millis);
    return result;
}
Also used : IonTimestamp(com.amazon.ion.IonTimestamp)

Example 4 with IonTimestamp

use of com.amazon.ion.IonTimestamp in project ion-java by amzn.

the class ReverseBinaryEncoder method writeIonTimestampContent.

private void writeIonTimestampContent(IonTimestamp val) {
    if (val.isNullValue()) {
        writeByte((byte) (TYPE_TIMESTAMP | NULL_LENGTH_MASK));
    } else {
        final int originalOffset = myBuffer.length - myOffset;
        Timestamp t = val.timestampValue();
        // Time and date portion
        switch(t.getPrecision()) {
            // Fall through each case - by design
            case FRACTION:
            case SECOND:
                {
                    BigDecimal fraction = t.getZFractionalSecond();
                    if (fraction != null) {
                        assert (fraction.signum() >= 0 && !fraction.equals(BigDecimal.ZERO)) : "Bad timestamp fraction: " + fraction;
                        writeIonDecimalContent(fraction);
                    }
                    writeVarUInt(t.getZSecond());
                }
            case MINUTE:
                writeVarUInt(t.getZMinute());
                writeVarUInt(t.getZHour());
            case DAY:
                writeVarUInt(t.getZDay());
            case MONTH:
                writeVarUInt(t.getZMonth());
            case YEAR:
                writeVarUInt(t.getZYear());
                break;
            default:
                throw new IllegalStateException("unrecognized Timestamp precision: " + t.getPrecision());
        }
        // Offset portion
        Integer offset = t.getLocalOffset();
        if (offset == null) {
            // Negative 0 (no timezone)
            writeByte((byte) (0x80 | 0x40));
        } else {
            writeVarInt(offset.intValue());
        }
        writePrefix(TYPE_TIMESTAMP, myBuffer.length - myOffset - originalOffset);
    }
}
Also used : BigInteger(java.math.BigInteger) Timestamp(com.amazon.ion.Timestamp) IonTimestamp(com.amazon.ion.IonTimestamp) com.amazon.ion.impl._Private_IonConstants.tidTimestamp(com.amazon.ion.impl._Private_IonConstants.tidTimestamp) BigDecimal(java.math.BigDecimal)

Example 5 with IonTimestamp

use of com.amazon.ion.IonTimestamp in project ion-java by amzn.

the class Equivalence method ionCompareToImpl.

private static int ionCompareToImpl(final IonValue v1, final IonValue v2, final Configuration configuration) {
    int result = 0;
    if (v1 == null || v2 == null) {
        if (v1 != null)
            result = 1;
        if (v2 != null)
            result = -1;
        // otherwise v1 == v2 == null and result == 0
        return result;
    }
    // check type
    IonType ty1 = v1.getType();
    IonType ty2 = v2.getType();
    result = ty1.compareTo(ty2);
    if (result == 0) {
        boolean bo1 = v1.isNullValue();
        boolean bo2 = v2.isNullValue();
        if (bo1 || bo2) {
            // the same type
            if (!bo1)
                result = 1;
            if (!bo2)
                result = -1;
        // othersize they're equal (and null values)
        } else {
            // value compare only if both are not null
            switch(ty1) {
                case NULL:
                    // never visited, precondition is that both are not null
                    break;
                case BOOL:
                    if (((IonBool) v1).booleanValue()) {
                        result = ((IonBool) v2).booleanValue() ? 0 : 1;
                    } else {
                        result = ((IonBool) v2).booleanValue() ? -1 : 0;
                    }
                    break;
                case INT:
                    result = ((IonInt) v1).bigIntegerValue().compareTo(((IonInt) v2).bigIntegerValue());
                    break;
                case FLOAT:
                    double double1 = ((IonFloat) v1).doubleValue();
                    double double2 = ((IonFloat) v2).doubleValue();
                    if (configuration.epsilon != null && (double1 == double2 || Math.abs(double1 - double2) <= configuration.epsilon)) {
                        result = 0;
                    } else {
                        result = Double.compare(double1, double2);
                    }
                    break;
                case DECIMAL:
                    // TODO amzn/ion-java/issues/26
                    assert !PUBLIC_COMPARISON_API;
                    result = Decimal.equals(((IonDecimal) v1).decimalValue(), ((IonDecimal) v2).decimalValue()) ? 0 : 1;
                    break;
                case TIMESTAMP:
                    if (configuration.isStrict) {
                        // TODO amzn/ion-java/issues/26
                        assert !PUBLIC_COMPARISON_API;
                        result = (((IonTimestamp) v1).timestampValue().equals(((IonTimestamp) v2).timestampValue()) ? 0 : 1);
                    } else {
                        // This is kind of lying here, the 'strict' boolean
                        // (if false) denotes ONLY that annotations are not
                        // check for equality. But what this is doing here is
                        // that it is also ignoring IonTimesamps' precision and
                        // local offset.
                        result = ((IonTimestamp) v1).timestampValue().compareTo(((IonTimestamp) v2).timestampValue());
                    }
                    break;
                case STRING:
                    result = (((IonText) v1).stringValue()).compareTo(((IonText) v2).stringValue());
                    break;
                case SYMBOL:
                    result = compareSymbolTokens(((IonSymbol) v1).symbolValue(), ((IonSymbol) v2).symbolValue());
                    break;
                case BLOB:
                case CLOB:
                    result = compareLobContents((IonLob) v1, (IonLob) v2);
                    break;
                case STRUCT:
                    // TODO amzn/ion-java/issues/26
                    assert !PUBLIC_COMPARISON_API;
                    result = compareStructs((IonStruct) v1, (IonStruct) v2, configuration);
                    break;
                case LIST:
                case SEXP:
                case DATAGRAM:
                    result = compareSequences((IonSequence) v1, (IonSequence) v2, configuration);
                    break;
            }
        }
    }
    // comparison, then we check the annotations
    if ((result == 0) && configuration.isStrict) {
        // check tuple equality over the annotations
        // (which are symbol tokens)
        result = compareAnnotations(v1.getTypeAnnotationSymbols(), v2.getTypeAnnotationSymbols());
    }
    return result;
}
Also used : IonType(com.amazon.ion.IonType) IonInt(com.amazon.ion.IonInt) IonSequence(com.amazon.ion.IonSequence) IonBool(com.amazon.ion.IonBool) IonSymbol(com.amazon.ion.IonSymbol) IonText(com.amazon.ion.IonText) IonStruct(com.amazon.ion.IonStruct) IonTimestamp(com.amazon.ion.IonTimestamp) IonLob(com.amazon.ion.IonLob) IonFloat(com.amazon.ion.IonFloat)

Aggregations

IonTimestamp (com.amazon.ion.IonTimestamp)15 Timestamp (com.amazon.ion.Timestamp)5 Test (org.junit.Test)5 IonStruct (com.amazon.ion.IonStruct)4 IonObjectMapper (com.fasterxml.jackson.dataformat.ion.IonObjectMapper)4 Instant (java.time.Instant)4 IonInt (com.amazon.ion.IonInt)3 IonBlob (com.amazon.ion.IonBlob)2 IonBool (com.amazon.ion.IonBool)2 IonClob (com.amazon.ion.IonClob)2 IonFloat (com.amazon.ion.IonFloat)2 IonList (com.amazon.ion.IonList)2 IonSexp (com.amazon.ion.IonSexp)2 IonString (com.amazon.ion.IonString)2 IonSymbol (com.amazon.ion.IonSymbol)2 BlobTest (com.amazon.ion.BlobTest)1 ClobTest (com.amazon.ion.ClobTest)1 IntTest (com.amazon.ion.IntTest)1 IonDecimal (com.amazon.ion.IonDecimal)1 IonException (com.amazon.ion.IonException)1