@Override protected byte[] getIdInternal(final Value value, final boolean p) throws DataAccessLayerException { byte[] id = _node2id_cache.get(value); if (id == null) { id = _decoratee.getID(value, p); _cacheStrategy.cacheValue(value, id); } else { _idHitsCount.incrementAndGet(); } return id; }
@Override public boolean contains(final Object o) { try { return (o instanceof Value && _ids.contains(_dict.getID((Value) o, _p))); } catch (DataAccessLayerException exception) { throw new RuntimeException(exception); } }
@Override protected byte[] getIdInternal(final Value value, final boolean p) throws DataAccessLayerException { byte[] id = _node2id_cache.get(value); if (id == null) { id = _decoratee.getID(value, p); _cacheStrategy.cacheValue(value, id); } else { _idHitsCount.incrementAndGet(); } return id; }
@Override public boolean contains(final Object o) { try { return (o instanceof Value && _ids.contains(_dict.getID((Value) o, _p))); } catch (DataAccessLayerException exception) { throw new RuntimeException(exception); } }
@Override protected byte[] getIdInternal(final Value value, final boolean p) throws DataAccessLayerException { if (value instanceof URI && contains(((URI) value).getNamespace())) { RUNTIME_CONTEXTS.get().isFirstLevelResult = true; _idKnownURIsHitsCount.incrementAndGet(); final String n3 = NTriplesUtil.toNTriplesString(value); byte[] id = null; synchronized (this) { id = getID(NTriplesUtil.toNTriplesString(value), p); if (id[0] == NOT_SET[0]) { id = newId(n3, _index); _index.putQuick(n3, id); } } return id; } else { RUNTIME_CONTEXTS.get().isFirstLevelResult = false; return _decoratee.getID(value, p); } }
@Override protected byte[] getIdInternal(final Value value, final boolean p) throws DataAccessLayerException { if (value instanceof URI && contains(((URI) value).getNamespace())) { RUNTIME_CONTEXTS.get().isFirstLevelResult = true; _idKnownURIsHitsCount.incrementAndGet(); final String n3 = NTriplesUtil.toNTriplesString(value); byte[] id = null; synchronized (this) { id = getID(NTriplesUtil.toNTriplesString(value), p); if (id[0] == NOT_SET[0]) { id = newId(n3, _index); _index.putQuick(n3, id); } } return id; } else { RUNTIME_CONTEXTS.get().isFirstLevelResult = false; return _decoratee.getID(value, p); } }
@Override protected byte[] getIdInternal(final Value value, final boolean p) throws DataAccessLayerException { if (value instanceof URI) { final URI uri = (URI) value; byte[] namespaceId = _namespaces.getID(uri.getNamespace(), p); byte[] localNameId = _localNames.getID(uri.getLocalName(), p); return concat(MARKER, namespaceId, localNameId); } else { return _bNodesAndLiterals.getID(value, p); } }
@Override protected byte[] getIdInternal(final Value value, final boolean p) throws DataAccessLayerException { if (value instanceof URI) { final URI uri = (URI) value; byte[] namespaceId = _namespaces.getID(uri.getNamespace(), p); byte[] localNameId = _localNames.getID(uri.getLocalName(), p); return concat(MARKER, namespaceId, localNameId); } else { return _bNodesAndLiterals.getID(value, p); } }
/** * Returns the identifier associated with the given value. * * @param value the {@link Value}. * @return the identifier associated with the given value. * @throws DataAccessLayerException in case of Data access failure. */ byte [] id(final Value value) throws DataAccessLayerException { if (value == null) { return null; } final INativeCumulusValue nativeValue = (INativeCumulusValue) value; if (nativeValue.hasInternalID()) { return nativeValue.getInternalID(); } else { final byte [] id = _crdf.getDictionary().getID(value, false); nativeValue.setInternalID(id); return id; } } }
/** * Returns the identifier associated with the given value. * * @param value the {@link Value}. * @return the identifier associated with the given value. * @throws DataAccessLayerException in case of Data access failure. */ byte [] id(final Value value) throws DataAccessLayerException { if (value == null) { return null; } final INativeCumulusValue nativeValue = (INativeCumulusValue) value; if (nativeValue.hasInternalID()) { return nativeValue.getInternalID(); } else { final byte [] id = _crdf.getDictionary().getID(value, false); nativeValue.setInternalID(id); return id; } } }
/** * Tests ID creation and caching. * * @throws Exception never otherwise the test fails. */ @Test public void getID() throws Exception { when(_decoratee.getID(_aValue, false)).thenReturn(_id); assertTrue(_cut._node2id_cache.isEmpty()); assertArrayEquals(_id, _cut.getID(_aValue, false)); assertEquals(_id, _cut._node2id_cache.get(_aValue)); assertArrayEquals(_id, _cut.getID(_aValue, false)); verify(_decoratee).getID(_aValue, false); }
/** * Remove() method itself has no effect on the {@link TransientValueDictionary} unless the given value is a long literal. * * @throws Exception never otherwise the test fails. */ @Test public void removeValueInvolvesLongLiteralDictionary() throws Exception { final byte[] persistentId = { TransientValueDictionary.THRESHOLD_EXCEEDED, 2, 3, 4, 5, 6, 7, 78, 8, 9 }; final ITopLevelDictionary decoratee = mock(ITopLevelDictionary.class); when(decoratee.getID(_longLiteral, false)).thenReturn(persistentId); _cut = new TransientValueDictionary(randomString(), decoratee, TransientValueDictionary.DEFAULT_THRESHOLD); _cut.removeValue(_longLiteral, false); verify(decoratee).removeValue(_longLiteral, false); }
@Override public Iterator<byte[][]> numericRangeQuery( final Value[] query, final double lowerBound, final boolean equalsLower, final double upperBound, final boolean equalsUpper, final boolean reverse, final int limit) throws DataAccessLayerException { final byte[] s = _dictionary.getID(query[0], false); final byte[] p = _dictionary.getID(query[1], true); final boolean subjectIsVariable = isVariable(s); final BoundStatement statement = _rangeQueries[getRangeQueryIndex(reverse, subjectIsVariable, true, !equalsUpper, !equalsLower)].bind(); int queryParameterIndex = 0; if (!subjectIsVariable) { statement.setBytesUnsafe(queryParameterIndex++, ID_SERIALIZER.serialize(s)); } statement.setBytesUnsafe(queryParameterIndex++, ID_SERIALIZER.serialize(p)); statement.setDouble(queryParameterIndex++, lowerBound); statement.setDouble(queryParameterIndex++, upperBound); statement.setInt(queryParameterIndex, limit); return new SPOCResultIterator(_session.executeAsync(statement), false); }
@Override public Iterator<byte[][]> dateRangeQuery( final Value[] query, final long lowerBound, final boolean equalsLower, final long upperBound, final boolean equalsUpper, final boolean reverse, final int limit) throws DataAccessLayerException { final byte[] s = _dictionary.getID(query[0], false); final byte[] p = _dictionary.getID(query[1], true); final boolean subjectIsVariable = isVariable(s); final BoundStatement statement = _rangeQueries[getRangeQueryIndex(reverse, subjectIsVariable, false, !equalsUpper, !equalsLower)].bind(); int queryParameterIndex = 0; if (!subjectIsVariable) { statement.setBytesUnsafe(queryParameterIndex++, ID_SERIALIZER.serialize(s)); } statement.setBytesUnsafe(queryParameterIndex++, ID_SERIALIZER.serialize(p)); statement.setDouble(queryParameterIndex++, lowerBound); statement.setDouble(queryParameterIndex++, upperBound); statement.setInt(queryParameterIndex, limit); return new SPOCResultIterator(_session.executeAsync(statement), false); }
@Override public Iterator<byte[][]> dateRangeQuery( final Value[] query, final long lowerBound, final boolean equalsLower, final long upperBound, final boolean equalsUpper, final boolean reverse, final int limit) throws DataAccessLayerException { final byte[] s = _dictionary.getID(query[0], false); final byte[] p = _dictionary.getID(query[1], true); final boolean subjectIsVariable = isVariable(s); final BoundStatement statement = _rangeQueries[getRangeQueryIndex(reverse, subjectIsVariable, false, !equalsUpper, !equalsLower)].bind(); int queryParameterIndex = 0; if (!subjectIsVariable) { statement.setBytesUnsafe(queryParameterIndex++, ID_SERIALIZER.serialize(s)); } statement.setBytesUnsafe(queryParameterIndex++, ID_SERIALIZER.serialize(p)); statement.setDouble(queryParameterIndex++, lowerBound); statement.setDouble(queryParameterIndex++, upperBound); statement.setInt(queryParameterIndex, limit); return new SPOCResultIterator(_session.executeAsync(statement), false); }
@Override public Iterator<byte[][]> numericRangeQuery( final Value[] query, final double lowerBound, final boolean equalsLower, final double upperBound, final boolean equalsUpper, final boolean reverse, final int limit) throws DataAccessLayerException { final byte[] s = _dictionary.getID(query[0], false); final byte[] p = _dictionary.getID(query[1], true); final boolean subjectIsVariable = isVariable(s); final BoundStatement statement = _rangeQueries[getRangeQueryIndex(reverse, subjectIsVariable, true, !equalsUpper, !equalsLower)].bind(); int queryParameterIndex = 0; if (!subjectIsVariable) { statement.setBytesUnsafe(queryParameterIndex++, ID_SERIALIZER.serialize(s)); } statement.setBytesUnsafe(queryParameterIndex++, ID_SERIALIZER.serialize(p)); statement.setDouble(queryParameterIndex++, lowerBound); statement.setDouble(queryParameterIndex++, upperBound); statement.setInt(queryParameterIndex, limit); return new SPOCResultIterator(_session.executeAsync(statement), false); }
/** * The remove() method must remove the value from cache and call remove() on decoratee. * * @throws Exception never otherwise the test fails. */ @Test public void removeValue() throws Exception { when(_decoratee.getID(_aValue, false)).thenReturn(_id); assertTrue(_cut._node2id_cache.isEmpty()); assertArrayEquals(_id, _cut.getID(_aValue, false)); assertEquals(_id, _cut._node2id_cache.get(_aValue)); assertArrayEquals(_id, _cut.getID(_aValue, false)); _cut.removeValue(_aValue, false); assertTrue(_cut._node2id_cache.isEmpty()); assertTrue(_cut._id2node_cache.isEmpty()); verify(_decoratee).removeValue(_aValue, false); }
/** * A request is received for a literal or a bnode. * * @throws Exception never otherwise the test fails. */ @Test public void getIdWithIgnorableValue() throws Exception { final Value[] ignorableValues = { buildLiteral(randomString()), buildBNode(randomString()) }; for (final Value value : ignorableValues) { _cut.getID(value, _isPredicate); verify(_decoratee).getID(value, _isPredicate); } }
/** * A request is received for a URI with a namespace not belonging to managed domains. * * @throws Exception never otherwise the test fails. */ @Test public void getIdWithIgnorableURI() throws Exception { final String[] ignorableNamespaces = { "http://pippo.pluto.paperino/", "http://this.should.not.be.already.managed", "http://also.this.shouldnt" }; for (final String ignorableNamespace : ignorableNamespaces) { assertFalse(_cut.contains(ignorableNamespace)); final Value ignorableURI = buildResource(ignorableNamespace + randomString()); _cut.getID(ignorableURI, _isPredicate); verify(_decoratee).getID(ignorableURI, _isPredicate); } }
/** * A request is received for a URI with a namespace that belongs to managed domains. * * @throws Exception never otherwise the test fails. */ @Test public void getIdWithManagedURI() throws Exception { final String[] managedNamespaces = { FOAF.NAMESPACE, RDFS.NAMESPACE, OWL.NAMESPACE }; for (final String managedNamespace : managedNamespaces) { assertTrue(_cut.contains(managedNamespace)); final Value uri = buildResource(managedNamespace + randomString()); final String n3 = NTriplesUtil.toNTriplesString(uri); // Make sure the mock index returns "Sorry, we don't have such value". when(_dummyIndex.get(n3)).thenReturn(ValueDictionaryBase.NOT_SET); // 1. ask for uri. byte[] id = _cut.getID(uri, _isPredicate); // 2. make sure the identifier is well-formed. assertEquals(KnownURIsDictionary.ID_LENGTH, id.length); assertEquals(KnownURIsDictionary.KNOWN_URI_MARKER, id[0]); assertEquals(ValueDictionaryBase.RESOURCE_BYTE_FLAG, id[1]); // 3. make sure the decoratee wasn't involved in identifier creation. verify(_decoratee, times(0)).getID(uri, _isPredicate); verify(_dummyIndex).putQuick(n3, id); reset(_decoratee, _dummyIndex); } }