Java 类org.bson.BasicBSONEncoder 实例源码

项目:mongodb-async-performance    文件:BsonPerformanceITest.java   
/**
 * Writes large documents to a {@link BasicBSONEncoder}.
 * 
 * @param levels
 *            The number of levels to create in the tree.
 * @return The time to write each document in microseconds.
 * @see #testLargeDocumentCreateAndWritePerformance()
 */
protected double doLegacyLargeDocCreateAndWrite(final int levels) {
    final BasicBSONEncoder encoder = new BasicBSONEncoder();

    final int iterations = ITERATIONS / (levels << 1);

    final long startTime = System.nanoTime();
    for (int i = 0; i < iterations; ++i) {

        final BasicBSONObject obj = new BasicBSONObject("_id",
                Integer.valueOf(myRandom.nextInt()));

        addLegacyLevel(obj, 1, levels);

        encoder.encode(obj);
    }

    final long endTime = System.nanoTime();
    final double delta = ((double) (endTime - startTime))
            / TimeUnit.MICROSECONDS.toNanos(1);
    return (delta / iterations);
}
项目:mongodb-async-performance    文件:BsonPerformanceITest.java   
/**
 * Writes large documents to a {@link BasicBSONEncoder}.
 * 
 * @param levels
 *            The number of levels to create in the tree.
 * @return The time to write each document in microseconds.
 * @see #testLargeDocumentWritePerformance()
 */
protected double doLegacyLargeDocWrite(final int levels) {
    final BasicBSONEncoder encoder = new BasicBSONEncoder();

    final int iterations = ITERATIONS / (levels << 1);

    // Create the tree once.
    final BasicBSONObject obj = new BasicBSONObject("_id",
            Integer.valueOf(myRandom.nextInt()));
    addLegacyLevel(obj, 1, levels);

    final long startTime = System.nanoTime();
    for (int i = 0; i < iterations; ++i) {
        encoder.encode(obj);
    }

    final long endTime = System.nanoTime();
    final double delta = ((double) (endTime - startTime))
            / TimeUnit.MICROSECONDS.toNanos(1);
    return (delta / iterations);
}
项目:mongodb-async-performance    文件:BsonPerformanceITest.java   
/**
 * Writes micro documents to a {@link BasicBSONEncoder}.
 * 
 * @return The time to write each document in microseconds.
 * @see #testMicroDocumentWritePerformance()
 */
protected double doLegacyMicroDocWrite() {
    final BasicBSONEncoder encoder = new BasicBSONEncoder();

    final long startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        final BasicBSONObject obj = new BasicBSONObject("_id",
                Integer.valueOf(myRandom.nextInt()));

        encoder.encode(obj);
    }

    final long endTime = System.nanoTime();
    final double delta = ((double) (endTime - startTime))
            / TimeUnit.MICROSECONDS.toNanos(1);
    return (delta / ITERATIONS);
}
项目:mongodb-async-performance    文件:BsonPerformanceITest.java   
/**
 * Writes small documents to a {@link BasicBSONEncoder}.
 * 
 * @return The time to write each document in microseconds.
 * @see #testSmallDocumentWritePerformance()
 */
protected double doLegacySmallDocWrite() {
    final BasicBSONEncoder encoder = new BasicBSONEncoder();

    final long startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        final BasicBSONObject obj = new BasicBSONObject("_id",
                Integer.valueOf(myRandom.nextInt()));
        obj.put("v", SMALL_VALUE);

        encoder.encode(obj);
    }

    final long endTime = System.nanoTime();
    final double delta = ((double) (endTime - startTime))
            / TimeUnit.MICROSECONDS.toNanos(1);
    return (delta / ITERATIONS);
}
项目:morphia    文件:Mapper.java   
<T> Key<T> createKey(final Class<T> clazz, final Object id) {
    if (id instanceof Serializable) {
        return createKey(clazz, (Serializable) id);
    }

    //TODO: cache the encoders, maybe use the pool version of the buffer that the driver does.
    final BSONEncoder enc = new BasicBSONEncoder();
    return new Key<T>(clazz, getCollectionName(clazz), enc.encode(toDBObject(id)));
}
项目:usergrid    文件:BSONUtils.java   
@Override
protected BSONEncoder initialValue() {
    return new BasicBSONEncoder();
}
项目:mongodb-async-performance    文件:BsonPerformanceITest.java   
/**
 * Measures the relative performance for reading and writing a large
 * document of the format below. The integer values are randomly generated.
 * <blockquote> <code><pre>
 * {
 *    _id : &lt;integer&gt;,
 *    left_1 : {
 *       left_2: {
 *          ... to N levels of nesting.
 *          v : &lt;integer&gt;
 *       }
 *       right_2: {
 *          ... to N levels of nesting.
 *          v : &lt;integer&gt;
 *       }
 *    }
 *    right_1 : {
 *       left_2: {
 *          ... to N levels of nesting.
 *          v : &lt;integer&gt;
 *       }
 *       right_2: {
 *          ... to N levels of nesting.
 *          v : &lt;integer&gt;
 *       }
 *    }
 * }
 * </pre></code></blockquote>
 */
@Test
public void testLargeDocumentReadPerformance() {

    final BasicBSONEncoder encoder = new BasicBSONEncoder();

    for (int level = 1; level <= LARGE_DOC_LEVELS; level += 3) {

        final BasicBSONObject obj = new BasicBSONObject("_id",
                Integer.valueOf(myRandom.nextInt()));
        addLegacyLevel(obj, 1, level);
        final byte[] docBytes = encoder.encode(obj);

        String label = "Read BSON " + level + " Level Tree ("
                + docBytes.length + " Bytes)";

        double legacy = doLegacyRead(docBytes.clone(), (level << 1));
        double bstream = doBStreamRead(docBytes.clone(), (level << 1));

        System.out.printf(DATA_FORMAT, label, Double.valueOf(legacy),
                Double.valueOf(bstream), Double.valueOf(-1));
    }
}
项目:mongodb-async-performance    文件:BsonPerformanceITest.java   
/**
 * Writes medium documents to a {@link BasicBSONEncoder}.
 * 
 * @return The time to write each document in microseconds.
 * @see #testMediumDocumentWritePerformance()
 */
protected double doLegacyMediumDocWrite() {
    final List<String> words = new ArrayList<>();
    for (int j = 0; j < 20; ++j) {
        words.add("10gen");
        words.add("web");
        words.add("open");
        words.add("source");
        words.add("application");
        words.add("paas");
        words.add("platforma-as-a-service");
        words.add("technology");
        words.add("helps");
        words.add("developers");
        words.add("focus");
        words.add("building");
        words.add("mongodb");
        words.add("mongo");
    }
    final BasicBSONObject meta = new BasicBSONObject();
    meta.append("description", "i am a long description string");
    meta.append("author", "Holly Man");
    meta.append("dynamically_create_meta_tag", "who know\n what");

    final BasicBSONObject struct = new BasicBSONObject();
    struct.append("counted_tags", Integer.valueOf(3450));
    struct.append("no_of_js_attached", Integer.valueOf(10));
    struct.append("no_of_images", Integer.valueOf(6));

    final BasicBSONObject obj = new BasicBSONObject("_id",
            new org.bson.types.ObjectId());

    obj.append("base_url", "http://www.example.com/test-me");
    obj.append("total_world_count", Integer.valueOf(6743));
    obj.append("access_time", new Date()); // current time
    obj.append("meta_tags", meta);
    obj.append("page_structure", struct);
    obj.append("harvested_words", words);

    final BasicBSONEncoder encoder = new BasicBSONEncoder();

    final long startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {

        encoder.encode(obj);
    }

    final long endTime = System.nanoTime();
    final double delta = ((double) (endTime - startTime))
            / TimeUnit.MICROSECONDS.toNanos(1);
    return (delta / ITERATIONS);
}