初回トライアルehcache 2.4に新しく追加された検索アプリ(search appi、EQL)
もっと読む
本文を読む時は、基本的にehcacheの配置、使用(分布式の配置を含む)を把握したほうがいいです.基礎的な配置過程、概念については詳しく説明しません.
ehcache 2.4バージョンから検索アプリをサポートしています.キーと値の中から任意の複雑な論理条件で必要な検索結果(EQLを通じて)を得ることができます.普段キャッシュされているvalueで自分の必要な結果を探したいです.一番愚かなforサイクルを使っています.EQLがあって、本当に素晴らしいです.シングルマシンと分散キャッシュの両方が使えます.公式の設定文書は非常に慌ただしいです.ehcache.xmlはehcachel.xmlと書いてあります.公式サイトのコードもダウンロードできません.
jarカバンはもう必要ないです.2.4を追加した900 K余りのjarにはもう新しい種類が含まれています.例えば、Results、Queryなどです.
幸い、分散キャッシュは正常に動作します.ノードを追加すればいいです.
overflowToDisk=「false」
class=「net.sf.ehcache.distributions.RMICachereplicator Factory」
properties=「replicate Asynch ronously=true、replicate Puts=true、replicapts ViaCopy=true、replicate Updates=true、replicate Updates=true、replicapdates Copy=true、replicavatrue=」
テストで遭遇したいくつかの問題とその解決方法:
1.Query is frozen and cannot be mutted
解決:createQuery()….end();endを除く()
2.No results specified.Please specify one or more of include Keys()、include Values()、include Aggator()or include Attribute()
解決:
query=obj.create Query()
query.include Keys()
query.addCriteria(Query.KEY.eq(sCacheId)
query.maxResults(1000)
results=query.execute()
3.isSearchable()はfalseです.
解決:設定後にサーバーを再起動します.つまり私はこのように解決しました.分散キャッシュでも正常に使えます.
基本的にテストコードを実行してもいいです.もちろんcacheに入れるvalueは簡単な値ではなく、pojo(dto,vo)です.この公式文書にも詳しく紹介されています.atributeを配置して、少しコード化すればいいです.
お問い合わせの内容をカウントしたい場合(Average,Count,Max,Min,Sum)は、参照できます.
http://ehcache.org/xref/net/sf/ehcache/search/aggregator/package-frame.html
公式テストの1万本の結果は、62 ms、125 ms、180 msで、現在のプロジェクトの中では十分だと思われます.彼は調査が100万以下であることを提案します.
使用するjunnitの例:
公式文書リンク:
http://www.ehcache.org/documentation/search.html
本文を読む時は、基本的にehcacheの配置、使用(分布式の配置を含む)を把握したほうがいいです.基礎的な配置過程、概念については詳しく説明しません.
ehcache 2.4バージョンから検索アプリをサポートしています.キーと値の中から任意の複雑な論理条件で必要な検索結果(EQLを通じて)を得ることができます.普段キャッシュされているvalueで自分の必要な結果を探したいです.一番愚かなforサイクルを使っています.EQLがあって、本当に素晴らしいです.シングルマシンと分散キャッシュの両方が使えます.公式の設定文書は非常に慌ただしいです.ehcache.xmlはehcachel.xmlと書いてあります.公式サイトのコードもダウンロードできません.
jarカバンはもう必要ないです.2.4を追加した900 K余りのjarにはもう新しい種類が含まれています.例えば、Results、Queryなどです.
幸い、分散キャッシュは正常に動作します.ノードを追加すればいいです.
overflowToDisk=「false」
class=「net.sf.ehcache.distributions.RMICachereplicator Factory」
properties=「replicate Asynch ronously=true、replicate Puts=true、replicapts ViaCopy=true、replicate Updates=true、replicate Updates=true、replicapdates Copy=true、replicavatrue=」
テストで遭遇したいくつかの問題とその解決方法:
1.Query is frozen and cannot be mutted
解決:createQuery()….end();endを除く()
2.No results specified.Please specify one or more of include Keys()、include Values()、include Aggator()or include Attribute()
解決:
query=obj.create Query()
query.include Keys()
query.addCriteria(Query.KEY.eq(sCacheId)
query.maxResults(1000)
results=query.execute()
3.isSearchable()はfalseです.
解決:設定後にサーバーを再起動します.つまり私はこのように解決しました.分散キャッシュでも正常に使えます.
基本的にテストコードを実行してもいいです.もちろんcacheに入れるvalueは簡単な値ではなく、pojo(dto,vo)です.この公式文書にも詳しく紹介されています.atributeを配置して、少しコード化すればいいです.
お問い合わせの内容をカウントしたい場合(Average,Count,Max,Min,Sum)は、参照できます.
http://ehcache.org/xref/net/sf/ehcache/search/aggregator/package-frame.html
公式テストの1万本の結果は、62 ms、125 ms、180 msで、現在のプロジェクトの中では十分だと思われます.彼は調査が100万以下であることを提案します.
使用するjunnitの例:
package net.sf.ehcache.search;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.Test;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration;
import net.sf.ehcache.config.SearchAttribute;
import net.sf.ehcache.config.Searchable;
import net.sf.ehcache.search.Person.Gender;
import net.sf.ehcache.search.aggregator.Aggregator;
import net.sf.ehcache.search.aggregator.AggregatorException;
import net.sf.ehcache.search.aggregator.AggregatorInstance;
import net.sf.ehcache.search.expression.Or;
public class BasicSearchTest {
@Test
public void testInvalidConfiguration() {
try {
new CacheManager(getClass().getResource("/ehcache-search-invalid-key.xml"));
fail();
} catch (CacheException ce) {
// expected
}
try {
new CacheManager(getClass().getResource("/ehcache-search-invalid-value.xml"));
fail();
} catch (CacheException ce) {
// expected
}
}
@Test
public void testNonSearchableCache() {
CacheManager cacheManager = new CacheManager(getClass().getResource("/ehcache-search.xml"));
Ehcache cache = cacheManager.getEhcache("not-searchable");
assertFalse(cache.isSearchable());
try {
cache.createQuery();
fail();
} catch (CacheException e) {
// expected
}
}
@Test
public void testDefaultSearchableCache() {
CacheManager cacheManager = new CacheManager(getClass().getResource("/ehcache-search.xml"));
Ehcache cache = cacheManager.getEhcache("default-searchable");
assertTrue(cache.isSearchable());
cache.put(new Element("key", new Object()));
cache.put(new Element(new Object(), "value"));
cache.put(new Element(new Object(), new Object()));
cache.put(new Element(null, null));
Query query;
Results results;
query = cache.createQuery();
query.includeKeys();
query.addCriteria(Query.KEY.eq("key")).end();
results = query.execute();
assertEquals(1, results.size());
assertEquals("key", results.all().iterator().next().getKey());
query = cache.createQuery();
query.includeKeys();
query.addCriteria(Query.VALUE.eq("value")).end();
results = query.execute();
assertEquals(1, results.size());
Object key = results.all().iterator().next().getKey();
assertEquals("value", cache.get(key).getObjectValue());
}
@Test
public void testQueryBuilder() {
CacheManager cacheManager = new CacheManager(getClass().getResource("/ehcache-search.xml"));
Ehcache cache = cacheManager.getEhcache("cache1");
Query query1 = cache.createQuery();
Query query2 = cache.createQuery();
// query instances should be unique
assertFalse(query1 == query2);
// null checks
try {
query1.addCriteria(null);
fail();
} catch (NullPointerException npe) {
// expected
}
try {
query1.addOrderBy(null, Direction.ASCENDING);
fail();
} catch (NullPointerException npe) {
// expected
}
try {
query1.addOrderBy(new Attribute("foo"), null);
fail();
} catch (NullPointerException npe) {
// expected
}
try {
query1.includeAggregator((Aggregator[]) null);
fail();
} catch (NullPointerException npe) {
// expected
}
try {
query1.includeAttribute((Attribute[]) null);
fail();
} catch (NullPointerException npe) {
// expected
}
try {
query1.includeAttribute(new Attribute[]{new Attribute("foo"), null});
fail();
} catch (NullPointerException npe) {
// expected
}
// freeze query
query1.end();
try {
query1.addCriteria(new Attribute("foo").le(35));
fail();
} catch (SearchException se) {
// expected
}
try {
query1.addOrderBy(new Attribute("foo"), Direction.ASCENDING);
fail();
} catch (SearchException se) {
// expected
}
try {
query1.includeAggregator(new Attribute("foo").max());
fail();
} catch (SearchException se) {
// expected
}
try {
query1.includeAttribute(new Attribute("foo"));
fail();
} catch (SearchException se) {
// expected
}
try {
query1.includeKeys();
fail();
} catch (SearchException se) {
// expected
}
try {
query1.maxResults(3);
fail();
} catch (SearchException se) {
// expected
}
}
@Test
public void testRange() {
CacheManager cacheManager = new CacheManager(getClass().getResource("/ehcache-search.xml"));
Ehcache cache = cacheManager.getEhcache("cache1");
SearchTestUtil.populateData(cache);
Query query = cache.createQuery();
query.includeKeys();
query.end();
Results results = query.execute();
assertEquals(4, results.all().size());
List keys = new ArrayList();
for (int i = 0; i < 4; i++) {
List range = results.range(i, 1);
assertEquals(1, range.size());
keys.add((Integer) range.get(0).getKey());
}
assertEquals(4, keys.size());
for (int i = 0; i < 4; i++) {
assertEquals(0, results.range(i, 0).size());
}
assertEquals(0, results.range(0, 0).size());
assertEquals(1, results.range(0, 1).size());
assertEquals(2, results.range(0, 2).size());
assertEquals(3, results.range(0, 3).size());
assertEquals(4, results.range(0, 4).size());
assertEquals(4, results.range(0, 5).size());
assertEquals(4, results.range(0, Integer.MAX_VALUE).size());
try {
results.range(-1, 1);
fail();
} catch (IllegalArgumentException iae) {
// expected
}
try {
results.range(0, -1);
fail();
} catch (IllegalArgumentException iae) {
// expected
}
}
@Test
public void testBasic() {
CacheManager cacheManager = new CacheManager(getClass().getResource("/ehcache-search.xml"));
// uses expression attribute extractors
basicQueries(cacheManager.getEhcache("cache1"));
// uses a "custom" attribute extractor too
basicQueries(cacheManager.getEhcache("cache2"));
// uses bean attributes
basicQueries(cacheManager.getEhcache("bean-attributes"));
}
@Test
public void testCustomAggregator() {
CacheManager cacheManager = new CacheManager(getClass().getResource("/ehcache-search.xml"));
Ehcache cache = cacheManager.getEhcache("cache1");
SearchTestUtil.populateData(cache);
Attribute age = cache.getSearchAttribute("age");
Query query = cache.createQuery();
query.includeAggregator(new Aggregator() {
public AggregatorInstance createInstance() {
return new AggregatorInstance() {
private int doubledSum;
public void accept(Object input) throws AggregatorException {
if (doubledSum == 0) {
doubledSum = (2 * (Integer) input);
} else {
doubledSum += (2 * (Integer) input);
}
}
public Integer aggregateResult() {
return doubledSum;
}
public Attribute> getAttribute() {
return new Attribute("age");
}
};
}
});
query.end();
Results results = query.execute();
assertEquals(1, results.size());
for (Result result : results.all()) {
assertEquals(246, result.getAggregatorResults().get(0));
}
}
@Test
public void testBuiltinFunctions() {
CacheManager cacheManager = new CacheManager(getClass().getResource("/ehcache-search.xml"));
Ehcache cache = cacheManager.getEhcache("cache1");
SearchTestUtil.populateData(cache);
Attribute age = cache.getSearchAttribute("age");
{
Query query = cache.createQuery();
query.includeAggregator(age.count());
query.end();
Results results = query.execute();
assertTrue(results.hasAggregators());
assertEquals(1, results.size());
for (Result result : results.all()) {
assertEquals(4, result.getAggregatorResults().get(0));
}
}
{
Query query = cache.createQuery();
query.includeAggregator(age.max());
query.end();
Results results = query.execute();
assertTrue(results.hasAggregators());
assertEquals(1, results.size());
for (Result result : results.all()) {
assertEquals(35, result.getAggregatorResults().get(0));
}
}
{
Query query = cache.createQuery();
query.includeAggregator(age.min());
query.end();
Results results = query.execute();
assertTrue(results.hasAggregators());
assertEquals(1, results.size());
for (Result result : results.all()) {
assertEquals(23, result.getAggregatorResults().get(0));
}
}
{
Query query = cache.createQuery();
query.includeAggregator(age.sum());
query.end();
Results results = query.execute();
assertTrue(results.hasAggregators());
assertEquals(1, results.size());
for (Result result : results.all()) {
assertEquals(123L, result.getAggregatorResults().get(0));
}
}
{
Query query = cache.createQuery();
query.includeAggregator(age.average());
query.end();
Results results = query.execute();
assertTrue(results.hasAggregators());
assertEquals(1, results.size());
for (Result result : results.all()) {
assertEquals(30.75F, result.getAggregatorResults().get(0));
}
}
{
// multiple aggregators
Query query = cache.createQuery();
query.includeAggregator(age.min());
query.includeAggregator(age.max());
query.end();
Results results = query.execute();
assertTrue(results.hasAggregators());
assertEquals(1, results.size());
for (Result result : results.all()) {
assertEquals(23, result.getAggregatorResults().get(0));
assertEquals(35, result.getAggregatorResults().get(1));
}
}
{
// use criteria with an aggregator
Query query = cache.createQuery();
query.includeAggregator(age.average());
query.addCriteria(age.between(0, 32));
query.end();
Results results = query.execute();
assertTrue(results.hasAggregators());
assertEquals(1, results.size());
for (Result result : results.all()) {
assertEquals(26.5F, result.getAggregatorResults().get(0));
}
}
{
// includeKeys in addition to an aggregator
Query query = cache.createQuery();
query.includeKeys();
query.includeAggregator(age.average());
query.addCriteria(age.between(0, 32));
query.end();
Results results = query.execute();
assertTrue(results.hasAggregators());
assertTrue(results.hasKeys());
assertEquals(2, results.size());
for (Result result : results.all()) {
assertEquals(26.5F, result.getAggregatorResults().get(0));
}
verify(cache, query, 2, 4);
}
{
// execute query twice
Query query = cache.createQuery();
query.includeAggregator(age.count());
query.end();
Results results = query.execute();
assertTrue(results.hasAggregators());
assertFalse(results.hasKeys());
for (Result result : results.all()) {
assertEquals(4, result.getAggregatorResults().get(0));
}
results = query.execute();
assertTrue(results.hasAggregators());
assertFalse(results.hasKeys());
for (Result result : results.all()) {
assertEquals(4, result.getAggregatorResults().get(0));
}
}
}
@Test
public void testMaxResults() {
CacheManager cacheManager = new CacheManager(getClass().getResource("/ehcache-search.xml"));
Ehcache cache = cacheManager.getEhcache("cache1");
SearchTestUtil.populateData(cache);
Attribute age = cache.getSearchAttribute("age");
Attribute gender = cache.getSearchAttribute("gender");
Query query = cache.createQuery();
query.includeKeys();
query.addCriteria(age.ne(35));
query.maxResults(1);
query.end();
Results results = query.execute();
assertEquals(1, results.size());
for (Result result : results.all()) {
switch ((Integer) result.getKey()) {
case 2:
case 4: {
break;
}
default: {
throw new AssertionError(result.getKey());
}
}
}
query = cache.createQuery();
query.includeKeys();
query.addCriteria(age.ne(35));
query.maxResults(0);
query.end();
results = query.execute();
assertEquals(0, results.size());
query = cache.createQuery();
query.includeKeys();
query.addCriteria(age.ne(35));
query.maxResults(2);
query.end();
results = query.execute();
assertEquals(2, results.size());
query = cache.createQuery();
query.includeKeys();
query.addCriteria(age.ne(35));
query.maxResults(2);
query.end();
results = query.execute();
assertEquals(2, results.size());
query = cache.createQuery();
query.includeKeys();
query.addCriteria(age.ne(35));
query.maxResults(-1);
query.end();
results = query.execute();
assertEquals(2, results.size());
}
@Test
public void testAttributeQuery() {
CacheManager cacheManager = new CacheManager(getClass().getResource("/ehcache-search.xml"));
Ehcache cache = cacheManager.getEhcache("cache1");
SearchTestUtil.populateData(cache);
Attribute age = cache.getSearchAttribute("age");
Attribute gender = cache.getSearchAttribute("gender");
Query query = cache.createQuery();
// not including keys
query.addCriteria(age.ne(35));
query.includeAttribute(age, gender);
query.end();
Results results = query.execute();
assertFalse(results.hasKeys());
assertFalse(results.hasAggregators());
assertTrue(results.hasAttributes());
for (Result result : results.all()) {
try {
result.getKey();
fail();
} catch (SearchException se) {
// expected
}
try {
result.getKey();
fail();
} catch (SearchException se) {
// expected
}
int ageAttr = result.getAttribute(age);
if (ageAttr == 23) {
assertEquals(Gender.FEMALE, result.getAttribute(gender));
} else if (ageAttr == 30) {
assertEquals(Gender.MALE, result.getAttribute(gender));
} else {
throw new AssertionError("unexpected age: " + ageAttr);
}
try {
result.getAttribute(new Attribute("does-not-exist"));
fail();
} catch (SearchException se) {
// expected
}
}
}
private void basicQueries(Ehcache cache) {
SearchTestUtil.populateData(cache);
Query query;
Attribute age = cache.getSearchAttribute("age");
query = cache.createQuery();
query.includeKeys();
query.addCriteria(age.ne(35));
query.end();
verify(cache, query, 2, 4);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(cache.getSearchAttribute("age").lt(30));
query.end();
query.execute();
verify(cache, query, 2);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(cache.getSearchAttribute("age").le(30));
query.end();
query.execute();
verify(cache, query, 2, 4);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(cache.getSearchAttribute("age").in(new HashSet(Arrays.asList(23, 35))));
query.end();
query.execute();
verify(cache, query, 1, 2, 3);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(cache.getSearchAttribute("age").gt(30));
query.end();
query.execute();
verify(cache, query, 1, 3);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(cache.getSearchAttribute("age").between(23, 35, true, false));
query.end();
query.execute();
verify(cache, query, 2, 4);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(cache.getSearchAttribute("age").ge(30));
query.end();
query.execute();
verify(cache, query, 1, 3, 4);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(cache.getSearchAttribute("age").eq(35).or(cache.getSearchAttribute("gender").eq(Gender.FEMALE)));
query.end();
verify(cache, query, 1, 2, 3);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(cache.getSearchAttribute("age").eq(35).and(cache.getSearchAttribute("gender").eq(Gender.MALE)));
query.end();
verify(cache, query, 1, 3);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(cache.getSearchAttribute("age").eq(35).and(cache.getSearchAttribute("gender").eq(Gender.FEMALE)));
query.end();
verify(cache, query);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(cache.getSearchAttribute("age").eq(35));
query.addCriteria(cache.getSearchAttribute("gender").eq(Gender.FEMALE));
query.end();
verify(cache, query);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(cache.getSearchAttribute("gender").eq(Gender.MALE).not());
query.end();
verify(cache, query, 2);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(cache.getSearchAttribute("name").eq("Tim Eck"));
query.addCriteria(cache.getSearchAttribute("gender").eq(Gender.MALE));
query.addCriteria(cache.getSearchAttribute("age").eq(35));
query.end();
verify(cache, query, 1);
query = cache.createQuery();
query.includeKeys();
Attribute name = cache.getSearchAttribute("name");
query.addCriteria(name.eq("Tim Eck").or(name.eq("Ari Zilka")).or(name.eq("Nabib El-Rahman")));
query.end();
verify(cache, query, 1, 3, 4);
try {
cache.getSearchAttribute("DOES_NOT_EXIST_PLEASE_DO_NOT_CREATE_ME");
fail();
} catch (CacheException ce) {
// expected
}
}
@Test
public void testOrdering() {
CacheManager cacheManager = new CacheManager(getClass().getResource("/ehcache-search.xml"));
Ehcache cache = cacheManager.getEhcache("cache1");
SearchTestUtil.populateData(cache);
Attribute age = cache.getSearchAttribute("age");
Attribute name = cache.getSearchAttribute("name");
Query query;
query = cache.createQuery();
query.includeKeys();
// no critera -- select all elements
query.addOrderBy(age, Direction.DESCENDING);
query.addOrderBy(name, Direction.ASCENDING);
query.end();
verifyOrdered(cache, query, 3, 1, 4, 2);
query = cache.createQuery();
query.includeKeys();
// no critera -- select all elements
query.addOrderBy(age, Direction.DESCENDING);
query.addOrderBy(name, Direction.ASCENDING);
query.maxResults(2);
query.end();
verifyOrdered(cache, query, 3, 1);
}
@Test
public void testILike() {
CacheManager cacheManager = new CacheManager(getClass().getResource("/ehcache-search.xml"));
Ehcache cache = cacheManager.getEhcache("cache1");
SearchTestUtil.populateData(cache);
Attribute name = cache.getSearchAttribute("name");
Query query;
query = cache.createQuery();
query.includeKeys();
query.addCriteria(new Or(name.ilike("tim*"), name.ilike("ari*")));
query.end();
verify(cache, query, 3, 1);
cache.removeAll();
cache.put(new Element(1, new Person("Test // Bob * ?", 35, Gender.MALE)));
cache.put(new Element(2, new Person("(..Test", 35, Gender.MALE)));
cache.put(new Element(3, new Person("lowercase", 35, Gender.MALE)));
cache.put(new Element(4, new Person("UPPERCASE", 35, Gender.MALE)));
cache.put(new Element(5, new Person("MiXeD", 35, Gender.MALE)));
cache.put(new Element(6, new Person("Hello there
I am on a newline
Me too
", 999, Gender.MALE)));
query = cache.createQuery();
query.includeKeys();
query.addCriteria(name.ilike("Test //// Bob //* //?"));
query.end();
verify(cache, query, 1);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(name.ilike("*Test*"));
query.end();
verify(cache, query, 1, 2);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(name.ilike("Test*//?"));
query.end();
verify(cache, query, 1);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(name.ilike("(..*"));
query.end();
verify(cache, query, 2);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(name.ilike("Lowercase"));
query.end();
verify(cache, query, 3);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(name.ilike("LOWER*"));
query.end();
verify(cache, query, 3);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(name.ilike("uppercase"));
query.end();
verify(cache, query, 4);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(name.ilike("mixed"));
query.end();
verify(cache, query, 5);
query = cache.createQuery();
query.includeKeys();
query.addCriteria(name.ilike("*am on a*"));
query.end();
verify(cache, query, 6);
}
@Test
public void testTypeChecking() {
CacheManager cm = new CacheManager(new Configuration().defaultCache(new CacheConfiguration()));
CacheConfiguration config = new CacheConfiguration("test", 0);
config.setOverflowToDisk(false);
config.diskPersistent(false);
config.setEternal(true);
Searchable searchable = new Searchable().searchAttribute(new SearchAttribute().name("attr").expression("value.getAttr()"));
config.addSearchable(searchable);
cm.addCache(new Cache(config));
class Value {
private final Object attr;
Value(Object attr) {
this.attr = attr;
}
Object getAttr() {
return attr;
}
}
Ehcache cache = cm.getEhcache("test");
cache.put(new Element(1, new Value("foo")));
Query query = cache.createQuery();
query.includeKeys();
query.addCriteria(cache.getSearchAttribute("attr").le(4));
query.end();
try {
query.execute();
fail();
} catch (SearchException se) {
// expected since the criteria wants INT, but actual attribute value is STRING
}
// with proper type search will execute
cache.put(new Element(1, new Value(4)));
assertEquals(1, query.execute().all().iterator().next().getKey());
}
@Test
public void testEmptyQueries() {
CacheManager cacheManager = new CacheManager(getClass().getResource("/ehcache-search.xml"));
Ehcache cache = cacheManager.getEhcache("cache1");
SearchTestUtil.populateData(cache);
{
Query query = cache.createQuery();
query.end();
try {
query.execute();
fail();
} catch (SearchException e) {
System.err.println("Expected " + e);
}
}
{
Attribute age = cache.getSearchAttribute("age");
Query query = cache.createQuery();
query.addCriteria(age.ne(35));
query.end();
try {
query.execute();
fail();
} catch (SearchException e) {
System.err.println("Expected " + e);
}
}
}
@Test
public void testIncludeValues() {
CacheManager cacheManager = new CacheManager(getClass().getResource("/ehcache-search.xml"));
Ehcache cache = cacheManager.getEhcache("cache1");
SearchTestUtil.populateData(cache);
{
Query query = cache.createQuery();
query.includeValues();
query.end();
Results results = query.execute();
assertTrue(results.hasValues());
assertEquals(4, results.size());
int ageSum = 0;
for (Result result : results.all()) {
Person p = (Person) result.getValue();
ageSum += p.getAge();
try {
result.getKey();
fail();
} catch (SearchException se) {
// expected since keys not included
}
}
assertEquals(123, ageSum);
}
{
Query query = cache.createQuery();
query.includeKeys();
query.end();
Results results = query.execute();
assertFalse(results.hasValues());
assertEquals(4, results.size());
for (Result result : results.all()) {
try {
result.getValue();
fail();
} catch (SearchException se) {
// expected since keys not included
}
}
}
}
private void verify(Ehcache cache, Query query, Integer... expectedKeys) {
Results results = query.execute();
assertEquals(expectedKeys.length, results.size());
assertTrue(results.hasKeys());
assertFalse(results.hasAttributes());
Set keys = new HashSet(Arrays.asList(expectedKeys));
for (Result result : results.all()) {
int key = (Integer) result.getKey();
if (!keys.remove(key)) {
throw new AssertionError("unexpected key: " + key);
}
}
}
private void verifyOrdered(Ehcache cache, Query query, Integer... expectedKeys) {
Results results = query.execute();
assertEquals(results.size(), expectedKeys.length);
int pos = 0;
for (Result result : results.all()) {
Object expectedKey = expectedKeys[pos++];
assertEquals(expectedKey, result.getKey());
}
}
}
慎重に新出来の機能について、今のプロジェクトの中で使う勇気がないので、テストだけで、EQLのこのようにすることがあることを知っていて、技術の急進的な1時(点)の達人に対してプロジェクトの中で実際に使ったことがあった後に更にフォローしても遅くありません.公式文書リンク:
http://www.ehcache.org/documentation/search.html