Java 类org.bson.codecs.DocumentCodec 实例源码

项目:drill    文件:MongoGroupScan.java   
@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);
  }
}
项目:mongolastic    文件:ElasticBulkService.java   
/**
 * 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();
    }
}
项目:emfjson-mongo    文件:MongoInputStream.java   
@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());
}
项目:mongowp    文件:MongoClientWrapper.java   
@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);
  }
}
项目:oap    文件:JsonCodec.java   
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 );
}
项目:cherimodata    文件:EntityCodecProvider.java   
@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;
}
项目:BsonMapper    文件:DefaultBsonMapper.java   
@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);
}
项目:verbum-domini    文件:VerseCodec.java   
public VerseCodec() {
     this.documentCodec = new DocumentCodec();
}
项目:verbum-domini    文件:BookCodec.java   
public BookCodec() {
     this.documentCodec = new DocumentCodec();
}
项目:verbum-domini    文件:BibleCodec.java   
public BibleCodec() {
     this.documentCodec = new DocumentCodec();
}
项目:verbum-domini    文件:UserCodec.java   
public UserCodec() {
     this.documentCodec = new DocumentCodec();
}
项目:verbum-domini    文件:ChapterCodec.java   
public ChapterCodec() {
     this.documentCodec = new DocumentCodec();
}
项目:lumongo    文件:LumongoUtil.java   
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();
}
项目:lumongo    文件:LumongoUtil.java   
public static Document byteArrayToMongoDocument(byte[] byteArray) {
    BsonBinaryReader bsonReader = new BsonBinaryReader(ByteBuffer.wrap(byteArray));
    return new DocumentCodec().decode(bsonReader, DecoderContext.builder().build());
}