package example.indexbuild;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.util.Date;
import org.sourceforge.beanindex.BeanIndex;
import org.sourceforge.beanindex.BeanIndexBuilder;
import org.sourceforge.beanindex.BeanIndexMetaInfo;
import org.sourceforge.beanindex.Index;
import example.util.DataFileReader;
import example.util.DigitalItemBean;
public class BuildExample {
private final static String DATA_FILE = "data/digital_item.csv";
public final static String INDEX_NAME = "example_idx";
// inverted indeces array.
private Index[] indeces = null;
private BeanIndexBuilder beanIndexBuilder = null;
public static void main(String[] args) throws Exception {
BuildExample instance = new BuildExample();
instance.initialize();
instance.build();
}
private void initialize() throws Exception {
// path to BeanIndex data directory
String path = null; // null defaults to the working directory
BeanIndex beanIndex = new BeanIndex(path);
// Lets have one index for this example
indeces = new Index[1];
indeces[0] = beanIndex.createIndex(INDEX_NAME);
Class beanClass = DigitalItemBean.class;
// decide on fields to be indexed
Field[] indexedFields = this.getIndexFields(beanClass);
// create BeanIndexMetaInfo
BeanIndexMetaInfo beanInfo = new BeanIndexMetaInfo(beanClass,
indexedFields, indeces);
// useVirtualMemory = true - use disk for intermediate dataset
boolean useVirtualMemory = false;
this.beanIndexBuilder = beanIndex.getBeanIndexBuilder(
beanInfo, useVirtualMemory);
}
private void build() {
Date start = new Date();
System.out.println(" BeanIndex build process started at : "
+ DateFormat.getInstance().format(start));
try {
// file reader reads in beans from data file
DataFileReader fileReader = new DataFileReader(DATA_FILE);
DigitalItemBean bean = null;
bean = fileReader.getNextBean();
int count = 0;
while (bean != null) {
// write each bean instance to one of the index
beanIndexBuilder.write(indeces[0], bean);
count++;
bean = fileReader.getNextBean();
}
beanIndexBuilder.close();
System.out.println(" Number of Beans added " + count);
} catch (Exception e) {
e.printStackTrace(System.err);
}
Date end = new Date();
System.out.println(" BeanIndex build process completed at : "
+ DateFormat.getInstance().format(end));
this.printTimeReport(start.getTime(), end.getTime());
}
/**
* This method returns the searchable fields
* @param clazz
* @return
*/
private Field[] getIndexFields(Class clazz) throws Exception {
Field[] indexedFields = new Field[4];
indexedFields[0] = clazz.getDeclaredField("keywords");
indexedFields[1] = clazz.getDeclaredField("description");
indexedFields[2] = clazz.getDeclaredField("size");
indexedFields[3] = clazz.getDeclaredField("price");
return indexedFields;
}
/**
* Prints the time taken for processing
* @param start
* @param end
*/
private void printTimeReport(long start, long end) {
end = end - start;
long minute = (int) (end / 60000);
long sec = (int) ((end - minute * 60000) / 1000);
long mill = end - minute * 60000 - sec * 1000;
System.out.println(" Process completed in : " + (minute) + " Minutes "
+ (sec) + " seconds " + mill + " millisecs");
}
}
|