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);
}
}
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;
}
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);
}
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'"));
}
}
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);
}
Aggregations