00001
00002 package de.aitools.aq.invertedindex.core;
00003
00004 import com.sun.jna.Native;
00005 import com.sun.jna.Pointer;
00006
00007 import de.aitools.aq.check.A;
00008 import de.aitools.aq.invertedindex.core.Configuration.ConfigurationImpl;
00009 import de.aitools.aq.invertedindex.core.Postlist.Head;
00010 import de.aitools.aq.invertedindex.core.Properties.PropertiesImpl;
00011 import de.aitools.aq.util.ToAsciiEncoder;
00012 import de.aitools.aq.value.Value;
00013
00020 public class Searcher<V extends Value> {
00021
00022 static {
00023 Native.register(Configuration.NATIVE_LIB);
00024 System.out.println(Configuration.NATIVE_LIB + " loaded");
00025 }
00026
00041 public static <V extends Value> Searcher<V>
00042 open(Class<V> clazz, Configuration config) {
00043 A.CHECK_NOT_NULL(clazz, config);
00044 Properties props = new Properties();
00045 Pointer nativeSearcher = searcher_open(
00046 Value.getNativeTypeId(clazz), config.impl, props.impl);
00047 if (nativeSearcher == null) {
00048 throw new RuntimeException("Cannot create native Searcher");
00049 }
00050 return new Searcher<V>(nativeSearcher, clazz, props);
00051 }
00052
00058 private Searcher(Pointer nativeSearcher, Class<V> clazz, Properties props) {
00059 A.CHECK_NOT_NULL(nativeSearcher, clazz, props);
00060 this.nativeSearcher = nativeSearcher;
00061 this.clazz = clazz;
00062 this.props = props;
00063 }
00064
00070 public void close() {
00071 searcher_close(nativeSearcher);
00072 nativeSearcher = null;
00073 }
00074
00089 public boolean contains(String key) {
00090 return getHead(key) != null;
00091 }
00092
00093 @Override
00094 protected void finalize() throws Throwable {
00095 close();
00096 super.finalize();
00097 }
00098
00112 public Postlist.Head getHead(String key) {
00113 A.CHECK_NOT_NULL(key);
00114 key = ToAsciiEncoder.encode(key);
00115 Postlist.Head head = new Postlist.Head();
00116 if (!searcher_search_head(nativeSearcher, key, head)) head = null;
00117 return head;
00118 }
00119
00125 public Properties getProperties() {
00126 return props;
00127 }
00128
00138 public Postlist<V> search(String key) {
00139 return search(key, Integer.MAX_VALUE);
00140 }
00141
00155 public Postlist<V> search(String key, int num) {
00156 return search(key, 0, num);
00157 }
00158
00174 public Postlist<V> search(String key, int begin, int num) {
00175 A.CHECK_NOT_NULL(key);
00176 key = ToAsciiEncoder.encode(key);
00177 Postlist.Head head = new Postlist.Head();
00178 Pointer nativePostlist = searcher_search_postlist(
00179 nativeSearcher, key, begin, num, head);
00180 return nativePostlist != null ?
00181 new Postlist<V>(clazz, nativePostlist, head) : null;
00182 }
00183
00184 private Pointer nativeSearcher;
00185 private Properties props;
00186 private Class<V> clazz;
00187
00188
00189
00190
00191
00192 private static native Pointer searcher_open(
00193 int type_id, ConfigurationImpl config, PropertiesImpl props);
00194 private native void searcher_close(Pointer raw_searcher);
00195 private native boolean searcher_search_head(
00196 Pointer raw_searcher, String key, Postlist.Head head);
00197 private native Pointer searcher_search_postlist(Pointer raw_searcher,
00198 String key, int begin, int num, Postlist.Head header);
00199 }