@Override public boolean containsField(final String key) { int i = _getInt(key, false); if (i < 0) { return false; } return i >= 0 && i < size(); }
@Override public Set<String> keySet() { return new StringRangeSet(size()); }
@Override public Object removeField(final String key) { int i = _getInt(key); if (i < 0) { return null; } if (i >= size()) { return null; } return remove(i); }
/** * Puts a value at an index. This will fill any unset indexes less than {@code index} with {@code null}. * * @param key the index at which to insert the value * @param value the value to insert * @return the value */ public Object put(final int key, final Object value) { while (key >= size()) { add(null); } set(key, value); return value; }
/** * Gets a value at an index. For interface compatibility. Must be passed a String that is parsable to an int. * * @param key the index * @return the value, if found, or null * @throws IllegalArgumentException if {@code key} cannot be parsed into an {@code int} */ public Object get(final String key) { int i = _getInt(key); if (i < 0) { return null; } if (i >= size()) { return null; } return get(i); }
/** * Returns this object's fields' names * * @return The names of the fields in this object */ public Set<String> keySet() { return new StringRangeSet(size()); }
/** * Checks if this object contains a given field * * @param key * field name * @return if the field exists */ public boolean containsField(String key) { int i = _getInt(key, false); if (i < 0) return false; return i >= 0 && i < size(); }
/** * Deletes a field from this object. * * @param key * the field name to remove * @return the object removed */ public Object removeField(String key) { int i = _getInt(key); if (i < 0) return null; if (i >= size()) return null; return remove(i); }
/** * Puts a value at an index. This will fill any unset indexes less than * <code>index</code> with <code>null</code>. * * @param key * the index at which to insert the value * @param v * the value to insert * @return the value */ public Object put(int key, Object v) { while (key >= size()) add(null); set(key, v); return v; }
@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; }
/** * Gets a value at an index. For interface compatibility. Must be passed a * String that is parsable to an int. * * @param key * the index * @return the value, if found, or null * @throws IllegalArgumentException * if <code>key</code> cannot be parsed into an <code>int</code> */ public Object get(String key) { int i = _getInt(key); if (i < 0) return null; if (i >= size()) return null; return get(i); }
private int getNodePort(BSONObject node) { if (node == null) { throw new BaseException(SDBError.SDB_SYS, "invalid information of node"); } Object services = node.get(SdbConstants.FIELD_NAME_GROUPSERVICE); if (services == null) { throw new BaseException(SDBError.SDB_SYS, node.toString()); } BasicBSONList serviceInfos = (BasicBSONList) services; if (serviceInfos.size() == 0) { throw new BaseException(SDBError.SDB_SYS, node.toString()); } int port = -1; for (Object obj : serviceInfos) { BSONObject service = (BSONObject) obj; if (service.get(SdbConstants.FIELD_NAME_SERVICETYPE) .toString().equals("0")) { port = Integer.parseInt(service.get( SdbConstants.FIELD_NAME_SERVICENAME).toString()); break; } } if (port == -1) { throw new BaseException(SDBError.SDB_SYS, node.toString()); } return port; } }
/** * Get the amount of the nodes with the specified status. * * @param status Node.NodeStatus * @return the amount of the nodes with the specified status * @throws BaseException If error happens. * @deprecated The status of node are invalid, never use this api again. */ public int getNodeNum(Node.NodeStatus status) throws BaseException { BSONObject group = sequoiadb.getDetailById(id); try { Object obj = group.get(SdbConstants.FIELD_NAME_GROUP); if (obj == null) { return 0; } BasicBSONList list = (BasicBSONList) obj; return list.size(); } catch (BaseException e) { throw e; } catch (Exception e) { throw new BaseException(SDBError.SDB_SYS, e); } }
if (nodesInfoList.size() == 0) { return null;
@Test public void testRemoveNonExistentMemberHasNoEffect() { BasicBSONObject config = makeSetConfig("replica-set-name", 1, makeSetMember(33, "example.com:7777"), makeSetMember(34, "example.com:7778")); BasicBSONList members = (BasicBSONList) config.get("members"); assertEquals(members.size(), 2); BasicBSONObject altered = ReplicaSetConfig.fromExistingConfig(config) .remove("foo", 99) .build(); members = (BasicBSONList) altered.get("members"); assertEquals(members.size(), 2); }
@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); }
@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"); }
@Test public void testRemoveMember() { int version = 44; BasicBSONObject config = makeSetConfig("replica-set-name", version, makeSetMember(33, "example.com:7777"), makeSetMember(34, "example.com:7778")); // Use existing set to add two more members BasicBSONObject newConfig = ReplicaSetConfig.fromExistingConfig(config) .remove("example.com", 7777) .build(); assertEquals(newConfig.get("version"), version + 1); BasicBSONList members = (BasicBSONList) newConfig.get("members"); assertEquals(members.size(), 1); assertEquals(BSONObject.class.cast(members.get(0)).get("host"), "example.com:7778"); newConfig = ReplicaSetConfig.fromExistingConfig(newConfig) .remove("example.com", 7778) .build(); members = (BasicBSONList) newConfig.get("members"); assertTrue(members.isEmpty()); }