package org.gbif.checklistbank.cli.importer;

import org.gbif.api.model.checklistbank.NameUsage;
import org.gbif.api.model.checklistbank.NameUsageMetrics;
import org.gbif.api.model.checklistbank.VerbatimNameUsage;
import org.gbif.checklistbank.index.NameUsageIndexService;
import org.gbif.checklistbank.model.UsageExtensions;
import org.gbif.checklistbank.service.DatasetImportService;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.annotation.Nullable;

import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A thin wrapper that delegates all import methods to both postgres and solr.
 * Solr index updates are handled asynchroneously in a separate thread but if the queue gets too large
 * it will block the current thread until the queue is small enough again.
 */
public class DatasetImportServiceCombined {
    private static final Logger LOG = LoggerFactory.getLogger(DatasetImportServiceCombined.class);

    private final DatasetImportService sqlService;
    private final NameUsageIndexService solrService;

    public DatasetImportServiceCombined(DatasetImportService sqlService, NameUsageIndexService solrService) {
        this.sqlService = sqlService;
        this.solrService = solrService;
    }

    public int syncUsage(boolean insert, NameUsage usage, List<Integer> parents, @Nullable VerbatimNameUsage verbatim, NameUsageMetrics metrics, @Nullable UsageExtensions extensions) {
        Preconditions.checkNotNull(usage.getDatasetKey(), "datasetKey must exist");
        int key = sqlService.syncUsage(insert, usage, verbatim, metrics, extensions);
        solrService.insertOrUpdate(usage, parents, extensions);
        return key;
    }

    public void updateForeignKeys(int usageKey, Integer parentKey, Integer basionymKey) {
        sqlService.updateForeignKeys(usageKey, parentKey, basionymKey);
        solrService.insertOrUpdate(usageKey);
    }

    public int deleteOldUsages(UUID datasetKey, Date before) {
        LOG.debug("Deleting all usages in dataset {} before {}", datasetKey, before);
        // iterate over all ids to be deleted and remove them from solr first
        int counter = 0;
        for (Integer id : sqlService.listOldUsages(datasetKey, before)) {
            solrService.delete(id);
            sqlService.delete(id);
            counter++;
        }
        LOG.info("Deleted {} usages from dataset {} before {}", counter, datasetKey, before);
        return counter;
    }

}

@@ -449,16 +449,100 @@
 l.UUID;%0A
+import java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0A
 import j
@@ -756,186 +756,69 @@
 solr
-.%0A * Solr index updates are handled asynchroneously in a separate thread but if the queue gets too large%0A * it will block the current thread until the queue is small enough again
+ using independent threads to parallelize the updates to both
 .%0A *
@@ -981,29 +981,30 @@
 e final 
-DatasetImport
+NameUsageIndex
 Service 
@@ -1000,26 +1000,27 @@
 dexService s
-ql
+olr
 Service;%0A   
@@ -1024,36 +1024,107 @@
     
-private final NameUsageIndex
+ExecutorService solrExecutor = Executors.newSingleThreadExecutor();%0A    private final DatasetImport
 Serv
@@ -1120,35 +1120,34 @@
 tImportService s
-olr
+ql
 Service;%0A%0A    pu
@@ -1698,39 +1698,47 @@
         solr
-Service.insertO
+Executor.submit(new Sol
 rUpdate(usag
@@ -1752,32 +1752,33 @@
 nts, extensions)
+)
 ;%0A        return
@@ -1968,30 +1968,42 @@
 solr
-Service.insertO
+Executor.submit(new Sol
 rUpdate
+ById
 (usa
@@ -2004,24 +2004,25 @@
 Id(usageKey)
+)
 ;%0A    %7D%0A%0A   
@@ -2357,19 +2357,18 @@
        s
-olr
+ql
 Service.
@@ -2388,35 +2388,52 @@
            s
-qlService.d
+olrExecutor.submit(new SolrD
 elete(id);%0A 
@@ -2421,32 +2421,33 @@
 w SolrDelete(id)
+)
 ;%0A            co
@@ -2590,10 +2590,1043 @@
 %0A    %7D%0A%0A
+    class SolrUpdate implements Runnable %7B%0A        private final NameUsage usage;%0A        private final List%3CInteger%3E parents;%0A        private final UsageExtensions extensions;%0A%0A        public SolrUpdate(NameUsage usage, List%3CInteger%3E parents, UsageExtensions extensions) %7B%0A            this.usage = usage;%0A            this.parents = parents;%0A            this.extensions = extensions;%0A        %7D%0A%0A        @Override%0A        public void run() %7B%0A            solrService.insertOrUpdate(usage, parents, extensions);%0A        %7D%0A    %7D%0A%0A    class SolrUpdateById implements Runnable %7B%0A        private final int id;%0A%0A        public SolrUpdateById(int id) %7B%0A            this.id = id;%0A        %7D%0A%0A        @Override%0A        public void run() %7B%0A            solrService.insertOrUpdate(id);%0A        %7D%0A    %7D%0A%0A    class SolrDelete implements Runnable %7B%0A        private final int id;%0A%0A        public SolrDelete(int id) %7B%0A            this.id = id;%0A        %7D%0A%0A        @Override%0A        public void run() %7B%0A            solrService.delete(id);%0A        %7D%0A    %7D%0A
 %7D%0A

Use a single threaded executor for solr syncs during import