Search in sources :

Example 1 with FilterList

use of com.torodb.packaging.config.model.protocol.mongo.FilterList in project torodb by torodb.

the class FilterListSerializer method serializeFields.

private void serializeFields(FilterList value, JsonGenerator jgen) throws IOException {
    for (Map.Entry<String, Map<String, List<IndexFilter>>> databaseEntry : value.entrySet()) {
        jgen.writeArrayFieldStart(databaseEntry.getKey());
        for (Map.Entry<String, List<IndexFilter>> collection : databaseEntry.getValue().entrySet()) {
            if (collection.getValue().isEmpty()) {
                jgen.writeString(collection.getKey());
            } else {
                jgen.writeStartObject();
                jgen.writeArrayFieldStart(collection.getKey());
                for (IndexFilter indexFilter : collection.getValue()) {
                    jgen.writeObject(indexFilter);
                }
                jgen.writeEndArray();
                jgen.writeEndObject();
            }
        }
        jgen.writeEndArray();
    }
}
Also used : List(java.util.List) FilterList(com.torodb.packaging.config.model.protocol.mongo.FilterList) IndexFilter(com.torodb.packaging.config.model.protocol.mongo.FilterList.IndexFilter) Map(java.util.Map)

Example 2 with FilterList

use of com.torodb.packaging.config.model.protocol.mongo.FilterList in project torodb by torodb.

the class FilterListDeserializer method deserialize.

@Override
@SuppressFBWarnings("REC_CATCH_EXCEPTION")
public FilterList deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    FilterList filterList = new FilterList();
    JsonNode node = jp.getCodec().readTree(jp);
    if (node instanceof ObjectNode) {
        Iterator<Entry<String, JsonNode>> databaseEntriesIterator = node.fields();
        while (databaseEntriesIterator.hasNext()) {
            Entry<String, JsonNode> databaseEntry = databaseEntriesIterator.next();
            try {
                Map<String, List<IndexFilter>> collections = new HashMap<>();
                if (databaseEntry.getValue() instanceof ObjectNode) {
                    readCollectionObject(jp, (ObjectNode) databaseEntry.getValue(), collections);
                } else if (databaseEntry.getValue() instanceof ArrayNode) {
                    ArrayNode collectionsArray = (ArrayNode) databaseEntry.getValue();
                    Iterator<JsonNode> collectionsIterator = collectionsArray.elements();
                    int position = 0;
                    while (collectionsIterator.hasNext()) {
                        try {
                            JsonNode collection = collectionsIterator.next();
                            if (collection instanceof ObjectNode) {
                                readCollectionObject(jp, (ObjectNode) collection, collections);
                            } else if (collection instanceof ArrayNode) {
                                throw new JsonMappingException("wrong filter format: collection value inside " + "database array can not be an array", jp.getCurrentLocation());
                            } else {
                                collections.put(collection.asText(), new ArrayList<>());
                            }
                            position++;
                        } catch (Exception e) {
                            throw JsonMappingException.wrapWithPath(e, collections, position);
                        }
                    }
                }
                filterList.put(databaseEntry.getKey(), collections);
            } catch (Exception e) {
                throw JsonMappingException.wrapWithPath(e, filterList, databaseEntry.getKey());
            }
        }
    } else {
        throw new JsonMappingException("wrong filter format: filter list was not an object", jp.getCurrentLocation());
    }
    return filterList;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashMap(java.util.HashMap) FilterList(com.torodb.packaging.config.model.protocol.mongo.FilterList) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Entry(java.util.Map.Entry) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) FilterList(com.torodb.packaging.config.model.protocol.mongo.FilterList) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 3 with FilterList

use of com.torodb.packaging.config.model.protocol.mongo.FilterList in project torodb by torodb.

the class ReplicationFiltersFactory method convertFilterList.

@SuppressWarnings("checkstyle:LineLength")
private static ImmutableMap<Pattern, ImmutableMap<Pattern, ImmutableList<IndexPattern>>> convertFilterList(FilterList filterList) {
    ImmutableMap.Builder<Pattern, ImmutableMap<Pattern, ImmutableList<IndexPattern>>> filterBuilder = ImmutableMap.builder();
    if (filterList != null) {
        for (Map.Entry<String, Map<String, List<IndexFilter>>> databaseEntry : filterList.entrySet()) {
            ImmutableMap.Builder<Pattern, ImmutableList<IndexPattern>> collectionsBuilder = ImmutableMap.builder();
            for (Map.Entry<String, List<IndexFilter>> collection : databaseEntry.getValue().entrySet()) {
                ImmutableList.Builder<IndexPattern> indexesBuilder = ImmutableList.builder();
                for (IndexFilter indexFilter : collection.getValue()) {
                    Pattern indexNamePattern = ANY;
                    if (indexFilter.getName() != null) {
                        indexNamePattern = SimpleRegExpDecoder.decode(indexFilter.getName());
                    }
                    IndexPattern.Builder indexPatternBuilder = new IndexPattern.Builder(indexNamePattern, indexFilter.getUnique());
                    for (Map.Entry<String, String> indexFieldFilter : indexFilter.getKeys().entrySet()) {
                        ImmutableList.Builder<Pattern> fieldReferencePatternBuilder = ImmutableList.builder();
                        for (String indexFieldKeyFilter : KEYS_SPLITTER.split(indexFieldFilter.getKey())) {
                            fieldReferencePatternBuilder.add(SimpleRegExpDecoder.decode(indexFieldKeyFilter));
                        }
                        indexPatternBuilder.addFieldPattern(fieldReferencePatternBuilder.build(), SimpleRegExpDecoder.decode(FilterList.getIndexType(indexFieldFilter.getValue()).getName()));
                    }
                    indexesBuilder.add(indexPatternBuilder.build());
                }
                collectionsBuilder.put(SimpleRegExpDecoder.decode(collection.getKey()), indexesBuilder.build());
            }
            filterBuilder.put(SimpleRegExpDecoder.decode(databaseEntry.getKey()), collectionsBuilder.build());
        }
    }
    return filterBuilder.build();
}
Also used : IndexPattern(com.torodb.mongodb.repl.ReplicationFilters.IndexPattern) Pattern(java.util.regex.Pattern) IndexPattern(com.torodb.mongodb.repl.ReplicationFilters.IndexPattern) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) FilterList(com.torodb.packaging.config.model.protocol.mongo.FilterList) IndexFilter(com.torodb.packaging.config.model.protocol.mongo.FilterList.IndexFilter) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Aggregations

FilterList (com.torodb.packaging.config.model.protocol.mongo.FilterList)3 List (java.util.List)3 IndexFilter (com.torodb.packaging.config.model.protocol.mongo.FilterList.IndexFilter)2 Map (java.util.Map)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 IndexPattern (com.torodb.mongodb.repl.ReplicationFilters.IndexPattern)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Entry (java.util.Map.Entry)1 Pattern (java.util.regex.Pattern)1