org.bson.types.BasicBSONList Java Examples
The following examples show how to use
org.bson.types.BasicBSONList.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReplicaSetConfigTest.java From brooklyn-library with Apache License 2.0 | 6 votes |
@Test public void testFourthServerOfFourIsGivenVoteWhenAnotherServerIsRemoved() { BasicBSONObject config = makeSetWithNMembers(4); HostAndPort toRemove = votingMembersOfSet(config).iterator().next(); BasicBSONObject updated = ReplicaSetConfig.fromExistingConfig(config) .remove(toRemove) .build(); assertEquals(votingMembersOfSet(updated).size(), 3); assertTrue(nonVotingMembersOfSet(updated).isEmpty()); BasicBSONList newMembers = BasicBSONList.class.cast(updated.get("members")); for (Object object : newMembers) { BasicBSONObject member = BasicBSONObject.class.cast(object); HostAndPort memberHostAndPort = HostAndPort.fromString(member.getString("host")); assertNotEquals(memberHostAndPort, toRemove); } }
Example #2
Source File: BSONValueLookupTest.java From secure-data-service with Apache License 2.0 | 6 votes |
@Test public void testGetValues() { // root.body.profile.name.first = George BSONObject root = new BasicBSONObject(); BSONObject body = new BasicBSONObject(); BasicBSONList list = new BasicBSONList(); list.add("hello"); list.add("goodbye"); list.add("have a nice day"); body.put("body", list); root.put("root", body); String[] values = BSONUtilities.getValues(root, "root.body"); assertNotNull(values); assertEquals(values.length, 3); }
Example #3
Source File: MongoDbStore.java From incubator-retired-wave with Apache License 2.0 | 6 votes |
private DBObject capabilitiesToObject(RobotCapabilities capabilities) { if (capabilities == null) { return null; } BasicDBObject capabilitiesObj = new BasicDBObject(); for (Capability capability : capabilities.getCapabilitiesMap().values()) { BasicBSONList contexts = new BasicBSONList(); for (Context c : capability.getContexts()) { contexts.add(c.name()); } capabilitiesObj.put(capability.getEventType().name(), new BasicDBObject() .append(CAPABILITY_CONTEXTS_FIELD, contexts) .append(CAPABILITY_FILTER_FIELD, capability.getFilter())); } BasicDBObject object = new BasicDBObject() .append(CAPABILITIES_CAPABILITIES_FIELD, capabilitiesObj) .append(CAPABILITIES_HASH_FIELD, capabilities.getCapabilitiesHash()) .append(CAPABILITIES_VERSION_FIELD, capabilities.getProtocolVersion().name()); return object; }
Example #4
Source File: MongoDbStore.java From swellrt with Apache License 2.0 | 6 votes |
private DBObject capabilitiesToObject(RobotCapabilities capabilities) { if (capabilities == null) { return null; } BasicDBObject capabilitiesObj = new BasicDBObject(); for (Capability capability : capabilities.getCapabilitiesMap().values()) { BasicBSONList contexts = new BasicBSONList(); for (Context c : capability.getContexts()) { contexts.add(c.name()); } capabilitiesObj.put(capability.getEventType().name(), new BasicDBObject() .append(CAPABILITY_CONTEXTS_FIELD, contexts) .append(CAPABILITY_FILTER_FIELD, capability.getFilter())); } BasicDBObject object = new BasicDBObject() .append(CAPABILITIES_CAPABILITIES_FIELD, capabilitiesObj) .append(CAPABILITIES_HASH_FIELD, capabilities.getCapabilitiesHash()) .append(CAPABILITIES_VERSION_FIELD, capabilities.getProtocolVersion().name()); return object; }
Example #5
Source File: DefaultUserService.java From socialite with Apache License 2.0 | 6 votes |
@Override public List<User> getFriendsOfFriendsAgg(final User user) { // Get the user's friends. BasicBSONList friend_ids = getFriendIdsUsingAgg(user); if (friend_ids.size() == 0) { // The user is not following anyone, will not have any friends of friends. return new ArrayList<User>(); } // Get their friends' _ids.. BasicBSONList fof_ids = getFriendsOfUsersAgg(user, friend_ids); if (fof_ids.size() == 0) { // None of the friends were following anyone, no friends of friends. return new ArrayList<User>(); } // Get the actual users. List<User> fofs = new ArrayList<User>(); DBCursor cursor = this.users.find(new BasicDBObject(USER_ID_KEY, new BasicDBObject("$in", fof_ids))); while (cursor.hasNext()) { fofs.add(new User(cursor.next())); } return fofs; }
Example #6
Source File: SequoiaSQLParser.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
private void parserDBObject(BasicBSONObject ob,String akey, String aop,Object aval){ boolean isok=false; if (!(ob.keySet().isEmpty())) { for (String field : ob.keySet()) { if (akey.equals(field)){ Object val = ob.get(field); if (val instanceof BasicBSONObject) { ((BasicBSONObject) val).put(aop, aval); ob.put(field, (BasicBSONObject) val); isok=true; break; } else if (val instanceof BasicBSONList) { // newobj.put(field, ((BasicDBList)val).copy()); } } } } if (isok==false) { BasicBSONObject xo = new BasicBSONObject(); xo.put(aop, aval); ob.put(akey,xo); } }
Example #7
Source File: SequoiaData.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
public void setGrouyBy(BSONObject gb) { this.groupby=gb; this.type=true; if (gb instanceof BasicBSONList) { Object gb2=((BasicBSONList)gb).get(0); if (gb2 instanceof BSONObject) { for (String field :((BSONObject)gb2).keySet()) { Object val = ((BSONObject)gb2).get(field); setField(field,getObjectToType(val)); } } } }
Example #8
Source File: ReplicaSetConfigTest.java From brooklyn-library with Apache License 2.0 | 5 votes |
private Collection<HostAndPort> votingMembersOfSet(BasicBSONObject config) { BasicBSONList membersObject = BasicBSONList.class.cast(config.get("members")); List<BasicBSONObject> members = Lists.newArrayList(); for (Object object : membersObject) members.add(BasicBSONObject.class.cast(object)); return FluentIterable.from(members) .filter(IS_VOTING_MEMBER) .transform(new Function<BasicBSONObject, HostAndPort>() { @Override public HostAndPort apply(BasicBSONObject input) { return HostAndPort.fromString(input.getString("host")); } }) .toList(); }
Example #9
Source File: ReplicaSetConfigTest.java From brooklyn-library with Apache License 2.0 | 5 votes |
private Collection<HostAndPort> nonVotingMembersOfSet(BasicBSONObject config) { BasicBSONList membersObject = BasicBSONList.class.cast(config.get("members")); List<BasicBSONObject> members = Lists.newArrayList(); for (Object object : membersObject) members.add(BasicBSONObject.class.cast(object)); return FluentIterable .from(members) .filter(Predicates.not(IS_VOTING_MEMBER)) .transform(new Function<BasicBSONObject, HostAndPort>() { @Override public HostAndPort apply(BasicBSONObject input) { return HostAndPort.fromString(input.getString("host")); } }) .toList(); }
Example #10
Source File: ReplicaSetConfigTest.java From brooklyn-library with Apache License 2.0 | 5 votes |
@Test public void testCreateFromScratch() { BasicBSONObject config = ReplicaSetConfig.builder("rs") .member("host-a", 12345, 1) .member("host-b", 54321, 2) .build(); assertEquals(config.get("_id"), "rs"); assertEquals(config.getInt("version"), 1); assertTrue(config.get("members") instanceof BasicBSONList); BasicBSONList members = (BasicBSONList) config.get("members"); assertEquals(members.size(), 2); }
Example #11
Source File: ReplicaSetConfigTest.java From brooklyn-library with Apache License 2.0 | 5 votes |
@Test public void testCreateFromExistingConfig() { // Replica set of one member int version = 44; BasicBSONObject config = makeSetConfig("replica-set-name", version, makeSetMember(33, "example.com:7777")); // Use existing set to add two more members BasicBSONObject newConfig = ReplicaSetConfig.fromExistingConfig(config) .member("foo", 8888, 34) .member("bar", 9999, 35) .build(); assertEquals(newConfig.get("_id"), "replica-set-name"); assertEquals(newConfig.get("version"), version + 1); BasicBSONList members = (BasicBSONList) newConfig.get("members"); assertEquals(members.size(), 3); BSONObject original = (BSONObject) members.get(0); assertEquals(original.get("_id"), 33); assertEquals(original.get("host"), "example.com:7777"); BSONObject second = (BSONObject) members.get(1); assertEquals(second.get("_id"), 34); assertEquals(second.get("host"), "foo:8888"); BSONObject third = (BSONObject) members.get(2); assertEquals(third.get("_id"), 35); assertEquals(third.get("host"), "bar:9999"); }
Example #12
Source File: SequoiaData.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
public BasicBSONList getGrouyBys() { if (this.groupby instanceof BasicBSONList) { return (BasicBSONList)this.groupby; } else { return null; } }
Example #13
Source File: MongoQueryConverterTest.java From secure-data-service with Apache License 2.0 | 5 votes |
/** * Checks the conversion of NeutralQueries (containing ORed criteria) * into Mongo-appropriate Query objects. * * This test uses an example similar to: * * select * * from student * where economicDisadvantaged = true * and studentUniqueStateId = '000000054') * */ @Test public void testOrConvert() { NeutralQuery mainQuery = new NeutralQuery(); //not part of the or, so added to the main query mainQuery.addCriteria(new NeutralCriteria("economicDisadvantaged=true")); //construct a query representing all the criteria in 1 or branch NeutralQuery orQuery1 = new NeutralQuery(); //construct a query representing all the criteria in a second or branch NeutralQuery orQuery2 = new NeutralQuery(); orQuery2.addCriteria(new NeutralCriteria("studentUniqueStateId", "=", "000000054")); //add the or queries mainQuery.addOrQuery(orQuery1); mainQuery.addOrQuery(orQuery2); //the converter will convert the NeutralQuery into a mongo Query Object Query query = mongoQueryConverter.convert("student", mainQuery); assertNotNull("Should not be null", query); DBObject obj = query.getQueryObject(); assertNotNull("Should not be null", obj); assertNotNull("Should not be null", obj.get("$or")); assertTrue(((BasicBSONList) obj.get("$or")).size() == 1); }
Example #14
Source File: ReplicaSetConfig.java From brooklyn-library with Apache License 2.0 | 5 votes |
/** * Creates a configuration from an existing configuration. * <p/> * Automatically increments the replica set's version number. */ public static ReplicaSetConfig fromExistingConfig(BSONObject config) { checkNotNull(config); checkArgument(config.containsField("_id"), "_id missing from replica set config"); checkArgument(config.containsField("version"), "version missing from replica set config"); checkArgument(config.containsField("members"), "members missing from replica set config"); String name = (String) config.get("_id"); Integer version = (Integer) config.get("version"); BasicBSONList members = (BasicBSONList) config.get("members"); return new ReplicaSetConfig(name, members).version(++version); }
Example #15
Source File: DefaultUserService.java From socialite with Apache License 2.0 | 4 votes |
/** * Use the aggregation framework to get the _ids of all users who have any of the users in 'friends_list' as a * follower, excluding 'user'. * @param user The original user. * @param friend_ids The _ids of 'user's friends. * @return The _ids of 'user's friends of friends. */ private BasicBSONList getFriendsOfUsersAgg(User user, BasicBSONList friend_ids) { DBCollection coll; // Depending on the settings, we'll have to query different collections. String friend_id_key; // This is the key that will have the friend's _id. String fof_id_key; // This is the key that will have the friend of friend's _id. if(config.maintain_following_collection){ // If there is a following collection, get the users directly. coll = this.following; friend_id_key = EDGE_OWNER_KEY; fof_id_key = EDGE_PEER_KEY; } else { // Otherwise, get them from the follower collection. coll = this.followers; friend_id_key = EDGE_PEER_KEY; fof_id_key = EDGE_OWNER_KEY; } List<DBObject> fof_pipeline = new ArrayList<DBObject>(2); // Pipeline to get the friends of friends // [{$match: {'$friend_id_key': {$in: <list of friends>}, '$fof_id_key': {$ne: <user's id>}}}, // {$group: {_id: null, followees: {$addToSet: '$fof_id_key'}}] // All users which any friend is following. fof_pipeline.add(new BasicDBObject("$match", new BasicDBObject( friend_id_key, new BasicDBObject("$in", friend_ids.toArray()) ).append(fof_id_key, new BasicDBObject("$ne", user.getUserId())))); // Combine all _ids into a set. fof_pipeline.add(new BasicDBObject("$group", new BasicDBObject("_id", null) .append("fofs", new BasicDBObject("$addToSet", "$" + fof_id_key)))); AggregationOutput output = coll.aggregate(fof_pipeline); if (!output.results().iterator().hasNext()) { return new BasicBSONList(); } // Should only be one result, the list of fofs. BasicBSONList fof_ids = (BasicBSONList)output.results().iterator().next().get("fofs"); assert(!output.results().iterator().hasNext()); return fof_ids; }
Example #16
Source File: MongoQueryTest.java From usergrid with Apache License 2.0 | 4 votes |
@Test public void and() throws Exception { UUID appId = emf.lookupApplication( "test-organization/test-app" ); EntityManager em = emf.getEntityManager( appId ); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "name", "Kings of Leon" ); properties.put( "genre", "Southern Rock" ); properties.put( "founded", 2000 ); em.create( "testand", properties ); properties = new LinkedHashMap<String, Object>(); properties.put( "name", "Stone Temple Pilots" ); properties.put( "genre", "Rock" ); properties.put( "founded", 1986 ); em.create( "testand", properties ); properties = new LinkedHashMap<String, Object>(); properties.put( "name", "Journey" ); properties.put( "genre", "Classic Rock" ); properties.put( "founded", 1973 ); em.create( "testand", properties ); // See http://www.mongodb.org/display/DOCS/Java+Tutorial Mongo m = new Mongo( "localhost", 27017 ); DB db = m.getDB( "test-organization/test-app" ); db.authenticate( "test", "test".toCharArray() ); BasicBSONList list = new BasicBSONList(); list.add( new BasicDBObject( "founded", new BasicDBObject( "$gte", 2000 ) ) ); list.add( new BasicDBObject( "founded", new BasicDBObject( "$lte", 2005 ) ) ); BasicDBObject query = new BasicDBObject(); query.put( "$and", list ); DBCollection coll = db.getCollection( "testands" ); DBCursor cur = coll.find( query ); assertTrue( cur.hasNext() ); DBObject result = cur.next(); assertEquals( "Kings of Leon", result.get( "name" ) ); assertEquals( "Southern Rock", result.get( "genre" ) ); assertFalse( cur.hasNext() ); }
Example #17
Source File: MongoQueryTest.java From usergrid with Apache License 2.0 | 4 votes |
@Test public void or() throws Exception { UUID appId = emf.lookupApplication( "test-organization/test-app" ); EntityManager em = emf.getEntityManager( appId ); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "name", "Kings of Leon" ); properties.put( "genre", "Southern Rock" ); properties.put( "founded", 2000 ); em.create( "testor", properties ); properties = new LinkedHashMap<String, Object>(); properties.put( "name", "Stone Temple Pilots" ); properties.put( "genre", "Rock" ); properties.put( "founded", 1986 ); em.create( "testor", properties ); properties = new LinkedHashMap<String, Object>(); properties.put( "name", "Journey" ); properties.put( "genre", "Classic Rock" ); properties.put( "founded", 1973 ); em.create( "testor", properties ); // See http://www.mongodb.org/display/DOCS/Java+Tutorial Mongo m = new Mongo( "localhost", 27017 ); DB db = m.getDB( "test-organization/test-app" ); db.authenticate( "test", "test".toCharArray() ); BasicBSONList list = new BasicBSONList(); list.add( new BasicDBObject( "founded", new BasicDBObject( "$gte", 2000 ) ) ); list.add( new BasicDBObject( "founded", new BasicDBObject( "$lte", 1973 ) ) ); BasicDBObject query = new BasicDBObject(); query.put( "$or", list ); DBCollection coll = db.getCollection( "testors" ); DBCursor cur = coll.find( query ); assertTrue( cur.hasNext() ); DBObject result = cur.next(); assertEquals( "Journey", result.get( "name" ) ); assertEquals( "Classic Rock", result.get( "genre" ) ); result = cur.next(); assertEquals( "Kings of Leon", result.get( "name" ) ); assertEquals( "Southern Rock", result.get( "genre" ) ); assertFalse( cur.hasNext() ); }
Example #18
Source File: MongoQueryTest.java From usergrid with Apache License 2.0 | 4 votes |
@Test public void in() throws Exception { UUID appId = emf.lookupApplication( "test-organization/test-app" ); EntityManager em = emf.getEntityManager( appId ); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "name", "Kings of Leon" ); properties.put( "genre", "Southern Rock" ); properties.put( "founded", 2000 ); em.create( "testin", properties ); properties = new LinkedHashMap<String, Object>(); properties.put( "name", "Stone Temple Pilots" ); properties.put( "genre", "Rock" ); properties.put( "founded", 1986 ); em.create( "testin", properties ); properties = new LinkedHashMap<String, Object>(); properties.put( "name", "Journey" ); properties.put( "genre", "Classic Rock" ); properties.put( "founded", 1973 ); em.create( "testin", properties ); // See http://www.mongodb.org/display/DOCS/Java+Tutorial Mongo m = new Mongo( "localhost", 27017 ); DB db = m.getDB( "test-organization/test-app" ); db.authenticate( "test", "test".toCharArray() ); BasicBSONList list = new BasicBSONList(); list.add( "Stone Temple Pilots" ); list.add( "Journey" ); BasicDBObject query = new BasicDBObject(); query.put( "name", new BasicDBObject( "$in", list ) ); DBCollection coll = db.getCollection( "testins" ); DBCursor cur = coll.find( query ); assertTrue( cur.hasNext() ); DBObject result = cur.next(); assertEquals( "Journey", result.get( "name" ) ); assertEquals( "Classic Rock", result.get( "genre" ) ); result = cur.next(); assertEquals( "Stone Temple Pilots", result.get( "name" ) ); assertEquals( "Rock", result.get( "genre" ) ); assertFalse( cur.hasNext() ); }
Example #19
Source File: MongoQueryParser.java From usergrid with Apache License 2.0 | 4 votes |
/** Handle an operand */ private static Operand handleOperand( String sourceField, BSONObject exp ) { Operand current = null; Object value = null; for ( String field : exp.keySet() ) { if ( field.startsWith( "$" ) ) { if ( "$gt".equals( field ) ) { value = exp.get( field ); GreaterThan gt = new GreaterThan(); gt.setProperty( sourceField ); gt.setLiteral( value ); current = gt; } else if ( "$gte".equals( field ) ) { value = exp.get( field ); GreaterThanEqual gte = new GreaterThanEqual(); gte.setProperty( sourceField ); gte.setLiteral( exp.get( field ) ); current = gte; // http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%3C%2C%3C%3D%2C%3E%2C%3E%3D // greater than equals // { "field" : { $gte: value } } } else if ( "$lt".equals( field ) ) { value = exp.get( field ); LessThan lt = new LessThan(); lt.setProperty( sourceField ); lt.setLiteral( value ); current = lt; } else if ( "$lte".equals( field ) ) { value = exp.get( field ); LessThanEqual lte = new LessThanEqual(); lte.setProperty( sourceField ); lte.setLiteral( value ); current = lte; } else if ( "$in".equals( field ) ) { value = exp.get( field ); BasicBSONList values = ( BasicBSONList ) value; int size = values.size(); Stack<Operand> expressions = new Stack<Operand>(); for (Object value1 : values) { Equal equal = new Equal(); equal.setProperty(sourceField); equal.setLiteral(value1); expressions.push(equal); } // we need to build a tree of expressions while ( expressions.size() > 1 ) { OrOperand or = new OrOperand(); or.addChild( expressions.pop() ); or.addChild( expressions.pop() ); expressions.push( or ); } current = expressions.pop(); } } } return current; }
Example #20
Source File: ReplicaSetConfig.java From brooklyn-library with Apache License 2.0 | 4 votes |
public ReplicaSetConfig(String name) { this(name, new BasicBSONList()); }
Example #21
Source File: DefaultUserService.java From socialite with Apache License 2.0 | 4 votes |
/** * Use the aggregation framework to get a list of all the _ids of users who 'user' is following. * @param user Any user. * @return The _ids of all users who 'user' is following. */ private BasicBSONList getFriendIdsUsingAgg(User user) { DBCollection coll; // Depending on the settings, we'll have to query different collections. String user_id_key; // This is the key that will have the friend's _id. String friend_id_key; // This is the key that will have the friend of friend's _id. if(config.maintain_following_collection){ // If there is a following collection, get the users directly. coll = this.following; user_id_key = EDGE_OWNER_KEY; friend_id_key = EDGE_PEER_KEY; } else { // Otherwise, get them from the follower collection. coll = this.followers; user_id_key = EDGE_PEER_KEY; friend_id_key = EDGE_OWNER_KEY; } List<DBObject> friends_pipeline = new ArrayList<DBObject>(2); // Pipeline to get the list of friends: // [{$match: {user_id_key: user_id}}, // {$group: {_id: null, followees: {$push: '$friend_id_key'}}] // Get all the users the given user is following. friends_pipeline.add(new BasicDBObject("$match", new BasicDBObject(user_id_key, user.getUserId()))); // Add them all to a set. friends_pipeline.add(new BasicDBObject("$group", new BasicDBObject("_id", null) .append("followees", new BasicDBObject("$addToSet", "$" + friend_id_key)))); AggregationOutput output = coll.aggregate(friends_pipeline); if (!output.results().iterator().hasNext()) { return new BasicBSONList(); } // There should only be one result, the list of friends. DBObject friends = output.results().iterator().next(); BasicBSONList friends_list = (BasicBSONList) friends.get("followees"); assert(!output.results().iterator().hasNext()); return friends_list; }
Example #22
Source File: ReplicaSetConfig.java From brooklyn-library with Apache License 2.0 | 4 votes |
public ReplicaSetConfig(String name, BasicBSONList existingMembers) { this.name = name; this.members = existingMembers; this.version = 1; }
Example #23
Source File: ReplicaSetConfigTest.java From brooklyn-library with Apache License 2.0 | 4 votes |
private BasicBSONObject makeSetConfig(String id, Integer version, BasicBSONObject... members) { BasicBSONList memberList = new BasicBSONList(); memberList.addAll(Arrays.asList(members)); return new BasicBSONObject(ImmutableMap.of("_id", id, "version", version, "members", memberList)); }