@Override
    public List<GrokPattern> saveAll(Collection<GrokPattern> patterns, ImportStrategy importStrategy) throws ValidationException {

        final Map<String, GrokPattern> newPatternsByName;
        try {
            newPatternsByName = patterns.stream().collect(Collectors.toMap(GrokPattern::name, Function.identity()));
        } catch (IllegalStateException e) {
            throw new ValidationException("The supplied Grok patterns contain conflicting names: " + e.getLocalizedMessage());
        }

        final Map<String, GrokPattern> existingPatternsByName =
                loadAll().stream().collect(Collectors.toMap(GrokPattern::name, Function.identity()));

        if (importStrategy == ABORT_ON_CONFLICT) {
            final Sets.SetView<String> conflictingNames
                    = Sets.intersection(newPatternsByName.keySet(), existingPatternsByName.keySet());
            if (!conflictingNames.isEmpty()) {
                final Iterable<String> limited = Iterables.limit(conflictingNames, MAX_DISPLAYED_CONFLICTS);
                throw new ValidationException("The following Grok patterns already exist: "
                        + StringUtils.join(limited, ", ")
                        + (conflictingNames.size() > MAX_DISPLAYED_CONFLICTS ? " (+ " + (conflictingNames.size() - MAX_DISPLAYED_CONFLICTS) + " more)" : "")
                        + ".");
            }
        }
        validateAllOrThrow(patterns, importStrategy);

        final List<GrokPattern> savedPatterns = patterns.stream()
                .map(newPattern -> {
                    final GrokPattern existingPattern = existingPatternsByName.get(newPattern.name());
                    if (existingPattern != null) {
                        return newPattern.toBuilder().id(existingPattern.id()).build();
                    } else {
                        return newPattern;
                    }
                })
                .map(dbCollection::save)
                .map(WriteResult::getSavedObject).collect(Collectors.toList());

        clusterBus.post(GrokPatternsUpdatedEvent.create(newPatternsByName.keySet()));

        return savedPatterns;
    }
@Test
    @MongoDBFixtures("MongoDbGrokPatternServiceTest.json")
    public void saveAllWithoutDropAllExisting() throws ValidationException {
        assertThat(collection.countDocuments()).isEqualTo(3);

        final List<GrokPattern> grokPatterns = ImmutableList.of(
                GrokPattern.create("Test4", "Pattern"),
                GrokPattern.create("56250da2d400000000000001", "Test", "Pattern", null)
        );
        final List<GrokPattern> savedGrokPatterns = service.saveAll(grokPatterns, ABORT_ON_CONFLICT);
        assertThat(savedGrokPatterns).hasSize(2);
        assertThat(collection.countDocuments()).isEqualTo(4);
    }