Skip to content

Commit

Permalink
Addendum to a986796.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisoelkers committed Jun 25, 2015
1 parent a857783 commit 93983be
Showing 1 changed file with 33 additions and 85 deletions.
Expand Up @@ -16,88 +16,45 @@
*/ */
package integration.util.mongodb; package integration.util.mongodb;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
import org.apache.commons.io.FilenameUtils;
import org.bson.BSONDecoder;
import org.bson.BSONObject;
import org.bson.BasicBSONDecoder;
import org.bson.Document; import org.bson.Document;


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;


public class MongodbSeed { public class MongodbSeed {
public List<Document> readBsonFile(String filename){ private final MongoClient mongoClient;
Path filePath = Paths.get(filename); private final String dbName;
List<Document> dataset = new ArrayList<>();

try {
ByteArrayInputStream fileBytes = new ByteArrayInputStream(Files.readAllBytes(filePath));
BSONDecoder decoder = new BasicBSONDecoder();
BSONObject obj;

while((obj = decoder.readObject(fileBytes)) != null) {
if(!obj.toString().trim().isEmpty()) {
Document mongoDocument = new Document();
mongoDocument.putAll(obj.toMap());
dataset.add(mongoDocument);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("Can not open BSON input file.", e);
} catch (JsonProcessingException e) {
e.printStackTrace();
throw new RuntimeException("Can not parse BSON data.", e);
} catch (IOException e) {
//EOF
}


return dataset; public MongodbSeed(String dbName) {
this.dbName = dbName;
mongoClient = new MongoClient(
URI.create(System.getProperty("gl2.baseuri", "http://localhost")).getHost(),
Integer.parseInt(System.getProperty("mongodb.port", "27017")));
mongoClient.dropDatabase(dbName);
} }


public Map<String, List<Document>> parseDatabaseDump(String dbPath){ private Map<String, List<Document>> parseDatabaseDump(URL seedUrl) throws IOException {
Map<String, List<Document>> collections = new HashMap<>(); final DumpReader dumpReader;

if (seedUrl.getPath().endsWith(".json")) {
URL seedUrl = Thread.currentThread().getContextClassLoader().getResource("integration/seeds/mongodb/" + dbPath); dumpReader = new JsonReader(seedUrl);
File dir = new File(seedUrl.getPath()); } else {
File[] collectionListing = dir.listFiles(); dumpReader = new BsonReader(seedUrl);

if (collectionListing != null) {
for (File collection : collectionListing) {
if (collection.getName().endsWith(".bson") && !collection.getName().startsWith("system.indexes.")) {
List<Document> collectionData = readBsonFile(collection.getAbsolutePath());
collections.put(FilenameUtils.removeExtension(collection.getName()), collectionData);
}
}
} }

return dumpReader.toMap();

return collections;
} }


public Map<String, List<Document>> updateNodeIdFirstNode(Map<String, List<Document>> collections, String nodeId) { private Map<String, List<Document>> updateNodeIdFirstNode(Map<String, List<Document>> collections, String nodeId) {
List<Document> nodes = new ArrayList<>(); final List<Document> nodes = collections.get("nodes");


for (Map.Entry<String, List<Document>> collection : collections.entrySet()) { if (nodes == null || nodes.isEmpty())
if(collection.getKey().equals("nodes")) { return collections;
nodes = collection.getValue();
}
}


Document firstNode = nodes.get(0); Document firstNode = nodes.get(0);
firstNode.put("node_id", nodeId); firstNode.put("node_id", nodeId);
Expand All @@ -109,18 +66,15 @@ public Map<String, List<Document>> updateNodeIdFirstNode(Map<String, List<Docume
return collections; return collections;
} }


public Map<String, List<Document>> updateNodeIdInputs(Map<String, List<Document>> collections, String nodeId) { private Map<String, List<Document>> updateNodeIdInputs(Map<String, List<Document>> collections, String nodeId) {
List<Document> inputs = new ArrayList<>(); List<Document> inputs = collections.get("inputs");


for (Map.Entry<String, List<Document>> collection : collections.entrySet()) { if (inputs == null) {
if(collection.getKey().equals("inputs")) { return collections;
for (Document input : collection.getValue()){ }
input.remove("node_id");
input.put("node_id", nodeId); for (Document input : inputs){
inputs.add(input); input.put("node_id", nodeId);
}

}
} }


collections.remove("inputs"); collections.remove("inputs");
Expand All @@ -129,25 +83,19 @@ public Map<String, List<Document>> updateNodeIdInputs(Map<String, List<Document>
return collections; return collections;
} }


public void loadDataset(String dbPath, String dbName, String nodeId){ public void loadDataset(URL dbPath, String nodeId) throws IOException {
Map<String, List<Document>> collections = parseDatabaseDump(dbPath); Map<String, List<Document>> collections = parseDatabaseDump(dbPath);
collections = updateNodeIdFirstNode(collections, nodeId); collections = updateNodeIdFirstNode(collections, nodeId);
collections = updateNodeIdInputs(collections, nodeId); collections = updateNodeIdInputs(collections, nodeId);


MongoClient mongoClient = new MongoClient( final MongoDatabase mongoDatabase = mongoClient.getDatabase(dbName);
URI.create(System.getProperty("gl2.baseuri", "http://localhost")).getHost(),
Integer.parseInt(System.getProperty("mongodb.port", "27017")));

mongoClient.dropDatabase(dbName);
MongoDatabase mongoDatabase = mongoClient.getDatabase(dbName);


for (Map.Entry<String, List<Document>> collection : collections.entrySet()) { for (Map.Entry<String, List<Document>> collection : collections.entrySet()) {
String collectionName = collection.getKey(); final String collectionName = collection.getKey();
mongoDatabase.createCollection(collectionName); mongoDatabase.createCollection(collectionName);
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection(collection.getKey()); final MongoCollection<Document> mongoCollection = mongoDatabase.getCollection(collection.getKey());
for (Document document : collection.getValue()) {
mongoCollection.insertOne(document); mongoCollection.insertMany(collection.getValue());
}
} }
} }
} }

0 comments on commit 93983be

Please sign in to comment.