@Override public int compareTo(final ObjectId other) { if (other == null) { throw new NullPointerException(); } byte[] byteArray = toByteArray(); byte[] otherByteArray = other.toByteArray(); for (int i = 0; i < OBJECT_ID_LENGTH; i++) { if (byteArray[i] != otherByteArray[i]) { return ((byteArray[i] & 0xff) < (otherByteArray[i] & 0xff)) ? -1 : 1; } } return 0; }
/** * Converts this instance into a 24-byte hexadecimal string representation. * * @return a string representation of the ObjectId in hexadecimal format */ public String toHexString() { char[] chars = new char[OBJECT_ID_LENGTH * 2]; int i = 0; for (byte b : toByteArray()) { chars[i++] = HEX_CHARS[b >> 4 & 0xF]; chars[i++] = HEX_CHARS[b & 0xF]; } return new String(chars); }
private static Id from(ObjectId objectId) { return new Id(objectId.toByteArray()); }
@Override public void writeObjectId(final ObjectId value) { write(value.toByteArray()); }
@Override public byte[] read(JsonReader in) throws IOException { checkArgument(in instanceof BsonReader, "Should be BsonReader, not some other JsonReader"); return ((BsonReader) in).unwrap().readObjectId().toByteArray(); }
@Description("mongodb ObjectId") @ScalarFunction("objectid") @SqlType("ObjectId") public static Slice ObjectId() { return Slices.wrappedBuffer(new ObjectId().toByteArray()); }
@Override public void doWriteObjectId(final ObjectId value) { bsonOutput.writeByte(BsonType.OBJECT_ID.getValue()); writeCurrentName(); bsonOutput.writeBytes(value.toByteArray()); }
private void writeSlice(BlockBuilder output, Type type, Object value) { String base = type.getTypeSignature().getBase(); if (base.equals(StandardTypes.VARCHAR)) { type.writeSlice(output, utf8Slice(toVarcharValue(value))); } else if (type.equals(OBJECT_ID)) { type.writeSlice(output, wrappedBuffer(((ObjectId) value).toByteArray())); } else if (type.equals(VARBINARY)) { if (value instanceof Binary) { type.writeSlice(output, wrappedBuffer(((Binary) value).getData())); } else { output.appendNull(); } } else { throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Slice: " + type.getTypeSignature()); } }
@Description("mongodb ObjectId from the given string") @ScalarFunction("objectid") @SqlType("ObjectId") public static Slice ObjectId(@SqlType(StandardTypes.VARCHAR) Slice value) { return Slices.wrappedBuffer(new ObjectId(CharMatcher.is(' ').removeFrom(value.toStringUtf8())).toByteArray()); }
@Override protected void doWriteDBPointer(final BsonDbPointer value) { bsonOutput.writeByte(BsonType.DB_POINTER.getValue()); writeCurrentName(); bsonOutput.writeString(value.getNamespace()); bsonOutput.writeBytes(value.getId().toByteArray()); }
@Test public void readWrite() throws Exception { InternalTypes types = ImmutableInternalTypes.builder() .objectId(ObjectId.get()) .id(Id.from(ObjectId.get().toByteArray())) .binary(Binary.create(new byte[] {1, 2, 3})) .time(TimeInstant.of(System.currentTimeMillis())) .build(); repository.insert(types).getUnchecked(); InternalTypes types2 = repository.findAll().fetchAll().getUnchecked().get(0); check(types2).is(types); }
static public ObjectId toObjectId( String stringId) { return new ObjectId( Base64.decodeBase64( stringId)); } static public String toString( ObjectId objectId) { return Base64.encodeBase64URLSafeString( objectId.toByteArray()); }
public String toStringMongod(){ byte b[] = toByteArray(); 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 byte[] getBinaryValue(Base64Variant b64variant) throws IOException { Object n = currentNode(); if (n instanceof byte[]) { return (byte[]) n; } else if (n instanceof org.bson.types.ObjectId) { return ((org.bson.types.ObjectId) n).toByteArray(); } return null; }
@Override public void write(final DataOutput out) throws IOException { out.writeUTF(inputURI.toString()); out.write(fileId.toByteArray()); out.writeInt(chunkId); out.writeLong(fileLength); out.writeInt(chunkSize); }
@Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException { Object n = currentNode(); if (n instanceof byte[]) { return (byte[]) n; } else if (n instanceof org.bson.types.ObjectId) { return ((org.bson.types.ObjectId) n).toByteArray(); } return null; }
@Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException { Object n = currentNode(); if (n instanceof byte[]) { return (byte[]) n; } else if (n instanceof org.bson.types.ObjectId) { return ((org.bson.types.ObjectId) n).toByteArray(); } return null; }
@Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException { Object n = currentNode(); if (n instanceof byte[]) { return (byte[]) n; } else if (n instanceof org.bson.types.ObjectId) { return ((org.bson.types.ObjectId) n).toByteArray(); } return null; }
@Override public Object toObject() { if(oid == null){ // Create a regular ObjectId and get the bytes byte[] oidBytes = (new ObjectId()).toByteArray(); // Paste the 6 byte HD time and construct OID writeTimeStamp(oidBytes, time); oid = new ObjectId(oidBytes); } return oid; }
@Description("mongodb ObjectId from the given string") @ScalarFunction("objectid") @SqlType("ObjectId") public static Slice ObjectId(@SqlType(StandardTypes.VARCHAR) Slice value) { return Slices.wrappedBuffer(new ObjectId(CharMatcher.is(' ').removeFrom(value.toStringUtf8())).toByteArray()); }