/** * Factory method for creating a new BSON List. * * @return a new BasicBSONList. */ protected BSONObject createList() { return new BasicBSONList(); }
/** * 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); }
/** * Puts a value at an index. For interface compatibility. Must be passed a String that is parsable to an int. * * @param key the index at which to insert the value * @param v the value to insert * @return the value * @throws IllegalArgumentException if {@code key} cannot be parsed into an {@code int} */ @Override public Object put(final String key, final Object v) { return put(_getInt(key), v); }
public BSONObject getBSONObject() { BSONObject obj = new BasicBSONObject(); BasicBSONList list = new BasicBSONList(); list.add(RESPONSE_TYPE); list.add(this.requestId); list.add(this.error); list.add(this.result); obj.put("data", list); return obj; } }
public BSONObject getBSONObject() { BSONObject obj = new BasicBSONObject(); BasicBSONList list = new BasicBSONList(); list.add(REQUEST_TYPE); list.add(this.requestId); list.add(this.methodName); BasicBSONList paramList = new BasicBSONList(); paramList.addAll(Arrays.asList(parameters)); list.add(paramList); obj.put("data", list); obj.put("return", this.responseQueue); return obj; }
if (nodesInfoList.isEmpty()) { throw new BaseException(SDBError.SDB_CLS_EMPTY_GROUP); for (int i = 0; i < nodesInfoList.size(); i++) { BSONObject nodeInfo = (BSONObject) nodesInfoList.get(i); Object nodeIdValue = nodeInfo.get(SdbConstants.FIELD_NAME_NODEID); if (nodeIdValue == null) { int nodeCount = nodesInfoList.size(); if (needGeneratePosition) { for (int i = 0; i < nodeCount; i++) { nodeInfoObj = (BSONObject) nodesInfoList.get(0); } else if (validPositions.size() == 1) { nodeInfoObj = (BSONObject) nodesInfoList.get(nodeIndex); } else { int position = 0; nodeInfoObj = (BSONObject) nodesInfoList.get(nodeIndex);
new BasicDBObject( friend_id_key, new BasicDBObject("$in", friend_ids.toArray()) ).append(fof_id_key, new BasicDBObject("$ne", user.getUserId())))); return new BasicBSONList();
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)); }
@Override public Set<String> keySet() { return new StringRangeSet(size()); }
public RPCRequest(BSONObject obj) { BasicBSONList list = (BasicBSONList) obj.get("data"); this.requestId = (Integer) list.get(1); this.methodName = list.get(2).toString(); this.parameters = ((BasicBSONList) list.get(3)).toArray(); if (obj.containsField("return")) { this.responseQueue = obj.get("return").toString(); } }
/** * Get all the BSON values. * @return All the BSON values by "java.util#Collection<Object>". * @throws Exception IllegalArgumentException when the type of BSON value is not supported. */ public Object asList() { Collection<Object> colletion = new LinkedList<Object>(); for (String key : this.keySet()) { Object v = this.get(key); if (v == null) { continue; } else if (BSON.IsBasicType(v)) { colletion.add(v); } else if (v instanceof BasicBSONList) { colletion.add(((BasicBSONList) v).asList()); } else if (v instanceof BasicBSONObject) { colletion.add(((BasicBSONObject) v).asMap()); } else { throw new IllegalArgumentException( "can't support in list. value_type=" + v.getClass()); } } return colletion; } }
@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"); }
/** * Adds a new member to the replica set config with the given hostname, port and id. Doesn't attempt to check * that the id is free. */ public ReplicaSetConfig member(String hostname, Integer port, Integer id) { BasicBSONObject member = new BasicBSONObject(); member.put("_id", id); member.put("host", String.format("%s:%s", hostname, port)); members.add(member); return this; }
/** * Map a DBObject for indexing * * @param dbObj */ private Map<String, Object> createObjectMap(DBObject dbObj) { Map<String, Object> mapData = new HashMap<String, Object>(); for (String key : dbObj.keySet()) { Object value = dbObj.get(key); if (value instanceof DBRef) { mapData.put(key, this.convertDbRef((DBRef) value)); } else if (value instanceof BasicDBList) { mapData.put(key, ((BasicBSONList) value).toArray()); } else if (value instanceof BasicDBObject) { mapData.put(key, this.createObjectMap((DBObject) value)); } else { mapData.put(key, value); } } return mapData; }
int _getInt(final String s) { return _getInt(s, true); }