Search in sources :

Example 1 with Tuple

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.");
}
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 Tuple

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);
    }
}
Also used : TupleIterator(org.vitrivr.cottontail.client.iterators.TupleIterator) StatusRuntimeException(io.grpc.StatusRuntimeException) ListSchemas(org.vitrivr.cottontail.client.language.ddl.ListSchemas) CreateSchema(org.vitrivr.cottontail.client.language.ddl.CreateSchema) Tuple(org.vitrivr.cottontail.client.iterators.Tuple)

Example 3 with Tuple

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;
}
Also used : PrimitiveTypeProvider(org.vitrivr.cineast.core.data.providers.primitive.PrimitiveTypeProvider) LinkedList(java.util.LinkedList) Tuple(org.vitrivr.cottontail.client.iterators.Tuple)

Example 4 with Tuple

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;
}
Also used : LinkedList(java.util.LinkedList) Tuple(org.vitrivr.cottontail.client.iterators.Tuple)

Example 5 with Tuple

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;
}
Also used : HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) PrimitiveTypeProvider(org.vitrivr.cineast.core.data.providers.primitive.PrimitiveTypeProvider) LinkedList(java.util.LinkedList) Tuple(org.vitrivr.cottontail.client.iterators.Tuple) StopWatch(org.apache.commons.lang3.time.StopWatch)

Aggregations

Tuple (org.vitrivr.cottontail.client.iterators.Tuple)7 LinkedList (java.util.LinkedList)5 TupleIterator (org.vitrivr.cottontail.client.iterators.TupleIterator)4 StatusRuntimeException (io.grpc.StatusRuntimeException)3 Query (org.vitrivr.cottontail.client.language.dql.Query)3 HashMap (java.util.HashMap)2 PrimitiveTypeProvider (org.vitrivr.cineast.core.data.providers.primitive.PrimitiveTypeProvider)2 LocalDateTime (java.time.LocalDateTime)1 Map (java.util.Map)1 StopWatch (org.apache.commons.lang3.time.StopWatch)1 MediaSegmentDescriptor (org.vitrivr.cineast.core.data.entities.MediaSegmentDescriptor)1 CottontailWrapper (org.vitrivr.cineast.core.db.cottontaildb.CottontailWrapper)1 CreateSchema (org.vitrivr.cottontail.client.language.ddl.CreateSchema)1 ListSchemas (org.vitrivr.cottontail.client.language.ddl.ListSchemas)1 Update (org.vitrivr.cottontail.client.language.dml.Update)1 Literal (org.vitrivr.cottontail.client.language.extensions.Literal)1 Literal (org.vitrivr.cottontail.grpc.CottontailGrpc.Literal)1