Search in sources :

Example 31 with OCompositeKey

use of com.orientechnologies.orient.core.index.OCompositeKey in project orientdb by orientechnologies.

the class OrientIndex method buildKeyValueIndex.

private OIndex<?> buildKeyValueIndex(final ODocument metadata) {
    final OIndexFactory factory = OIndexes.getFactory(OClass.INDEX_TYPE.DICTIONARY.toString(), null);
    final OIndex<?> recordKeyValueIndex = new OIndexTxAwareOneValue(graph.getRawGraph(), (OIndex<OIdentifiable>) graph.getRawGraph().getMetadata().getIndexManager().createIndex("__@recordmap@___" + underlying.getName(), OClass.INDEX_TYPE.DICTIONARY.toString(), new OSimpleKeyIndexDefinition(factory.getLastVersion(), OType.LINK, OType.STRING), null, null, null));
    final List<ODocument> entries = graph.getRawGraph().query(new OSQLSynchQuery<Object>("select  from index:" + underlying.getName()));
    for (ODocument entry : entries) {
        final OIdentifiable rid = entry.field("rid");
        if (rid != null)
            recordKeyValueIndex.put(new OCompositeKey(rid, entry.field("key")), rid);
    }
    metadata.field(CONFIG_RECORD_MAP_NAME, recordKeyValueIndex.getName());
    return recordKeyValueIndex;
}
Also used : OSimpleKeyIndexDefinition(com.orientechnologies.orient.core.index.OSimpleKeyIndexDefinition) OIndexTxAwareOneValue(com.orientechnologies.orient.core.index.OIndexTxAwareOneValue) OIndexFactory(com.orientechnologies.orient.core.index.OIndexFactory) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 32 with OCompositeKey

use of com.orientechnologies.orient.core.index.OCompositeKey in project orientdb by orientechnologies.

the class OLuceneFacetManager method addFacetContext.

public void addFacetContext(OLuceneQueryContext queryContext, Object key) throws IOException {
    queryContext.setFacet(true);
    queryContext.setFacetField(facetField);
    queryContext.setFacetConfig(config);
    // queryContext.setfacetDim(facetDim);
    queryContext.setReader(new DirectoryTaxonomyReader(getTaxDirectory(owner.getDatabase())));
    if (key instanceof OCompositeKey) {
        List<Object> keys = ((OCompositeKey) key).getKeys();
        for (Object o : keys) {
            if (o instanceof Map) {
                String drillDown = (String) ((Map) o).get("drillDown");
                queryContext.setDrillDownQuery(drillDown);
            }
        }
    }
}
Also used : OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey) Map(java.util.Map) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)

Example 33 with OCompositeKey

use of com.orientechnologies.orient.core.index.OCompositeKey in project orientdb by orientechnologies.

the class OLuceneIndexType method createExactQuery.

public static Query createExactQuery(OIndexDefinition index, Object key) {
    Query query = null;
    if (key instanceof String) {
        final BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
        if (index.getFields().size() > 0) {
            for (String idx : index.getFields()) {
                queryBuilder.add(new TermQuery(new Term(idx, key.toString())), BooleanClause.Occur.SHOULD);
            }
        } else {
            queryBuilder.add(new TermQuery(new Term(OLuceneIndexEngineAbstract.KEY, key.toString())), BooleanClause.Occur.SHOULD);
        }
        query = queryBuilder.build();
    } else if (key instanceof OCompositeKey) {
        final BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
        int i = 0;
        OCompositeKey keys = (OCompositeKey) key;
        for (String idx : index.getFields()) {
            String val = (String) keys.getKeys().get(i);
            queryBuilder.add(new TermQuery(new Term(idx, val)), BooleanClause.Occur.MUST);
            i++;
        }
        query = queryBuilder.build();
    }
    return query;
}
Also used : Term(org.apache.lucene.index.Term) OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey)

Example 34 with OCompositeKey

use of com.orientechnologies.orient.core.index.OCompositeKey in project orientdb by orientechnologies.

the class OTransactionOptimisticProxy method fillIndexOperations.

private void fillIndexOperations(final ODocument remoteIndexEntries) {
    for (Entry<String, Object> indexEntry : remoteIndexEntries) {
        final String indexName = indexEntry.getKey();
        final ODocument indexDoc = (ODocument) indexEntry.getValue();
        if (indexDoc == null)
            continue;
        OTransactionIndexChanges transactionIndexChanges = indexEntries.get(indexEntry.getKey());
        if (transactionIndexChanges == null) {
            transactionIndexChanges = new OTransactionIndexChanges();
            indexEntries.put(indexEntry.getKey(), transactionIndexChanges);
        }
        final Boolean clearAll = indexDoc.field("clear");
        if (clearAll != null && clearAll)
            transactionIndexChanges.setCleared();
        final Collection<ODocument> entries = indexDoc.field("entries");
        if (entries == null)
            continue;
        for (final ODocument entry : entries) {
            final List<ODocument> operations = entry.field("ops");
            if (operations == null)
                continue;
            final Object key;
            try {
                ODocument keyContainer;
                if (protocolVersion <= OChannelBinaryProtocol.PROTOCOL_VERSION_24) {
                    final String serializedKey = OStringSerializerHelper.decode((String) entry.field("k"));
                    if (serializedKey.equals("*"))
                        keyContainer = null;
                    else {
                        keyContainer = new ODocument();
                        keyContainer.setLazyLoad(false);
                        ORecordSerializerSchemaAware2CSV.INSTANCE.fromString(serializedKey, keyContainer, null);
                    }
                } else {
                    keyContainer = entry.field("k");
                }
                if (keyContainer != null) {
                    final Object storedKey = keyContainer.field("key");
                    if (storedKey instanceof List)
                        key = new OCompositeKey((List<? extends Comparable<?>>) storedKey);
                    else if (Boolean.TRUE.equals(keyContainer.field("binary"))) {
                        key = OStreamSerializerAnyStreamable.INSTANCE.fromStream((byte[]) storedKey);
                    } else
                        key = storedKey;
                } else
                    key = null;
            } catch (IOException ioe) {
                throw OException.wrapException(new OTransactionException("Error during index changes deserialization. "), ioe);
            }
            for (final ODocument op : operations) {
                final int operation = (Integer) op.rawField("o");
                final OTransactionIndexChanges.OPERATION indexOperation = OTransactionIndexChanges.OPERATION.values()[operation];
                final OIdentifiable value = op.field("v");
                transactionIndexChanges.getChangesPerKey(key).add(value, indexOperation);
                if (value == null)
                    continue;
                final ORID rid = value.getIdentity();
                List<OTransactionRecordIndexOperation> txIndexOperations = recordIndexOperations.get(rid);
                if (txIndexOperations == null) {
                    txIndexOperations = new ArrayList<OTransactionRecordIndexOperation>();
                    recordIndexOperations.put(rid, txIndexOperations);
                }
                txIndexOperations.add(new OTransactionRecordIndexOperation(indexName, key, indexOperation));
            }
        }
    }
}
Also used : OTransactionIndexChanges(com.orientechnologies.orient.core.tx.OTransactionIndexChanges) IOException(java.io.IOException) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) ORID(com.orientechnologies.orient.core.id.ORID) OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 35 with OCompositeKey

use of com.orientechnologies.orient.core.index.OCompositeKey in project orientdb by orientechnologies.

the class ByteArrayKeyTest method testAutomaticCompositeUsage.

public void testAutomaticCompositeUsage() {
    byte[] key1 = new byte[] { 1, 2, 3 };
    byte[] key2 = new byte[] { 4, 5, 6 };
    ODocument doc1 = new ODocument("CompositeByteArrayKeyTest");
    doc1.field("byteArrayKey", key1);
    doc1.field("intKey", 1);
    doc1.save();
    ODocument doc2 = new ODocument("CompositeByteArrayKeyTest");
    doc2.field("byteArrayKey", key2);
    doc2.field("intKey", 2);
    doc2.save();
    OIndex<?> index = database.getMetadata().getIndexManager().getIndex("compositeByteArrayKey");
    Assert.assertEquals(index.get(new OCompositeKey(key1, 1)), doc1);
    Assert.assertEquals(index.get(new OCompositeKey(key2, 2)), doc2);
}
Also used : OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OCompositeKey (com.orientechnologies.orient.core.index.OCompositeKey)40 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)22 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)11 ORecordId (com.orientechnologies.orient.core.id.ORecordId)10 ORID (com.orientechnologies.orient.core.id.ORID)8 OBinarySerializerFactory (com.orientechnologies.orient.core.serialization.serializer.binary.OBinarySerializerFactory)5 OBinarySerializer (com.orientechnologies.common.serialization.types.OBinarySerializer)4 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)4 OIndex (com.orientechnologies.orient.core.index.OIndex)3 Collection (java.util.Collection)3 Date (java.util.Date)3 List (java.util.List)3 OMultiCollectionIterator (com.orientechnologies.common.collection.OMultiCollectionIterator)2 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)2 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)2 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)2 OType (com.orientechnologies.orient.core.metadata.schema.OType)2 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)2 OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)2 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)2