@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 addEncodeStatements(TypeMirror type, CodeGeneratorContext ctx) { ctx.builder() .addStatement("Codec codec = this.registry.get($L.getClass())", ctx.getter()) .addStatement("encoderContext.encodeWithChildContext(codec, writer, $L)", ctx.getter()); // ctx.builder() // .addAnnotation(AnnotationSpec.builder(SuppressWarnings.class) // .addMember("value", "$S", "unchecked") // .addMember("value", "$S", "rawtypes").build()); ctx.instanceFields().add(ImmutableInstanceField.builder() .type(ClassName.get(CodecRegistry.class)).name("registry").build()); ctx.instanceFields() .add(ImmutableInstanceField.builder() .type(ClassName.get(BsonTypeClassMap.class)) .name("bsonTypeClassMap").build()); }
synchronized public static DefaultReader get(CodecRegistry registry) { synchronized (lock) { if (instance == null) { instance = new DefaultReader(registry, new BsonTypeClassMap()); } else { instance.registry.set(registry); } } return instance; }
public static void registerCustomizedBsonTypeClassMap(BsonTypeClassMap bsonTypeClassMap) { Utils.checkNotNull(bsonTypeClassMap, "bsonTypeClassMap should not be null!"); BSON_TYPE_CLASS_MAP = bsonTypeClassMap; }
private DefaultReader(CodecRegistry registry) { this(registry, new BsonTypeClassMap()); }
private DefaultReader(CodecRegistry registry, BsonTypeClassMap bsonTypeClassMap) { this.registry = new AtomicReference<CodecRegistry>(registry); this.bsonTypeClassMap = bsonTypeClassMap; }
public ArrayCodec( CodecRegistry registry, BsonTypeClassMap bsonTypeClassMap ) { listCodec = new ListCodec( registry, bsonTypeClassMap ); }
/** * Creates a new {@code ListCodec}, which will use the given {@code CodecRegistry} and {@code BsonTypeClassMap} for * encoding and decoding the values in the List. * * @param registry contains the codecs required to encode and decode {@code List} values * @param bsonTypeClassMap will be used for decoding BSON values into the List. */ public ListCodec( final CodecRegistry registry, final BsonTypeClassMap bsonTypeClassMap ) { this.registry = registry; this.bsonTypeClassMap = bsonTypeClassMap; }