static ClassMapBasedObjectSerializer addCommonSerializers() { ClassMapBasedObjectSerializer serializer = new ClassMapBasedObjectSerializer(); serializer.addObjectSerializer(Object[].class, new ObjectArraySerializer(serializer)); serializer.addObjectSerializer(Boolean.class, new ToStringSerializer()); serializer.addObjectSerializer(Code.class, new CodeSerializer(serializer)); serializer.addObjectSerializer(CodeWScope.class, new CodeWScopeSerializer(serializer)); serializer.addObjectSerializer(DBObject.class, new DBObjectSerializer(serializer)); serializer.addObjectSerializer(DBRefBase.class, new DBRefBaseSerializer(serializer)); serializer.addObjectSerializer(Iterable.class, new IterableSerializer(serializer)); serializer.addObjectSerializer(Map.class, new MapSerializer(serializer)); serializer.addObjectSerializer(MaxKey.class, new MaxKeySerializer(serializer)); serializer.addObjectSerializer(MinKey.class, new MinKeySerializer(serializer)); serializer.addObjectSerializer(Number.class, new ToStringSerializer()); serializer.addObjectSerializer(ObjectId.class, new ObjectIdSerializer(serializer)); serializer.addObjectSerializer(Pattern.class, new PatternSerializer(serializer)); serializer.addObjectSerializer(String.class, new StringSerializer()); serializer.addObjectSerializer(UUID.class, new UUIDSerializer(serializer)); return serializer; }
/** * @param pMin * an Object representing the minimum value of the Chunk range or * null if this is the lowest range * @param pMax * an Object representing the minimum value of the Chunk range or * null if this is the lowest range * @param pShard * the name of the Shard this Chunk belongs to */ public Chunk(Object pMin, Object pMax, String pShard) { DBObject min = (DBObject) pMin, max = (DBObject) pMax; _shardkey = max.toMap().keySet().toArray()[0].toString(); _shard = pShard; @SuppressWarnings("rawtypes") Iterator it = max.toMap().values().iterator(); _max = it.next(); if (_max instanceof MaxKey) { _max = null; } it = min.toMap().values().iterator(); _min = it.next(); if (_min instanceof MinKey) { _min = null; } }
protected static int mongoToKettleType( Object fieldValue ) { if ( fieldValue == null ) { return ValueMetaInterface.TYPE_STRING; } if ( fieldValue instanceof Symbol || fieldValue instanceof String || fieldValue instanceof Code || fieldValue instanceof ObjectId || fieldValue instanceof MinKey || fieldValue instanceof MaxKey ) { return ValueMetaInterface.TYPE_STRING; } else if ( fieldValue instanceof Date ) { return ValueMetaInterface.TYPE_DATE; } else if ( fieldValue instanceof Number ) { // try to parse as an Integer try { Integer.parseInt( fieldValue.toString() ); return ValueMetaInterface.TYPE_INTEGER; } catch ( NumberFormatException e ) { return ValueMetaInterface.TYPE_NUMBER; } } else if ( fieldValue instanceof Binary ) { return ValueMetaInterface.TYPE_BINARY; } else if ( fieldValue instanceof BSONTimestamp ) { return ValueMetaInterface.TYPE_INTEGER; } return ValueMetaInterface.TYPE_STRING; }
/** * convert bson types to java primitives. BasicBSONList, Binary, * BSONTimestamp, Code, CodeWScope, MinKey, MaxKey, Symbol, ObjectId */ private Object convert(Object o) { if (o instanceof BSONTimestamp) { return ((BSONTimestamp) o).getTime(); } else if (o instanceof Symbol || o instanceof Code || o instanceof CodeWScope || o instanceof MinKey || o instanceof MaxKey || o instanceof ObjectId) { return o.toString(); } else if (o instanceof BasicBSONList) { List<Object> l = new ArrayList<Object>(); for (Object item : ((BasicBSONList) o)) { l.add(convert(item)); } return l; } else { return o; } }
protected void checkValue(Object value) { if (value instanceof MObject) return; if (value == null) return; if (value instanceof Number) return; if (value instanceof String) return; if (value instanceof Boolean) return; if (value instanceof Character) return; if (value instanceof ObjectId) return; if (value instanceof Date) return; if (value instanceof Pattern) return; if (value instanceof UUID) return; if (value instanceof MaxKey || value instanceof MinKey) return; if (value instanceof byte[]) return; if (value instanceof BSONTimestamp || value instanceof Symbol || value instanceof Code || value instanceof CodeWScope) return; throw new IllegalArgumentException(getClass().getSimpleName() + " can't store a " + value.getClass().getName()); }
@Override public MaxKey decode(BsonValue bsonValue) { return new MaxKey(); }
@Override public BsonMaxKey encode(MaxKey object) { return new BsonMaxKey(); }
@Override public MaxKey decode(BsonReader bsonReader) { return new MaxKey(); }
@Override public void encode(BsonWriter bsonWriter, MaxKey value) { bsonWriter.writeMaxKey(); }
public void gotMaxKey( String name ){ cur().put( name , new MaxKey() ); }
protected void _putObjectField( String name , Object val ){ if ( name.equals( "_transientFields" ) ) return; if ( DEBUG ) System.out.println( "\t put thing : " + name ); if ( name.contains( "\0" ) ) throw new IllegalArgumentException( "Document field names can't have a NULL character. (Bad Key: '" + name + "')" ); if ( name.equals( "$where") && val instanceof String ){ _put( CODE , name ); _putValueString( val.toString() ); return; } val = BSON.applyEncodingHooks( val ); if ( val == null ) putNull(name); else if ( val instanceof Date ) putDate( name , (Date)val ); else if ( val instanceof Number ) putNumber(name, (Number)val ); else if ( val instanceof Character ) putString(name, val.toString() ); else if ( val instanceof String ) putString(name, val.toString() ); else if ( val instanceof ObjectId ) putObjectId(name, (ObjectId)val ); else if ( val instanceof BSONObject ) putObject(name, (BSONObject)val ); else if ( val instanceof Boolean ) putBoolean(name, (Boolean)val ); else if ( val instanceof Pattern ) putPattern(name, (Pattern)val ); else if ( val instanceof Map ) putMap( name , (Map)val ); else if ( val instanceof Iterable) putIterable( name , (Iterable)val ); else if ( val instanceof byte[] ) putBinary( name , (byte[])val ); else if ( val instanceof Binary ) putBinary( name , (Binary)val ); else if ( val instanceof UUID ) putUUID( name , (UUID)val ); else if ( val.getClass().isArray() ) putArray( name , val ); else if (val instanceof Symbol) { putSymbol(name, (Symbol) val); } else if (val instanceof BSONTimestamp) { putTimestamp( name , (BSONTimestamp)val ); } else if (val instanceof CodeWScope) { putCodeWScope( name , (CodeWScope)val ); } else if (val instanceof Code) { putCode( name , (Code)val ); } else if (val instanceof DBRefBase) { BSONObject temp = new BasicBSONObject(); temp.put("$ref", ((DBRefBase)val).getRef()); temp.put("$id", ((DBRefBase)val).getId()); putObject( name, temp ); } else if ( val instanceof MinKey ) putMinKey( name ); else if ( val instanceof MaxKey ) putMaxKey( name ); else if ( putSpecial( name , val ) ){ // no-op } else { throw new IllegalArgumentException( "can't serialize " + val.getClass() ); } }
/** * @deprecated This method is NOT a part of public API and will be dropped in 3.x versions. */ @Deprecated protected Object getElementValue( ElementRecord record ){ switch ( record.type ){ case BSON.EOO: case BSON.UNDEFINED: case BSON.NULL: return null; case BSON.MAXKEY: return new MaxKey(); case BSON.MINKEY: return new MinKey(); case BSON.BOOLEAN: return ( _input.get( record.valueOffset ) != 0 ); case BSON.NUMBER_INT: return _input.getInt( record.valueOffset ); case BSON.TIMESTAMP: int inc = _input.getInt( record.valueOffset ); int time = _input.getInt( record.valueOffset + 4 ); return new BSONTimestamp( time, inc ); case BSON.DATE: return new Date( _input.getLong( record.valueOffset ) ); case BSON.NUMBER_LONG: return _input.getLong( record.valueOffset ); case BSON.NUMBER: return Double.longBitsToDouble( _input.getLong( record.valueOffset ) ); case BSON.OID: return new ObjectId( _input.getIntBE( record.valueOffset ), _input.getIntBE( record.valueOffset + 4 ), _input.getIntBE( record.valueOffset + 8 ) ); case BSON.SYMBOL: return new Symbol( _input.getUTF8String( record.valueOffset ) ); case BSON.CODE: return new Code( _input.getUTF8String( record.valueOffset ) ); case BSON.STRING: return _input.getUTF8String( record.valueOffset ); case BSON.CODE_W_SCOPE: int strsize = _input.getInt( record.valueOffset + 4 ); String code = _input.getUTF8String( record.valueOffset + 4 ); BSONObject scope = (BSONObject) _callback.createObject( _input.array(), record.valueOffset + 4 + 4 + strsize ); return new CodeWScope( code, scope ); case BSON.REF: int csize = _input.getInt( record.valueOffset ); String ns = _input.getCString( record.valueOffset + 4 ); int oidOffset = record.valueOffset + csize + 4; ObjectId oid = new ObjectId( _input.getIntBE( oidOffset ), _input.getIntBE( oidOffset + 4 ), _input.getIntBE( oidOffset + 8 ) ); return _callback.createDBRef( ns, oid ); case BSON.OBJECT: return _callback.createObject( _input.array(), record.valueOffset ); case BSON.ARRAY: return _callback.createArray( _input.array(), record.valueOffset ); case BSON.BINARY: return readBinary( record.valueOffset ); case BSON.REGEX: int patternCStringSize = sizeCString( record.valueOffset ); String pattern = _input.getCString( record.valueOffset ); String flags = _input.getCString( record.valueOffset + patternCStringSize ); return Pattern.compile( pattern, BSON.regexFlags( flags ) ); default: throw new BSONException( "Invalid type " + record.type + " for field " + getElementFieldName( record.offset ) ); } }