Skip to content

Instantly share code, notes, and snippets.

@JaiHirsch
Last active January 23, 2017 19:45
Show Gist options
  • Save JaiHirsch/f9e8cd0e7e063a655230c74b0f417ab3 to your computer and use it in GitHub Desktop.
Save JaiHirsch/f9e8cd0e7e063a655230c74b0f417ab3 to your computer and use it in GitHub Desktop.
package com.carfax.rnd;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import org.bson.Document;
import org.bson.types.Binary;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
public class HexChecker {
public static void main(String[] args) {
try (MongoClient mc = new MongoClient()) {
MongoCollection<Document> alphaCollection = mc.getDatabase("test").getCollection("hex_alpha_test");
alphaCollection.deleteMany(new Document());
MongoCollection<Document> bigIntCollection = mc.getDatabase("test").getCollection("hex_bigint_test");
bigIntCollection.deleteMany(new Document());
alphaTest(alphaCollection);
bigIntTest(bigIntCollection);
}
}
private static void bigIntTest(MongoCollection<Document> bigIntCollection) {
// BigInteger cannot parse 0x, so it must be stripped off
// the "16" argument represent base 16 radix
BigInteger floor = new BigInteger("0x12f".substring(2), 16);
BigInteger ceiling = new BigInteger("0x12ea".substring(2), 16);
BigInteger between = new BigInteger("0x555".substring(2), 16);
// the toByteArray() method on BigInteger returns a byte array
// containing the two's-complement representation of this BigInteger.
// MongoDB will store the byte array as a binary data field
List<Document> documents = Arrays.asList(new Document[] { new Document("value", floor.toByteArray()),
new Document("value", between.toByteArray()), new Document("value", ceiling.toByteArray()) });
bigIntCollection.insertMany(documents);
rangeQuery(bigIntCollection, floor, ceiling);
// Test with values greater than Long.MAX_VALUE
BigInteger newFloor = new BigInteger("8000000000000000", 16);
BigInteger newBetween = new BigInteger("1dcd64ffffffffffe58250e3", 16);
BigInteger newCeiling = new BigInteger("4563918244f3fffff538dcfb7617ffff", 16);
List<Document> newDocuments = Arrays.asList(new Document[] { new Document("value", newFloor.toByteArray()),
new Document("value", newBetween.toByteArray()), new Document("value", newCeiling.toByteArray()) });
bigIntCollection.insertMany(newDocuments);
rangeQuery(bigIntCollection, newFloor, newCeiling);
}
private static void rangeQuery(MongoCollection<Document> bigIntCollection, BigInteger floor, BigInteger ceiling) {
Document filter = new Document("value", new Document("$gt", floor.toByteArray()).append("$lt",
ceiling.toByteArray()));
FindIterable<Document> find = bigIntCollection.find().filter(filter);
find.iterator().forEachRemaining(new Consumer<Document>() {
@Override
public void accept(Document t) {
byte[] data = ((Binary) t.get("value")).getData();
System.out.println(new BigInteger(data).toString(16));
}
});
}
private static void alphaTest(MongoCollection<Document> collection) {
System.out.println("====================== String data Test =======================\n\n");
for (long i = 0; i < 30; i++) {
collection.insertOne(new Document("hexvalue", Long.toHexString(i)));
}
Document query = new Document("hexvalue", new Document("$gte", Long.toHexString(0l)).append("$lte",
Long.toHexString(5l)));
for (Document document : collection.find(query).sort(new Document("hexvalue", -1))) {
String data = document.getString("hexvalue");
System.out.println("hex: " + data + " decimal: " + Long.valueOf(data, 16));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment