Search in sources :

Example 1 with IndexMutation

use of com.thinkaurelius.titan.diskstorage.indexing.IndexMutation in project incubator-atlas by apache.

the class Solr5Index method mutate.

@Override
public void mutate(Map<String, Map<String, IndexMutation>> mutations, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
    logger.debug("Mutating SOLR");
    try {
        for (Map.Entry<String, Map<String, IndexMutation>> stores : mutations.entrySet()) {
            String collectionName = stores.getKey();
            String keyIdField = getKeyFieldId(collectionName);
            List<String> deleteIds = new ArrayList<>();
            Collection<SolrInputDocument> changes = new ArrayList<>();
            for (Map.Entry<String, IndexMutation> entry : stores.getValue().entrySet()) {
                String docId = entry.getKey();
                IndexMutation mutation = entry.getValue();
                Preconditions.checkArgument(!(mutation.isNew() && mutation.isDeleted()));
                Preconditions.checkArgument(!mutation.isNew() || !mutation.hasDeletions());
                Preconditions.checkArgument(!mutation.isDeleted() || !mutation.hasAdditions());
                //Handle any deletions
                if (mutation.hasDeletions()) {
                    if (mutation.isDeleted()) {
                        logger.trace("Deleting entire document {}", docId);
                        deleteIds.add(docId);
                    } else {
                        HashSet<IndexEntry> fieldDeletions = Sets.newHashSet(mutation.getDeletions());
                        if (mutation.hasAdditions()) {
                            for (IndexEntry indexEntry : mutation.getAdditions()) {
                                fieldDeletions.remove(indexEntry);
                            }
                        }
                        deleteIndividualFieldsFromIndex(collectionName, keyIdField, docId, fieldDeletions);
                    }
                }
                if (mutation.hasAdditions()) {
                    int ttl = mutation.determineTTL();
                    SolrInputDocument doc = new SolrInputDocument();
                    doc.setField(keyIdField, docId);
                    boolean isNewDoc = mutation.isNew();
                    if (isNewDoc)
                        logger.trace("Adding new document {}", docId);
                    for (IndexEntry e : mutation.getAdditions()) {
                        final Object fieldValue = convertValue(e.value);
                        doc.setField(e.field, isNewDoc ? fieldValue : new HashMap<String, Object>(1) {

                            {
                                put("set", fieldValue);
                            }
                        });
                    }
                    if (ttl > 0) {
                        Preconditions.checkArgument(isNewDoc, "Solr only supports TTL on new documents [%s]", docId);
                        doc.setField(ttlField, String.format("+%dSECONDS", ttl));
                    }
                    changes.add(doc);
                }
            }
            commitDeletes(collectionName, deleteIds);
            commitDocumentChanges(collectionName, changes);
        }
    } catch (Exception e) {
        throw storageException(e);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IndexEntry(com.thinkaurelius.titan.diskstorage.indexing.IndexEntry) TemporaryBackendException(com.thinkaurelius.titan.diskstorage.TemporaryBackendException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) PermanentBackendException(com.thinkaurelius.titan.diskstorage.PermanentBackendException) BackendException(com.thinkaurelius.titan.diskstorage.BackendException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) SolrInputDocument(org.apache.solr.common.SolrInputDocument) IndexMutation(com.thinkaurelius.titan.diskstorage.indexing.IndexMutation) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

BackendException (com.thinkaurelius.titan.diskstorage.BackendException)1 PermanentBackendException (com.thinkaurelius.titan.diskstorage.PermanentBackendException)1 TemporaryBackendException (com.thinkaurelius.titan.diskstorage.TemporaryBackendException)1 IndexEntry (com.thinkaurelius.titan.diskstorage.indexing.IndexEntry)1 IndexMutation (com.thinkaurelius.titan.diskstorage.indexing.IndexMutation)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1 KeeperException (org.apache.zookeeper.KeeperException)1