Search in sources :

Example 46 with QueryService

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

the class DataCommandFunction method select.

@SuppressWarnings("rawtypes")
private DataCommandResult select(Object principal, String queryString) {
    InternalCache cache = getCache();
    AtomicInteger nestedObjectCount = new AtomicInteger(0);
    if (StringUtils.isEmpty(queryString)) {
        return DataCommandResult.createSelectInfoResult(null, null, -1, null, CliStrings.QUERY__MSG__QUERY_EMPTY, false);
    }
    QueryService qs = cache.getQueryService();
    // TODO : Find out if is this optimised use. Can you have something equivalent of parsed
    // queries with names where name can be retrieved to avoid parsing every-time
    Query query = qs.newQuery(queryString);
    DefaultQuery tracedQuery = (DefaultQuery) query;
    WrappedIndexTrackingQueryObserver queryObserver = null;
    String queryVerboseMsg = null;
    long startTime = -1;
    if (tracedQuery.isTraced()) {
        startTime = NanoTimer.getTime();
        queryObserver = new WrappedIndexTrackingQueryObserver();
        QueryObserverHolder.setInstance(queryObserver);
    }
    List<SelectResultRow> list = new ArrayList<>();
    try {
        Object results = query.execute();
        if (tracedQuery.isTraced()) {
            queryVerboseMsg = getLogMessage(queryObserver, startTime, queryString);
            queryObserver.reset2();
        }
        if (results instanceof SelectResults) {
            select_SelectResults((SelectResults) results, principal, list, nestedObjectCount);
        } else {
            select_NonSelectResults(results, list);
        }
        return DataCommandResult.createSelectResult(queryString, list, queryVerboseMsg, null, null, true);
    } catch (FunctionDomainException | GfJsonException | QueryInvocationTargetException | NameResolutionException | TypeMismatchException e) {
        logger.warn(e.getMessage(), e);
        return DataCommandResult.createSelectResult(queryString, null, queryVerboseMsg, e, e.getMessage(), false);
    } finally {
        if (queryObserver != null) {
            QueryObserverHolder.reset();
        }
    }
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) ArrayList(java.util.ArrayList) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) InternalCache(org.apache.geode.internal.cache.InternalCache) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) SelectResults(org.apache.geode.cache.query.SelectResults) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryService(org.apache.geode.cache.query.QueryService) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) SelectResultRow(org.apache.geode.management.internal.cli.domain.DataCommandResult.SelectResultRow) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject)

Example 47 with QueryService

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

the class CreateIndexFunction method execute.

@Override
public void execute(FunctionContext context) {
    final IndexInfo indexInfo = (IndexInfo) context.getArguments();
    String memberId = null;
    try {
        Cache cache = CacheFactory.getAnyInstance();
        memberId = cache.getDistributedSystem().getDistributedMember().getId();
        QueryService queryService = cache.getQueryService();
        String indexName = indexInfo.getIndexName();
        String indexedExpression = indexInfo.getIndexedExpression();
        String fromClause = indexInfo.getRegionPath();
        // Check to see if the region path contains an alias e.g "/region1 r1"
        // Then the first string will be the regionPath
        String[] regionPathTokens = fromClause.trim().split(" ");
        String regionPath = regionPathTokens[0];
        switch(indexInfo.getIndexType()) {
            case IndexInfo.RANGE_INDEX:
                queryService.createIndex(indexName, indexedExpression, fromClause);
                break;
            case IndexInfo.KEY_INDEX:
                queryService.createKeyIndex(indexName, indexedExpression, fromClause);
                break;
            case IndexInfo.HASH_INDEX:
                queryService.createHashIndex(indexName, indexedExpression, fromClause);
                break;
            default:
                queryService.createIndex(indexName, indexedExpression, fromClause);
        }
        regionPath = getValidRegionName(cache, regionPath);
        setResultInSender(context, indexInfo, memberId, cache, regionPath);
    } catch (IndexExistsException e) {
        String message = CliStrings.format(CliStrings.CREATE_INDEX__INDEX__EXISTS, indexInfo.getIndexName());
        context.getResultSender().lastResult(new CliFunctionResult(memberId, false, message));
    } catch (IndexNameConflictException e) {
        String message = CliStrings.format(CliStrings.CREATE_INDEX__NAME__CONFLICT, indexInfo.getIndexName());
        context.getResultSender().lastResult(new CliFunctionResult(memberId, false, message));
    } catch (RegionNotFoundException e) {
        String message = CliStrings.format(CliStrings.CREATE_INDEX__INVALID__REGIONPATH, indexInfo.getRegionPath());
        context.getResultSender().lastResult(new CliFunctionResult(memberId, false, message));
    } catch (IndexInvalidException e) {
        context.getResultSender().lastResult(new CliFunctionResult(memberId, e, e.getMessage()));
    } catch (Exception e) {
        String exceptionMessage = CliStrings.format(CliStrings.EXCEPTION_CLASS_AND_MESSAGE, e.getClass().getName(), e.getMessage());
        context.getResultSender().lastResult(new CliFunctionResult(memberId, e, e.getMessage()));
    }
}
Also used : IndexExistsException(org.apache.geode.cache.query.IndexExistsException) QueryService(org.apache.geode.cache.query.QueryService) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) IndexInfo(org.apache.geode.management.internal.cli.domain.IndexInfo) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) Cache(org.apache.geode.cache.Cache)

Example 48 with QueryService

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

the class SelectStarQueryDUnitTest method testByteArrayReplicatedRegion.

@Test
public void testByteArrayReplicatedRegion() throws Exception {
    final Host host = Host.getHost(0);
    final VM server1 = host.getVM(0);
    final VM client = host.getVM(3);
    final byte[] ba = new byte[] { 1, 2, 3, 4, 5 };
    // create servers and regions
    final int port = (Integer) server1.invoke(new SerializableCallable("Create Server1") {

        @Override
        public Object call() throws Exception {
            Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regName);
            // put domain objects
            for (int i = 0; i < 10; i++) {
                r1.put("key-" + i, ba);
            }
            CacheServer server = getCache().addCacheServer();
            int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
            server.setPort(port);
            server.start();
            return port;
        }
    });
    server1.invoke(new SerializableCallable("Set observer") {

        @Override
        public Object call() throws Exception {
            oldObserver = QueryObserverHolder.setInstance(new QueryResultTrackingObserver());
            return null;
        }
    });
    // create client
    client.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            ClientCacheFactory cf = new ClientCacheFactory();
            cf.addPoolServer(getServerHostName(server1.getHost()), port);
            ClientCache cache = getClientCache(cf);
            cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regName);
            return null;
        }
    });
    // put serialized PortfolioPdx objects
    client.invoke(new SerializableCallable("Put objects") {

        @Override
        public Object call() throws Exception {
            Region r1 = getRootRegion(regName);
            for (int i = 10; i < 20; i++) {
                r1.put("key-" + i, ba);
            }
            return null;
        }
    });
    // query remotely from client
    client.invoke(new SerializableCallable("Query") {

        @Override
        public Object call() throws Exception {
            getLogWriter().info("Querying remotely from client");
            QueryService localQS = null;
            QueryService remoteQS = null;
            try {
                localQS = ((ClientCache) getCache()).getLocalQueryService();
                remoteQS = ((ClientCache) getCache()).getQueryService();
            } catch (Exception e) {
                fail("Exception getting query service ", e);
            }
            SelectResults res = null;
            SelectResults[][] sr = new SelectResults[1][2];
            for (int i = 0; i < 6; i++) {
                try {
                    res = (SelectResults) localQS.newQuery(queries[i]).execute();
                    sr[0][0] = res;
                    res = (SelectResults) remoteQS.newQuery(queries[i]).execute();
                    sr[0][1] = res;
                    CacheUtils.compareResultsOfWithAndWithoutIndex(sr);
                } catch (Exception e) {
                    fail("Error executing query: " + queries[i], e);
                }
                assertEquals(resultSize[i], res.size());
                if (i == 3) {
                    int cnt = ((Integer) res.iterator().next());
                    assertEquals(20, cnt);
                } else {
                    for (Object o : res) {
                        if (o instanceof byte[]) {
                            int j = 0;
                            for (byte b : ((byte[]) o)) {
                                if (b != ba[j++]) {
                                    fail("Bytes in byte array are different when queried from client");
                                }
                            }
                        } else {
                            fail("Result objects for remote client query: " + queries[i] + " should be instance of PortfolioPdx and not " + o.getClass());
                        }
                    }
                }
            }
            return null;
        }
    });
    // verify if objects iterated by query are serialized
    server1.invoke(new SerializableCallable("Get observer") {

        @Override
        public Object call() throws Exception {
            QueryObserver observer = QueryObserverHolder.getInstance();
            assertTrue(QueryObserverHolder.hasObserver());
            assertTrue(observer instanceof QueryResultTrackingObserver);
            QueryResultTrackingObserver resultObserver = (QueryResultTrackingObserver) observer;
            assertFalse(resultObserver.isObjectSerialized());
            return null;
        }
    });
    // verify if objects returned by local server query are not serialized
    server1.invoke(new SerializableCallable("Query") {

        @Override
        public Object call() throws Exception {
            QueryObserver observer = QueryObserverHolder.setInstance(new QueryResultTrackingObserver());
            QueryService qs = null;
            try {
                qs = getCache().getQueryService();
            } catch (Exception e) {
                fail("Exception getting query service ", e);
            }
            SelectResults res = null;
            for (int i = 0; i < 6; i++) {
                try {
                    res = (SelectResults) qs.newQuery(queries[i]).execute();
                } catch (Exception e) {
                    fail("Error executing query: " + queries[i], e);
                }
                assertEquals(resultSize[i], res.size());
                if (i == 3) {
                    int cnt = ((Integer) res.iterator().next());
                    assertEquals(20, cnt);
                } else {
                    for (Object o : res) {
                        if (o instanceof byte[]) {
                            int j = 0;
                            for (byte b : ((byte[]) o)) {
                                if (b != ba[j++]) {
                                    fail("Bytes in byte array are different when queried locally on server");
                                }
                            }
                        } else {
                            fail("Result objects for server local query: " + queries[i] + " should be instance of byte array and not " + o.getClass());
                        }
                    }
                }
            }
            observer = QueryObserverHolder.getInstance();
            assertTrue(QueryObserverHolder.hasObserver());
            assertTrue(observer instanceof QueryResultTrackingObserver);
            QueryResultTrackingObserver resultObserver = (QueryResultTrackingObserver) observer;
            assertFalse(resultObserver.isObjectSerialized());
            // reset observer
            QueryObserverHolder.setInstance(oldObserver);
            return null;
        }
    });
    closeCache(client);
    closeCache(server1);
}
Also used : Host(org.apache.geode.test.dunit.Host) ClientCache(org.apache.geode.cache.client.ClientCache) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) CacheServer(org.apache.geode.cache.server.CacheServer) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 49 with QueryService

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

the class SelectStarQueryDUnitTest method testByteArrayPartitionedRegion.

@Test
public void testByteArrayPartitionedRegion() throws Exception {
    final Host host = Host.getHost(0);
    final VM server1 = host.getVM(0);
    final VM client = host.getVM(3);
    final VM server2 = host.getVM(1);
    final VM server3 = host.getVM(2);
    final byte[][] objs = new byte[10][5];
    for (int i = 0; i < objs.length; i++) {
        objs[i] = new byte[] { 1, 2, 3, 4, 5 };
    }
    // create servers and regions
    final int port1 = startPartitionedCacheServer(server1, objs);
    final int port2 = startPartitionedCacheServer(server2, objs);
    final int port3 = startPartitionedCacheServer(server3, objs);
    server1.invoke(new SerializableCallable("Set observer") {

        @Override
        public Object call() throws Exception {
            oldObserver = QueryObserverHolder.setInstance(new QueryResultTrackingObserver());
            return null;
        }
    });
    // create client
    client.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            ClientCacheFactory cf = new ClientCacheFactory();
            cf.addPoolServer(getServerHostName(server1.getHost()), port1);
            cf.addPoolServer(getServerHostName(server2.getHost()), port2);
            cf.addPoolServer(getServerHostName(server3.getHost()), port3);
            ClientCache cache = getClientCache(cf);
            cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regName);
            return null;
        }
    });
    // put serialized PortfolioPdx objects
    client.invoke(new SerializableCallable("Put objects") {

        @Override
        public Object call() throws Exception {
            Region r1 = getRootRegion(regName);
            for (int i = 10; i < 20; i++) {
                r1.put("key-" + i, new byte[] { 1, 2, 3, 4, 5 });
            }
            return null;
        }
    });
    // query remotely from client
    client.invoke(new SerializableCallable("Query") {

        @Override
        public Object call() throws Exception {
            getLogWriter().info("Querying remotely from client");
            QueryService localQS = null;
            QueryService remoteQS = null;
            try {
                localQS = ((ClientCache) getCache()).getLocalQueryService();
                remoteQS = ((ClientCache) getCache()).getQueryService();
            } catch (Exception e) {
                fail("Exception getting query service ", e);
            }
            SelectResults res = null;
            SelectResults[][] sr = new SelectResults[1][2];
            for (int i = 0; i < 6; i++) {
                try {
                    res = (SelectResults) localQS.newQuery(queries[i]).execute();
                    sr[0][0] = res;
                    res = (SelectResults) remoteQS.newQuery(queries[i]).execute();
                    sr[0][1] = res;
                    CacheUtils.compareResultsOfWithAndWithoutIndex(sr);
                } catch (Exception e) {
                    fail("Error executing query: " + queries[i], e);
                }
                assertEquals(resultSize[i], res.size());
                if (i == 3) {
                    int cnt = ((Integer) res.iterator().next());
                    assertEquals(20, cnt);
                } else {
                    for (Object o : res) {
                        if (o instanceof byte[]) {
                            int j = 0;
                            for (byte b : ((byte[]) o)) {
                                if (b != objs[0][j++]) {
                                    fail("Bytes in byte array are different when queried from client");
                                }
                            }
                        } else {
                            fail("Result objects for remote client query: " + queries[i] + " should be instance of PortfolioPdx and not " + o.getClass());
                        }
                    }
                }
            }
            return null;
        }
    });
    // verify if objects iterated by query are serialized
    server1.invoke(new SerializableCallable("Get observer") {

        @Override
        public Object call() throws Exception {
            QueryObserver observer = QueryObserverHolder.getInstance();
            assertTrue(QueryObserverHolder.hasObserver());
            assertTrue(observer instanceof QueryResultTrackingObserver);
            QueryResultTrackingObserver resultObserver = (QueryResultTrackingObserver) observer;
            assertFalse(resultObserver.isObjectSerialized());
            return null;
        }
    });
    // verify if objects returned by local server query are not serialized
    server1.invoke(new SerializableCallable("Query") {

        @Override
        public Object call() throws Exception {
            QueryObserver observer = QueryObserverHolder.setInstance(new QueryResultTrackingObserver());
            QueryService qs = null;
            try {
                qs = getCache().getQueryService();
            } catch (Exception e) {
                fail("Exception getting query service ", e);
            }
            SelectResults res = null;
            for (int i = 0; i < 6; i++) {
                try {
                    res = (SelectResults) qs.newQuery(queries[i]).execute();
                } catch (Exception e) {
                    fail("Error executing query: " + queries[i], e);
                }
                assertEquals(resultSize[i], res.size());
                if (i == 3) {
                    int cnt = ((Integer) res.iterator().next());
                    assertEquals(20, cnt);
                } else {
                    for (Object o : res) {
                        if (o instanceof byte[]) {
                            int j = 0;
                            for (byte b : ((byte[]) o)) {
                                if (b != objs[0][j++]) {
                                    fail("Bytes in byte array are different when queried locally on server");
                                }
                            }
                        } else {
                            fail("Result objects for server local query: " + queries[i] + " should be instance of byte array and not " + o.getClass());
                        }
                    }
                }
            }
            observer = QueryObserverHolder.getInstance();
            assertTrue(QueryObserverHolder.hasObserver());
            assertTrue(observer instanceof QueryResultTrackingObserver);
            QueryResultTrackingObserver resultObserver = (QueryResultTrackingObserver) observer;
            assertFalse(resultObserver.isObjectSerialized());
            // reset observer
            QueryObserverHolder.setInstance(oldObserver);
            return null;
        }
    });
    closeCache(client);
    closeCache(server1);
}
Also used : Host(org.apache.geode.test.dunit.Host) ClientCache(org.apache.geode.cache.client.ClientCache) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 50 with QueryService

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

the class ComparisonOperatorsJUnitTest method testCompareWithNULL.

@Test
public void testCompareWithNULL() throws Exception {
    String var = "P2";
    Object value = null;
    QueryService qs = CacheUtils.getQueryService();
    for (int i = 0; i < operators.length; i++) {
        Query query = qs.newQuery("SELECT DISTINCT * FROM /Portfolios where " + var + operators[i] + value);
        Object result = query.execute();
        if (result instanceof Collection) {
            Iterator iter = ((Collection) result).iterator();
            while (iter.hasNext()) {
                boolean isPassed = false;
                Portfolio p = (Portfolio) iter.next();
                switch(i) {
                    case 0:
                        isPassed = (p.getP2() == value);
                        break;
                    default:
                        isPassed = (p.getP2() != value);
                        break;
                }
                if (!isPassed)
                    fail(this.getName() + " failed for operator " + operators[i]);
            }
        } else {
            fail(this.getName() + " failed for operator " + operators[i]);
        }
    }
}
Also used : Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) Portfolio(org.apache.geode.cache.query.data.Portfolio) Iterator(java.util.Iterator) Collection(java.util.Collection) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

QueryService (org.apache.geode.cache.query.QueryService)532 Test (org.junit.Test)383 SelectResults (org.apache.geode.cache.query.SelectResults)331 Region (org.apache.geode.cache.Region)316 Query (org.apache.geode.cache.query.Query)316 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)215 Portfolio (org.apache.geode.cache.query.data.Portfolio)199 CacheException (org.apache.geode.cache.CacheException)139 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)118 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)110 Index (org.apache.geode.cache.query.Index)109 Host (org.apache.geode.test.dunit.Host)104 VM (org.apache.geode.test.dunit.VM)104 Iterator (java.util.Iterator)102 Cache (org.apache.geode.cache.Cache)92 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)86 Struct (org.apache.geode.cache.query.Struct)70 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)68 IOException (java.io.IOException)67 DefaultQueryService (org.apache.geode.cache.query.internal.DefaultQueryService)65