@Override public List<KeyValueConfigEntity> findAll(@Nonnull KeyValueConfigName configName) throws Exception { Objects.requireNonNull(configName); String collectionName = configName.getQualifiedName(); MongoCollection<RawBsonDocument> collection = connector.getDatabase().getCollection(collectionName, RawBsonDocument.class); MongoCursor<RawBsonDocument> it = collection.find().iterator(); if (!it.hasNext()) { return Collections.emptyList(); } RawBsonDocument document = it.next(); ByteArrayInputStream bin = new ByteArrayInputStream(document.getByteBuffer().array()); ObjectMapper objectMapper = MongoConfigObjectMapper.getInstance(); ObjectReader objectReader = objectMapper.readerFor(MongoConfigEntity.class); List<KeyValueConfigEntity> result = ((MongoConfigEntity) objectReader.readValue(bin)).getConfig(); // set groupName on returned config key-value pairs return result.stream().map(input -> input.setConfigName(configName)).collect(Collectors.toList()); }
public void process() { MongoDatabase db = mongoClient.getDatabase("local"); MongoCollection<RawBsonDocument> oplog = db.getCollection("oplog.rs", RawBsonDocument.class); RawBsonDocument lastOplogEntry = oplog.find().sort(new Document("$natural", -1)).first(); BsonTimestamp lastTimestamp = (BsonTimestamp) lastOplogEntry.get("ts"); System.out.println(lastTimestamp); Document query = new Document("ts", new Document("$lt", lastTimestamp)); for (RawBsonDocument doc : oplog.find(query).noCursorTimeout(true)) { BsonString ns = (BsonString) doc.get("ns"); BsonString op = (BsonString) doc.get("op"); // ignore no-op if (!op.getValue().equals("n")) { OplogEntryKey key = new OplogEntryKey(ns.getValue(), op.getValue()); EntryAccumulator accum = accumulators.get(key); if (accum == null) { accum = new EntryAccumulator(key); accumulators.put(key, accum); } long len = doc.getByteBuffer().asNIO().array().length; accum.addExecution(len); } if (stop) { mongoClient.close(); report(); break; } } }
private void insertOne(AuditLogEntity input) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ObjectMapper objectMapper = MongoConfigObjectMapper.getInstance(); objectMapper.writer().writeValue(baos, input); } catch (Exception e) { LOGGER.error("Exception at converting obj: {} to bson, cause: {}", input, e); throw ThrowableUtil.propagate(e); } MongoCollection<RawBsonDocument> collection = connector.getDatabase().getCollection(collectionName, RawBsonDocument.class); collection.insertOne(new RawBsonDocument(baos.toByteArray())); }
public JacksonCodec(ObjectMapper bsonObjectMapper, CodecRegistry codecRegistry, Class<T> type) { this.bsonObjectMapper = bsonObjectMapper; this.rawBsonDocumentCodec = codecRegistry.get(RawBsonDocument.class); this.type = type; }
@Override public T decode(BsonReader reader, DecoderContext decoderContext) { try { RawBsonDocument document = rawBsonDocumentCodec.decode(reader, decoderContext); return bsonObjectMapper.readValue(document.getByteBuffer().array(), type); } catch (IOException e) { throw new UncheckedIOException(e); } }
@Override public void encode(BsonWriter writer, Object value, EncoderContext encoderContext) { try { byte[] data = bsonObjectMapper.writeValueAsBytes(value); rawBsonDocumentCodec.encode(writer, new RawBsonDocument(data), encoderContext); } catch (IOException e) { throw new UncheckedIOException(e); } }