-
Notifications
You must be signed in to change notification settings - Fork 3
/
LuceneIndexer.java
276 lines (220 loc) · 8.29 KB
/
LuceneIndexer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.net.URLClassLoader;
import java.net.URL;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import oracle.ODCI.ODCIIndexInfo;
import oracle.ODCI.ODCIEnv;
import oracle.ODCI.ODCIPredInfo;
import oracle.ODCI.ODCIQueryInfo;
import oracle.ODCI.ODCIRidList;
import java.sql.Blob;
import java.io.InputStream;
import java.io.OutputStream;
import oracle.ODCI.ODCIColInfo;
import java.io.*;
import oracle.sql.*;
import java.sql.*;
import oracle.CartridgeServices.*;
import java.math.*;
public class LuceneIndexer implements SQLData {
final static String BASE_PATH = "/home/oracle/MMDB/";
final static BigDecimal SUCCESS = new BigDecimal("0");
final static BigDecimal ERROR = new BigDecimal("1");
public BigDecimal key;
public static String indexName;
String sql_type;
public String getSQLTypeName() throws SQLException
{
return sql_type;
}
public void readSQL(SQLInput stream, String typeName) throws SQLException
{
sql_type = typeName;
key = stream.readBigDecimal();
}
public void writeSQL(SQLOutput stream) throws SQLException
{
stream.writeBigDecimal(key);
}
public static void init(ODCIIndexInfo indexInfo) throws Exception
{
// Disable all security. Follwing SQL has to be executed first:
// call dbms_java.grant_permission( 'PUBLIC', 'SYS:java.lang.RuntimePermission', 'setSecurityManager', '' );
System.setSecurityManager(null);
// Create directory for logs
(new File(BASE_PATH + "/logs")).mkdirs();
System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(BASE_PATH + "logs/stdout", true)), true));
System.setErr(new PrintStream(new BufferedOutputStream(new FileOutputStream(BASE_PATH + "logs/stderr", true)), true));
// Add dependencies dynamically
URLClassLoader systemClassLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
String[] jars = {"commons-io-2.5", "commons-math3-3.6.1", "jopensurf-1.0.0", "lire",
"lucene-analyzers-common-6.3.0", "lucene-core-6.3.0", "lucene-queries-6.3.0",
"lucene-queryparser-6.3.0", "lucene-sandbox-6.3.0"};
for (String jar : jars)
method.invoke(systemClassLoader, new URL("file://" + BASE_PATH + "lib/lire/" + jar + ".jar"));
indexName = indexInfo.getIndexName();
}
private static Class<?> getLuceneIndexerWrapper() throws Exception {
URLClassLoader urlClassLoader = new URLClassLoader(new URL[] {new URL("file://" + BASE_PATH)});
return urlClassLoader.loadClass("LuceneIndexerWrapper");
}
public static void writeImageToFile(String fileName, Blob image) throws Exception
{
File tempFile = new File(BASE_PATH + indexName + "/images/" + fileName);
InputStream in = image.getBinaryStream();
OutputStream out = new FileOutputStream(tempFile);
byte[] buff = image.getBytes(1, (int)image.length());
out.write(buff);
out.close();
}
public static void deleteTempImage(String fileName)
{
File tempFile = new File(BASE_PATH + indexName + "/images/" + fileName);
tempFile.delete();
}
public static String fromRowID(String rowid) {
// TODO: Check if more special characters have to be replaced.
return rowid.replace('/', '_') + ".jpg";
}
public static String toRowID(String fileName) {
return fileName.split("\\.")[0].replace('_', '/');
}
public static void updateIndex() throws Exception
{
getLuceneIndexerWrapper().getDeclaredMethod("updateIndex", String.class, String.class).
invoke(null, BASE_PATH, indexName);
}
public static BigDecimal ODCIInsert(ODCIIndexInfo indexInfo, String rowid, Blob newVal,
ODCIEnv env) throws Exception
{
init(indexInfo);
System.out.println("ODCIInsert");
System.out.println("rowid: " + rowid);
writeImageToFile(fromRowID(rowid), newVal);
updateIndex();
return SUCCESS;
}
public static BigDecimal ODCICreate(ODCIIndexInfo indexInfo, String params, ODCIEnv env) throws Exception
{
init(indexInfo);
System.out.println("ODCICreate");
Connection conn = DriverManager.getConnection("jdbc:default:connection:");
// Assume that only one column will be indexed
ODCIColInfo indexColumn = indexInfo.getIndexCols().getArray()[0];
String tableName = indexColumn.getTableName();
String columnName = indexColumn.getColName();
System.out.println("tableName: " + tableName);
System.out.println("columnName: " + columnName);
// Create folder for images
(new File(BASE_PATH + indexName + "/images")).mkdirs();
// Get all the images together with their rowids
String sql = "SELECT ROWID, " + columnName + " FROM " + tableName;
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) {
String fileName = fromRowID(resultSet.getString(1));
System.out.println("fileName: " + fileName);
writeImageToFile(fileName, resultSet.getBlob(2));
}
resultSet.close();
conn.close();
updateIndex();
return SUCCESS;
}
static void deleteRecursive(File f) throws IOException {
if (f.isDirectory()) {
for (File c : f.listFiles())
deleteRecursive(c);
}
f.delete();
}
public static BigDecimal ODCIDrop(ODCIIndexInfo indexInfo, ODCIEnv env) throws Exception
{
init(indexInfo);
System.out.println("ODCIDrop");
System.out.println("Delete directory: " + BASE_PATH + indexName);
deleteRecursive(new File(BASE_PATH + indexName));
return SUCCESS;
}
public static BigDecimal ODCIDelete(ODCIIndexInfo indexInfo, String rowid, Blob oldVal, ODCIEnv env) throws Exception
{
init(indexInfo);
System.out.println("ODCIDelete");
System.out.println("rowid: " + rowid);
System.out.println("oldVal: " + oldVal);
deleteTempImage(fromRowID(rowid));
updateIndex();
return SUCCESS;
}
public static BigDecimal ODCIUpdate(ODCIIndexInfo indexInfo, String rowid,
Blob oldVal, Blob newVal, ODCIEnv env) throws Exception
{
init(indexInfo);
System.out.println("ODCIUpdate");
System.out.println("rowid: " + rowid);
deleteTempImage(fromRowID(rowid));
writeImageToFile(fromRowID(rowid), newVal);
updateIndex();
return SUCCESS;
}
public static BigDecimal ODCIStart(STRUCT[] sctx, ODCIIndexInfo indexInfo, ODCIPredInfo predicateInfo,
oracle.ODCI.ODCIQueryInfo queryInfo, BigDecimal start, BigDecimal stop, Blob compareVal, ODCIEnv env) throws Exception
{
init(indexInfo);
System.out.println("ODCIStart");
Connection conn = DriverManager.getConnection("jdbc:default:connection:");
// TODO: replace by blob that is passed by operator parameter
//InputStream testStream = new FileInputStream(BASE_PATH + "corel10k/666.jpg");
String[] fileNames = (String[])getLuceneIndexerWrapper().
getDeclaredMethod("search", InputStream.class, String.class, String.class).
invoke(null, /*testStream*/compareVal.getBinaryStream(), BASE_PATH, indexName);
StoredContext storedContext = new StoredContext(fileNames);
int keyToStore = ContextManager.setContext(storedContext);
System.out.println("keyToStore: " + keyToStore);
Object[] impAttr = new Object[1];
impAttr[0] = new BigDecimal(keyToStore);
StructDescriptor sd = new StructDescriptor("LUCENEINDEXER", conn);
sctx[0] = new STRUCT(sd, conn, impAttr);
return SUCCESS;
}
public BigDecimal ODCIFetch(BigDecimal numRows, ODCIRidList[] rowids, ODCIEnv env) throws Exception
{
System.out.println("ODCIFetch");
System.out.println("key after pass: " + key.intValue());
// retrieve stored context using the key
StoredContext storedContext;
storedContext = (StoredContext)ContextManager.getContext(key.intValue());
System.out.println("storedContext.fileNames.length: " + storedContext.fileNames.length);
String[] rlist = new String[numRows.intValue()];
for (int i = 0; i < numRows.intValue(); i++)
{
if (i < storedContext.fileNames.length)
{
rlist[i] = toRowID(storedContext.fileNames[i]);
System.out.println("rlist[i]: " + rlist[i]);
}
else
{
rlist[i] = null;
break;
}
}
rowids[0] = new ODCIRidList(rlist);
return SUCCESS;
}
public BigDecimal ODCIClose(ODCIEnv env) throws Exception
{
System.out.println("ODCIClose");
System.out.println("key on close: " + key.intValue());
ContextManager.clearContext(key.intValue());
return SUCCESS;
}
}