Permalink
Browse files

Really add everything related to BSON symbols.

I forget to commit everything I've wanted last time, so here's an amendment for
it. Also I didn't indicate I've updated the DefaultPredicate class (which I
did), sooo ... that's a wrap!
  • Loading branch information...
1 parent 04ab8ec commit 1644dce78bca67536a1efa18695d65c5dca6181d @kohanyirobert committed May 15, 2012
@@ -118,7 +118,8 @@
* type.
* </p>
*/
- SYMBOL(BsonBytes.SYMBOL),
+ SYMBOL(BsonBytes.SYMBOL, DefaultPredicate.SYMBOL,
+ DefaultReader.SYMBOL, DefaultWriter.SYMBOL),
/**
* JavaScript code with scope.
@@ -117,6 +117,16 @@ public boolean apply(Class<?> input) {
}
},
+ SYMBOL {
+
+ @Override
+ public boolean apply(Class<?> input) {
+ return input == null
+ ? false
+ : BsonSymbol.class.isAssignableFrom(input);
+ }
+ },
+
INT32 {
@Override
@@ -180,6 +180,14 @@ private int optionToFlag(char option) {
}
},
+ SYMBOL {
+
+ @Override
+ Object checkedReadFrom(ByteBuffer buffer) {
+ return new BasicSymbol(buffer);
+ }
+ },
+
INT32 {
@Override
@@ -169,6 +169,15 @@ private boolean hasFlag(int flags, int flag) {
}
},
+ SYMBOL {
+
+ @Override
+ public void writeTo(ByteBuffer buffer, Object reference) {
+ ByteBuffer symbol = ((BsonSymbol) reference).symbol();
+ buffer.putInt(symbol.capacity() + 1).put(symbol).put(BsonBytes.EOO);
+ }
+ },
+
INT32 {
@Override
@@ -0,0 +1,42 @@
+package com.github.kohanyirobert.ebson;
+
+import com.google.common.base.Charsets;
+import com.google.common.primitives.Ints;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+public final class DefaultSymbolReaderWriterTest extends AbstractReaderWriterTest {
+
+ private static final String SYMBOL_ALPHABET = "abcdefghijklmnopqrstuvwxyz";
+ private static final int MAX_SYMBOL_LENGTH = 1024;
+ private static final ThreadLocal<ByteBuffer> RANDOM_SYMBOL = new ThreadLocal<ByteBuffer>() {
+
+ @Override
+ protected ByteBuffer initialValue() {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < BsonRandom.nextInt(MAX_SYMBOL_LENGTH); i++)
+ sb.append(SYMBOL_ALPHABET.charAt(BsonRandom.nextInt(SYMBOL_ALPHABET.length())));
+ ByteBuffer symbol = ByteBuffer.allocate(Ints.BYTES + sb.length() + 1)
+ .order(ByteOrder.LITTLE_ENDIAN)
+ .putInt(sb.length() + 1)
+ .put(sb.toString().getBytes(Charsets.UTF_8))
+ .put(BsonBytes.EOO);
+ symbol.flip();
+ return symbol;
+ }
+ };
+
+ public DefaultSymbolReaderWriterTest() {
+ super(DefaultReader.SYMBOL, DefaultWriter.SYMBOL);
+ }
+
+ @Test
+ public void randomSymbol() {
+ assertEquals(writeTo(new BasicSymbol(RANDOM_SYMBOL.get())), readFrom());
+ }
+}

0 comments on commit 1644dce

Please sign in to comment.