@Override public Object createComposedIndexKey(Object... keys) { return new OCompositeKey(keys); }
private OCompositeKey convertToCompositeKey(Object key) { final OCompositeKey compositeKey = new OCompositeKey(); int paramsIndex = 0; for (int i = 0; i < indexDefinitions.size(); i++) { final OIndexDefinition indexDefinition = indexDefinitions.get(i); if (i != multiValueIndex) { compositeKey.addKey(indexDefinition.createValue(params[paramsIndex])); paramsIndex++; } else compositeKey.addKey(((OIndexDefinitionMultiValue) indexDefinition).createSingleValue(key)); } return compositeKey; } }
/** * {@inheritDoc} */ @Override public OCompositeKey deserializeFromByteBufferObject(ByteBuffer buffer) { final OCompositeKey compositeKey = new OCompositeKey(); buffer.position(buffer.position() + OIntegerSerializer.INT_SIZE); final int keysSize = buffer.getInt(); final OBinarySerializerFactory factory = OBinarySerializerFactory.getInstance(); for (int i = 0; i < keysSize; i++) { final byte serializerId = buffer.get(); @SuppressWarnings("unchecked") OBinarySerializer<Object> binarySerializer = (OBinarySerializer<Object>) factory.getObjectSerializer(serializerId); final Object key = binarySerializer.deserializeFromByteBufferObject(buffer); compositeKey.addKey(key); } return compositeKey; }
/** * {@inheritDoc} */ public Object getDocumentValueToIndex(final ODocument iDocument) { final List<OCompositeKey> compositeKeys = new ArrayList<OCompositeKey>(10); final OCompositeKey firstKey = new OCompositeKey(); boolean containsCollection = false; compositeKeys.add(firstKey); for (final OIndexDefinition indexDefinition : indexDefinitions) { final Object result = indexDefinition.getDocumentValueToIndex(iDocument); if (result == null && isNullValuesIgnored()) return null; //for empty collections we add null key in index if (result instanceof Collection && ((Collection) result).isEmpty() && isNullValuesIgnored()) return null; containsCollection = addKey(firstKey, compositeKeys, containsCollection, result); } if (!containsCollection) return firstKey; return compositeKeys; }
protected Object convertKey(final OIndex<?> idx, Object iValue) { if (iValue != null) { final OType[] types = idx.getKeyTypes(); if (types.length == 0) iValue = iValue.toString(); else if (types.length == 1) { iValue = OType.convert(iValue, types[0].getDefaultJavaType()); } else { // if it's a composite key let it through. Otherwise build a composite key for the multivalue if (!(iValue instanceof OCompositeKey) && OMultiValue.isMultiValue(iValue)) { Iterable<Object> values = OMultiValue.getMultiValueIterable(iValue); List<Object> keys = new ArrayList<Object>(); for (Object value : values) { keys.add(value); } if (keys.size() <= types.length) { for (int i = 0; i < types.length; i++) { keys.set(i, OType.convert(keys.get(i), types[i].getDefaultJavaType())); } } else { throw new IllegalArgumentException( "Cannot build a composite key from the input. The size of the parameters is major than the number indexed fields"); } iValue = new OCompositeKey(keys); } } } return iValue; }
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); } }
private K enhanceCompositeKey(final K key, final PartialSearchMode partialSearchMode) { if (!(key instanceof OCompositeKey)) { return key; } final OCompositeKey compositeKey = (OCompositeKey) key; if (!(keySize == 1 || compositeKey.getKeys().size() == keySize || partialSearchMode.equals(PartialSearchMode.NONE))) { final OCompositeKey fullKey = new OCompositeKey(compositeKey); final int itemsToAdd = keySize - fullKey.getKeys().size(); final Comparable<?> keyItem; if (partialSearchMode.equals(PartialSearchMode.HIGHEST_BOUNDARY)) { keyItem = ALWAYS_GREATER_KEY; } else { keyItem = ALWAYS_LESS_KEY; } for (int i = 0; i < itemsToAdd; i++) { fullKey.addKey(keyItem); } //noinspection unchecked return (K) fullKey; } return key; }
private K enhanceCompositeKey(final K key, final PartialSearchMode partialSearchMode) { if (!(key instanceof OCompositeKey)) { return key; } final OCompositeKey compositeKey = (OCompositeKey) key; if (!(keySize == 1 || compositeKey.getKeys().size() == keySize || partialSearchMode.equals(PartialSearchMode.NONE))) { final OCompositeKey fullKey = new OCompositeKey(compositeKey); final int itemsToAdd = keySize - fullKey.getKeys().size(); final Comparable<?> keyItem; if (partialSearchMode.equals(PartialSearchMode.HIGHEST_BOUNDARY)) { keyItem = ALWAYS_GREATER_KEY; } else { keyItem = ALWAYS_LESS_KEY; } for (int i = 0; i < itemsToAdd; i++) { fullKey.addKey(keyItem); } //noinspection unchecked return (K) fullKey; } return key; }
private K enhanceCompositeKey(final K key, final PartialSearchMode partialSearchMode) { if (!(key instanceof OCompositeKey)) { return key; } final OCompositeKey compositeKey = (OCompositeKey) key; if (!(keySize == 1 || compositeKey.getKeys().size() == keySize || partialSearchMode.equals(PartialSearchMode.NONE))) { final OCompositeKey fullKey = new OCompositeKey(compositeKey); final int itemsToAdd = keySize - fullKey.getKeys().size(); final Comparable<?> keyItem; if (partialSearchMode.equals(PartialSearchMode.HIGHEST_BOUNDARY)) { keyItem = ALWAYS_GREATER_KEY; } else { keyItem = ALWAYS_LESS_KEY; } for (int i = 0; i < itemsToAdd; i++) { fullKey.addKey(keyItem); } //noinspection unchecked return (K) fullKey; } return key; }
protected Object enhanceCompositeKey(Object key, PartialSearchMode partialSearchMode) { if (!(key instanceof OCompositeKey)) return key; final OCompositeKey compositeKey = (OCompositeKey) key; final int keySize = getDefinition().getParamCount(); if (!(keySize == 1 || compositeKey.getKeys().size() == keySize || partialSearchMode.equals(PartialSearchMode.NONE))) { final OCompositeKey fullKey = new OCompositeKey(compositeKey); int itemsToAdd = keySize - fullKey.getKeys().size(); final Comparable<?> keyItem; if (partialSearchMode.equals(PartialSearchMode.HIGHEST_BOUNDARY)) keyItem = ALWAYS_GREATER_KEY; else keyItem = ALWAYS_LESS_KEY; for (int i = 0; i < itemsToAdd; i++) fullKey.addKey(keyItem); return fullKey; } return key; }
protected List<String> getTagEntityListDb(String host, String tagId) { List<String> entityList = null; OrientGraph graph = ServiceLocator.getInstance().getGraph(); try { OIndex<?> tagHostIdIdx = graph.getRawGraph().getMetadata().getIndexManager().getIndex("tagHostIdIdx"); OCompositeKey key = new OCompositeKey(host, tagId); OIdentifiable oid = (OIdentifiable) tagHostIdIdx.get(key); if (oid != null) { ODocument doc = (ODocument)oid.getRecord(); entityList = new ArrayList<String>(); ORidBag entities = doc.field("in_HasTag"); Iterator<OIdentifiable> iterator = entities.iterator(); while (iterator.hasNext()) { OIdentifiable identifiable = iterator.next(); entityList.add(identifiable.getIdentity().toString()); } } } catch (Exception e) { logger.error("Exception:", e); } finally { graph.shutdown(); } return entityList; }
public Object createValue(final Object... params) { if (params == null || params.length == 0) return null; if (keyTypes.length == 1) return OType.convert(params[0], keyTypes[0].getDefaultJavaType()); final OCompositeKey compositeKey = new OCompositeKey(); for (int i = 0; i < params.length; ++i) { final Comparable<?> paramValue = (Comparable<?>) OType.convert(params[i], keyTypes[i].getDefaultJavaType()); if (paramValue == null) return null; compositeKey.addKey(paramValue); } return compositeKey; }
protected ODocument getODocumentByHostId(OrientGraph graph, String index, String host, String id) { ODocument doc = null; OIndex<?> hostIdIdx = graph.getRawGraph().getMetadata().getIndexManager().getIndex(index); // this is a unique index, so it retrieves a OIdentifiable OCompositeKey key = new OCompositeKey(host, id); OIdentifiable oid = (OIdentifiable) hostIdIdx.get(key); if (oid != null) { doc = (ODocument)oid.getRecord(); } return doc; }
public OrientVertex getConfigByHostId(OrientGraph graph, String host, String configId) { OrientVertex config = null; OIndex<?> hostIdIdx = graph.getRawGraph().getMetadata().getIndexManager().getIndex("configHostIdIdx"); OCompositeKey key = new OCompositeKey(host, configId); OIdentifiable oid = (OIdentifiable) hostIdIdx.get(key); if (oid != null) { config = graph.getVertex(oid.getRecord()); } return config; }
public OrientVertex getBranchByHostId(OrientGraph graph, String branchType, String host, String categoryId) { OrientVertex branch = null; OIndex<?> hostIdIdx = graph.getRawGraph().getMetadata().getIndexManager().getIndex(branchType + "HostIdIdx"); OCompositeKey key = new OCompositeKey(host, categoryId); OIdentifiable oid = (OIdentifiable) hostIdIdx.get(key); if (oid != null) { branch = graph.getVertex(oid.getRecord()); } return branch; }
@SuppressWarnings("unchecked") public OCompositeKey deserialize(byte[] stream, int startPosition) { final OCompositeKey compositeKey = new OCompositeKey(); startPosition += OIntegerSerializer.INT_SIZE; final int keysSize = OIntegerSerializer.INSTANCE.deserializeLiteral(stream, startPosition); startPosition += OIntegerSerializer.INSTANCE.getObjectSize(keysSize); final OBinarySerializerFactory factory = OBinarySerializerFactory.getInstance(); for (int i = 0; i < keysSize; i++) { final byte serializerId = stream[startPosition]; startPosition += OBinarySerializerFactory.TYPE_IDENTIFIER_SIZE; OBinarySerializer<Object> binarySerializer = (OBinarySerializer<Object>) factory.getObjectSerializer(serializerId); final Object key = binarySerializer.deserialize(stream, startPosition); compositeKey.addKey(key); startPosition += binarySerializer.getObjectSize(key); } return compositeKey; }
public OCompositeKey deserializeNativeObject(byte[] stream, int startPosition) { final OCompositeKey compositeKey = new OCompositeKey(); startPosition += OIntegerSerializer.INT_SIZE; final int keysSize = OIntegerSerializer.INSTANCE.deserializeNative(stream, startPosition); startPosition += OIntegerSerializer.INSTANCE.getObjectSize(keysSize); final OBinarySerializerFactory factory = OBinarySerializerFactory.getInstance(); for (int i = 0; i < keysSize; i++) { final byte serializerId = stream[startPosition]; startPosition += OBinarySerializerFactory.TYPE_IDENTIFIER_SIZE; @SuppressWarnings("unchecked") OBinarySerializer<Object> binarySerializer = (OBinarySerializer<Object>) factory.getObjectSerializer(serializerId); final Object key = binarySerializer.deserializeNativeObject(stream, startPosition); compositeKey.addKey(key); startPosition += binarySerializer.getObjectSize(key); } return compositeKey; }
/** * {@inheritDoc} */ @Override public OCompositeKey deserializeFromByteBufferObject(ByteBuffer buffer, OWALChanges walChanges, int offset) { final OCompositeKey compositeKey = new OCompositeKey(); offset += OIntegerSerializer.INT_SIZE; final int keysSize = walChanges.getIntValue(buffer, offset); offset += OIntegerSerializer.INT_SIZE; final OBinarySerializerFactory factory = OBinarySerializerFactory.getInstance(); for (int i = 0; i < keysSize; i++) { final byte serializerId = walChanges.getByteValue(buffer, offset); offset += OBinarySerializerFactory.TYPE_IDENTIFIER_SIZE; @SuppressWarnings("unchecked") OBinarySerializer<Object> binarySerializer = (OBinarySerializer<Object>) factory.getObjectSerializer(serializerId); final Object key = binarySerializer.deserializeFromByteBufferObject(buffer, walChanges, offset); compositeKey.addKey(key); offset += binarySerializer.getObjectSize(key); } return compositeKey; }
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()); }
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; }