/** * Returns the identifier of a given N3 resource. * * @param n3 the resource (N3 representation). * @param p a flag indicating if the resource is a predicate. * @return the identifier of the given resource. * @throws DataAccessLayerException in case of data access failure. */ protected byte[] getID(final String n3, final boolean p) throws DataAccessLayerException { return (n3 == null || n3.isEmpty() || n3.charAt(0) == '?') ? null : p ? _pIndex.get(n3) : _soIndex.get(n3); }
/** * Identifiers for managed URI must be be directly served by the dictionary, without involving the decoratee. * * @throws Exception never otherwise the test fails. */ @Test public void getURIWithManagedId() throws Exception { final URI managedUri = buildResource(FOAF.NAMESPACE + System.currentTimeMillis()); final String n3 = NTriplesUtil.toNTriplesString(managedUri); when(_dummyIndex.getQuick(any(byte[].class))).thenReturn(n3); when(_dummyIndex.get(n3)).thenReturn(ValueDictionaryBase.NOT_SET); byte[] id = _cut.getID(managedUri, _isPredicate); final Value value = _cut.getValue(id, _isPredicate); assertEquals(managedUri, value); verify(_decoratee, times(0)).getValue(id, _isPredicate); }
/** * Returns the identifier of a given N3 resource. * * @param n3 the resource (N3 representation). * @param p a flag indicating if the resource is a predicate. * @return the identifier of the given resource. * @throws DataAccessLayerException in case of data access failure. */ protected byte[] getID(final String n3, final boolean p) throws DataAccessLayerException { return (n3 == null || n3.isEmpty() || n3.charAt(0) == '?') ? null : p ? _pIndex.get(n3) : _soIndex.get(n3); }
/** * Returns the identifier of a given N3 resource. * * @param n3 the resource (N3 representation). * @param p a flag indicating if the resource is a predicate. * @return the identifier of the given resource. * @throws DataAccessLayerException in case of data access failure. */ protected byte[] getID(final String n3, final boolean p) throws DataAccessLayerException { return (n3 == null || n3.isEmpty() || n3.charAt(0) == '?') ? null : _index.get(n3); }
/** * Returns the identifier of a given N3 resource. * * @param n3 the resource (N3 representation). * @param p a flag indicating if the resource is a predicate. * @return the identifier of the given resource. * @throws DataAccessLayerException in case of data access failure. */ protected byte[] getID(final String n3, final boolean p) throws DataAccessLayerException { return (n3 == null || n3.isEmpty() || n3.charAt(0) == '?') ? null : _index.get(n3); }
@Override protected byte[] getIdInternal(final String value, final boolean p) throws DataAccessLayerException { if (value.trim().length() == 0) { return EMPTY_VALUE; } byte[] id = null; synchronized (this) { id = _index.get(value); if (id[0] == NOT_SET[0]) { id = newId(value, _index); _index.putQuick(value, id); } } RUNTIME_CONTEXTS.get().isFirstLevelResult = true; return id; }
@Override protected byte[] getIdInternal(final String value, final boolean p) throws DataAccessLayerException { if (value.trim().length() == 0) { return EMPTY_VALUE; } byte[] id = null; synchronized (this) { id = _index.get(value); if (id[0] == NOT_SET[0]) { id = newId(value, _index); _index.putQuick(value, id); } } RUNTIME_CONTEXTS.get().isFirstLevelResult = true; return id; }
/** * Positive test for getID() method. * * @throws Exception never otherwise the test fails. */ @Test public void getIDPreviouslySet() throws Exception { final String aValue = randomString(); final byte[] id = new byte[PersistentStringDictionary.ID_LENGTH]; RANDOMIZER.nextBytes(id); id[0] = (byte) (IDictionary.NOT_SET[0] + 1); when(_dummyIndex.get(aValue)).thenReturn(id); byte[] result = _cut.getID(aValue, RANDOMIZER.nextBoolean()); assertArrayEquals(id, result); verify(_dummyIndex).get(aValue); }
/** * Positive test for getID() method. * * @throws Exception never otherwise the test fails. */ @Test public void getIDNotPreviouslySet() throws Exception { when(_dummyIndex.contains(any(byte[].class))).thenReturn(false); final String aValue = randomString(); when(_dummyIndex.get(aValue)).thenReturn(IDictionary.NOT_SET); byte[] result = _cut.getID(aValue, RANDOMIZER.nextBoolean()); assertEquals(PersistentStringDictionary.ID_LENGTH, result.length); verify(_dummyIndex).get(aValue); verify(_dummyIndex).putQuick(aValue, result); }
/** * get method should use the "ByValue" index. * * @throws Exception never otherwise the test fails. */ @Test public void get() throws Exception { final String value = randomString(); _cut.get(value); verify(_byValue).get(value); }
/** * 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); } }