From d1c05752079696dd596a6f11aa344d8b5e96a960 Mon Sep 17 00:00:00 2001 From: Denis Magda Date: Wed, 6 Jul 2016 13:05:14 +0300 Subject: [PATCH] gg-11253: load tests based on SB cases (cherry picked from commit f267a30) --- .../IgniteCacheRandomOperationBenchmark.java | 704 ++++++++++-------- 1 file changed, 378 insertions(+), 326 deletions(-) diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/cache/load/IgniteCacheRandomOperationBenchmark.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/cache/load/IgniteCacheRandomOperationBenchmark.java index bcea03c0d486d..c2947ba0e008f 100644 --- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/cache/load/IgniteCacheRandomOperationBenchmark.java +++ b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/cache/load/IgniteCacheRandomOperationBenchmark.java @@ -38,9 +38,7 @@ import javax.cache.event.CacheEntryListenerException; import javax.cache.event.CacheEntryUpdatedListener; import javax.cache.processor.EntryProcessor; -import javax.cache.processor.EntryProcessorException; import javax.cache.processor.MutableEntry; - import org.apache.ignite.Ignite; import org.apache.ignite.IgniteCache; import org.apache.ignite.IgniteCompute; @@ -91,16 +89,16 @@ public class IgniteCacheRandomOperationBenchmark extends IgniteAbstractBenchmark private static BenchmarkIgniteBiPredicate igniteBiPred = new BenchmarkIgniteBiPredicate(); /** Amount partitions. */ - private static final int SCAN_QUERY_PARTITIN_AMOUNT = 10; + private static final int SCAN_QUERY_PARTITION_AMOUNT = 10; /** List off all available cache. */ - private List availableCaches; + private List> availableCaches; /** List of available transactional cache. */ - private List txCaches; + private List> txCaches; /** List of affinity cache. */ - private List affCaches; + private List> affCaches; /** Map cache name on key classes. */ private Map keysCacheClasses; @@ -143,7 +141,7 @@ public class IgniteCacheRandomOperationBenchmark extends IgniteAbstractBenchmark /** {@inheritDoc} */ @Override public void onException(Throwable e) { - BenchmarkUtils.errorHelp(cfg, "The benchmark of random operation failed."); + BenchmarkUtils.error("The benchmark of random operation failed.", e); super.onException(e); } @@ -162,14 +160,21 @@ public class IgniteCacheRandomOperationBenchmark extends IgniteAbstractBenchmark /** {@inheritDoc} */ @Override public void tearDown() throws Exception { - BenchmarkUtils.println("Benchmark statistics"); - for (String cacheName : ignite().cacheNames()) { - BenchmarkUtils.println(String.format("Operations over cache '%s'", cacheName)); - for (Operation op : Operation.values()) - BenchmarkUtils.println(cfg, String.format("%s: %s", op, - operationStatistics.get(op + "_" + cacheName))); + try { + BenchmarkUtils.println("Benchmark statistics"); + + for (String cacheName : ignite().cacheNames()) { + BenchmarkUtils.println(String.format("Operations over cache '%s'", cacheName)); + + for (Operation op : Operation.values()) { + BenchmarkUtils.println(cfg, String.format("%s: %s", op, + operationStatistics.get(op + "_" + cacheName))); + } + } + } + finally { + super.tearDown(); } - super.tearDown(); } /** @@ -212,26 +217,34 @@ private void searchCache() throws Exception { Collection entries = configuration.getQueryEntities(); for (QueryEntity queryEntity : entries) { - if (queryEntity.getKeyType() != null) { - Class keyCls = Class.forName(queryEntity.getKeyType()); - - if (ModelUtil.canCreateInstance(keyCls)) - keys.add(keyCls); - else - throw new IgniteException("Class is unknown for the load test. Make sure you " + - "specified its full name [clsName=" + queryEntity.getKeyType() + ']'); - } - - if (queryEntity.getValueType() != null) { - Class valCls = Class.forName(queryEntity.getValueType()); - - if (ModelUtil.canCreateInstance(valCls)) - values.add(valCls); - else - throw new IgniteException("Class is unknown for the load test. Make sure you " + - "specified its full name [clsName=" + queryEntity.getKeyType() + ']'); - - cofigureCacheSqlDescriptor(cacheName, queryEntity, valCls); + try { + if (queryEntity.getKeyType() != null) { + Class keyCls = Class.forName(queryEntity.getKeyType()); + + if (ModelUtil.canCreateInstance(keyCls)) + keys.add(keyCls); + else + throw new IgniteException("Class is unknown for the load test. Make sure you " + + "specified its full name [clsName=" + queryEntity.getKeyType() + ']'); + } + + if (queryEntity.getValueType() != null) { + Class valCls = Class.forName(queryEntity.getValueType()); + + if (ModelUtil.canCreateInstance(valCls)) + values.add(valCls); + else + throw new IgniteException("Class is unknown for the load test. Make sure you " + + "specified its full name [clsName=" + queryEntity.getKeyType() + ']'); + + cofigureCacheSqlDescriptor(cacheName, queryEntity, valCls); + } + } catch (ClassNotFoundException e) { + BenchmarkUtils.println(e.getMessage()); + BenchmarkUtils.println("This can be a BinaryObject. Ignoring exception."); + + if (!cacheSqlDescriptors.containsKey(cacheName)) + cacheSqlDescriptors.put(cacheName, new ArrayList()); } } } @@ -240,38 +253,44 @@ private void searchCache() throws Exception { Collection entries = configuration.getTypeMetadata(); for (CacheTypeMetadata cacheTypeMetadata : entries) { - if (cacheTypeMetadata.getKeyType() != null) { - Class keyCls = Class.forName(cacheTypeMetadata.getKeyType()); - - if (ModelUtil.canCreateInstance(keyCls)) - keys.add(keyCls); - else - throw new IgniteException("Class is unknown for the load test. Make sure you " + - "specified its full name [clsName=" + cacheTypeMetadata.getKeyType() + ']'); - } - - if (cacheTypeMetadata.getValueType() != null) { - Class valCls = Class.forName(cacheTypeMetadata.getValueType()); - - if (ModelUtil.canCreateInstance(valCls)) - values.add(valCls); - else - throw new IgniteException("Class is unknown for the load test. Make sure you " + - "specified its full name [clsName=" + cacheTypeMetadata.getKeyType() + ']'); + try { + if (cacheTypeMetadata.getKeyType() != null) { + Class keyCls = Class.forName(cacheTypeMetadata.getKeyType()); + + if (ModelUtil.canCreateInstance(keyCls)) + keys.add(keyCls); + else + throw new IgniteException("Class is unknown for the load test. Make sure you " + + "specified its full name [clsName=" + cacheTypeMetadata.getKeyType() + ']'); + } + + if (cacheTypeMetadata.getValueType() != null) { + Class valCls = Class.forName(cacheTypeMetadata.getValueType()); + + if (ModelUtil.canCreateInstance(valCls)) + values.add(valCls); + else + throw new IgniteException("Class is unknown for the load test. Make sure you " + + "specified its full name [clsName=" + cacheTypeMetadata.getKeyType() + ']'); + } + } catch (ClassNotFoundException e) { + BenchmarkUtils.println(e.getMessage()); + BenchmarkUtils.println("This can be a BinaryObject. Ignoring exception."); + + if (!cacheSqlDescriptors.containsKey(cacheName)) + cacheSqlDescriptors.put(cacheName, new ArrayList()); } } } - if (keys.isEmpty() || values.isEmpty()) - continue; - keysCacheClasses.put(cacheName, keys.toArray(new Class[] {})); valuesCacheClasses.put(cacheName, values.toArray(new Class[] {})); } else - keysCacheClasses.put(cacheName, - new Class[] {randomKeyClass(cacheName)}); + keysCacheClasses.put(cacheName, new Class[] {randomKeyClass(cacheName)}); + + valuesCacheClasses.put(cacheName, determineValueClasses(cacheName)); if (configuration.getCacheMode() != CacheMode.LOCAL) affCaches.add(cache); @@ -372,7 +391,7 @@ private void preLoading() throws Exception { threads[i] = new Thread() { @Override public void run() { - try (IgniteDataStreamer dataLdr = ignite().dataStreamer(cacheName)) { + try (IgniteDataStreamer dataLdr = ignite().dataStreamer(cacheName)) { for (int i = 0; i < args.preloadAmount() && !isInterrupted(); i++) dataLdr.addData(createRandomKey(i, cacheName), createRandomValue(i, cacheName)); } @@ -394,30 +413,30 @@ private void preLoading() throws Exception { */ private Map> personCachePartitions(String cacheName) { // Getting affinity for person cache. - Affinity affinity = ignite().affinity(cacheName); + Affinity affinity = ignite().affinity(cacheName); // Building a list of all partitions numbers. - List randmPartitions = new ArrayList<>(10); + List rndParts = new ArrayList<>(10); - if (affinity.partitions() <= SCAN_QUERY_PARTITIN_AMOUNT) + if (affinity.partitions() <= SCAN_QUERY_PARTITION_AMOUNT) for (int i = 0; i < affinity.partitions(); i++) - randmPartitions.add(i); + rndParts.add(i); else { - for (int i = 0; i < SCAN_QUERY_PARTITIN_AMOUNT; i++) { + for (int i = 0; i < SCAN_QUERY_PARTITION_AMOUNT; i++) { int partNum; do partNum = nextRandom(affinity.partitions()); - while (randmPartitions.contains(partNum)); + while (rndParts.contains(partNum)); - randmPartitions.add(partNum); + rndParts.add(partNum); } } - Collections.sort(randmPartitions); + Collections.sort(rndParts); // Getting partition to node mapping. - Map partPerNodes = affinity.mapPartitionsToNodes(randmPartitions); + Map partPerNodes = affinity.mapPartitionsToNodes(rndParts); // Building node to partitions mapping. Map> nodesToPart = new HashMap<>(); @@ -453,13 +472,21 @@ private Object createRandomKey(int id, String cacheName) { * @return Random key class. */ private Class randomKeyClass(String cacheName) { - Class[] keys = keysCacheClasses.containsKey(cacheName) ? keysCacheClasses.get(cacheName) : ModelUtil.keyClasses(); return keys[nextRandom(keys.length)]; } + /** + * @param cacheName Cache name. + * @return Set classes for cache. + */ + private Class[] determineValueClasses(@NotNull String cacheName) { + return cacheName.toLowerCase().contains("fat-values") ? ModelUtil.fatValueClasses() : + ModelUtil.simpleValueClasses(); + } + /** * @param id Object identifier. * @param cacheName Name of Ignite cache. @@ -476,7 +503,6 @@ private Object createRandomValue(int id, String cacheName) { * @return Random value class. */ private Class randomValueClass(String cacheName) { - Class[] values = valuesCacheClasses.containsKey(cacheName) ? valuesCacheClasses.get(cacheName) : ModelUtil.valueClasses(); @@ -489,7 +515,7 @@ private Class randomValueClass(String cacheName) { * @throws Exception If fail. */ private void executeOutOfTx(Map map, boolean withoutTransactionCache) throws Exception { - for (IgniteCache cache : availableCaches) { + for (IgniteCache cache : availableCaches) { if (withoutTransactionCache && txCaches.contains(cache)) continue; @@ -502,7 +528,7 @@ private void executeOutOfTx(Map map, boolean withoutTransactionC * @param cache Ignite cache. * @throws Exception If fail. */ - private void executeRandomOperation(Map map, IgniteCache cache) throws Exception { + private void executeRandomOperation(Map map, IgniteCache cache) throws Exception { Operation op = nextRandomOperation(); switch (op) { @@ -621,9 +647,8 @@ private void executeInTransaction(final Map map) throws Exceptio TransactionIsolation.fromOrdinal(nextRandom(TransactionIsolation.values().length)), new Callable() { - @Override - public Object call() throws Exception { - for (IgniteCache cache : txCaches) + @Override public Object call() throws Exception { + for (IgniteCache cache : txCaches) if (nextBoolean()) executeRandomOperation(map, cache); @@ -636,7 +661,7 @@ public Object call() throws Exception { * @param cache Ignite cache. * @throws Exception If failed. */ - private void doPut(IgniteCache cache) throws Exception { + private void doPut(IgniteCache cache) throws Exception { int i = nextRandom(args.range()); cache.put(createRandomKey(i, cache.getName()), createRandomValue(i, cache.getName())); @@ -646,8 +671,8 @@ private void doPut(IgniteCache cache) throws Exception { * @param cache Ignite cache. * @throws Exception If failed. */ - private void doPutAll(IgniteCache cache) throws Exception { - Map putMap = new TreeMap(); + private void doPutAll(IgniteCache cache) throws Exception { + Map putMap = new TreeMap<>(); Class keyCass = randomKeyClass(cache.getName()); @@ -664,7 +689,7 @@ private void doPutAll(IgniteCache cache) throws Exception { * @param cache Ignite cache. * @throws Exception If failed. */ - private void doGet(IgniteCache cache) throws Exception { + private void doGet(IgniteCache cache) throws Exception { int i = nextRandom(args.range()); cache.get(createRandomKey(i, cache.getName())); @@ -674,8 +699,8 @@ private void doGet(IgniteCache cache) throws Exception { * @param cache Ignite cache. * @throws Exception If failed. */ - private void doGetAll(IgniteCache cache) throws Exception { - Set keys = new TreeSet(); + private void doGetAll(IgniteCache cache) throws Exception { + Set keys = new TreeSet<>(); Class keyCls = randomKeyClass(cache.getName()); @@ -692,7 +717,7 @@ private void doGetAll(IgniteCache cache) throws Exception { * @param cache Ignite cache. * @throws Exception If failed. */ - private void doInvoke(final IgniteCache cache) throws Exception { + private void doInvoke(final IgniteCache cache) throws Exception { final int i = nextRandom(args.range()); if (nextBoolean()) @@ -703,54 +728,54 @@ private void doInvoke(final IgniteCache cache) throws Exception { } -/** - * Entry processor for local benchmark replace value task. - */ -private static class BenchmarkReplaceValueEntryProcessor implements EntryProcessor, Serializable { - /** - * New value for update during process by default. - */ - private Object newVal; - /** - * @param newVal default new value + * Entry processor for local benchmark replace value task. */ - private BenchmarkReplaceValueEntryProcessor(Object newVal) { - this.newVal = newVal; - } + private static class BenchmarkReplaceValueEntryProcessor implements EntryProcessor, Serializable { + /** + * New value for update during process by default. + */ + private Object newVal; + + /** + * @param newVal default new value + */ + private BenchmarkReplaceValueEntryProcessor(Object newVal) { + this.newVal = newVal; + } - /** {@inheritDoc} */ - @Override public Object process(MutableEntry entry, Object... arguments) throws EntryProcessorException { - Object newVal = arguments == null || arguments[0] == null ? this.newVal : arguments[0]; + /** {@inheritDoc} */ + @Override public Object process(MutableEntry entry, Object... arguments) { + Object newVal = arguments == null || arguments[0] == null ? this.newVal : arguments[0]; - Object oldVal = entry.getValue(); + Object oldVal = entry.getValue(); - entry.setValue(newVal); + entry.setValue(newVal); - return oldVal; + return oldVal; + } } -} -/** - * Entry processor for local benchmark remove entry task. - */ -private static class BenchmarkRemoveEntryProcessor implements EntryProcessor, Serializable { - /** {@inheritDoc} */ - @Override public Object process(MutableEntry entry, Object... arguments) throws EntryProcessorException { - Object oldVal = entry.getValue(); + /** + * Entry processor for local benchmark remove entry task. + */ + private static class BenchmarkRemoveEntryProcessor implements EntryProcessor, Serializable { + /** {@inheritDoc} */ + @Override public Object process(MutableEntry entry, Object... arguments) { + Object oldVal = entry.getValue(); - entry.remove(); + entry.remove(); - return oldVal; + return oldVal; + } } -} /** * @param cache Ignite cache. * @throws Exception If failed. */ - private void doInvokeAll(final IgniteCache cache) throws Exception { - Map map = new TreeMap<>(); + private void doInvokeAll(IgniteCache cache) throws Exception { + Map> map = new TreeMap<>(); Class keyCls = randomKeyClass(cache.getName()); @@ -773,7 +798,7 @@ private void doInvokeAll(final IgniteCache cache) throws Exception { * @param cache Ignite cache. * @throws Exception If failed. */ - private void doRemove(IgniteCache cache) throws Exception { + private void doRemove(IgniteCache cache) throws Exception { int i = nextRandom(args.range()); cache.remove(createRandomKey(i, cache.getName())); @@ -783,8 +808,9 @@ private void doRemove(IgniteCache cache) throws Exception { * @param cache Ignite cache. * @throws Exception If failed. */ - private void doRemoveAll(IgniteCache cache) throws Exception { - Set keys = new TreeSet(); + private void doRemoveAll(IgniteCache cache) throws Exception { + Set keys = new TreeSet<>(); + Class keyCls = randomKeyClass(cache.getName()); for (int cnt = 0; cnt < args.batch(); cnt++) { @@ -800,7 +826,7 @@ private void doRemoveAll(IgniteCache cache) throws Exception { * @param cache Ignite cache. * @throws Exception If failed. */ - private void doPutIfAbsent(IgniteCache cache) throws Exception { + private void doPutIfAbsent(IgniteCache cache) throws Exception { int i = nextRandom(args.range()); cache.putIfAbsent(createRandomKey(i, cache.getName()), createRandomValue(i, cache.getName())); @@ -810,7 +836,7 @@ private void doPutIfAbsent(IgniteCache cache) throws Exception { * @param cache Ignite cache. * @throws Exception If failed. */ - private void doReplace(IgniteCache cache) throws Exception { + private void doReplace(IgniteCache cache) throws Exception { int i = nextRandom(args.range()); cache.replace(createRandomKey(i, cache.getName()), @@ -822,7 +848,7 @@ private void doReplace(IgniteCache cache) throws Exception { * @param cache Ignite cache. * @throws Exception If failed. */ - private void doScanQuery(IgniteCache cache) throws Exception { + private void doScanQuery(IgniteCache cache) throws Exception { if (!affCaches.contains(cache)) return; @@ -841,33 +867,62 @@ private void doScanQuery(IgniteCache cache) throws Exception { * @param cache Ignite cache. * @throws Exception If failed. */ - private void doSqlQuery(IgniteCache cache) throws Exception { + private void doSqlQuery(IgniteCache cache) throws Exception { List descriptors = cacheSqlDescriptors.get(cache.getName()); - if (descriptors != null && !descriptors.isEmpty()) { - SqlCacheDescriptor randomDesc = descriptors.get(nextRandom(descriptors.size())); + if (descriptors != null) { + Query sq = null; - int id = nextRandom(args.range()); + if (queries.isEmpty()) { + if (!descriptors.isEmpty()) { + SqlCacheDescriptor randomDesc = descriptors.get(nextRandom(descriptors.size())); - Query sq; - if (queries.isEmpty()) - sq = nextBoolean() ? randomDesc.getSqlQuery(id) : randomDesc.getSqlFieldsQuery(id); - else - sq = new SqlFieldsQuery(queries.get(nextRandom(queries.size()))); + int id = nextRandom(args.range()); + + sq = nextBoolean() ? randomDesc.getSqlQuery(id) : randomDesc.getSqlFieldsQuery(id); + } + } + else { + String sql = rendomizeSql(); + + BenchmarkUtils.println(sql); - try (QueryCursor cursor = cache.query(sq)) { - for (Object obj : cursor) - ; + sq = new SqlFieldsQuery(sql); } + + if (sq != null) + try (QueryCursor cursor = cache.query(sq)) { + for (Object obj : cursor) { + // No-op. + } + } } } + /** + * @return SQL string. + */ + private String rendomizeSql() { + String sql = queries.get(nextRandom(queries.size())); + + int count = StringUtils.countOccurrencesOf(sql, "%s"); + + Integer[] sub = new Integer[count]; + + for (int i=0; i map) throws Exception { + private void doContinuousQuery(IgniteCache cache, Map map) throws Exception { List cursors = (ArrayList)map.get(cache.getName()); if (cursors == null) { @@ -890,260 +945,257 @@ private void doContinuousQuery(IgniteCache cache, Map map) throw cursors.add(cache.query(qry)); } -/** - * Continuous query updater class. - */ -private static class ContinuousQueryUpdater implements CacheEntryUpdatedListener, Serializable { - - /** {@inheritDoc} */ - @Override public void onUpdated(Iterable iterable) throws CacheEntryListenerException { - for (Object o : iterable) - ; + /** + * @return Nex random boolean value. + */ + private boolean nextBoolean() { + return ThreadLocalRandom.current().nextBoolean(); } -} - -/** - * Continuous query filter class. - */ -private static class ContinuousQueryFilter implements CacheEntryEventSerializableFilter, Serializable { /** - * Flag. + * Continuous query updater class. */ - private boolean flag = true; + private static class ContinuousQueryUpdater implements CacheEntryUpdatedListener, Serializable { - /** {@inheritDoc} */ - @Override public boolean evaluate(CacheEntryEvent evt) throws CacheEntryListenerException { - return flag =! flag; + /** {@inheritDoc} */ + @Override public void onUpdated(Iterable iterable) throws CacheEntryListenerException { + for (Object o : iterable) { + // No-op. + } + } } -} -/** - * Closure for scan query executing. - */ -private static class ScanQueryBroadcastClosure implements IgniteRunnable { /** - * Ignite node. + * Continuous query filter class. */ - @IgniteInstanceResource - private Ignite node; + private static class ContinuousQueryFilter implements CacheEntryEventSerializableFilter, Serializable { + /** */ + private boolean flag = true; - /** - * Information about partition. - */ - private Map> cachePart; + /** {@inheritDoc} */ + @Override public boolean evaluate(CacheEntryEvent evt) throws CacheEntryListenerException { + return flag =! flag; + } + } /** - * Name of Ignite cache. + * Closure for scan query executing. */ - private String cacheName; + private static class ScanQueryBroadcastClosure implements IgniteRunnable { + /** + * Ignite node. + */ + @IgniteInstanceResource + private Ignite node; + + /** + * Information about partition. + */ + private Map> cachePart; + + /** + * Name of Ignite cache. + */ + private String cacheName; + + /** + * @param cacheName Name of Ignite cache. + * @param cachePart Partition by node for Ignite cache. + */ + private ScanQueryBroadcastClosure(String cacheName, Map> cachePart) { + this.cachePart = cachePart; + this.cacheName = cacheName; + } - /** - * @param cacheName Name of Ignite cache. - * @param cachePart Partition by node for Ignite cache. - */ - private ScanQueryBroadcastClosure(String cacheName, Map> cachePart) { - this.cachePart = cachePart; - this.cacheName = cacheName; - } + /** {@inheritDoc} */ + @Override public void run() { + IgniteCache cache = node.cache(cacheName); - /** {@inheritDoc} */ - @Override public void run() { - IgniteCache cache = node.cache(cacheName); + // Getting a list of the partitions owned by this node. + List myPartitions = cachePart.get(node.cluster().localNode().id()); - // Getting a list of the partitions owned by this node. - List myPartitions = cachePart.get(node.cluster().localNode().id()); + for (Integer part : myPartitions) { - for (Integer part : myPartitions) { + ScanQuery scanQry = new ScanQuery(); - ScanQuery scanQry = new ScanQuery(); + scanQry.setPartition(part); - scanQry.setPartition(part); + scanQry.setFilter(igniteBiPred); - scanQry.setFilter(igniteBiPred); + try (QueryCursor cursor = cache.query(scanQry)) { + for (Object obj : cursor) { + // No-op. + } + } - try (QueryCursor cursor = cache.query(scanQry)) { - for (Object obj : cursor) - ; } - } } -} - -/** - * Scan query predicate class. - */ -private static class BenchmarkIgniteBiPredicate implements IgniteBiPredicate { /** - * @param key Cache key. - * @param val Cache value. - * @return true If is hit. + * Scan query predicate class. */ - @Override public boolean apply(Object key, Object val) { - return true; + private static class BenchmarkIgniteBiPredicate implements IgniteBiPredicate { + /** + * @param key Cache key. + * @param val Cache value. + * @return true If is hit. + */ + @Override public boolean apply(Object key, Object val) { + return true; + } } -} - -/** - * Query descriptor. - */ -private static class SqlCacheDescriptor { - - /** - * Class of value. - */ - private Class valCls; - - /** - * Select fields. - */ - private Set fields; /** - * Indexed fields. - */ - private Map indexedFieldsByCls; - - /** - * @param valCls Class of value. - * @param fields All select fields. - * @param indexedFieldsByCls Indexed fields. + * Query descriptor. */ - SqlCacheDescriptor(Class valCls, Set fields, - Map indexedFieldsByCls) { - this.valCls = valCls; - this.fields = fields; - this.indexedFieldsByCls = indexedFieldsByCls; - } + private static class SqlCacheDescriptor { + /** + * Class of value. + */ + private Class valCls; + + /** + * Select fields. + */ + private Set fields; + + /** + * Indexed fields. + */ + private Map indexedFieldsByCls; + + /** + * @param valCls Class of value. + * @param fields All select fields. + * @param indexedFieldsByCls Indexed fields. + */ + SqlCacheDescriptor(Class valCls, Set fields, + Map indexedFieldsByCls) { + this.valCls = valCls; + this.fields = fields; + this.indexedFieldsByCls = indexedFieldsByCls; + } - /** - * @param id Query id. - * @return Condition string. - */ - private String makeQuerySelect(int id) { - return StringUtils.collectionToDelimitedString(fields, ", "); - } + /** + * @param id Query id. + * @return Condition string. + */ + private String makeQuerySelect(int id) { + return StringUtils.collectionToDelimitedString(fields, ", "); + } - /** - * @param id Query id. - * @return Condition string. - */ - private String makeQueryCondition(int id) { - StringBuilder sb = new StringBuilder(); + /** + * @param id Query id. + * @return Condition string. + */ + private String makeQueryCondition(int id) { + StringBuilder sb = new StringBuilder(); - for (String iField : indexedFieldsByCls.keySet()) { - Class cl = indexedFieldsByCls.get(iField); + for (String iField : indexedFieldsByCls.keySet()) { + Class cl = indexedFieldsByCls.get(iField); - if (!Number.class.isAssignableFrom(cl) && !String.class.equals(cl)) - continue; + if (!Number.class.isAssignableFrom(cl) && !String.class.equals(cl)) + continue; - if (sb.length() != 0) { - switch (id % 3 % 2) { - case 0: - sb.append(" OR "); - break; - case 1: - sb.append(" AND "); - break; + if (sb.length() != 0) { + switch (id % 3 % 2) { + case 0: + sb.append(" OR "); + break; + case 1: + sb.append(" AND "); + break; + } } - } - if (Number.class.isAssignableFrom(cl)) { - sb.append(iField); - switch (id % 2) { - case 0: - sb.append(" > "); - break; - case 1: - sb.append(" < "); - break; + if (Number.class.isAssignableFrom(cl)) { + sb.append(iField); + switch (id % 2) { + case 0: + sb.append(" > "); + break; + case 1: + sb.append(" < "); + break; + } + sb.append(id); } - sb.append(id); + else if (String.class.equals(cl)) + sb.append("lower(").append(iField).append(") LIKE lower('%").append(id).append("%')"); + } - else if (String.class.equals(cl)) - sb.append("lower(").append(iField).append(") LIKE lower('%").append(id).append("%')"); + return sb.toString(); + } + /** + * @param id Query id. + * @return SQL query object. + */ + SqlQuery getSqlQuery(int id) { + return new SqlQuery(valCls, makeQueryCondition(id)); } - return sb.toString(); - } - /** - * @param id Query id. - * @return SQL query object. - */ - SqlQuery getSqlQuery(int id) { - return new SqlQuery(valCls, makeQueryCondition(id)); - } + /** + * @param id Query id. + * @return SQL filed query object. + */ + SqlFieldsQuery getSqlFieldsQuery(int id) { + return new SqlFieldsQuery(String.format("SELECT %s FROM %s WHERE %s", + makeQuerySelect(id), valCls.getSimpleName(), makeQueryCondition(id))); + } - /** - * @param id Query id. - * @return SQL filed query object. - */ - SqlFieldsQuery getSqlFieldsQuery(int id) { - return new SqlFieldsQuery(String.format("SELECT %s FROM %s WHERE %s", - makeQuerySelect(id), valCls.getSimpleName(), makeQueryCondition(id))); } -} - /** - * @return Nex random boolean value. + * Cache operation enum. */ - private boolean nextBoolean() { - return ThreadLocalRandom.current().nextBoolean(); - } + private enum Operation { + /** Put operation. */ + PUT, -/** - * Cache operation enum. - */ -private static enum Operation { - /** Put operation. */ - PUT, + /** Put all operation. */ + PUT_ALL, - /** Put all operation. */ - PUT_ALL, + /** Get operation. */ + GET, - /** Get operation. */ - GET, + /** Get all operation. */ + GET_ALL, - /** Get all operation. */ - GET_ALL, + /** Invoke operation. */ + INVOKE, - /** Invoke operation. */ - INVOKE, + /** Invoke all operation. */ + INVOKE_ALL, - /** Invoke all operation. */ - INVOKE_ALL, + /** Remove operation. */ + REMOVE, - /** Remove operation. */ - REMOVE, + /** Remove all operation. */ + REMOVE_ALL, - /** Remove all operation. */ - REMOVE_ALL, + /** Put if absent operation. */ + PUT_IF_ABSENT, - /** Put if absent operation. */ - PUT_IF_ABSENT, + /** Replace operation. */ + REPLACE, - /** Replace operation. */ - REPLACE, + /** Scan query operation. */ + SCAN_QUERY, - /** Scan query operation. */ - SCAN_QUERY, + /** SQL query operation. */ + SQL_QUERY, - /** SQL query operation. */ - SQL_QUERY, + /** Continuous Query. */ + CONTINUOUS_QUERY; - /** Continuous Query. */ - CONTINUOUS_QUERY; - - /** - * @param num Number of operation. - * @return Operation. - */ - public static Operation valueOf(int num) { - return values()[num]; + /** + * @param num Number of operation. + * @return Operation. + */ + public static Operation valueOf(int num) { + return values()[num]; + } } } -}