Search in sources :

Example 1 with AliasEntry

use of org.summerb.properties.impl.dao.AliasEntry in project summerb by skarpushin.

the class StringIdAliasServiceEagerImplFactory method createDaoMock.

private static StringIdAliasDao createDaoMock() {
    StringIdAliasDao ret = Mockito.mock(StringIdAliasDao.class);
    when(ret.createAliasFor(NAME)).thenReturn(NAME_ALIAS);
    when(ret.findAliasFor(NAME)).thenReturn(NAME_ALIAS);
    when(ret.loadAllAliases(any(PagerParams.class))).thenAnswer(new Answer<PaginatedList<Entry<String, Long>>>() {

        @Override
        public PaginatedList<Entry<String, Long>> answer(InvocationOnMock invocation) throws Throwable {
            PagerParams pagerParams = (PagerParams) invocation.getArguments()[0];
            // Synthetic pause, simulate
            Thread.sleep(250);
            PaginatedList<Entry<String, Long>> result = new PaginatedList<Entry<String, Long>>();
            result.setPagerParams(pagerParams);
            result.setTotalResults(150);
            ArrayList<Entry<String, Long>> items = new ArrayList<Entry<String, Long>>();
            result.setItems(items);
            long offset = pagerParams.getOffset();
            long max = -1;
            if (offset == 0) {
                max = 100;
            } else if (offset == 100) {
                max = 50;
            } else {
                fail();
            }
            for (long i = offset; i < max; i++) {
                items.add(new AliasEntry("str" + i, i));
            }
            return result;
        }
    });
    return ret;
}
Also used : PagerParams(org.summerb.easycrud.api.dto.PagerParams) ArrayList(java.util.ArrayList) StringIdAliasDao(org.summerb.properties.impl.dao.StringIdAliasDao) AliasEntry(org.summerb.properties.impl.dao.AliasEntry) Entry(java.util.Map.Entry) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PaginatedList(org.summerb.easycrud.api.dto.PaginatedList) AliasEntry(org.summerb.properties.impl.dao.AliasEntry)

Example 2 with AliasEntry

use of org.summerb.properties.impl.dao.AliasEntry in project summerb by skarpushin.

the class StringIdAliasServiceEagerImpl method loadAllAliases.

protected final BiMap<String, Long> loadAllAliases() {
    try {
        BiMap<String, Long> ret = HashBiMap.create();
        long maxOffset = -2;
        for (long offset = 0; maxOffset == -2 || offset < maxOffset; offset = offset + EAGER_LOAD_BATCH_SIZE) {
            PagerParams pagerParams = new PagerParams(offset, EAGER_LOAD_BATCH_SIZE);
            PaginatedList<AliasEntry> loadedAliases = stringIdAliasDao.loadAllAliases(pagerParams);
            maxOffset = loadedAliases.getTotalResults() - 1;
            for (Entry<String, Long> entry : loadedAliases.getItems()) {
                ret.put(entry.getKey(), entry.getValue());
            }
        }
        return ret;
    } catch (Throwable t) {
        String msg = "Failed to eagerly load alias map";
        log.error(msg, t);
        throw new PropertyServiceUnexpectedException(msg, t);
    }
}
Also used : PagerParams(org.summerb.easycrud.api.dto.PagerParams) PropertyServiceUnexpectedException(org.summerb.properties.api.exceptions.PropertyServiceUnexpectedException) AliasEntry(org.summerb.properties.impl.dao.AliasEntry)

Aggregations

PagerParams (org.summerb.easycrud.api.dto.PagerParams)2 AliasEntry (org.summerb.properties.impl.dao.AliasEntry)2 ArrayList (java.util.ArrayList)1 Entry (java.util.Map.Entry)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 PaginatedList (org.summerb.easycrud.api.dto.PaginatedList)1 PropertyServiceUnexpectedException (org.summerb.properties.api.exceptions.PropertyServiceUnexpectedException)1 StringIdAliasDao (org.summerb.properties.impl.dao.StringIdAliasDao)1