@Override public ObjectId createObjectId(int time, int machine, int process, int inc) { return new ObjectId(time, machine, process, inc); }
@Override public boolean equals( Object obj ) { if (obj == this) return true; if (obj instanceof ObjectId) { ObjectId that = (ObjectId)obj; return this.getTime() == that.getTime() && this.getMachine() == that.getMachine() && this.getProcess() == this.getProcess() && this.getInc() == that.getInc(); } return false; }
} else if (value instanceof ObjectId) { ObjectId id = (ObjectId)value; value = new de.undercouch.bson4jackson.types.ObjectId(id.getTime(), id.getMachine(), id.getInc()); } else if (value instanceof Timestamp) { Timestamp ts = (Timestamp)value;
public String getBytesInBase16() { byte b[] = getBytes(); StringBuilder buf = new StringBuilder(24); for (int i = 0; i < b.length; i++) { int x = b[i] & 0xFF; String s = Integer.toHexString(x); if (s.length() == 1) { buf.append("0"); } buf.append(s); } return buf.toString(); }
@Override public int hashCode() { return getTime(); }
protected void write( ObjectId value, Writer writer ) throws IOException { write(new BasicDocument(OBJECT_ID, value.getBytesInBase16()), writer); }
public String getBytesInBase16() { byte b[] = getBytes(); StringBuilder buf = new StringBuilder(24); for (int i = 0; i < b.length; i++) { int x = b[i] & 0xFF; String s = Integer.toHexString(x); if (s.length() == 1) { buf.append("0"); } buf.append(s); } return buf.toString(); }
@Override public int hashCode() { return getTime(); }
protected void write( ObjectId value, Writer writer ) throws IOException { write(new BasicDocument(OBJECT_ID, value.getBytesInBase16()), writer); }
@Override public boolean equals( Object obj ) { if (obj == this) return true; if (obj instanceof ObjectId) { ObjectId that = (ObjectId)obj; return this.getTime() == that.getTime() && this.getMachine() == that.getMachine() && this.getProcess() == this.getProcess() && this.getInc() == that.getInc(); } return false; }
@Override public ObjectId createObjectId(int time, int machine, int process, int inc) { return new ObjectId(time, machine, process, inc); }
protected void write( String name, ObjectId value, BsonDataOutput output ) { output.writeByte(Type.OBJECTID); writeCString(name, output); output.write(value.getBytes()); }
/** * Write the 12-byte representation of the ObjectId per the BSON specification (or rather the <a * href="http://www.mongodb.org/display/DOCS/Object+IDs">MongoDB documentation</a>). * * @param id * the ObjectId; may not be null * @param b * the bytes into which the object ID should be written */ public static void writeObjectId(ObjectId id, byte[] b) { int time = id.getTime(); int machine = id.getMachine(); int process = id.getProcess(); int inc = id.getInc(); b[0] = (byte) ((time >>> 24) & 0xFF); b[1] = (byte) ((time >>> 16) & 0xFF); b[2] = (byte) ((time >>> 8) & 0xFF); b[3] = (byte) ((time >>> 0) & 0xFF); b[4] = (byte) ((machine >>> 16) & 0xFF); b[5] = (byte) ((machine >>> 8) & 0xFF); b[6] = (byte) ((machine >>> 0) & 0xFF); b[7] = (byte) ((process >>> 8) & 0xFF); b[8] = (byte) ((process >>> 0) & 0xFF); b[9] = (byte) ((inc >>> 16) & 0xFF); b[10] = (byte) ((inc >>> 8) & 0xFF); b[11] = (byte) ((inc >>> 0) & 0xFF); }
/** * The BSON specification (or rather the <a href="http://www.mongodb.org/display/DOCS/Object+IDs">MongoDB * documentation</a>) defines the structure of this data: * <p> * <quote>"A BSON ObjectID is a 12-byte value consisting of a 4-byte timestamp (seconds since epoch), a 3-byte * machine id, a 2-byte process id, and a 3-byte counter. Note that the timestamp and counter fields must be stored * big endian unlike the rest of BSON. This is because they are compared byte-by-byte and we want to ensure a mostly * increasing order."</quote> * </p> * * @param bytes * @return the ObjectId */ public static ObjectId readObjectId(byte[] bytes) { // Compute the values in big-endian ... int time = ((bytes[0] & 0xff) << 24) + ((bytes[1] & 0xff) << 16) + ((bytes[2] & 0xff) << 8) + ((bytes[3] & 0xff) << 0); int machine = ((bytes[4] & 0xff) << 16) + ((bytes[5] & 0xff) << 8) + ((bytes[6] & 0xff) << 0); int process = ((bytes[7] & 0xff) << 8) + ((bytes[8] & 0xff) << 0); int inc = ((bytes[9] & 0xff) << 16) + ((bytes[10] & 0xff) << 8) + ((bytes[11] & 0xff) << 0); // Create the value object ... return new ObjectId(time, machine, process, inc); }
protected void write( String name, ObjectId value, BsonDataOutput output ) { output.writeByte(Type.OBJECTID); writeCString(name, output); output.write(value.getBytes()); }
/** * Write the 12-byte representation of the ObjectId per the BSON specification (or rather the <a * href="http://www.mongodb.org/display/DOCS/Object+IDs">MongoDB documentation</a>). * * @param id * the ObjectId; may not be null * @param b * the bytes into which the object ID should be written */ public static void writeObjectId(ObjectId id, byte[] b) { int time = id.getTime(); int machine = id.getMachine(); int process = id.getProcess(); int inc = id.getInc(); b[0] = (byte) ((time >>> 24) & 0xFF); b[1] = (byte) ((time >>> 16) & 0xFF); b[2] = (byte) ((time >>> 8) & 0xFF); b[3] = (byte) ((time >>> 0) & 0xFF); b[4] = (byte) ((machine >>> 16) & 0xFF); b[5] = (byte) ((machine >>> 8) & 0xFF); b[6] = (byte) ((machine >>> 0) & 0xFF); b[7] = (byte) ((process >>> 8) & 0xFF); b[8] = (byte) ((process >>> 0) & 0xFF); b[9] = (byte) ((inc >>> 16) & 0xFF); b[10] = (byte) ((inc >>> 8) & 0xFF); b[11] = (byte) ((inc >>> 0) & 0xFF); }
/** * The BSON specification (or rather the <a href="http://www.mongodb.org/display/DOCS/Object+IDs">MongoDB * documentation</a>) defines the structure of this data: * <p> * <quote>"A BSON ObjectID is a 12-byte value consisting of a 4-byte timestamp (seconds since epoch), a 3-byte * machine id, a 2-byte process id, and a 3-byte counter. Note that the timestamp and counter fields must be stored * big endian unlike the rest of BSON. This is because they are compared byte-by-byte and we want to ensure a mostly * increasing order."</quote> * </p> * * @param bytes * @return the ObjectId */ public static ObjectId readObjectId(byte[] bytes) { // Compute the values in big-endian ... int time = ((bytes[0] & 0xff) << 24) + ((bytes[1] & 0xff) << 16) + ((bytes[2] & 0xff) << 8) + ((bytes[3] & 0xff) << 0); int machine = ((bytes[4] & 0xff) << 16) + ((bytes[5] & 0xff) << 8) + ((bytes[6] & 0xff) << 0); int process = ((bytes[7] & 0xff) << 8) + ((bytes[8] & 0xff) << 0); int inc = ((bytes[9] & 0xff) << 16) + ((bytes[10] & 0xff) << 8) + ((bytes[11] & 0xff) << 0); // Create the value object ... return new ObjectId(time, machine, process, inc); }
} else if (value instanceof ObjectId) { ObjectId id = (ObjectId)value; value = new org.bson.types.ObjectId(id.getBytes()); } else if (value instanceof Timestamp) { Timestamp ts = (Timestamp)value;
@Test public void shouldRoundTripSimpleBsonObjectWithObjectId() { // print = true; int time = Math.abs((int) new Date().getTime()); if (print) System.out.println("time value: " + time); input = new BasicDocument("foo", new ObjectId(time, 1, 2, 3)); assertRoundtrip(input); }
@Test public void shouldCorrectlyWriteObjectId() { ObjectId id = new ObjectId(300, 200, 9, 15); String expected = "{ \"$oid\" : \"0000012c0000c8000900000f\" }"; String actual = writer.write(id); assertSame(expected, actual); }