use of org.vitrivr.cottontail.client.iterators.Tuple in project cineast by vitrivr.
the class LSC21TemporalUpdateCommand method run.
@Override
@SuppressWarnings("unchecked")
public void run() {
if (Config.sharedConfig().getDatabase().getSelector() != DataSource.COTTONTAIL) {
System.out.println("Other DB than Cottontail DB not supported (yet). Aborting");
return;
}
/* Preparation. */
final CottontailWrapper cottontail = new CottontailWrapper(Config.sharedConfig().getDatabase().getHost(), Config.sharedConfig().getDatabase().getPort());
long txId = cottontail.client.begin();
final Query query = new Query(ENTITY_NAME).select("*", null).txId(txId);
final TupleIterator ti = cottontail.client.query(query);
final LinkedList<Update> updates = new LinkedList<>();
int counter = 0;
int totalCounter = 0;
/* Prepare updates. */
while (ti.hasNext()) {
final Tuple t = ti.next();
final MediaSegmentDescriptor segment = convert(t);
try {
final Optional<LocalDateTime> oldt = extractTimeInformation(segment.getSegmentId());
if (!oldt.isPresent()) {
continue;
}
final LocalDateTime ldt = oldt.get();
final long msOfTheDay = ldt.getHour() * 60 * 60 * 1000 + ldt.getMinute() * 60 * 1000 + ldt.getSecond() * 1000;
final long sAbs = ldt.toInstant(ZoneOffset.UTC).toEpochMilli() / 1000L;
final Update update = new Update(ENTITY_NAME).values(new Pair<>(MediaSegmentDescriptor.SEGMENT_START_COL_NAME, (double) msOfTheDay), new Pair<>(MediaSegmentDescriptor.SEGMENT_END_COL_NAME, (double) msOfTheDay + 1), new Pair<>(MediaSegmentDescriptor.SEGMENT_STARTABS_COL_NAME, resetAbsTime ? 0.0 : sAbs), new Pair<>(MediaSegmentDescriptor.SEGMENT_ENDABS_COL_NAME, resetAbsTime ? 0.0 : (sAbs + 1))).where(new org.vitrivr.cottontail.client.language.extensions.Literal(CineastConstants.SEGMENT_ID_COLUMN_QUALIFIER, "=", segment.getSegmentId())).txId(txId);
updates.add(update);
} catch (Exception e) {
LOGGER.warn("Could not update " + segment.getSegmentId() + " due to exception: " + e.getMessage());
}
}
/* Execute updates. */
Update update;
while ((update = updates.poll()) != null) {
cottontail.client.update(update);
totalCounter++;
if (counter++ > 99) {
if (progress) {
System.out.println("Updated " + totalCounter + " rows.");
}
counter = 0;
}
}
cottontail.client.commit(txId);
System.out.println("Done.");
}
use of org.vitrivr.cottontail.client.iterators.Tuple in project cineast by vitrivr.
the class CottontailEntityCreator method init.
/**
* Makes sure that schema 'cineast' is available.
*/
private void init() {
final long txId = this.cottontail.client.begin();
try {
final ListSchemas list = new ListSchemas().txId(txId);
final TupleIterator iterator = this.cottontail.client.list(list);
boolean exists = false;
while (iterator.hasNext()) {
Tuple next = iterator.next();
if (Objects.equals(next.asString(Constants.COLUMN_NAME_DBO), CottontailWrapper.FQN_CINEAST_SCHEMA)) {
exists = true;
break;
}
}
if (!exists) {
this.cottontail.client.create(new CreateSchema(CottontailWrapper.CINEAST_SCHEMA).txId(txId));
}
this.cottontail.client.commit(txId);
} catch (StatusRuntimeException e) {
LOGGER.error("Error during initialization", e);
this.cottontail.client.rollback(txId);
}
}
use of org.vitrivr.cottontail.client.iterators.Tuple in project cineast by vitrivr.
the class CottontailSelector method toSingleCol.
/**
* Converts a {@link TupleIterator} response generated by Cottontail DB into a {@link List} {@link PrimitiveTypeProvider}s that contain the results of a single column.
*
* @param results {@link TupleIterator} to gather the results from.
* @param colName The name of the column that should be collected.
* @return {@link List} of {@link Map}s that contains the results.
*/
private List<PrimitiveTypeProvider> toSingleCol(TupleIterator results, String colName) {
final List<PrimitiveTypeProvider> _return = new LinkedList<>();
while (results.hasNext()) {
final Tuple t = results.next();
_return.add(PrimitiveTypeProvider.fromObject(t.get(colName)));
}
return _return;
}
use of org.vitrivr.cottontail.client.iterators.Tuple in project cineast by vitrivr.
the class CottontailSelector method handleNearestNeighbourResponse.
/**
* Converts a {@link TupleIterator} response generated by Cottontail DB into a {@link List} of {@link DistanceElement}s.
*
* @param response {@link TupleIterator} to gather the results from.
* @param distanceElementClass The type of {@link DistanceElement} to create.
* @return {@link List} of {@link DistanceElement}s.
*/
private static <T extends DistanceElement> List<T> handleNearestNeighbourResponse(TupleIterator response, Class<? extends T> distanceElementClass) {
final List<T> result = new LinkedList<>();
while (response.hasNext()) {
try {
final Tuple t = response.next();
final String id = t.asString(GENERIC_ID_COLUMN_QUALIFIER);
double distance = t.asDouble(DB_DISTANCE_VALUE_QUALIFIER);
/* This should be fine. */
T e = DistanceElement.create(distanceElementClass, id, distance);
result.add(e);
} catch (NullPointerException e) {
LOGGER.warn("Encountered null entry (id, distance) is nearest neighbor search response!");
}
}
return result;
}
use of org.vitrivr.cottontail.client.iterators.Tuple in project cineast by vitrivr.
the class CottontailSelector method processResults.
private static List<Map<String, PrimitiveTypeProvider>> processResults(TupleIterator results, Map<String, String> mappings) {
final List<Map<String, PrimitiveTypeProvider>> _return = new LinkedList<>();
final StopWatch watch = StopWatch.createStarted();
final Collection<String> columns = results.getSimpleNames();
while (results.hasNext()) {
final Tuple t = results.next();
final Map<String, PrimitiveTypeProvider> map = new HashMap<>(results.getNumberOfColumns());
for (String c : columns) {
map.put(mappings.getOrDefault(c, c), PrimitiveTypeProvider.fromObject(t.get(c)));
}
_return.add(map);
}
LOGGER.trace("Processed {} results in {} ms", _return.size(), watch.getTime(TimeUnit.MILLISECONDS));
return _return;
}
Aggregations