@Override public ScanStats getScanStats() { try{ MongoClient client = storagePlugin.getClient(); MongoDatabase db = client.getDatabase(scanSpec.getDbName()); MongoCollection<Document> collection = db.getCollection(scanSpec.getCollectionName()); long numDocs = collection.count(); float approxDiskCost = 0; if (numDocs != 0) { //toJson should use client's codec, otherwise toJson could fail on // some types not known to DocumentCodec, e.g. DBRef. final DocumentCodec codec = new DocumentCodec(client.getMongoClientOptions().getCodecRegistry(), new BsonTypeClassMap()); String json = collection.find().first().toJson(codec); approxDiskCost = json.getBytes().length * numDocs; } return new ScanStats(GroupScanProperty.EXACT_ROW_COUNT, numDocs, 1, approxDiskCost); } catch (Exception e) { throw new DrillRuntimeException(e.getMessage(), e); } }
/** * Customizations for the document.toJson output. * <p> * http://mongodb.github.io/mongo-java-driver/3.0/bson/codecs/ * * @return the toJson encoder. */ private Encoder<Document> getEncoder() { ArrayList<Codec<?>> codecs = new ArrayList<>(); if (config.getElastic().getDateFormat() != null) { // Replace default DateCodec class to use the custom date formatter. codecs.add(new CustomDateCodec(config.getElastic().getDateFormat())); } if (config.getElastic().getLongToString()) { // Replace default LongCodec class codecs.add(new CustomLongCodec()); } if (codecs.size() > 0) { BsonTypeClassMap bsonTypeClassMap = new BsonTypeClassMap(); CodecRegistry codecRegistry = CodecRegistries.fromRegistries( CodecRegistries.fromCodecs(codecs), MongoClient.getDefaultCodecRegistry()); return new DocumentCodec(codecRegistry, bsonTypeClassMap); } else { return new DocumentCodec(); } }
@Override public void loadResource(Resource resource) throws IOException { final MongoCollection<Document> collection = handler.getCollection(uri); if (!resource.getContents().isEmpty()) { resource.getContents().clear(); } final String id = uri.segment(2); final Document filter = new Document(MongoHandler.ID_FIELD, id); final Document document = collection .find(filter) .limit(1) .first(); if (document == null) { throw new IOException("Cannot find document with " + MongoHandler.ID_FIELD + " " + id); } JsonWriter writer = new JsonWriter(new StringWriter()); new DocumentCodec().encode(writer, document, EncoderContext.builder() .isEncodingCollectibleDocument(true) .build()); readJson(resource, writer.getWriter().toString()); }
@Inject public MongoClientWrapper(MongoClientConfiguration configuration) throws UnreachableMongoServerException { try { MongoClientOptions options = toMongoClientOptions(configuration); ImmutableList<MongoCredential> credentials = toMongoCredentials(configuration); testAddress(configuration.getHostAndPort(), options); this.configuration = configuration; this.driverClient = new com.mongodb.MongoClient( new ServerAddress( configuration.getHostAndPort().getHostText(), configuration.getHostAndPort().getPort()), credentials, options ); version = calculateVersion(); codecRegistry = CodecRegistries.fromCodecs(new DocumentCodec()); closed = false; } catch (com.mongodb.MongoException ex) { throw new UnreachableMongoServerException(configuration.getHostAndPort(), ex); } }
public JsonCodec( TypeReference<T> tr, Class<T> clazz, Function<T, String> idFunc ) { this.clazz = clazz; this.idFunc = idFunc; documentCodec = new DocumentCodec(); fileReader = Binder.json.readerFor( tr ); fileWriter = Binder.json.writerFor( tr ); }
@Override @SuppressWarnings( "unchecked" ) public <T> Codec<T> get( Class<T> clazz, final CodecRegistry registry ) { if ( clazz.isPrimitive() ) { clazz = Primitives.wrap( clazz ); } if ( codecs.containsKey( clazz ) ) { return (Codec<T>) codecs.get( clazz ); } if ( Entity.class.isAssignableFrom( clazz ) ) { // there are two possible class types we can get. Some are the real interfaces and the other classes are // proxy based Class<?> eclass = Proxy.isProxyClass( clazz ) ? clazz.getInterfaces()[0] : clazz; return (Codec<T>) new EntityCodec( db, EntityFactory.getProperties( (Class<? extends Entity>) eclass ) ); } if ( Document.class.isAssignableFrom( clazz ) ) { return (Codec<T>) new DocumentCodec( registry, mapping ); } if ( List.class.isAssignableFrom( clazz ) ) { return (Codec<T>) new ListCodec( registry, mapping ); } if ( clazz.isArray() ) { return (Codec<T>) new ArrayCodec( registry, mapping ); } return null; }
@Override @SuppressWarnings("unchecked") public <T> T readFrom(Document document, Class<T> target) { BsonDocument bsonDocument = document.toBsonDocument(target, CodecRegistries.fromCodecs(new DocumentCodec())); return (T) TDocument.fromBsonDocument(bsonDocument, target).getEquivalentPOJO(this); }
public VerseCodec() { this.documentCodec = new DocumentCodec(); }
public BookCodec() { this.documentCodec = new DocumentCodec(); }
public BibleCodec() { this.documentCodec = new DocumentCodec(); }
public UserCodec() { this.documentCodec = new DocumentCodec(); }
public ChapterCodec() { this.documentCodec = new DocumentCodec(); }
public static byte[] mongoDocumentToByteArray(Document mongoDocument) { BasicOutputBuffer outputBuffer = new BasicOutputBuffer(); BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer); new DocumentCodec().encode(writer, mongoDocument, EncoderContext.builder().isEncodingCollectibleDocument(true).build()); return outputBuffer.toByteArray(); }
public static Document byteArrayToMongoDocument(byte[] byteArray) { BsonBinaryReader bsonReader = new BsonBinaryReader(ByteBuffer.wrap(byteArray)); return new DocumentCodec().decode(bsonReader, DecoderContext.builder().build()); }