use of org.vitrivr.cottontail.client.language.dql.Query 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.language.dql.Query in project cineast by vitrivr.
the class CottontailSelector method getFulltextRows.
@Override
public List<Map<String, PrimitiveTypeProvider>> getFulltextRows(int rows, String fieldname, ReadableQueryConfig queryConfig, String... terms) {
/* Prepare plain query. */
final String predicate = Arrays.stream(terms).map(String::trim).collect(Collectors.joining(" OR "));
final Query query = new Query(this.fqn).select("*", null).fulltext(fieldname, predicate, DB_DISTANCE_VALUE_QUALIFIER);
/* Process predicates. */
if (queryConfig != null && !queryConfig.getRelevantSegmentIds().isEmpty()) {
final Set<String> relevant = queryConfig.getRelevantSegmentIds();
final Literal segmentIds = new Literal(GENERIC_ID_COLUMN_QUALIFIER, "IN", relevant.toArray());
query.where(new And(segmentIds, new Literal(DB_DISTANCE_VALUE_QUALIFIER, ">", 0.0)));
} else {
query.where(new Literal(DB_DISTANCE_VALUE_QUALIFIER, ">", 0.0));
}
try {
return processResults(this.cottontail.client.query(query));
} catch (StatusRuntimeException e) {
LOGGER.warn("Error occurred during query execution in getFulltextRows(): {}", e.getMessage());
return new ArrayList<>(0);
}
}
use of org.vitrivr.cottontail.client.language.dql.Query in project cineast by vitrivr.
the class CottontailSelector method getMetadataByIdAndSpec.
@Override
public List<Map<String, PrimitiveTypeProvider>> getMetadataByIdAndSpec(List<String> ids, List<MetadataAccessSpecification> spec, String idColName) {
final Query query = new Query(this.fqn).select("*", null);
final Optional<Predicate> predicates = generateQueryFromMetadataSpec(spec);
final Literal segmentIds = new Literal(idColName, "IN", ids.toArray());
if (predicates.isPresent()) {
query.where(new And(segmentIds, predicates.get()));
} else {
query.where(segmentIds);
}
return processResults(this.cottontail.client.query(query));
}
use of org.vitrivr.cottontail.client.language.dql.Query in project cineast by vitrivr.
the class CottontailWriter method exists.
@Override
public boolean exists(String key, String value) {
final Query query = new Query(this.fqn).exists().where(new Literal(key, "=", value));
final TupleIterator results = this.cottontail.client.query(query);
final Boolean b = results.next().asBoolean("exists");
if (b != null) {
return b;
} else {
throw new IllegalArgumentException("Unexpected value in result set.");
}
}
use of org.vitrivr.cottontail.client.language.dql.Query in project cineast by vitrivr.
the class CottontailSelector method getRows.
@Override
public List<Map<String, PrimitiveTypeProvider>> getRows(String fieldName, RelationalOperator operator, Iterable<PrimitiveTypeProvider> values) {
final Object[] mapped = StreamSupport.stream(values.spliterator(), false).map(PrimitiveTypeProvider::toObject).toArray();
final String op = toOperator(operator);
final Query query = new Query(this.fqn).select("*", null).where(new Literal(fieldName, op, mapped));
try {
return processResults(this.cottontail.client.query(query));
} catch (StatusRuntimeException e) {
LOGGER.warn("Error occurred during query execution in getRows(): {}", e.getMessage());
return new ArrayList<>(0);
}
}
Aggregations