Search in sources :

Example 6 with IndexEntry

use of com.thinkaurelius.titan.diskstorage.indexing.IndexEntry 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)

Example 7 with IndexEntry

use of com.thinkaurelius.titan.diskstorage.indexing.IndexEntry in project titan by thinkaurelius.

the class IndexRepairJob method process.

@Override
public void process(TitanVertex vertex, ScanMetrics metrics) {
    try {
        BackendTransaction mutator = writeTx.getTxHandle();
        if (index instanceof RelationTypeIndex) {
            RelationTypeIndexWrapper wrapper = (RelationTypeIndexWrapper) index;
            InternalRelationType wrappedType = wrapper.getWrappedType();
            EdgeSerializer edgeSerializer = writeTx.getEdgeSerializer();
            List<Entry> additions = new ArrayList<>();
            for (TitanRelation relation : vertex.query().types(indexRelationTypeName).direction(Direction.OUT).relations()) {
                InternalRelation titanRelation = (InternalRelation) relation;
                for (int pos = 0; pos < titanRelation.getArity(); pos++) {
                    if (!wrappedType.isUnidirected(Direction.BOTH) && !wrappedType.isUnidirected(EdgeDirection.fromPosition(pos)))
                        //Directionality is not covered
                        continue;
                    Entry entry = edgeSerializer.writeRelation(titanRelation, wrappedType, pos, writeTx);
                    additions.add(entry);
                }
            }
            StaticBuffer vertexKey = writeTx.getIdInspector().getKey(vertex.longId());
            mutator.mutateEdges(vertexKey, additions, KCVSCache.NO_DELETIONS);
            metrics.incrementCustom(ADDED_RECORDS_COUNT, additions.size());
        } else if (index instanceof TitanGraphIndex) {
            IndexType indexType = mgmt.getSchemaVertex(index).asIndexType();
            assert indexType != null;
            IndexSerializer indexSerializer = graph.getIndexSerializer();
            //Gather elements to index
            List<TitanElement> elements;
            switch(indexType.getElement()) {
                case VERTEX:
                    elements = ImmutableList.of(vertex);
                    break;
                case PROPERTY:
                    elements = Lists.newArrayList();
                    for (TitanVertexProperty p : addIndexSchemaConstraint(vertex.query(), indexType).properties()) {
                        elements.add(p);
                    }
                    break;
                case EDGE:
                    elements = Lists.newArrayList();
                    for (TitanEdge e : addIndexSchemaConstraint(vertex.query().direction(Direction.OUT), indexType).edges()) {
                        elements.add(e);
                    }
                    break;
                default:
                    throw new AssertionError("Unexpected category: " + indexType.getElement());
            }
            if (indexType.isCompositeIndex()) {
                for (TitanElement element : elements) {
                    Set<IndexSerializer.IndexUpdate<StaticBuffer, Entry>> updates = indexSerializer.reindexElement(element, (CompositeIndexType) indexType);
                    for (IndexSerializer.IndexUpdate<StaticBuffer, Entry> update : updates) {
                        log.debug("Mutating index {}: {}", indexType, update.getEntry());
                        mutator.mutateIndex(update.getKey(), Lists.newArrayList(update.getEntry()), KCVSCache.NO_DELETIONS);
                        metrics.incrementCustom(ADDED_RECORDS_COUNT);
                    }
                }
            } else {
                assert indexType.isMixedIndex();
                Map<String, Map<String, List<IndexEntry>>> documentsPerStore = new HashMap<>();
                for (TitanElement element : elements) {
                    indexSerializer.reindexElement(element, (MixedIndexType) indexType, documentsPerStore);
                    metrics.incrementCustom(DOCUMENT_UPDATES_COUNT);
                }
                mutator.getIndexTransaction(indexType.getBackingIndexName()).restore(documentsPerStore);
            }
        } else
            throw new UnsupportedOperationException("Unsupported index found: " + index);
    } catch (final Exception e) {
        mgmt.rollback();
        writeTx.rollback();
        metrics.incrementCustom(FAILED_TX);
        throw new TitanException(e.getMessage(), e);
    }
}
Also used : InternalRelation(com.thinkaurelius.titan.graphdb.internal.InternalRelation) Entry(com.thinkaurelius.titan.diskstorage.Entry) IndexEntry(com.thinkaurelius.titan.diskstorage.indexing.IndexEntry) EdgeSerializer(com.thinkaurelius.titan.graphdb.database.EdgeSerializer) RelationTypeIndexWrapper(com.thinkaurelius.titan.graphdb.database.management.RelationTypeIndexWrapper) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) ImmutableList(com.google.common.collect.ImmutableList) MixedIndexType(com.thinkaurelius.titan.graphdb.types.MixedIndexType) CompositeIndexType(com.thinkaurelius.titan.graphdb.types.CompositeIndexType) IndexType(com.thinkaurelius.titan.graphdb.types.IndexType) BackendTransaction(com.thinkaurelius.titan.diskstorage.BackendTransaction) MixedIndexType(com.thinkaurelius.titan.graphdb.types.MixedIndexType) IndexSerializer(com.thinkaurelius.titan.graphdb.database.IndexSerializer) CompositeIndexType(com.thinkaurelius.titan.graphdb.types.CompositeIndexType) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType)

Example 8 with IndexEntry

use of com.thinkaurelius.titan.diskstorage.indexing.IndexEntry in project 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

IndexEntry (com.thinkaurelius.titan.diskstorage.indexing.IndexEntry)8 HashMap (java.util.HashMap)6 SolrInputDocument (org.apache.solr.common.SolrInputDocument)6 BackendException (com.thinkaurelius.titan.diskstorage.BackendException)4 PermanentBackendException (com.thinkaurelius.titan.diskstorage.PermanentBackendException)4 TemporaryBackendException (com.thinkaurelius.titan.diskstorage.TemporaryBackendException)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 SolrServerException (org.apache.solr.client.solrj.SolrServerException)4 KeeperException (org.apache.zookeeper.KeeperException)4 IndexMutation (com.thinkaurelius.titan.diskstorage.indexing.IndexMutation)2 InternalRelation (com.thinkaurelius.titan.graphdb.internal.InternalRelation)2 InternalRelationType (com.thinkaurelius.titan.graphdb.internal.InternalRelationType)2 CompositeIndexType (com.thinkaurelius.titan.graphdb.types.CompositeIndexType)2 MixedIndexType (com.thinkaurelius.titan.graphdb.types.MixedIndexType)2 List (java.util.List)2 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)2 LongArrayList (com.carrotsearch.hppc.LongArrayList)1 ImmutableList (com.google.common.collect.ImmutableList)1