Search in sources :

Example 1 with Query

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.");
}
Also used : LocalDateTime(java.time.LocalDateTime) CottontailWrapper(org.vitrivr.cineast.core.db.cottontaildb.CottontailWrapper) Query(org.vitrivr.cottontail.client.language.dql.Query) Update(org.vitrivr.cottontail.client.language.dml.Update) LinkedList(java.util.LinkedList) Literal(org.vitrivr.cottontail.grpc.CottontailGrpc.Literal) TupleIterator(org.vitrivr.cottontail.client.iterators.TupleIterator) MediaSegmentDescriptor(org.vitrivr.cineast.core.data.entities.MediaSegmentDescriptor) Tuple(org.vitrivr.cottontail.client.iterators.Tuple)

Example 2 with Query

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);
    }
}
Also used : Query(org.vitrivr.cottontail.client.language.dql.Query) And(org.vitrivr.cottontail.client.language.extensions.And) Literal(org.vitrivr.cottontail.client.language.extensions.Literal) StatusRuntimeException(io.grpc.StatusRuntimeException)

Example 3 with Query

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));
}
Also used : Query(org.vitrivr.cottontail.client.language.dql.Query) And(org.vitrivr.cottontail.client.language.extensions.And) Literal(org.vitrivr.cottontail.client.language.extensions.Literal) Predicate(org.vitrivr.cottontail.client.language.extensions.Predicate)

Example 4 with 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.");
    }
}
Also used : Query(org.vitrivr.cottontail.client.language.dql.Query) Literal(org.vitrivr.cottontail.client.language.extensions.Literal) TupleIterator(org.vitrivr.cottontail.client.iterators.TupleIterator)

Example 5 with Query

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);
    }
}
Also used : Query(org.vitrivr.cottontail.client.language.dql.Query) Literal(org.vitrivr.cottontail.client.language.extensions.Literal) StatusRuntimeException(io.grpc.StatusRuntimeException)

Aggregations

Query (org.vitrivr.cottontail.client.language.dql.Query)12 Literal (org.vitrivr.cottontail.client.language.extensions.Literal)9 StatusRuntimeException (io.grpc.StatusRuntimeException)7 TupleIterator (org.vitrivr.cottontail.client.iterators.TupleIterator)5 Tuple (org.vitrivr.cottontail.client.iterators.Tuple)4 LinkedList (java.util.LinkedList)3 And (org.vitrivr.cottontail.client.language.extensions.And)3 Predicate (org.vitrivr.cottontail.client.language.extensions.Predicate)3 HashMap (java.util.HashMap)2 Distances (org.vitrivr.cottontail.client.language.basics.Distances)2 Code (io.grpc.Status.Code)1 LocalDateTime (java.time.LocalDateTime)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 TimeUnit (java.util.concurrent.TimeUnit)1