private static TagSet parseArg(ObjectNode arg) { List<Tag> tags = new ArrayList<>(); for (Iterator<Map.Entry<String, JsonNode>> itr = arg.fields(); itr.hasNext();) { Map.Entry<String, JsonNode> entry = itr.next(); tags.add(new Tag(entry.getKey(), entry.getValue().asText())); } return new TagSet(tags); }
ReadPreferenceParser(ConnectionString connectionString, JsonObject config) { ReadPreference connStringReadPreference = connectionString != null ? connectionString.getReadPreference() : null; if (connStringReadPreference != null) { // Prefer connection string's read preference readPreference = connStringReadPreference; } else { ReadPreference rp; String rps = config.getString("readPreference"); if (rps != null) { JsonArray readPreferenceTags = config.getJsonArray("readPreferenceTags"); if (readPreferenceTags == null) { rp = ReadPreference.valueOf(rps); if (rp == null) throw new IllegalArgumentException("Invalid ReadPreference " + rps); } else { // Support advanced ReadPreference Tags List<TagSet> tagSet = new ArrayList<>(); readPreferenceTags.forEach(o -> { String tagString = (String) o; List<Tag> tags = Stream.of(tagString.trim().split(",")) .map(s -> s.split(":")) .filter(array -> { if (array.length != 2) { throw new IllegalArgumentException("Invalid readPreferenceTags value '" + tagString + "'"); } return true; }).map(array -> new Tag(array[0], array[1])).collect(Collectors.toList()); tagSet.add(new TagSet(tags)); }); rp = ReadPreference.valueOf(rps, tagSet); } } else { rp = null; } readPreference = rp; } }
private static TagSet toTags( DBObject tagsDocument ) { List<Tag> tagList = new ArrayList<Tag>( ); for ( String key : tagsDocument.keySet( ) ) { tagList.add( new Tag( key, tagsDocument.get( key ).toString( ) ) ); } return new TagSet( tagList ); }
@Test public void testNearestArgs() { TaggableReadPreference pref = (TaggableReadPreference) MongoReadPreference.parse("nearest ( {\"x\":1} )"); Assert.assertTrue(pref.equals(ReadPreference.nearest(new TagSet(Arrays.asList(new Tag("x", "1")))))); }
@Test public void testNearestArgs2() { TaggableReadPreference pref = (TaggableReadPreference) MongoReadPreference.parse("nearest ( [ {\"x\":1}, {\"y\":\"a\"}] )"); Assert.assertTrue(pref.equals(ReadPreference.nearest(Arrays.asList(new TagSet(Arrays.asList(new Tag("x", "1"))), new TagSet(Arrays.asList(new Tag("y", "a"))))))); }
@Test public void testNearestArgs3() { TaggableReadPreference pref = (TaggableReadPreference) MongoReadPreference.parse("nearest([ {\"x\":1}, {\"y\":\"a\"}])"); Assert.assertTrue(pref.equals(ReadPreference.nearest(Arrays.asList(new TagSet(Arrays.asList(new Tag("x", "1"))), new TagSet(Arrays.asList(new Tag("y", "a"))))))); }