理解Compass的配置文件

Compass是建立在Lucene基礎之上的一款開放源碼的JAVA搜索引擎框架。關於Compass的介紹在此我不多講了,更多瞭解請直接參考http://www.opensymphony.com/compass
    很多看了Compass的人多少對Compass的配置文件都有點雲裏霧裏的感覺。由於Compass有很多地方都借鑑了Hibernate的思想,在此我結合Hibernate中的思想來幫助我們對Compass的配置文件進行理解。
    Hibernate是一個O/R Mapping工具,它可以將實體對象和數據庫中的表進行映射。最終通過訪問實體對象來達到訪問數據庫的目的。
    Compass是一個搜索引擎框架,它可以將common meta data與實體對象進行映射。最終通過訪問common meta data來達到訪問對象的目的。
    綜上所述,Hibernate是對象到數據庫。而Compass是common meta data 到對象。
Compass的配置文件主要分成三類:

    第一類:*.cmd.xml文件

    *.cmd.xml文件是對common meta data進行定義,我們就可以把它是當作Hibernate中的POJO來理解。裏面定義了最終搜索的結果中的最基本的元數據。下面是一個*.cmd.xml文件的片斷:

< compass - core - meta - data >
    
< meta - data - group id = " petclinic "  displayName = " Petclinic Meta Data " >     
        
< descrīption > Petclinic Meta Data </ descrīption >  
        
< uri > http: // compass/sample/petclinic</uri>        
         < alias id = " vet "  displayName = " Vet " >
            
< descrīption > Vet alias </ descrīption >
            
< uri > http: // compass/sample/petclinic/alias/vet</uri>
             < name > vet </ name >
        
</ alias >
       …………
        
< meta - data id = " petType "  displayName = " Pet Type " >
            
< descrīption > The type of a pet </ descrīption >
            
< uri > http: // compass/sample/petclinic/petType</uri>
             < name > petType </ name >
        
</ meta - data >  
    
</ meta - data - group >
</ compass - core - meta - data >    

在這裏

< meta - data - group id = " petclinic "  displayName = " Petclinic Meta Data " >   

可當作是POJO的package來理解,petclinic可以看成是package name。

< alias id = " vet "  displayName = " Vet " >  

可當作是POJO的Class來理解,vet 可以看成是class name。

< meta - data id = " petType "  displayName = " Pet Type " >

可當作是POJO的property來理解,petType可以看成是property name。

上面xml中所定義的meta data 和alias就是compass需要查詢的所有的基本元素組件。

然而common meta data的定義與POJO所不同的是:POJO是用java代碼來體現,而common meta data使用xml來體現。POJO的class中定義了property,common meta data中的alias和meta-data是分開定義。

當然上面都是一個類比而已, 其目的是爲了便於讓我們理解,實際生成的代碼並非如上所述的一一對應。compass提供了一個ant task可以將common meta data生成一個class。代碼片斷如下:

public   final   class  Petclinic  {
    
/**
     * Petclinic Meta Data
     
*/

    
public   static   final   class  Group  {
       
public   static   final  String Id  =   " petclinic " ;
       
public   static   final  String DispayName  =   " Petclinic Meta Data " ;
       
public   static   final  String Uri  =   " http://compass/sample/petclinic " ;
    }

    
public   static   final   class  Alias  {
       
/**
        * Owner alias
        
*/

       
public   static   final   class  Owner  {
           
public   static   final  String Id  =   " owner " ;
           
public   static   final  String Name  =   " owner " ;
           
public   static   final  String DisplayName  =   " Owner " ;
           
public   static   final  String Uri  =   " http://compass/sample/petclinic/alias/owner " ;
           
public   static   final  String GroupId  =   " petclinic " ;
       }

    }

    ……
}

    第二類:*.cpm.xml文件

    *.cpm.xml就是Object/Search Engine Mapping了。 我們也可以拿她當作同Hibernate中的*.hbm.xml來理解。他的作用就是提供了POJO到common meta data的映射。下面是一個*.cpm.xml文件的片斷:

< compass - core - mapping  package = " org.compass.sample.petclinic " >

    
< contract alias = " entity " >
        
< id name = " id "   />
    
</ contract >

    
< contract alias = " person "   extends = " entity " >
        
< property name = " firstName " >
            
< meta - data > $ {petclinic.firstName} </ meta - data >
        
</ property >
        
< property name = " lastName " >
            
< meta - data > $ {petclinic.lastName} </ meta - data >
        
</ property >
        
< property name = " address " >
            
< meta - data > $ {petclinic.address} </ meta - data >
        
</ property >
        
< property name = " city " >
            
< meta - data > $ {petclinic.city} </ meta - data >
        
</ property >
        
< property name = " telephone " >
            
< meta - data > $ {petclinic.telephone} </ meta - data >
        
</ property >
    
</ contract >

    
< class  name = " Specialty "  alias = " ${petclinic.specialty} "  root = " false " >
        
< property name = " name " >
            
< meta - data > $ {petclinic.specialty} </ meta - data >
        
</ property >
    
</ class >

    
< class  name = " Owner "  alias = " ${petclinic.owner} "   extends = " person " >
        
< property name = " firstName " >
            
< meta - data > $ {petclinic.firstName} </ meta - data >
        
</ property >
        
< property name = " lastName " >
            
< meta - data > $ {petclinic.lastName} </ meta - data >
        
</ property >
        
< property name = " address " >
            
< meta - data > $ {petclinic.address} </ meta - data >
        
</ property >
        
< property name = " city " >
            
< meta - data > $ {petclinic.city} </ meta - data >
        
</ property >
        
< property name = " telephone " >
            
< meta - data > $ {petclinic.telephone} </ meta - data >
        
</ property >
        
< reference name = " petsInternal "  ref - alias = " ${petclinic.pet} "    />
    
</ class >
</ compass - core - mapping >

   上面 package對應了POJO的包名,class對應了POJO類名,contract爲POJO中一些較爲Base類, property對應了POJO的屬性。上面看到的像ANT中的${*}就是*.cmd.xml 中所定義的common meta data。通常來說*.cmd.xml中的alias同POJO的Class進行映射。Meta data 同Class中的property進行映射。更多的映射的細節在此不多講。可以參考 Compass的中自帶的Sample Petclinic來理解。

    第三類:*.cfg.xml文件

    Compass的*.cfg.xml文件就和Hibernate的*.cfg.xml有些類似了。下面是一個*.cfg.xml文件的內容:

< compass - core - configuration >
    
< compass >     
        
< setting name = " compass.engine.connection " > target / index </ setting >         
        
< meta - data resource = " org/compass/sample/library/library.cmd.xml "   />      
        
< mappings >
               
< class  name = " test.Author "   />
        
</ mappings >
    
</ compass >
</ compass - core - configuration >

上面

< setting name = " compass.engine.connection " > target / index </ setting >   

  指定了索引文件存放的路徑。

< meta - data resource = " org/compass/sample/library/library.cmd.xml "   />

  指定了*.cmd.xml文件存放的路徑。

< mappings >< class  name = " test.Author "   /></ mappings >

是對compass的mapping文件進行指定。Test.Author對應的文件是test/Author.cpm.xml.

<compass>是對compass中的compass類進行的一些參數設置,這個compass是一個重量級的類,類似於Hibernate中SessionFactory.


當Hibernate同Spring進行整合後,Hibernate就不需要*.cfg.xml這個文件了,借而代之的是用Spring的配置文件來進行配置。同樣,Compass同Spring進行整合後,*.cfg.xml也可以不需要了。比如如下的Spring配置。

     < bean id = " compass "   class = " org.compass.spring.LocalCompassBean " >
        
< property name = " resourceLocations " >
            
< list >
                
< value > classpath:org / compass / sample / petclinic / petclinic.cmd.xml </ value >
                
< value > classpath:petclinic.cpm.xml </ value >
            
</ list >
        
</ property >
        
< property name = " compassSettings " >
            
< props >
                
< prop key = " compass.engine.connection " > file: // ${user.home}/compass/petclinic</prop>
                 < prop key = " compass.transaction.factory " > org.compass.spring.transaction.SpringSyncTransactionFactory </ prop >
            
</ props >
        
</ property >
        
< property name = " transactionManager " >
            
< ref local = " transactionManager "   />
        
</ property >
    
</ bean >

     
< bean id = " hibernateGpsDevice "   class = " org.compass.spring.device.hibernate.SpringHibernate3GpsDevice " >
       
< property name = " name " >< value > hibernateDevice </ value ></ property >
       
< property name = " sessionFactory " >< ref local = " sessionFactory "   /></ property >
     
</ bean >

     
< bean id = " compassGps "   class = " org.compass.gps.impl.SingleCompassGps "  init - method = " start "  destroy - method = " stop " >
       
< property name = " compass " >< ref bean = " compass "   /></ property >
       
< property name = " gpsDevices " >
         
< list >
           
< ref local = " hibernateGpsDevice "   />
         
</ list >
       
</ property >
     
</ bean >


    在上訴的配置文件中又引出了兩個新的知識點,CompassGps和CompassGpsDevice。

1 :CompassGps像是一個Service,他需要在application startup時啓動服務, application shutdown停止服務,
2 :CompassGpsDevice不能獨立的存在,他需要依賴CompassGps,

CompassGps爲CompassGpsDevice提供Compass對象,他們一起爲程序提供Index的實時更新。 Compass整合Hibernate 等等 persitance framework的代碼就在CompassGpsDevice裏,你需要提供不同的Device,如HibernateDevice, JDODevice。你也可以實現自己的Device, CompassGpsDevice會把domain object的更新事件通過CompassGps去通知Compass去實時更新索引文件。

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章