Search in sources :

Example 16 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class PDXPostProcessorDUnitTest method testQuery.

@Test
public void testQuery() {
    client2.invoke(() -> {
        ClientCache cache = createClientCache("super-user", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        // put in a value that's a domain object
        region.put("key1", new SimpleClass(1, (byte) 1));
        region.put("key2", BYTES);
    });
    client1.invoke(() -> {
        ClientCache cache = createClientCache("super-user", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        // post process for query
        String query = "select * from /AuthRegion";
        SelectResults result = region.query(query);
        Iterator itr = result.iterator();
        while (itr.hasNext()) {
            Object obj = itr.next();
            if (obj instanceof byte[]) {
                assertTrue(Arrays.equals(BYTES, (byte[]) obj));
            } else {
                assertTrue(obj instanceof SimpleClass);
            }
        }
    });
    // this makes sure PostProcessor is getting called
    PDXPostProcessor pp = (PDXPostProcessor) SecurityService.getSecurityService().getPostProcessor();
    assertEquals(pp.getCount(), 2);
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) SecurityTestUtil.createProxyRegion(org.apache.geode.security.SecurityTestUtil.createProxyRegion) ClientCache(org.apache.geode.cache.client.ClientCache) SecurityTestUtil.createClientCache(org.apache.geode.security.SecurityTestUtil.createClientCache) SimpleClass(org.apache.geode.pdx.SimpleClass) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 17 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class PostProcessorDUnitTest method testPostProcessQuery.

@Test
public void testPostProcessQuery() {
    client1.invoke(() -> {
        ClientCache cache = createClientCache("super-user", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        // post process for query
        String query = "select * from /AuthRegion";
        SelectResults result = region.query(query);
        assertEquals(5, result.size());
        assertTrue(result.contains("super-user/null/null/value0"));
        assertTrue(result.contains("super-user/null/null/value1"));
        assertTrue(result.contains("super-user/null/null/value2"));
        assertTrue(result.contains("super-user/null/null/value3"));
        assertTrue(result.contains("super-user/null/null/value4"));
        Pool pool = PoolManager.find(region);
        result = (SelectResults) pool.getQueryService().newQuery(query).execute();
        assertTrue(result.contains("super-user/null/null/value0"));
        assertTrue(result.contains("super-user/null/null/value1"));
        assertTrue(result.contains("super-user/null/null/value2"));
        assertTrue(result.contains("super-user/null/null/value3"));
        assertTrue(result.contains("super-user/null/null/value4"));
    });
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Region(org.apache.geode.cache.Region) SecurityTestUtil.createProxyRegion(org.apache.geode.security.SecurityTestUtil.createProxyRegion) Pool(org.apache.geode.cache.client.Pool) ClientCache(org.apache.geode.cache.client.ClientCache) SecurityTestUtil.createClientCache(org.apache.geode.security.SecurityTestUtil.createClientCache) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 18 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class SecurityTestUtils method doQueryExecuteP.

private static void doQueryExecuteP(final int multiUserIndex, final int expectedResult, final int expectedValue) {
    Region region = null;
    try {
        if (multiUserAuthMode) {
            region = proxyCaches[multiUserIndex].getRegion(REGION_NAME);
        } else {
            region = getCache().getRegion(REGION_NAME);
        }
        assertNotNull(region);
    } catch (Exception ex) {
        if (expectedResult == OTHER_EXCEPTION) {
            getLogWriter().info("Got expected exception when executing query: " + ex);
        } else {
            fail("Got unexpected exception when executing query", ex);
        }
    }
    try {
        String queryString = "SELECT DISTINCT * FROM " + region.getFullPath();
        Query query = null;
        if (multiUserAuthMode) {
            query = proxyCaches[multiUserIndex].getQueryService().newQuery(queryString);
        } else {
            region.getCache().getQueryService().newQuery(queryString);
        }
        SelectResults result = (SelectResults) query.execute();
        if (expectedResult != NO_EXCEPTION) {
            fail("Expected a NotAuthorizedException while executing function");
        }
        assertEquals(expectedValue, result.asList().size());
    } catch (NoAvailableServersException ex) {
        if (expectedResult == NO_AVAILABLE_SERVERS) {
            getLogWriter().info("Got expected NoAvailableServers when executing query: " + ex.getCause());
        } else {
            fail("Got unexpected exception when executing query", ex);
        }
    } catch (ServerConnectivityException ex) {
        if ((expectedResult == NOTAUTHZ_EXCEPTION) && (ex.getCause() instanceof NotAuthorizedException)) {
            getLogWriter().info("Got expected NotAuthorizedException when executing query: " + ex.getCause());
        } else if (expectedResult == OTHER_EXCEPTION) {
            getLogWriter().info("Got expected exception when executing query: " + ex);
        } else {
            fail("Got unexpected exception when executing query", ex);
        }
    } catch (Exception ex) {
        if (expectedResult == OTHER_EXCEPTION) {
            getLogWriter().info("Got expected exception when executing query: " + ex);
        } else {
            fail("Got unexpected exception when executing query", ex);
        }
    }
}
Also used : ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) NoAvailableServersException(org.apache.geode.cache.client.NoAvailableServersException) Region(org.apache.geode.cache.Region) ServerRefusedConnectionException(org.apache.geode.cache.client.ServerRefusedConnectionException) NoAvailableServersException(org.apache.geode.cache.client.NoAvailableServersException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) IOException(java.io.IOException)

Example 19 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class ZRangeByScoreExecutor method getKeys.

private Collection<?> getKeys(ByteArrayWrapper key, Region<ByteArrayWrapper, DoubleWrapper> keyRegion, ExecutionHandlerContext context, double start, double stop, boolean startInclusive, boolean stopInclusive, int offset, int limit) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    if (start == Double.POSITIVE_INFINITY || stop == Double.NEGATIVE_INFINITY || start > stop || (start == stop && (!startInclusive || !stopInclusive)))
        return null;
    if (start == Double.NEGATIVE_INFINITY && stop == Double.POSITIVE_INFINITY)
        return new HashSet(keyRegion.entrySet());
    Query query;
    Object[] params;
    if (isReverse()) {
        if (startInclusive) {
            if (stopInclusive) {
                query = getQuery(key, SortedSetQuery.ZREVRBSSTISI, context);
            } else {
                query = getQuery(key, SortedSetQuery.ZREVRBSSTI, context);
            }
        } else {
            if (stopInclusive) {
                query = getQuery(key, SortedSetQuery.ZREVRBSSI, context);
            } else {
                query = getQuery(key, SortedSetQuery.ZREVRBS, context);
            }
        }
        params = new Object[] { start, stop, INFINITY_LIMIT };
    } else {
        if (startInclusive) {
            if (stopInclusive) {
                query = getQuery(key, SortedSetQuery.ZRBSSTISI, context);
            } else {
                query = getQuery(key, SortedSetQuery.ZRBSSTI, context);
            }
        } else {
            if (stopInclusive) {
                query = getQuery(key, SortedSetQuery.ZRBSSI, context);
            } else {
                query = getQuery(key, SortedSetQuery.ZRBS, context);
            }
        }
        params = new Object[] { start, stop, INFINITY_LIMIT };
    }
    if (limit > 0)
        params[params.length - 1] = (limit + offset);
    SelectResults<?> results = (SelectResults<?>) query.execute(params);
    if (offset < results.size())
        return (Collection<Struct>) results.asList().subList(offset, results.size());
    else
        return null;
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) SortedSetQuery(org.apache.geode.redis.internal.executor.SortedSetQuery) HashSet(java.util.HashSet) Struct(org.apache.geode.cache.query.Struct)

Example 20 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class ZRemRangeByLexExecutor method getRange.

private Collection<ByteArrayWrapper> getRange(ByteArrayWrapper key, Region<ByteArrayWrapper, DoubleWrapper> keyRegion, ExecutionHandlerContext context, ByteArrayWrapper start, ByteArrayWrapper stop, boolean startInclusive, boolean stopInclusive) throws Exception {
    if (start.equals("-") && stop.equals("+"))
        return new ArrayList<ByteArrayWrapper>(keyRegion.keySet());
    else if (start.equals("+") || stop.equals("-"))
        return null;
    Query query;
    Object[] params;
    if (start.equals("-")) {
        if (stopInclusive) {
            query = getQuery(key, SortedSetQuery.ZRANGEBYLEXNINFI, context);
        } else {
            query = getQuery(key, SortedSetQuery.ZRANGEBYLEXNINF, context);
        }
        params = new Object[] { stop, INFINITY_LIMIT };
    } else if (stop.equals("+")) {
        if (startInclusive) {
            query = getQuery(key, SortedSetQuery.ZRANGEBYLEXPINFI, context);
        } else {
            query = getQuery(key, SortedSetQuery.ZRANGEBYLEXPINF, context);
        }
        params = new Object[] { start, INFINITY_LIMIT };
    } else {
        if (startInclusive) {
            if (stopInclusive) {
                query = getQuery(key, SortedSetQuery.ZRANGEBYLEXSTISI, context);
            } else {
                query = getQuery(key, SortedSetQuery.ZRANGEBYLEXSTI, context);
            }
        } else {
            if (stopInclusive) {
                query = getQuery(key, SortedSetQuery.ZRANGEBYLEXSI, context);
            } else {
                query = getQuery(key, SortedSetQuery.ZRANGEBYLEX, context);
            }
        }
        params = new Object[] { start, stop, INFINITY_LIMIT };
    }
    @SuppressWarnings("unchecked") SelectResults<ByteArrayWrapper> results = (SelectResults<ByteArrayWrapper>) query.execute(params);
    return results.asList();
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) Query(org.apache.geode.cache.query.Query) SortedSetQuery(org.apache.geode.redis.internal.executor.SortedSetQuery)

Aggregations

SelectResults (org.apache.geode.cache.query.SelectResults)577 Test (org.junit.Test)423 Query (org.apache.geode.cache.query.Query)360 Region (org.apache.geode.cache.Region)336 QueryService (org.apache.geode.cache.query.QueryService)331 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)256 Portfolio (org.apache.geode.cache.query.data.Portfolio)249 Index (org.apache.geode.cache.query.Index)133 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)112 Host (org.apache.geode.test.dunit.Host)107 VM (org.apache.geode.test.dunit.VM)107 CacheException (org.apache.geode.cache.CacheException)105 Iterator (java.util.Iterator)104 AttributesFactory (org.apache.geode.cache.AttributesFactory)101 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)92 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)89 Cache (org.apache.geode.cache.Cache)84 Struct (org.apache.geode.cache.query.Struct)80 LocalRegion (org.apache.geode.internal.cache.LocalRegion)67 ObjectType (org.apache.geode.cache.query.types.ObjectType)66