Permalink
Browse files

refactoring for index addition, tests for type conversion

  • Loading branch information...
1 parent dfa2601 commit 81ff8e48c86f53d0f7a46ef4d17c7be95b2bcecd @jexp committed Aug 21, 2012
@@ -77,15 +77,12 @@ public static void main(String[] args) throws IOException {
if (nodesFile.exists()) importBatch.importNodes(nodesFile);
if (relationshipsFile.exists()) importBatch.importRelationships(relationshipsFile);
for (int i = 3; i < args.length; i = i + 4) {
- indexName = args[i+1];
- indexType = args[i+2];
indexFile = new File(args[i + 3]);
- if (args[i].equals("node_index")) {
- if (indexFile.exists()) importBatch.importNodeIndexes(indexFile, indexName, indexType);
- } else {
- if (indexFile.exists()) importBatch.importRelationshipIndexes(indexFile, indexName, indexType);
- }
-
+ if (!indexFile.exists()) continue;
+ indexName = args[i+1];
+ indexType = args[i+2];
+ BatchInserterIndex index = args[i].equals("node_index") ? importBatch.nodeIndexFor(indexName, indexType) : importBatch.relationshipIndexFor(indexName, indexType);
+ importBatch.importIndex(indexFile, indexName, index);
}
} finally {
importBatch.finish();
@@ -98,7 +95,7 @@ private void finish() {
report.finish();
}
- static class Data {
+ public static class Data {
private Object[] data;
private final int offset;
private final String delim;
@@ -237,14 +234,8 @@ private void importRelationships(File file) throws IOException {
report.finishImport("Relationships");
}
- private void importNodeIndexes(File file, String indexName, String indexType) throws IOException {
- BatchInserterIndex index;
- if (indexType.equals("fulltext")) {
- index = lucene.nodeIndex( indexName, FULLTEXT_CONFIG );
- } else {
- index = lucene.nodeIndex( indexName, EXACT_CONFIG );
- }
-
+ private void importIndex(File file, String indexName, BatchInserterIndex index) throws IOException {
+
BufferedReader bf = new BufferedReader(new FileReader(file));
final Data data = new Data(bf.readLine(), "\t", 1);
@@ -257,33 +248,20 @@ private void importNodeIndexes(File file, String indexName, String indexType) th
report.dots();
}
- report.finishImport("Nodes into " + indexName + " Index");
+ report.finishImport("Done inserting into " + indexName + " Index");
}
- private void importRelationshipIndexes(File file, String indexName, String indexType) throws IOException {
- BatchInserterIndex index;
- if (indexType.equals("fulltext")) {
- index = lucene.relationshipIndex( indexName, FULLTEXT_CONFIG );
- } else {
- index = lucene.relationshipIndex( indexName, EXACT_CONFIG );
- }
-
- BufferedReader bf = new BufferedReader(new FileReader(file));
-
- final Data data = new Data(bf.readLine(), "\t", 1);
- Object[] rel = new Object[1];
- String line;
- report.reset();
- while ((line = bf.readLine()) != null) {
- final Map<String, Object> properties = data.update(line, rel);
- index.add(id(rel[0]), properties);
- report.dots();
- }
-
- report.finishImport("Relationships into " + indexName + " Index");
+ private BatchInserterIndex nodeIndexFor(String indexName, String indexType) {
+ return lucene.nodeIndex(indexName, configFor(indexType));
+ }
+ private BatchInserterIndex relationshipIndexFor(String indexName, String indexType) {
+ return lucene.relationshipIndex(indexName, configFor(indexType));
}
+ private Map<String, String> configFor(String indexType) {
+ return indexType.equals("fulltext") ? FULLTEXT_CONFIG : EXACT_CONFIG;
+ }
static class RelType implements RelationshipType {
String name;
@@ -298,7 +276,7 @@ public String name() {
}
}
- enum Type {
+ public enum Type {
BOOLEAN {
@Override
public Object convert(String value) {
@@ -0,0 +1,21 @@
+import org.junit.Test;
+import org.neo4j.batchimport.Importer;
+
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+
+public class DataTest {
+ @Test
+ public void testConvertType() throws Exception {
+ Importer.Data data = new Importer.Data("a:int\tb:float\tc:float", "\t", 0);
+ Map<String,Object> row = data.update("100\t100.0\t1E+10");
+ assertEquals(100, row.get("a"));
+ assertEquals(true,row.get("b") instanceof Float);
+ assertEquals(100.0F, row.get("b"));
+ assertEquals(true,row.get("b") instanceof Float);
+ assertEquals(100.0F, row.get("b"));
+ assertEquals(true,row.get("c") instanceof Float);
+ assertEquals(1E+10F, row.get("c"));
+ }
+}
@@ -1,3 +1,5 @@
+import org.junit.Ignore;
+
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
@@ -7,6 +9,7 @@
* @author mh
* @since 13.01.12
*/
+@Ignore
public class TestDataGenerator {
private static int NODES = 75 * 1000 * 100; // * 1000;

0 comments on commit 81ff8e4

Please sign in to comment.