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 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; } }
@Override public Object createComposedIndexKey(Object... keys) { return new OCompositeKey(keys); }
@Override public List<Object> edgeLookup(String edgeLabel, String indexPostfix, Object key) { OrientBaseGraph orientBaseGraph = unwrapCurrentGraph(); List<Object> ids = new ArrayList<>(); // Load the edge type in order to access the indices of the edge OrientEdgeType edgeType = orientBaseGraph.getEdgeType(edgeLabel); if (edgeType != null) { // Fetch the required index OIndex<?> index = edgeType.getClassIndex("e." + edgeLabel.toLowerCase() + "_" + indexPostfix); if (index != null) { // Iterate over the sb-tree index entries OIndexCursor cursor = index.iterateEntriesMajor(new OCompositeKey(key), true, false); while (cursor.hasNext()) { Entry<Object, OIdentifiable> entry = cursor.nextEntry(); if (entry != null) { OCompositeKey entryKey = (OCompositeKey) entry.getKey(); // The index returns all entries. We thus need to filter it manually if (entryKey.getKeys().get(1).equals(key)) { Object inId = entryKey.getKeys().get(0); // Only add the inbound vertex id to the list of ids ids.add(inId); } } else { break; } } } } return ids; }
public OCompositeKey(final Object... keys) { this.keys = new ArrayList<Object>(keys.length); this.comparator = ODefaultComparator.INSTANCE; for (final Object key : keys) addKey(key); }
/** * {@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; }
/** * {@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; }
public OCompositeKey(final List<?> keys) { this.keys = new ArrayList<Object>(keys.size()); this.comparator = ODefaultComparator.INSTANCE; for (final Object key : keys) addKey(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; }
final OCompositeKey compositeKey = new OCompositeKey((List<?>) p.getValue()); if (p.getKey() instanceof String && Character.isDigit(((String) p.getKey()).charAt(0))) parameters.put(Integer.parseInt((String) p.getKey()), compositeKey);
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; }
/** * Add new key value to the list of already registered values. * <p> * If passed in value is {@link OCompositeKey} itself then its values will be copied in current index. But key itself will not be * added. * * @param key Key to add. */ public void addKey(final Object key) { if (key instanceof OCompositeKey) { final OCompositeKey compositeKey = (OCompositeKey) key; for (final Object inKey : compositeKey.keys) { addKey(inKey); } } else { keys.add(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 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 OCompositeKey createSingleValue(final List<?> params) { final OCompositeKey compositeKey = new OCompositeKey(); int currentParamIndex = 0; for (final OIndexDefinition indexDefinition : indexDefinitions) { if (currentParamIndex + 1 > params.size()) break; final int endIndex; if (currentParamIndex + indexDefinition.getParamCount() > params.size()) endIndex = params.size(); else endIndex = currentParamIndex + indexDefinition.getParamCount(); final List<?> indexParams = params.subList(currentParamIndex, endIndex); currentParamIndex += indexDefinition.getParamCount(); final Object keyValue; if (indexDefinition instanceof OIndexDefinitionMultiValue) keyValue = ((OIndexDefinitionMultiValue) indexDefinition).createSingleValue(indexParams.toArray()); else keyValue = indexDefinition.createValue(indexParams); if (keyValue == null && isNullValuesIgnored()) return null; compositeKey.addKey(keyValue); } return compositeKey; }
final OCompositeKey compositeKey = new OCompositeKey(firstKey.getKeys()); compositeKeys.add(compositeKey); for (final Object keyItem : collectionKey) { final OCompositeKey compositeKey = compositeKeys.get(compositeIndex); compositeKey.addKey(keyItem); firstKey.addKey(null); } else if (containsCollection) for (final OCompositeKey compositeKey : compositeKeys) compositeKey.addKey(keyValue); else firstKey.addKey(keyValue);