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