Search in sources :

Example 1 with TupleIterator

use of org.vitrivr.cottontail.client.iterators.TupleIterator 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 TupleIterator

use of org.vitrivr.cottontail.client.iterators.TupleIterator 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 TupleIterator

use of org.vitrivr.cottontail.client.iterators.TupleIterator 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 4 with TupleIterator

use of org.vitrivr.cottontail.client.iterators.TupleIterator in project cineast by vitrivr.

the class CottontailSelector method countDistinctValues.

public Map<String, Integer> countDistinctValues(String column) {
    final Query query = new Query(this.fqn).select(column, null);
    final Map<String, Integer> count = new HashMap<>();
    try {
        final TupleIterator results = this.cottontail.client.query(query);
        while (results.hasNext()) {
            final Tuple t = results.next();
            count.merge(t.asString(column), 1, (old, one) -> old + 1);
        }
        return count;
    } catch (StatusRuntimeException e) {
        LOGGER.warn("Error occurred during query execution in countDistinctValues(): {}", e.getMessage());
        return new HashMap<>(0);
    }
}
Also used : Query(org.vitrivr.cottontail.client.language.dql.Query) HashMap(java.util.HashMap) TupleIterator(org.vitrivr.cottontail.client.iterators.TupleIterator) StatusRuntimeException(io.grpc.StatusRuntimeException) Tuple(org.vitrivr.cottontail.client.iterators.Tuple)

Example 5 with TupleIterator

use of org.vitrivr.cottontail.client.iterators.TupleIterator in project cineast by vitrivr.

the class CottontailSelector method getFeatureVectors.

@Override
public List<float[]> getFeatureVectors(String fieldName, PrimitiveTypeProvider value, String vectorName) {
    final Query query = new Query(this.fqn).select(vectorName, null).where(new Literal(fieldName, "==", value.toObject()));
    try {
        final TupleIterator results = this.cottontail.client.query(query);
        final List<float[]> _return = new LinkedList<>();
        while (results.hasNext()) {
            final Tuple t = results.next();
            _return.add(t.asFloatVector(vectorName));
        }
        return _return;
    } catch (StatusRuntimeException e) {
        LOGGER.warn("Error occurred during query execution in getFeatureVectors(): {}", e.getMessage());
        return new ArrayList<>(0);
    }
}
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) StatusRuntimeException(io.grpc.StatusRuntimeException) LinkedList(java.util.LinkedList) Tuple(org.vitrivr.cottontail.client.iterators.Tuple)

Aggregations

TupleIterator (org.vitrivr.cottontail.client.iterators.TupleIterator)5 Tuple (org.vitrivr.cottontail.client.iterators.Tuple)4 Query (org.vitrivr.cottontail.client.language.dql.Query)4 StatusRuntimeException (io.grpc.StatusRuntimeException)3 LinkedList (java.util.LinkedList)2 Literal (org.vitrivr.cottontail.client.language.extensions.Literal)2 LocalDateTime (java.time.LocalDateTime)1 HashMap (java.util.HashMap)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.grpc.CottontailGrpc.Literal)1