读书人

Compass基础知识1

发布时间: 2012-09-27 11:11:17 作者: rapoo

Compass基础知识一

1、Compass简单介绍

?


? ?Compass和Hibernate很相像,无非就是Hibernate把JDBC封装了一把.所以从结构上来说,只要我们了解了Hibernate,就已经对Compass有了了解.那么Hibernate需要提供API和配置文件来对JDBC进行操作,那么Compass呢?Compass不仅从结构上模仿了Hibernate,就连API风格也不尽相同.我们把它和Hibernate的API做个对比就知道了:

Compass基础知识1

再把两个session的核心方法比较一下:

Hibernate session API

CompassSession API

?

save(Object)

create(Object)

建立索引

saveOrUpdate(Object)

save(Object)

保存或更新

delete(Object)

delete(Class, ids...)

删除索引

get()

get()

获取

createQuery(hql).list()

find(String)

使用查询字符串查询

所以说,Compass与Hibernate极为相似,Compass总结起来就两句话:

?

<!-- 使用annotation配置,指定要转换的POJO。PO类在compass的classMappings值指定 --> <bean id="annotationConfiguration" ref="annotationConfiguration"/> <property name="compassSettings"> <props> <!-- 索引文件在服务器上的存储路径 如:file://d:/index --> <prop key="compass.engine.connection">/lucene/indexes</prop> <!-- 在内存中建立索引 <prop key="compass.engine.connection">ram://index</prop> --> <prop key="compass.transaction.factory"> org.compass.spring.transaction.SpringSyncTransactionFactory </prop> <!-- 配置高亮为红色 --> <prop key="compass.engine.highlighter.default.formatter.simple.pre"> <![CDATA[<font color="red"><b>]]> </prop> <prop key="compass.engine.highlighter.default.formatter.simple.post"> <![CDATA[</b></font>]]> </prop> </props> </property> <property name="transactionManager" ref="transactionManager"/> </bean> <bean id="compassTemplate" ref="compass"/> </bean> <!-- 与hibernate的绑定,经Hiberante的数据改变会自动被反射到索引里面(增加、修改、删除操作). --> <bean id="hibernateGpsDevice" ref="sessionFactory"/> <property name="mirrorDataChanges"> <value>true</value> </property> </bean> <bean id="compassGps" destroy-method="stop"> <property name="compass" ref="compass"/> <property name="gpsDevices"> <list> <ref local="hibernateGpsDevice"/> </list> </property> </bean> <!-- 定时重建索引(利用quartz)或随Spring ApplicationContext启动而重建索引 --> <bean id="compassIndexBuilder" lazy-init="false"> <property name="compassGps" ref="compassGps"/> <property name="buildIndex" value="true"/> <property name="lazyTime" value="10"/> </bean> public class CompassIndexBuilder implements InitializingBean { private static final Logger log = Logger.getLogger(CompassIndexBuilder.class); // 是否需要建立索引,可被设置为false使本Builder失效. private boolean buildIndex = true; // 索引操作线程延时启动的时间,单位为秒 private int lazyTime = 10; // Compass封装 private CompassGps compassGps; // 索引线程 private Thread indexThread = new Thread() { @Override public void run() { try { Thread.sleep(lazyTime * 1000); log.info("begin compass index..."); long beginTime = System.currentTimeMillis(); // 重建索引. // 如果compass实体中定义的索引文件已存在,索引过程中会建立临时索引, // 索引完成后再进行覆盖. compassGps.index(); long costTime = System.currentTimeMillis() - beginTime; log.info("compss index finished."); log.info("costed " + costTime + " milliseconds"); } catch (InterruptedException e) { } } }; /** * 实现<code>InitializingBean</code>接口,在完成注入后调用启动索引线程. * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ public void afterPropertiesSet() throws Exception { if (buildIndex) { indexThread.setDaemon(true); indexThread.setName("Compass Indexer"); indexThread.start(); } } public void setBuildIndex(boolean buildIndex) { this.buildIndex = buildIndex; } public void setLazyTime(int lazyTime) { this.lazyTime = lazyTime; } public void setCompassGps(CompassGps compassGps) { this.compassGps = compassGps; }}?

?

Compass compass = compassTemplate.getCompass();CompassSession session=compass.openSession();List list = new ArrayList();CompassHits hits= session.queryBuilder().queryString(queryString).toQuery().hits();for(int i=0;i<hits.length();i++){    Person hit=( Person)hits.data(i);    list.add(hit);}session.close();return list;
?

?




以上内容有参考他人之处,不在这列出,谢谢!


?

读书人网 >开源软件

热点推荐