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; }
/** * 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()); }
protected void putCodeWScope( String name , CodeWScope code ){ _put( CODE_W_SCOPE , name ); int temp = _buf.getPosition(); _buf.writeInt( 0 ); _putValueString( code.getCode() ); putObject( code.getScope() ); _buf.writeInt( temp , _buf.getPosition() - temp ); }
@Override public void serialize(Object obj, StringBuilder buf) { CodeWScope c = (CodeWScope) obj; BasicDBObject temp = new BasicDBObject(); temp.put("$code", c.getCode()); temp.put("$scope", c.getScope()); serializer.serialize(temp, buf); }
/** * Checks if the Class given is a property type * * @param type the Class to examine * @return true if the Class's type is considered a property type */ public static boolean isPropertyType(final Class type) { return type != null && (isPrimitiveLike(type) || type == DBRef.class || type == Pattern.class || type == CodeWScope.class || type == ObjectId.class || type == Key.class || type == DBObject.class || type == BasicDBObject.class); }
@Test public void testWhereCodeWScopeQuery() { getDs().save(new PhotoWithKeywords(new Keyword("california"), new Keyword("nevada"), new Keyword("arizona"))); // CodeWScope hasKeyword = new CodeWScope("for (kw in this.keywords) { if(kw.keyword == kwd) return true; } return false; // ", new BasicDBObject("kwd","california")); final CodeWScope hasKeyword = new CodeWScope("this.keywords != null", new BasicDBObject()); assertNotNull(getDs().find(PhotoWithKeywords.class).where(hasKeyword).get()); }
public void gotCodeWScope( String name , String code , Object scope ){ _put( name , new CodeWScope( code, (BSONObject)scope ) ); }
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 ) ); } }
/** * Gets the type byte for a given object. * @param o the object * @return the byte value associated with the type, or -1 if no type is matched */ @SuppressWarnings("deprecation") public static byte getType( Object o ){ if ( o == null ) return NULL; if ( o instanceof DBPointer ) return REF; if (o instanceof Integer || o instanceof Short || o instanceof Byte || o instanceof AtomicInteger) { return NUMBER_INT; } if (o instanceof Long || o instanceof AtomicLong) { return NUMBER_LONG; } if ( o instanceof Number ) return NUMBER; if ( o instanceof String ) return STRING; if ( o instanceof java.util.List ) return ARRAY; if ( o instanceof byte[] ) return BINARY; if ( o instanceof ObjectId ) return OID; if ( o instanceof Boolean ) return BOOLEAN; if ( o instanceof java.util.Date ) return DATE; if ( o instanceof BSONTimestamp ) return TIMESTAMP; if ( o instanceof java.util.regex.Pattern ) return REGEX; if ( o instanceof DBObject || o instanceof DBRefBase ) return OBJECT; if ( o instanceof Code ) return CODE; if ( o instanceof CodeWScope ) return CODE_W_SCOPE; return -1; }
@Override public Query<T> where(final CodeWScope js) { add(new WhereCriteria(js)); return this; }
/** * Limit the query using this javascript block; only one per query * * @param js the javascript block to apply * @return this */ Query<T> where(CodeWScope js);
/** * Creates a WhereCriteria with the given javascript * * @param js the javascript */ public WhereCriteria(final CodeWScope js) { this.js = js; }