Search in sources :

Example 1 with Extractors

use of com.hazelcast.query.impl.getters.Extractors in project hazelcast by hazelcast.

the class JsonQueryTargetDescriptorTest method test_create.

@Test
@Parameters({ "true", "false" })
public void test_create(boolean key) {
    Extractors extractors = Extractors.newBuilder(SERIALIZATION_SERVICE).build();
    JsonQueryTargetDescriptor descriptor = JsonQueryTargetDescriptor.INSTANCE;
    // when
    QueryTarget target = descriptor.create(SERIALIZATION_SERVICE, extractors, key);
    // then
    assertThat(target).isInstanceOf(JsonQueryTarget.class);
}
Also used : QueryTarget(com.hazelcast.sql.impl.extract.QueryTarget) Extractors(com.hazelcast.query.impl.getters.Extractors) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 2 with Extractors

use of com.hazelcast.query.impl.getters.Extractors in project hazelcast by hazelcast.

the class HazelcastJsonQueryTargetTest method test_get.

@Test
@Parameters(method = "values")
public void test_get(Object value) {
    Extractors extractors = Extractors.newBuilder(SERIALIZATION_SERVICE).build();
    QueryTarget target = new HazelcastJsonQueryTarget(SERIALIZATION_SERVICE, extractors, true);
    QueryExtractor topExtractor = target.createExtractor(null, OBJECT);
    QueryExtractor nonExistingExtractor = target.createExtractor("nonExisting", OBJECT);
    QueryExtractor stringExtractor = target.createExtractor("string", VARCHAR);
    QueryExtractor booleanExtractor = target.createExtractor("boolean", BOOLEAN);
    QueryExtractor byteExtractor = target.createExtractor("byte", TINYINT);
    QueryExtractor shortExtractor = target.createExtractor("short", SMALLINT);
    QueryExtractor intExtractor = target.createExtractor("int", INT);
    QueryExtractor longExtractor = target.createExtractor("long", BIGINT);
    QueryExtractor floatExtractor = target.createExtractor("float", REAL);
    QueryExtractor doubleExtractor = target.createExtractor("double", DOUBLE);
    QueryExtractor decimalExtractor = target.createExtractor("decimal", DECIMAL);
    QueryExtractor timeExtractor = target.createExtractor("time", TIME);
    QueryExtractor dateExtractor = target.createExtractor("date", DATE);
    QueryExtractor timestampExtractor = target.createExtractor("timestamp", TIMESTAMP);
    QueryExtractor timestampZoneExtractor = target.createExtractor("timestampTz", TIMESTAMP_WITH_TZ_OFFSET_DATE_TIME);
    QueryExtractor nullExtractor = target.createExtractor("null", OBJECT);
    QueryExtractor objectExtractor = target.createExtractor("object", OBJECT);
    target.setTarget(value, null);
    assertThat(topExtractor.get()).isInstanceOf(HazelcastJsonValue.class);
    assertThat(nonExistingExtractor.get()).isNull();
    assertThat(stringExtractor.get()).isEqualTo("string");
    assertThat(booleanExtractor.get()).isEqualTo(true);
    assertThat(byteExtractor.get()).isEqualTo((byte) 127);
    assertThat(shortExtractor.get()).isEqualTo((short) 32767);
    assertThat(intExtractor.get()).isEqualTo(2147483647);
    assertThat(longExtractor.get()).isEqualTo(9223372036854775807L);
    assertThat(floatExtractor.get()).isEqualTo(1234567890.1F);
    assertThat(doubleExtractor.get()).isEqualTo(123451234567890.1D);
    assertThat(decimalExtractor.get()).isEqualTo(new BigDecimal("9223372036854775.123"));
    assertThat(timeExtractor.get()).isEqualTo(LocalTime.of(12, 23, 34));
    assertThat(dateExtractor.get()).isEqualTo(LocalDate.of(2020, 9, 9));
    assertThat(timestampExtractor.get()).isEqualTo(LocalDateTime.of(2020, 9, 9, 12, 23, 34, 100_000_000));
    assertThat(timestampZoneExtractor.get()).isEqualTo(OffsetDateTime.of(2020, 9, 9, 12, 23, 34, 200_000_000, UTC));
    assertThat(nullExtractor.get()).isNull();
    assertThat(objectExtractor.get()).isNotNull();
}
Also used : QueryTarget(com.hazelcast.sql.impl.extract.QueryTarget) Extractors(com.hazelcast.query.impl.getters.Extractors) BigDecimal(java.math.BigDecimal) QueryExtractor(com.hazelcast.sql.impl.extract.QueryExtractor) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 3 with Extractors

use of com.hazelcast.query.impl.getters.Extractors in project hazelcast by hazelcast.

the class PartitionScanRunner method run.

/**
 * Executes the predicate on a partition chunk. The offset in the partition
 * is defined by the {@code pointers} and the soft limit is defined by the
 * {@code fetchSize}. The method returns the matched entries and updated
 * pointers from which new entries can be fetched which allows for efficient
 * iteration of query results.
 * <p>
 * <b>NOTE</b>
 * The iteration may be done when the map is being mutated or when there are
 * membership changes. The iterator does not reflect the state when it has
 * been constructed - it may return some entries that were added after the
 * iteration has started and may not return some entries that were removed
 * after iteration has started.
 * The iterator will not, however, skip an entry if it has not been changed
 * and will not return an entry twice.
 *
 * @param mapName     the map name
 * @param predicate   the predicate which the entries must match
 * @param partitionId the partition which is queried
 * @param pointers    the pointers defining the state of iteration
 * @param fetchSize   the soft limit for the number of entries to fetch
 * @return entries matching the predicate and a table index from which new
 * entries can be fetched
 */
public QueryableEntriesSegment run(String mapName, Predicate predicate, int partitionId, IterationPointer[] pointers, int fetchSize) {
    List<QueryableEntry> resultList = new LinkedList<>();
    PartitionContainer partitionContainer = mapServiceContext.getPartitionContainer(partitionId);
    RecordStore recordStore = partitionContainer.getRecordStore(mapName);
    Extractors extractors = mapServiceContext.getExtractors(mapName);
    while (resultList.size() < fetchSize && pointers[pointers.length - 1].getIndex() >= 0) {
        MapEntriesWithCursor cursor = recordStore.fetchEntries(pointers, fetchSize - resultList.size());
        pointers = cursor.getIterationPointers();
        Collection<? extends Entry<Data, Data>> entries = cursor.getBatch();
        if (entries.isEmpty()) {
            break;
        }
        for (Entry<Data, Data> entry : entries) {
            QueryableEntry queryEntry = new LazyMapEntry(entry.getKey(), entry.getValue(), ss, extractors);
            if (predicate.apply(queryEntry)) {
                resultList.add(queryEntry);
            }
        }
    }
    return new QueryableEntriesSegment(resultList, pointers);
}
Also used : Extractors(com.hazelcast.query.impl.getters.Extractors) PartitionContainer(com.hazelcast.map.impl.PartitionContainer) RecordStore(com.hazelcast.map.impl.recordstore.RecordStore) MapEntriesWithCursor(com.hazelcast.map.impl.iterator.MapEntriesWithCursor) LazyMapEntry(com.hazelcast.map.impl.LazyMapEntry) ToHeapDataConverter.toHeapData(com.hazelcast.internal.util.ToHeapDataConverter.toHeapData) Data(com.hazelcast.internal.serialization.Data) QueryableEntriesSegment(com.hazelcast.query.impl.QueryableEntriesSegment) QueryableEntry(com.hazelcast.query.impl.QueryableEntry) LinkedList(java.util.LinkedList)

Example 4 with Extractors

use of com.hazelcast.query.impl.getters.Extractors in project hazelcast by hazelcast.

the class PartitionScanRunner method run.

@SuppressWarnings("unchecked")
public void run(String mapName, Predicate predicate, int partitionId, Result result) {
    PagingPredicateImpl pagingPredicate = predicate instanceof PagingPredicateImpl ? (PagingPredicateImpl) predicate : null;
    PartitionContainer partitionContainer = mapServiceContext.getPartitionContainer(partitionId);
    MapContainer mapContainer = mapServiceContext.getMapContainer(mapName);
    RecordStore<Record> recordStore = partitionContainer.getRecordStore(mapName);
    boolean nativeMemory = recordStore.getInMemoryFormat() == InMemoryFormat.NATIVE;
    boolean useCachedValues = isUseCachedDeserializedValuesEnabled(mapContainer, partitionId);
    Extractors extractors = mapServiceContext.getExtractors(mapName);
    Map.Entry<Integer, Map.Entry> nearestAnchorEntry = pagingPredicate == null ? null : pagingPredicate.getNearestAnchorEntry();
    recordStore.forEachAfterLoad(new BiConsumer<Data, Record>() {

        LazyMapEntry queryEntry = new LazyMapEntry();

        @Override
        public void accept(Data key, Record record) {
            Object value = useCachedValues ? getValueOrCachedValue(record, ss) : record.getValue();
            // TODO how can a value be null?
            if (value == null) {
                return;
            }
            queryEntry.init(ss, key, value, extractors);
            queryEntry.setRecord(record);
            queryEntry.setMetadata(recordStore.getOrCreateMetadataStore().get(key));
            if (predicate.apply(queryEntry) && compareAnchor(pagingPredicate, queryEntry, nearestAnchorEntry)) {
                // always copy key&value to heap if map is backed by native memory
                value = nativeMemory ? toHeapData((Data) value) : value;
                result.add(queryEntry.init(ss, toHeapData(key), value, extractors));
                // We can't reuse the existing entry after it was added to the
                // result. Allocate the new one.
                queryEntry = new LazyMapEntry();
            }
        }
    }, false);
    result.orderAndLimit(pagingPredicate, nearestAnchorEntry);
}
Also used : PartitionContainer(com.hazelcast.map.impl.PartitionContainer) ToHeapDataConverter.toHeapData(com.hazelcast.internal.util.ToHeapDataConverter.toHeapData) Data(com.hazelcast.internal.serialization.Data) MapContainer(com.hazelcast.map.impl.MapContainer) Extractors(com.hazelcast.query.impl.getters.Extractors) LazyMapEntry(com.hazelcast.map.impl.LazyMapEntry) QueryableEntry(com.hazelcast.query.impl.QueryableEntry) Entry(java.util.Map.Entry) LazyMapEntry(com.hazelcast.map.impl.LazyMapEntry) Record(com.hazelcast.map.impl.record.Record) Map(java.util.Map) PagingPredicateImpl(com.hazelcast.query.impl.predicates.PagingPredicateImpl)

Example 5 with Extractors

use of com.hazelcast.query.impl.getters.Extractors in project hazelcast by hazelcast.

the class TransactionalMapProxy method values.

@Override
@SuppressWarnings("unchecked")
public Collection values(Predicate predicate) {
    checkTransactionState();
    checkNotNull(predicate, "Predicate can not be null!");
    checkNotInstanceOf(PagingPredicate.class, predicate, "Paging is not supported for Transactional queries");
    QueryEngine queryEngine = mapServiceContext.getQueryEngine(name);
    Query query = Query.of().mapName(name).predicate(predicate).iterationType(IterationType.ENTRY).build();
    QueryResult queryResult = queryEngine.execute(query, Target.ALL_NODES);
    Set result = QueryResultUtils.transformToSet(ss, queryResult, predicate, IterationType.ENTRY, true, true);
    // TODO: can't we just use the original set?
    List<Object> valueSet = new ArrayList<>();
    Set<Data> keyWontBeIncluded = new HashSet<>();
    Extractors extractors = mapServiceContext.getExtractors(name);
    CachedQueryEntry cachedQueryEntry = new CachedQueryEntry();
    // iterate over the txMap and see if the values are updated or removed
    for (Map.Entry<Data, TxnValueWrapper> entry : txMap.entrySet()) {
        boolean isRemoved = Type.REMOVED.equals(entry.getValue().type);
        boolean isUpdated = Type.UPDATED.equals(entry.getValue().type);
        if (isRemoved) {
            keyWontBeIncluded.add(entry.getKey());
        } else {
            if (isUpdated) {
                keyWontBeIncluded.add(entry.getKey());
            }
            Object entryValue = entry.getValue().value;
            cachedQueryEntry.init(ss, entry.getKey(), entryValue, extractors);
            if (predicate.apply(cachedQueryEntry)) {
                valueSet.add(toObjectIfNeeded(cachedQueryEntry.getValueData()));
            }
        }
    }
    removeFromResultSet(result, valueSet, keyWontBeIncluded);
    return valueSet;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Query(com.hazelcast.map.impl.query.Query) ArrayList(java.util.ArrayList) Data(com.hazelcast.internal.serialization.Data) QueryEngine(com.hazelcast.map.impl.query.QueryEngine) QueryResult(com.hazelcast.map.impl.query.QueryResult) Extractors(com.hazelcast.query.impl.getters.Extractors) CachedQueryEntry(com.hazelcast.query.impl.CachedQueryEntry) HashMap(java.util.HashMap) Map(java.util.Map) TransactionalMap(com.hazelcast.transaction.TransactionalMap) HashSet(java.util.HashSet)

Aggregations

Extractors (com.hazelcast.query.impl.getters.Extractors)13 Data (com.hazelcast.internal.serialization.Data)4 CachedQueryEntry (com.hazelcast.query.impl.CachedQueryEntry)4 QueryableEntry (com.hazelcast.query.impl.QueryableEntry)4 QueryTarget (com.hazelcast.sql.impl.extract.QueryTarget)4 Map (java.util.Map)4 Parameters (junitparams.Parameters)4 Test (org.junit.Test)4 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)3 PartitionContainer (com.hazelcast.map.impl.PartitionContainer)3 IndexConfig (com.hazelcast.config.IndexConfig)2 ToHeapDataConverter.toHeapData (com.hazelcast.internal.util.ToHeapDataConverter.toHeapData)2 LazyMapEntry (com.hazelcast.map.impl.LazyMapEntry)2 MapContainer (com.hazelcast.map.impl.MapContainer)2 Query (com.hazelcast.map.impl.query.Query)2 QueryEngine (com.hazelcast.map.impl.query.QueryEngine)2 QueryResult (com.hazelcast.map.impl.query.QueryResult)2 Record (com.hazelcast.map.impl.record.Record)2 TransactionalMap (com.hazelcast.transaction.TransactionalMap)2 HashMap (java.util.HashMap)2