use of org.bson.json.JsonReader in project mongo-java-driver by mongodb.
the class MessageHelper method encodeJson.
private static ByteBuf encodeJson(final String json) {
OutputBuffer outputBuffer = new BasicOutputBuffer();
JsonReader jsonReader = new JsonReader(json);
BsonDocumentCodec codec = new BsonDocumentCodec();
BsonDocument document = codec.decode(jsonReader, DecoderContext.builder().build());
BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
codec.encode(writer, document, EncoderContext.builder().build());
ByteBuffer documentByteBuffer = ByteBuffer.allocate(outputBuffer.size());
documentByteBuffer.put(outputBuffer.toByteArray());
return new ByteBufNIO(documentByteBuffer);
}
use of org.bson.json.JsonReader in project mongo-java-driver by mongodb.
the class Document method parse.
/**
* Parses a string in MongoDB Extended JSON format to a {@code Document}
*
* @param json the JSON string
* @param decoder the {@code Decoder} to use to parse the JSON string into a {@code Document}
* @return a corresponding {@code Document} object
* @see org.bson.json.JsonReader
* @mongodb.driver.manual reference/mongodb-extended-json/ MongoDB Extended JSON
*/
public static Document parse(final String json, final Decoder<Document> decoder) {
notNull("codec", decoder);
JsonReader bsonReader = new JsonReader(json);
return decoder.decode(bsonReader, DecoderContext.builder().build());
}
use of org.bson.json.JsonReader in project mongo-java-driver by mongodb.
the class MultiFileImportBenchmark method importJsonFile.
private Runnable importJsonFile(final CountDownLatch latch, final int fileId) {
final RawBsonDocumentCodec codec = new RawBsonDocumentCodec();
return new Runnable() {
@Override
public void run() {
try {
String resourcePath = "parallel/ldjson_multi/ldjson" + String.format("%03d", fileId) + ".txt";
BufferedReader reader = new BufferedReader(readFromRelativePath(resourcePath), 1024 * 64);
try {
String json;
List<RawBsonDocument> documents = new ArrayList<RawBsonDocument>(1000);
while ((json = reader.readLine()) != null) {
RawBsonDocument document = codec.decode(new JsonReader(json), DecoderContext.builder().build());
documents.add(document);
if (documents.size() == 1000) {
final List<RawBsonDocument> documentsToInsert = documents;
documentWritingService.submit(new Runnable() {
@Override
public void run() {
collection.insertMany(documentsToInsert, new InsertManyOptions().ordered(false));
latch.countDown();
}
});
documents = new ArrayList<RawBsonDocument>(1000);
}
}
if (!documents.isEmpty()) {
throw new IllegalStateException("Document count not a multiple of 1000");
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
reader.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
}
use of org.bson.json.JsonReader in project mongo-java-driver by mongodb.
the class MultiFileExportBenchmark method importJsonFiles.
private void importJsonFiles() throws InterruptedException {
ExecutorService importService = Executors.newFixedThreadPool(FILE_READING_THREAD_POOL_SIZE);
final CountDownLatch latch = new CountDownLatch(100);
for (int i = 0; i < 100; i++) {
final int fileId = i;
importService.submit(new Runnable() {
@Override
public void run() {
try {
String resourcePath = "parallel/ldjson_multi/ldjson" + String.format("%03d", fileId) + ".txt";
BufferedReader reader = new BufferedReader(readFromRelativePath(resourcePath), 1024 * 64);
try {
String json;
List<BsonDocument> documents = new ArrayList<BsonDocument>(1000);
while ((json = reader.readLine()) != null) {
BsonDocument document = new BsonDocumentCodec().decode(new JsonReader(json), DecoderContext.builder().build());
document.put("fileId", new BsonInt32(fileId));
documents.add(document);
}
database.getCollection(COLLECTION_NAME, BsonDocument.class).insertMany(documents, new InsertManyOptions().ordered(false));
latch.countDown();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
reader.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}
latch.await(1, TimeUnit.MINUTES);
collection.createIndex(new BsonDocument("fileId", new BsonInt32(1)));
importService.shutdown();
importService.awaitTermination(1, TimeUnit.MINUTES);
}
use of org.bson.json.JsonReader in project mongo-java-driver by mongodb.
the class AbstractFindBenchmark method setUp.
public void setUp() throws Exception {
super.setUp();
collection = client.getDatabase(DATABASE_NAME).getCollection(COLLECTION_NAME, clazz);
byte[] bytes = readAllBytesFromRelativePath(resourcePath);
fileLength = bytes.length;
MongoDatabase setUpDatabase = client.getDatabase(DATABASE_NAME);
setUpDatabase.drop();
insertCopiesOfDocument(setUpDatabase.getCollection(COLLECTION_NAME, BsonDocument.class), new BsonDocumentCodec().decode(new JsonReader(new String(bytes, StandardCharsets.UTF_8)), DecoderContext.builder().build()));
}
Aggregations