Search in sources :

Example 6 with IonStruct

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

the class TransferVehicleOwnership method findPrimaryOwnerForVehicle.

/**
 * Find the primary owner for the given VIN.
 *
 * @param txn
 *              The {@link TransactionExecutor} for lambda execute.
 * @param vin
 *              Unique VIN for a vehicle.
 * @return a {@link Person} object.
 * @throws IllegalStateException if failed to convert parameter into {@link IonValue}.
 */
public static Person findPrimaryOwnerForVehicle(final TransactionExecutor txn, final String vin) {
    try {
        log.info("Finding primary owner for vehicle with Vin: {}...", vin);
        final String query = "SELECT Owners.PrimaryOwner.PersonId FROM VehicleRegistration AS v WHERE v.VIN = ?";
        final List<IonValue> parameters = Collections.singletonList(Constants.MAPPER.writeValueAsIonValue(vin));
        Result result = txn.execute(query, parameters);
        final List<IonStruct> documents = ScanTable.toIonStructs(result);
        ScanTable.printDocuments(documents);
        if (documents.isEmpty()) {
            throw new IllegalStateException("Unable to find registrations with VIN: " + vin);
        }
        final IonReader reader = IonReaderBuilder.standard().build(documents.get(0));
        final String personId = Constants.MAPPER.readValue(reader, LinkedHashMap.class).get("PersonId").toString();
        return findPersonFromDocumentId(txn, personId);
    } catch (IOException ioe) {
        throw new IllegalStateException(ioe);
    }
}
Also used : IonValue(com.amazon.ion.IonValue) IonStruct(com.amazon.ion.IonStruct) IonReader(com.amazon.ion.IonReader) IOException(java.io.IOException) Result(software.amazon.qldb.Result)

Example 7 with IonStruct

use of com.amazon.ion.IonStruct 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 8 with IonStruct

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

the class SharedSymbolTableTest method testMalformedSymbolEntry.

public void testMalformedSymbolEntry(String symbolValue) {
    IonStruct s = sharedSymtabStruct(system(), "ST", 5);
    IonValue entry = system().singleValue(symbolValue);
    s.put(SystemSymbols.SYMBOLS).newList(entry);
    SymbolTable st = myMaker.newSharedSymtab(system(), s);
    checkSharedTable("ST", 5, new String[] { null }, st);
    assertEquals(1, st.getMaxId());
    checkUnknownSymbol(1, st);
}
Also used : IonValue(com.amazon.ion.IonValue) IonStruct(com.amazon.ion.IonStruct) SymbolTable(com.amazon.ion.SymbolTable)

Example 9 with IonStruct

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

the class SharedSymbolTableTest method testMalformedName.

public void testMalformedName(String nameValue) {
    IonStruct s = sharedSymtabStruct(system(), "dummy", 1, "x");
    putParsedValue(s, SystemSymbols.NAME, nameValue);
    try {
        myMaker.newSharedSymtab(system(), s);
        fail("Expected exception");
    } catch (IonException e) {
        assertTrue(e.getMessage().contains("'name'"));
    }
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonException(com.amazon.ion.IonException)

Example 10 with IonStruct

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

the class SharedSymbolTableTest method testMalformedSymbols.

public void testMalformedSymbols(String symbolValue) {
    IonStruct s = sharedSymtabStruct(system(), "ST", 5);
    putParsedValue(s, SystemSymbols.SYMBOLS, symbolValue);
    SymbolTable st = myMaker.newSharedSymtab(system(), s);
    checkSharedTable("ST", 5, EMPTY_STRING_ARRAY, st);
}
Also used : IonStruct(com.amazon.ion.IonStruct) SymbolTable(com.amazon.ion.SymbolTable)

Aggregations

IonStruct (com.amazon.ion.IonStruct)68 Test (org.junit.Test)28 IonValue (com.amazon.ion.IonValue)18 IonDatagram (com.amazon.ion.IonDatagram)13 IonList (com.amazon.ion.IonList)13 IonReader (com.amazon.ion.IonReader)13 SymbolTable (com.amazon.ion.SymbolTable)13 IOException (java.io.IOException)8 IonString (com.amazon.ion.IonString)7 IonSystem (com.amazon.ion.IonSystem)6 IonException (com.amazon.ion.IonException)5 IonSequence (com.amazon.ion.IonSequence)5 IonType (com.amazon.ion.IonType)5 IonFloat (com.amazon.ion.IonFloat)4 IonSymbol (com.amazon.ion.IonSymbol)4 IonTimestamp (com.amazon.ion.IonTimestamp)4 IonWriter (com.amazon.ion.IonWriter)4 SymbolToken (com.amazon.ion.SymbolToken)4 ArrayList (java.util.ArrayList)4 IonBlob (com.amazon.ion.IonBlob)3