Example #1
0
 @Override
 public boolean apply(Map.Entry mapEntry) {
   for (Predicate predicate : predicates) {
     if (!predicate.apply(mapEntry)) {
       return false;
     }
   }
   return true;
 }
  @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");

    MapService service = getService();
    MapServiceContext mapServiceContext = service.getMapServiceContext();
    MapQueryEngine queryEngine = mapServiceContext.getMapQueryEngine(name);
    SerializationService serializationService = getNodeEngine().getSerializationService();

    QueryResult result = queryEngine.invokeQueryAllPartitions(name, predicate, IterationType.ENTRY);
    QueryResultCollection<Map.Entry> queryResult =
        new QueryResultCollection<Map.Entry>(
            serializationService, IterationType.ENTRY, false, true, result);

    // TODO: Can't we just use the original set?
    List<Object> valueSet = new ArrayList<Object>();
    Set<Object> keyWontBeIncluded = new HashSet<Object>();

    // iterate over the txMap and see if the values are updated or removed
    for (Map.Entry<Data, TxnValueWrapper> entry : txMap.entrySet()) {
      boolean isRemoved = TxnValueWrapper.Type.REMOVED.equals(entry.getValue().type);
      boolean isUpdated = TxnValueWrapper.Type.UPDATED.equals(entry.getValue().type);

      Object keyObject = serializationService.toObject(entry.getKey());
      if (isRemoved) {
        keyWontBeIncluded.add(keyObject);
      } else {
        if (isUpdated) {
          keyWontBeIncluded.add(keyObject);
        }
        Object entryValue = entry.getValue().value;
        QueryableEntry queryEntry =
            new CachedQueryEntry(serializationService, entry.getKey(), entryValue);
        if (predicate.apply(queryEntry)) {
          valueSet.add(queryEntry.getValue());
        }
      }
    }
    removeFromResultSet(queryResult, valueSet, keyWontBeIncluded);
    return valueSet;
  }
Example #3
0
 public QueryResult queryOnPartition(String mapName, Predicate predicate, int partitionId) {
   final QueryResult result = new QueryResult();
   List<QueryEntry> list = new LinkedList<QueryEntry>();
   PartitionContainer container = getPartitionContainer(partitionId);
   RecordStore recordStore = container.getRecordStore(mapName);
   Map<Data, Record> records = recordStore.getReadonlyRecordMap();
   SerializationService serializationService = nodeEngine.getSerializationService();
   final PagingPredicate pagingPredicate =
       predicate instanceof PagingPredicate ? (PagingPredicate) predicate : null;
   Comparator<Map.Entry> wrapperComparator = SortingUtil.newComparator(pagingPredicate);
   for (Record record : records.values()) {
     Data key = record.getKey();
     Object value = record.getValue();
     if (value == null) {
       continue;
     }
     QueryEntry queryEntry = new QueryEntry(serializationService, key, key, value);
     if (predicate.apply(queryEntry)) {
       if (pagingPredicate != null) {
         Map.Entry anchor = pagingPredicate.getAnchor();
         if (anchor != null
             && SortingUtil.compare(
                     pagingPredicate.getComparator(),
                     pagingPredicate.getIterationType(),
                     anchor,
                     queryEntry)
                 >= 0) {
           continue;
         }
       }
       list.add(queryEntry);
     }
   }
   if (pagingPredicate != null) {
     Collections.sort(list, wrapperComparator);
     if (list.size() > pagingPredicate.getPageSize()) {
       list = list.subList(0, pagingPredicate.getPageSize());
     }
   }
   for (QueryEntry entry : list) {
     result.add(
         new QueryResultEntryImpl(entry.getKeyData(), entry.getKeyData(), entry.getValueData()));
   }
   return result;
 }
  @Override
  @SuppressWarnings("unchecked")
  public Set keySet(Predicate predicate) {
    checkTransactionState();
    checkNotNull(predicate, "Predicate should not be null!");
    checkNotInstanceOf(
        PagingPredicate.class, predicate, "Paging is not supported for Transactional queries!");

    MapService service = getService();
    MapServiceContext mapServiceContext = service.getMapServiceContext();
    MapQueryEngine queryEngine = mapServiceContext.getMapQueryEngine(name);
    SerializationService serializationService = getNodeEngine().getSerializationService();

    QueryResult result = queryEngine.invokeQueryAllPartitions(name, predicate, IterationType.KEY);
    Set<Object> queryResult =
        new QueryResultCollection(serializationService, IterationType.KEY, false, true, result);

    // TODO: Can't we just use the original set?
    Set<Object> keySet = new HashSet<Object>(queryResult);
    for (Map.Entry<Data, TxnValueWrapper> entry : txMap.entrySet()) {
      Data keyData = entry.getKey();
      if (!TxnValueWrapper.Type.REMOVED.equals(entry.getValue().type)) {
        Object value =
            (entry.getValue().value instanceof Data)
                ? mapServiceContext.toObject(entry.getValue().value)
                : entry.getValue().value;

        QueryableEntry queryEntry = new CachedQueryEntry(serializationService, keyData, value);
        // apply predicate on txMap
        if (predicate.apply(queryEntry)) {
          Object keyObject = serializationService.toObject(keyData);
          keySet.add(keyObject);
        }
      } else {
        // meanwhile remove keys which are not in txMap
        Object keyObject = serializationService.toObject(keyData);
        keySet.remove(keyObject);
      }
    }
    return keySet;
  }