Search in sources :

Example 16 with IdMapper

use of org.neo4j.unsafe.impl.batchimport.cache.idmapping.IdMapper in project neo4j by neo4j.

the class EncodingIdMapperTest method shouldCopeWithMixedActualAndAccidentalCollisions.

@Test
public void shouldCopeWithMixedActualAndAccidentalCollisions() throws Exception {
    // GIVEN
    Monitor monitor = mock(Monitor.class);
    Encoder encoder = mock(Encoder.class);
    // Create these explicit instances so that we can use them in mock, even for same values
    String a = new String("a");
    String b = new String("b");
    String c = new String("c");
    String a2 = new String("a");
    String e = new String("e");
    String f = new String("f");
    when(encoder.encode(a)).thenReturn(1L);
    when(encoder.encode(b)).thenReturn(1L);
    when(encoder.encode(c)).thenReturn(3L);
    when(encoder.encode(a2)).thenReturn(1L);
    when(encoder.encode(e)).thenReturn(2L);
    when(encoder.encode(f)).thenReturn(1L);
    IdMapper mapper = mapper(encoder, Radix.STRING, monitor);
    InputIterable<Object> ids = wrap("source", Arrays.<Object>asList("a", "b", "c", "a", "e", "f"));
    Group.Adapter groupA = new Group.Adapter(1, "A");
    Group.Adapter groupB = new Group.Adapter(2, "B");
    Group[] groups = new Group[] { groupA, groupA, groupA, groupB, groupB, groupB };
    // WHEN
    try (ResourceIterator<Object> iterator = ids.iterator()) {
        for (int i = 0; iterator.hasNext(); i++) {
            mapper.put(iterator.next(), i, groups[i]);
        }
    }
    Collector collector = mock(Collector.class);
    mapper.prepare(ids, collector, mock(ProgressListener.class));
    // THEN
    verify(monitor).numberOfCollisions(4);
    assertEquals(0L, mapper.get(a, groupA));
    assertEquals(1L, mapper.get(b, groupA));
    assertEquals(2L, mapper.get(c, groupA));
    assertEquals(3L, mapper.get(a2, groupB));
    assertEquals(4L, mapper.get(e, groupB));
    assertEquals(5L, mapper.get(f, groupB));
}
Also used : Group(org.neo4j.unsafe.impl.batchimport.input.Group) IdMapper(org.neo4j.unsafe.impl.batchimport.cache.idmapping.IdMapper) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) Monitor(org.neo4j.unsafe.impl.batchimport.cache.idmapping.string.EncodingIdMapper.Monitor) ProgressListener(org.neo4j.helpers.progress.ProgressListener) Collector(org.neo4j.unsafe.impl.batchimport.input.Collector) Collectors.badCollector(org.neo4j.unsafe.impl.batchimport.input.Collectors.badCollector) Test(org.junit.Test)

Example 17 with IdMapper

use of org.neo4j.unsafe.impl.batchimport.cache.idmapping.IdMapper in project neo4j by neo4j.

the class EncodingIdMapperTest method shouldReturnExpectedValueForNotFound.

@Test
public void shouldReturnExpectedValueForNotFound() throws Exception {
    // GIVEN
    IdMapper idMapper = mapper(new StringEncoder(), Radix.STRING, NO_MONITOR);
    idMapper.prepare(null, mock(Collector.class), NONE);
    // WHEN
    long id = idMapper.get("123", GLOBAL);
    // THEN
    assertEquals(ID_NOT_FOUND, id);
}
Also used : Collector(org.neo4j.unsafe.impl.batchimport.input.Collector) Collectors.badCollector(org.neo4j.unsafe.impl.batchimport.input.Collectors.badCollector) IdMapper(org.neo4j.unsafe.impl.batchimport.cache.idmapping.IdMapper) Test(org.junit.Test)

Aggregations

IdMapper (org.neo4j.unsafe.impl.batchimport.cache.idmapping.IdMapper)17 Test (org.junit.Test)16 Collector (org.neo4j.unsafe.impl.batchimport.input.Collector)15 Collectors.badCollector (org.neo4j.unsafe.impl.batchimport.input.Collectors.badCollector)14 Group (org.neo4j.unsafe.impl.batchimport.input.Group)6 ArrayList (java.util.ArrayList)3 ProgressListener (org.neo4j.helpers.progress.ProgressListener)3 Monitor (org.neo4j.unsafe.impl.batchimport.cache.idmapping.string.EncodingIdMapper.Monitor)3 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Matchers.anyString (org.mockito.Matchers.anyString)2 PrimitiveLongIterator (org.neo4j.collection.primitive.PrimitiveLongIterator)2 Groups (org.neo4j.unsafe.impl.batchimport.input.Groups)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Matchers.anyLong (org.mockito.Matchers.anyLong)1 ResourceIterator (org.neo4j.graphdb.ResourceIterator)1 PrefetchingIterator (org.neo4j.helpers.collection.PrefetchingIterator)1 CountsAccessor (org.neo4j.kernel.impl.api.CountsAccessor)1