use of com.torodb.kvdocument.values.KvValue in project torodb by torodb.
the class SqlTorodTransaction method findByAttRefInProjection.
@Override
public Cursor<Tuple2<Integer, KvValue<?>>> findByAttRefInProjection(String dbName, String colName, AttributeReference attRef, Collection<KvValue<?>> values) {
MetaDatabase db = getInternalTransaction().getMetaSnapshot().getMetaDatabaseByName(dbName);
if (db == null) {
LOGGER.trace("Db with name " + dbName + " does not exist. An empty cursor is returned");
return new EmptyCursor<>();
}
MetaCollection col = db.getMetaCollectionByName(colName);
if (col == null) {
LOGGER.trace("Collection " + dbName + '.' + colName + " does not exist. An empty cursor is returned");
return new EmptyCursor<>();
}
if (values.isEmpty()) {
LOGGER.trace("An empty list of values have been given as in condition. An empty cursor is returned");
return new EmptyCursor<>();
}
TableRef ref = extractTableRef(attRef);
String lastKey = extractKeyName(attRef.getKeys().get(attRef.getKeys().size() - 1));
MetaDocPart docPart = col.getMetaDocPartByTableRef(ref);
if (docPart == null) {
LOGGER.trace("DocPart " + dbName + '.' + colName + '.' + ref + " does not exist. An empty cursor is returned");
return new EmptyCursor<>();
}
Multimap<MetaField, KvValue<?>> valuesMap = ArrayListMultimap.create();
for (KvValue<?> value : values) {
MetaField field = docPart.getMetaFieldByNameAndType(lastKey, FieldType.from(value.getType()));
if (field != null) {
valuesMap.put(field, value);
}
}
return getInternalTransaction().getBackendTransaction().findByFieldInProjection(db, col, docPart, valuesMap);
}
use of com.torodb.kvdocument.values.KvValue in project torodb by torodb.
the class AbstractReadInterface method getCollectionDidsAndProjectionWithFieldsInBatch.
@SuppressFBWarnings(value = { "OBL_UNSATISFIED_OBLIGATION", "ODR_OPEN_DATABASE_RESOURCE" }, justification = "ResultSet is wrapped in a Cursor<Tuple2<Integer, KVValue<?>>>. " + "It's iterated and closed in caller code")
private Cursor<Tuple2<Integer, KvValue<?>>> getCollectionDidsAndProjectionWithFieldsInBatch(DSLContext dsl, MetaDatabase metaDatabase, MetaCollection metaCol, MetaDocPart metaDocPart, MetaField metaField, Collection<KvValue<?>> values) throws SQLException {
String statement = getReadCollectionDidsAndProjectionWithFieldInStatement(metaDatabase.getIdentifier(), metaDocPart.getIdentifier(), metaField.getIdentifier(), values.size());
Connection connection = dsl.configuration().connectionProvider().acquire();
try {
PreparedStatement preparedStatement = connection.prepareStatement(statement);
int parameterIndex = 1;
for (KvValue<?> value : values) {
sqlHelper.setPreparedStatementValue(preparedStatement, parameterIndex, metaField.getType(), value);
parameterIndex++;
}
return new AbstractCursor<Tuple2<Integer, KvValue<?>>>(errorHandler, preparedStatement.executeQuery()) {
@Override
protected Tuple2<Integer, KvValue<?>> read(ResultSet resultSet) throws SQLException {
return new Tuple2<>(resultSet.getInt(1), sqlHelper.getResultSetKvValue(metaField.getType(), dataTypeProvider.getDataType(metaField.getType()), resultSet, 2));
}
};
} finally {
dsl.configuration().connectionProvider().release(connection);
}
}
use of com.torodb.kvdocument.values.KvValue in project torodb by torodb.
the class AbstractReadInterface method getCollectionDidsWithFieldsInBatch.
@SuppressFBWarnings(value = { "OBL_UNSATISFIED_OBLIGATION", "ODR_OPEN_DATABASE_RESOURCE" }, justification = "ResultSet is wrapped in a Cursor<Integer>. It's iterated and closed in caller code")
private Cursor<Integer> getCollectionDidsWithFieldsInBatch(DSLContext dsl, MetaDatabase metaDatabase, MetaCollection metaCol, MetaDocPart metaDocPart, Multimap<MetaField, KvValue<?>> valuesMultimap) throws SQLException {
@SuppressWarnings("checkstyle:LineLength") Provider<Stream<Map.Entry<MetaField, Collection<KvValue<?>>>>> valuesMultimapSortedStreamProvider = () -> valuesMultimap.asMap().entrySet().stream().sorted((e1, e2) -> e1.getKey().getIdentifier().compareTo(e2.getKey().getIdentifier()));
String statement = getReadCollectionDidsWithFieldInStatement(metaDatabase.getIdentifier(), metaDocPart.getIdentifier(), valuesMultimapSortedStreamProvider.get().map(e -> new Tuple2<String, Integer>(e.getKey().getIdentifier(), e.getValue().size())));
Connection connection = dsl.configuration().connectionProvider().acquire();
try {
PreparedStatement preparedStatement = connection.prepareStatement(statement);
int parameterIndex = 1;
Iterator<Map.Entry<MetaField, Collection<KvValue<?>>>> valuesMultimapSortedIterator = valuesMultimapSortedStreamProvider.get().iterator();
while (valuesMultimapSortedIterator.hasNext()) {
Map.Entry<MetaField, Collection<KvValue<?>>> valuesMultimapEntry = valuesMultimapSortedIterator.next();
for (KvValue<?> value : valuesMultimapEntry.getValue()) {
sqlHelper.setPreparedStatementValue(preparedStatement, parameterIndex, valuesMultimapEntry.getKey().getType(), value);
parameterIndex++;
}
}
return new DefaultDidCursor(errorHandler, preparedStatement.executeQuery());
} finally {
dsl.configuration().connectionProvider().release(connection);
}
}
use of com.torodb.kvdocument.values.KvValue in project torodb by torodb.
the class MapToKvValueConverter method convertMap.
private KvValue<?> convertMap(Map<String, Object> source) {
if (isSpecialObject(source)) {
return buildSpecialObject(source);
}
LinkedHashMap<String, KvValue<?>> docMap = new LinkedHashMap<>();
source.forEach((key, value) -> {
String interned = key.intern();
docMap.put(interned, convertValue(value));
});
return new MapKvDocument(docMap);
}
Aggregations