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.Configuration.KeySorting;
00010 import de.aitools.aq.invertedindex.core.Configuration.ValueSorting;
00011 import de.aitools.aq.invertedindex.core.Properties.PropertiesImpl;
00012 import de.aitools.aq.util.Record;
00013 import de.aitools.aq.util.ToAsciiEncoder;
00014 import de.aitools.aq.value.Value;
00015
00023 public class Indexer<V extends Value> {
00024
00025 static {
00026 Native.register(Configuration.NATIVE_LIB);
00027 System.out.println(Configuration.NATIVE_LIB + " loaded");
00028 }
00029
00053 public static <V extends Value> Indexer<V>
00054 open(Class<V> clazz, Configuration config) {
00055 Pointer natIndexer =
00056 indexer_open(Value.getNativeTypeId(clazz), config.impl);
00057 if (natIndexer == null) {
00058 throw new RuntimeException("Cannot create native Indexer");
00059 }
00060 return new Indexer<V>(natIndexer);
00061 }
00062
00069 private Indexer(Pointer nativeIndexer) {
00070 A.CHECK_NOT_NULL(nativeIndexer);
00071 this.nativeIndexer = nativeIndexer;
00072 }
00073
00080 public void close() {
00081 indexer_close(nativeIndexer);
00082 nativeIndexer = null;
00083 }
00084
00098 public void setExpectedNumberOfRecords(long numberOfRecords) {
00099 indexer_set_expected_record_count(nativeIndexer, numberOfRecords);
00100 }
00101
00102 @Override
00103 protected void finalize() throws Throwable {
00104 close();
00105 super.finalize();
00106 }
00107
00123 public Properties index() {
00124 Properties props = new Properties();
00125 indexer_index(nativeIndexer, props.impl);
00126 nativeIndexer = null;
00127 return props;
00128 }
00129
00143 public boolean put(String key, V value) {
00144 Pointer buffer = value.getSharedMemory();
00145 value.copyTo(buffer);
00146 return indexer_insert(nativeIndexer,
00147 ToAsciiEncoder.encode(key), buffer);
00148 }
00149
00160 public boolean put(Record<V> record) {
00161 return put(record.getKey(), record.getValue());
00162 }
00163
00164 private Pointer nativeIndexer;
00165
00166
00167
00168
00169
00170 private static native Pointer indexer_open(
00171 int type_id, ConfigurationImpl config);
00172 private native void indexer_set_expected_record_count(
00173 Pointer raw_indexer, long record_count);
00174 private native boolean indexer_insert(
00175 Pointer raw_indexer, String key, Pointer value);
00176 private native void indexer_index(
00177 Pointer raw_indexer, PropertiesImpl props);
00178 private native void indexer_close(Pointer raw_indexer);
00179 }