
com.tinkerpop.blueprints.impls.orient.OrientIndex Maven / Gradle / Ivy
/*
*
* * Copyright 2014 Orient Technologies LTD (info(at)orientechnologies.com)
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
* *
* * For more information: http://www.orientechnologies.com
*
*/
package com.tinkerpop.blueprints.impls.orient;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.index.OCompositeKey;
import com.orientechnologies.orient.core.index.OIndex;
import com.orientechnologies.orient.core.index.OIndexFactory;
import com.orientechnologies.orient.core.index.OIndexTxAwareMultiValue;
import com.orientechnologies.orient.core.index.OIndexTxAwareOneValue;
import com.orientechnologies.orient.core.index.OIndexes;
import com.orientechnologies.orient.core.index.OSimpleKeyIndexDefinition;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.tinkerpop.blueprints.CloseableIterable;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Index;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.util.StringFactory;
import com.tinkerpop.blueprints.util.WrappingCloseableIterable;
/**
* @author Luca Garulli (http://www.orientechnologies.com)
*/
@SuppressWarnings("unchecked")
public class OrientIndex implements Index {
public static final String CONFIG_CLASSNAME = "blueprintsIndexClass";
public static final String CONFIG_RECORD_MAP_NAME = "record_map_name";
protected static final String VERTEX = "Vertex";
protected static final String EDGE = "Edge";
protected static final String SEPARATOR = "!=!";
protected OrientBaseGraph graph;
protected OIndex> underlying;
protected OIndex> recordKeyValueIndex;
protected Class extends Element> indexClass;
protected OrientIndex(final OrientBaseGraph graph, final String indexName, final Class extends Element> indexClass,
final OType iType) {
this.graph = graph;
this.indexClass = indexClass;
create(indexName, this.indexClass, iType);
}
protected OrientIndex(final OrientBaseGraph orientGraph, final OIndex> rawIndex) {
this.graph = orientGraph;
this.underlying = rawIndex instanceof OIndexTxAwareMultiValue ? rawIndex : new OIndexTxAwareMultiValue(
orientGraph.getRawGraph(), (OIndex>) rawIndex);
final ODocument metadata = rawIndex.getMetadata();
if (metadata == null) {
load(rawIndex.getConfiguration());
} else
load(metadata);
}
public String getIndexName() {
return underlying.getName();
}
public Class getIndexClass() {
return (Class) this.indexClass;
}
public void put(final String key, final Object value, final T element) {
final String keyTemp = key + SEPARATOR + value;
final ODocument doc = element.getRecord();
if (!doc.getIdentity().isValid())
doc.save();
graph.setCurrentGraphInThreadLocal();
graph.autoStartTransaction();
underlying.put(keyTemp, doc);
recordKeyValueIndex.put(new OCompositeKey(doc.getIdentity(), keyTemp), doc.getIdentity());
}
@SuppressWarnings("rawtypes")
public CloseableIterable get(final String key, final Object iValue) {
final String keyTemp = key + SEPARATOR + iValue;
Collection records = (Collection) underlying.get(keyTemp);
if (records == null || records.isEmpty())
return new WrappingCloseableIterable(Collections.emptySet());
return new OrientElementIterable(graph, records);
}
public CloseableIterable query(final String key, final Object query) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
public long count(final String key, final Object value) {
final String keyTemp = key + SEPARATOR + value;
final Collection records = (Collection) underlying.get(keyTemp);
if (records == null)
return 0;
return records.size();
}
public void remove(final String key, final Object value, final T element) {
final String keyTemp = key + SEPARATOR + value;
graph.setCurrentGraphInThreadLocal();
graph.autoStartTransaction();
try {
underlying.remove(keyTemp, element.getRecord());
recordKeyValueIndex.remove(new OCompositeKey(element.getIdentity(), keyTemp), element.getIdentity());
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public String toString() {
return StringFactory.indexString(this);
}
public OIndex> getUnderlying() {
return underlying;
}
public void close() {
if (underlying != null) {
underlying.flush();
underlying = null;
}
graph = null;
}
protected void removeElement(final T element) {
graph.setCurrentGraphInThreadLocal();
graph.autoStartTransaction();
final OSQLSynchQuery query = new OSQLSynchQuery("select from index:" + recordKeyValueIndex.getName()
+ " where key between [" + element.getIdentity() + "] and [" + element.getIdentity() + "]");
final Collection entries = (Collection) graph.getRawGraph().query(query);
for (ODocument entry : entries) {
final OCompositeKey key = entry.field("key");
final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy