com.gemstone.gemfire.cache.query.functional.IndexPrimaryKeyUsageTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gemfire-junit Show documentation
Show all versions of gemfire-junit Show documentation
SnappyData store based off Pivotal GemFireXD
/*
* Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License. See accompanying
* LICENSE file.
*/
/*
* IndexPrimaryKeyUsageTest.java
*
* Created on June 7, 2005, 12:32 PM
*/
package com.gemstone.gemfire.cache.query.functional;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.query.CacheUtils;
import com.gemstone.gemfire.cache.query.Index;
import com.gemstone.gemfire.cache.query.IndexType;
import com.gemstone.gemfire.cache.query.Query;
import com.gemstone.gemfire.cache.query.QueryService;
import com.gemstone.gemfire.cache.query.SelectResults;
import com.gemstone.gemfire.cache.query.Utils;
import com.gemstone.gemfire.cache.query.data.Address;
import com.gemstone.gemfire.cache.query.data.Employee;
import com.gemstone.gemfire.cache.query.data.Portfolio;
import com.gemstone.gemfire.cache.query.internal.QueryObserverAdapter;
import com.gemstone.gemfire.cache.query.internal.QueryObserverHolder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import junit.framework.*;
/**
*
* @author vikramj
*/
public class IndexPrimaryKeyUsageTest extends TestCase {
/** Creates a new instance of IndexPrimaryKeyUsageTest */
public IndexPrimaryKeyUsageTest(String testName) {
super(testName);
}
////////// main method ///////////
public static void main(java.lang.String[] args) {
junit.textui.TestRunner.run(new TestSuite(IndexPrimaryKeyUsageTest.class));
}//end of main method
protected void setUp() throws java.lang.Exception {
CacheUtils.startCache();
Region r = CacheUtils.createRegion("portfolios", Portfolio.class);
for(int i=0;i<4;i++)
r.put(i+"", new Portfolio(i));
}
protected void tearDown() throws java.lang.Exception {
CacheUtils.closeCache();
}
public void testPrimaryKeyIndexUsage() throws Exception {
// Task ID: PKI 1
QueryService qs;
qs = CacheUtils.getQueryService();
String queries[] = {
"select distinct * from /portfolios x, x.positions.values where x.pk = '1'",
"select distinct * from /portfolios x, x.positions.values where x.pkid = '1'",
// BUG # 32707: FIXED
"select distinct * from /portfolios p, p.positions.values where p.pkid != '53'"
};
SelectResults r[][]= new SelectResults[queries.length][2];
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
// DebuggerSupport.waitForJavaDebugger(CacheUtils.getLogger());
r[i][0] = (SelectResults)q.execute();
if(!observer.isIndexesUsed){
System.out.println("NO INDEX USED");
}
System.out.println(Utils.printResult(r[i][0]));
if ((r[i][0]).size() != 0){
System.out.println("As Expected, Results Size is NON ZERO");
}else {
fail("FAILED:Search result Size is zero");
}
} catch (Exception e) {
e.printStackTrace();
fail(e.toString());
}
}
// Create an Index on status and execute the same query again.
qs = CacheUtils.getQueryService();
qs.createIndex("pkIndex", IndexType.PRIMARY_KEY, "pk", "/portfolios");
qs.createIndex("pkidIndex", IndexType.PRIMARY_KEY, "pkid", "/portfolios");
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
QueryObserverImpl observer2 = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer2);
q = CacheUtils.getQueryService().newQuery(queries[i]);
r[i][1] =(SelectResults) q.execute();
if(observer2.isIndexesUsed == true){
System.out.println("As expected, INDEX is USED!");
}else {
fail("FAILED: INDEX IS NOT USED!");
}
System.out.println(Utils.printResult(r[i][1]));
if ((r[i][1]).size() != 0){
System.out.println("As Expected, Results Size is NON ZERO");
}else {
fail("FAILED:Search result Size is zero");
}
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
CacheUtils.compareResultsOfWithAndWithoutIndex(r,this);
}
public void testPrimaryKeyIndexUsageNegativeTestA() throws Exception{
// Task ID: PKI 2
Object r[]= new Object[5];
QueryService qs;
qs = CacheUtils.getQueryService();
String queries[] = {
"select distinct * from /portfolios x, x.positions.values where x.pk = '1'",
"select distinct * from /portfolios.entries x, x.value.positions.values where x.value.pkid = '1'",
"select distinct * from /portfolios.entries x, x.value.positions.values where x.key = '1'",
};
qs = CacheUtils.getQueryService();
qs.createIndex("pkidIndex", IndexType.PRIMARY_KEY, "pkid", "/portfolios");
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
q = CacheUtils.getQueryService().newQuery(queries[i]);
r[i] = q.execute();
if(!observer.isIndexesUsed == false){
fail("FAILED: INDEX IS USED!");
}else{
System.out.println("As Expected, INDEX is NOT USED");
}
System.out.println(Utils.printResult(r[i]) );
if (((SelectResults)r[i]).size() != 0){
System.out.println("As Expected, Results Size is NON ZERO");
}else {
fail("FAILED:Search result Size is zero");
}
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
}
public void testPrimaryKeyIndexUsageNegativeTestB() throws Exception{
//Task ID : PKI 3
Object r[]= new Object[5];
QueryService qs;
qs = CacheUtils.getQueryService();
String queries[] = {
"select distinct * from /portfolios x, x.positions.values where x.pkid = '1'",
};
qs = CacheUtils.getQueryService();
qs.createIndex("pkIndex", IndexType.PRIMARY_KEY, "pk", "/portfolios");
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
q = CacheUtils.getQueryService().newQuery(queries[i]);
r[i] = q.execute();
if(observer.isIndexesUsed){
fail("INDEX IS USED!");
}else{
System.out.println("As Expected, Index Is Not Used");
}
System.out.println(Utils.printResult(r[i]) );
if (((SelectResults)r[i]).size() != 0){
System.out.println("As Expected, Results Size is NON ZERO");
}else {
fail("FAILED:Search result Size is zero");
}
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
}
public void testFunctionalAndPrimaryKey() throws Exception {
// Task ID: PKI4
Object r[] = new Object[7];
QueryService qs;
qs = CacheUtils.getQueryService();
String queries[] = {
"select distinct * from /portfolios p, p.positions.values where p.ID > 1 ",
"select distinct * from /portfolios p, p.positions.values where p.ID < 3 ",
"select distinct * from /portfolios p, p.positions.values where p.ID >= 1 ",
"select distinct * from /portfolios p, p.positions.values where p.ID <= 1 ",
};
qs = CacheUtils.getQueryService();
qs.createIndex("IDPRKIndex", IndexType.PRIMARY_KEY, "pkid", "/portfolios");
qs.createIndex("IDFNLIndex", IndexType.FUNCTIONAL, "ID", "/portfolios");
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
q = CacheUtils.getQueryService().newQuery(queries[i]);
r[i] = q.execute();
if(observer.isIndexesUsed){
System.out.println("YES, INDEX IS USED!");
}else{
fail("ERROR:Index Is Not Used");
}
System.out.println(Utils.printResult(r[i]) );
if (observer.IndexTypeFunctional != 1){
fail("IMPROPER INDEX USAGE: INDEX USED IS NOT OF TYPE FUNCTIONAL");
}
if (((SelectResults)r[i]).size() != 0){
System.out.println("As Expected, Results Size is NON ZERO");
}else {
fail("FAILED:Search result Size is zero");
}
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
}
public void testPublicKeyUsageOnTwoRegions() throws Exception {
// Task ID: PKI5
Region rg2 = CacheUtils.createRegion("employees", Employee.class);
Set add1 = new HashSet();
Set add2 = new HashSet();
add1.add(new Address("411045", "Baner"));
add2.add(new Address("411046", "Aundh"));
rg2.put("1", new Employee("aaa", 27, 270, "QA", 1800, add1));
rg2.put("2", new Employee("bbb", 28, 280, "QA", 1900, add2));
Object r[] = new Object[5];
QueryService qs;
qs = CacheUtils.getQueryService();
String queries[] = {
"select distinct * from /portfolios p, /employees e where p.pkid = '1' ",
};
qs = CacheUtils.getQueryService();
qs.createIndex("IDFNLIndex", IndexType.FUNCTIONAL, "pkid", "/portfolios");
qs.createIndex("IDPRKIndex", IndexType.PRIMARY_KEY, "pkid", "/portfolios");
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
q = CacheUtils.getQueryService().newQuery(queries[i]);
r[i] = q.execute();
if(observer.isIndexesUsed){
System.out.println("YES, INDEX IS USED!");
}else{
fail("FAILED:Index Is Not Used");
}
System.out.println(Utils.printResult(r[i]) );
if (observer.IndexTypePrimKey != 2){
fail("IMPROPER INDEX USAGE: INDEX USED IS NOT OF TYPE PRIMARY_KEY");
}
if (((SelectResults)r[i]).size() != 0){
System.out.println("As Expected, Results Size is NON ZERO");
}else {
fail("FAILED:Search result Size is zero");
}
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
}
class QueryObserverImpl extends QueryObserverAdapter{
boolean isIndexesUsed = false;
ArrayList indexesUsed = new ArrayList();
int IndexTypeFunctional = 0 ;
int IndexTypePrimKey = 0 ;
public void beforeIndexLookup(Index index, int oper, Object key) {
indexesUsed.add(index.getName());
if ((index.getType()).equals(IndexType.FUNCTIONAL)){
IndexTypeFunctional = 1;
}else {
IndexTypePrimKey = 2;
}
}
public void afterIndexLookup(Collection results) {
if(results != null){
isIndexesUsed = true;
//System.out.println(Utils.printResult(results));
}
}
}
}