Search in sources :

Example 41 with SimpleEntry

use of java.util.AbstractMap.SimpleEntry in project chassis by Kixeye.

the class ConfigurationBuilderTest method zookeeperOverridesApplicationProperties.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void zookeeperOverridesApplicationProperties() throws Exception {
    String key1 = UUID.randomUUID().toString();
    String value1 = UUID.randomUUID().toString();
    String key2 = UUID.randomUUID().toString();
    String value2 = UUID.randomUUID().toString();
    TestUtils.writePropertiesToFile(TEST_APP_CONFIG_PROPERTIES, filesCreated, new SimpleEntry(key1, value1), new SimpleEntry(key2, value2));
    curatorFramework.create().creatingParentsIfNeeded().forPath(ZOOKEEPER_CONFIG_ROOT + "/" + key1, "zookeepervalue".getBytes());
    configurationBuilder.withConfigurationProvider(new ZookeeperConfigurationProvider(zookeeperServer.getConnectString()));
    configurationBuilder.withApplicationProperties(TEST_APP_CONFIG_PROPERTIES_SPRING_PATH);
    Configuration configuration = configurationBuilder.build();
    //assert that the zookeeper config overrode the app properties config
    Assert.assertEquals("zookeepervalue", configuration.getString(key1));
    //assert that the proper which exists in app properties but not in zookeeper is found.
    Assert.assertEquals(value2, configuration.getString(key2));
}
Also used : Configuration(org.apache.commons.configuration.Configuration) SimpleEntry(java.util.AbstractMap.SimpleEntry) ZookeeperConfigurationProvider(com.kixeye.chassis.bootstrap.configuration.zookeeper.ZookeeperConfigurationProvider) Test(org.junit.Test)

Example 42 with SimpleEntry

use of java.util.AbstractMap.SimpleEntry in project lucene-solr by apache.

the class TestInputIterator method testTerms.

public void testTerms() throws Exception {
    Random random = random();
    int num = atLeast(10000);
    TreeMap<BytesRef, SimpleEntry<Long, BytesRef>> sorted = new TreeMap<>();
    TreeMap<BytesRef, Long> sortedWithoutPayload = new TreeMap<>();
    TreeMap<BytesRef, SimpleEntry<Long, Set<BytesRef>>> sortedWithContext = new TreeMap<>();
    TreeMap<BytesRef, SimpleEntry<Long, SimpleEntry<BytesRef, Set<BytesRef>>>> sortedWithPayloadAndContext = new TreeMap<>();
    Input[] unsorted = new Input[num];
    Input[] unsortedWithoutPayload = new Input[num];
    Input[] unsortedWithContexts = new Input[num];
    Input[] unsortedWithPayloadAndContext = new Input[num];
    Set<BytesRef> ctxs;
    for (int i = 0; i < num; i++) {
        BytesRef key;
        BytesRef payload;
        ctxs = new HashSet<>();
        do {
            key = new BytesRef(TestUtil.randomUnicodeString(random));
            payload = new BytesRef(TestUtil.randomUnicodeString(random));
            for (int j = 0; j < atLeast(2); j++) {
                ctxs.add(new BytesRef(TestUtil.randomUnicodeString(random)));
            }
        } while (sorted.containsKey(key));
        long value = random.nextLong();
        sortedWithoutPayload.put(key, value);
        sorted.put(key, new SimpleEntry<>(value, payload));
        sortedWithContext.put(key, new SimpleEntry<>(value, ctxs));
        sortedWithPayloadAndContext.put(key, new SimpleEntry<>(value, new SimpleEntry<>(payload, ctxs)));
        unsorted[i] = new Input(key, value, payload);
        unsortedWithoutPayload[i] = new Input(key, value);
        unsortedWithContexts[i] = new Input(key, value, ctxs);
        unsortedWithPayloadAndContext[i] = new Input(key, value, payload, ctxs);
    }
    // test the sorted iterator wrapper with payloads
    try (Directory tempDir = getDirectory()) {
        InputIterator wrapper = new SortedInputIterator(tempDir, "sorted", new InputArrayIterator(unsorted));
        Iterator<Map.Entry<BytesRef, SimpleEntry<Long, BytesRef>>> expected = sorted.entrySet().iterator();
        while (expected.hasNext()) {
            Map.Entry<BytesRef, SimpleEntry<Long, BytesRef>> entry = expected.next();
            assertEquals(entry.getKey(), wrapper.next());
            assertEquals(entry.getValue().getKey().longValue(), wrapper.weight());
            assertEquals(entry.getValue().getValue(), wrapper.payload());
        }
        assertNull(wrapper.next());
    }
    // test the sorted iterator wrapper with contexts
    try (Directory tempDir = getDirectory()) {
        InputIterator wrapper = new SortedInputIterator(tempDir, "sorted", new InputArrayIterator(unsortedWithContexts));
        Iterator<Map.Entry<BytesRef, SimpleEntry<Long, Set<BytesRef>>>> actualEntries = sortedWithContext.entrySet().iterator();
        while (actualEntries.hasNext()) {
            Map.Entry<BytesRef, SimpleEntry<Long, Set<BytesRef>>> entry = actualEntries.next();
            assertEquals(entry.getKey(), wrapper.next());
            assertEquals(entry.getValue().getKey().longValue(), wrapper.weight());
            Set<BytesRef> actualCtxs = entry.getValue().getValue();
            assertEquals(actualCtxs, wrapper.contexts());
        }
        assertNull(wrapper.next());
    }
    // test the sorted iterator wrapper with contexts and payload
    try (Directory tempDir = getDirectory()) {
        InputIterator wrapper = new SortedInputIterator(tempDir, "sorter", new InputArrayIterator(unsortedWithPayloadAndContext));
        Iterator<Map.Entry<BytesRef, SimpleEntry<Long, SimpleEntry<BytesRef, Set<BytesRef>>>>> expectedPayloadContextEntries = sortedWithPayloadAndContext.entrySet().iterator();
        while (expectedPayloadContextEntries.hasNext()) {
            Map.Entry<BytesRef, SimpleEntry<Long, SimpleEntry<BytesRef, Set<BytesRef>>>> entry = expectedPayloadContextEntries.next();
            assertEquals(entry.getKey(), wrapper.next());
            assertEquals(entry.getValue().getKey().longValue(), wrapper.weight());
            Set<BytesRef> actualCtxs = entry.getValue().getValue().getValue();
            assertEquals(actualCtxs, wrapper.contexts());
            BytesRef actualPayload = entry.getValue().getValue().getKey();
            assertEquals(actualPayload, wrapper.payload());
        }
        assertNull(wrapper.next());
    }
    // test the unsorted iterator wrapper with payloads
    InputIterator wrapper = new UnsortedInputIterator(new InputArrayIterator(unsorted));
    TreeMap<BytesRef, SimpleEntry<Long, BytesRef>> actual = new TreeMap<>();
    BytesRef key;
    while ((key = wrapper.next()) != null) {
        long value = wrapper.weight();
        BytesRef payload = wrapper.payload();
        actual.put(BytesRef.deepCopyOf(key), new SimpleEntry<>(value, BytesRef.deepCopyOf(payload)));
    }
    assertEquals(sorted, actual);
    // test the sorted iterator wrapper without payloads
    try (Directory tempDir = getDirectory()) {
        InputIterator wrapperWithoutPayload = new SortedInputIterator(tempDir, "sorted", new InputArrayIterator(unsortedWithoutPayload));
        Iterator<Map.Entry<BytesRef, Long>> expectedWithoutPayload = sortedWithoutPayload.entrySet().iterator();
        while (expectedWithoutPayload.hasNext()) {
            Map.Entry<BytesRef, Long> entry = expectedWithoutPayload.next();
            assertEquals(entry.getKey(), wrapperWithoutPayload.next());
            assertEquals(entry.getValue().longValue(), wrapperWithoutPayload.weight());
            assertNull(wrapperWithoutPayload.payload());
        }
        assertNull(wrapperWithoutPayload.next());
    }
    // test the unsorted iterator wrapper without payloads
    InputIterator wrapperWithoutPayload = new UnsortedInputIterator(new InputArrayIterator(unsortedWithoutPayload));
    TreeMap<BytesRef, Long> actualWithoutPayload = new TreeMap<>();
    while ((key = wrapperWithoutPayload.next()) != null) {
        long value = wrapperWithoutPayload.weight();
        assertNull(wrapperWithoutPayload.payload());
        actualWithoutPayload.put(BytesRef.deepCopyOf(key), value);
    }
    assertEquals(sortedWithoutPayload, actualWithoutPayload);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) SimpleEntry(java.util.AbstractMap.SimpleEntry) Random(java.util.Random) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory) SimpleEntry(java.util.AbstractMap.SimpleEntry) TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 43 with SimpleEntry

use of java.util.AbstractMap.SimpleEntry in project BKCommonLib by bergerhealer.

the class CommonTag method nbtToCommon.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected static Object nbtToCommon(Object data, boolean wrapData) {
    if (NMSNBT.Base.T.isInstance(data)) {
        return create(data);
    } else if (data instanceof CommonTag || data == null) {
        return data;
    } else if (data instanceof Entry) {
        Entry old = (Entry) data;
        // Replace
        Object newKey = nbtToCommon(old.getKey(), wrapData);
        Object newValue = nbtToCommon(old.getValue(), wrapData);
        // If changed, return new type
        if (newKey != old.getKey() || newValue != old.getValue()) {
            return new SimpleEntry(newKey, newValue);
        } else {
            return data;
        }
    } else if (data instanceof Set) {
        Set<Object> elems = (Set<Object>) data;
        HashSet<Object> tags = new HashSet<Object>(elems.size());
        // Replace
        for (Object value : elems) {
            tags.add(nbtToCommon(value, wrapData));
        }
        return tags;
    } else if (data instanceof Map) {
        Map<String, Object> elems = (Map<String, Object>) data;
        HashMap<String, Object> tags = new HashMap<String, Object>(elems.size());
        // Replace
        for (Entry<String, Object> entry : elems.entrySet()) {
            tags.put(entry.getKey(), nbtToCommon(entry.getValue(), wrapData));
        }
        return tags;
    } else if (data instanceof Collection) {
        Collection<Object> elems = (Collection<Object>) data;
        ArrayList<Object> tags = new ArrayList<Object>(elems.size());
        // Replace
        for (Object value : elems) {
            tags.add(nbtToCommon(value, wrapData));
        }
        return tags;
    } else if (wrapData) {
        return createForData(data);
    } else {
        return data;
    }
}
Also used : SimpleEntry(java.util.AbstractMap.SimpleEntry) Entry(java.util.Map.Entry) SimpleEntry(java.util.AbstractMap.SimpleEntry)

Example 44 with SimpleEntry

use of java.util.AbstractMap.SimpleEntry in project BKCommonLib by bergerhealer.

the class CommonTag method commonToNbt.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected static Object commonToNbt(Object data) {
    if (data instanceof CommonTag) {
        return ((CommonTag) data).getRawHandle();
    } else if (NMSNBT.Base.T.isInstance(data) || data == null) {
        return data;
    } else if (data instanceof Entry) {
        Entry old = (Entry) data;
        // Replace
        Object newKey = commonToNbt(old.getKey());
        Object newValue = commonToNbt(old.getValue());
        // If changed, return new type
        if (newKey != old.getKey() || newValue != old.getValue()) {
            return new SimpleEntry(newKey, newValue);
        } else {
            return data;
        }
    } else if (data instanceof Set) {
        Set<Object> elems = (Set<Object>) data;
        HashSet<Object> tags = new HashSet<Object>(elems.size());
        // Replace
        for (Object value : elems) {
            tags.add(commonToNbt(value));
        }
        return tags;
    } else if (data instanceof Map) {
        Map<String, Object> elems = (Map<String, Object>) data;
        HashMap<String, Object> tags = new HashMap<String, Object>(elems.size());
        // Replace
        for (Entry<String, Object> entry : elems.entrySet()) {
            Object value = commonToNbt(entry.getValue());
            tags.put(entry.getKey(), value);
        }
        return tags;
    } else if (data instanceof Collection) {
        Collection<Object> elems = (Collection<Object>) data;
        ArrayList<Object> tags = new ArrayList<Object>(elems.size());
        // Replace
        for (Object value : elems) {
            tags.add(commonToNbt(value));
        }
        return tags;
    } else {
        return NMSNBT.createHandle(data);
    }
}
Also used : SimpleEntry(java.util.AbstractMap.SimpleEntry) Entry(java.util.Map.Entry) SimpleEntry(java.util.AbstractMap.SimpleEntry)

Example 45 with SimpleEntry

use of java.util.AbstractMap.SimpleEntry in project ddf by codice.

the class SeedCommand method executeWithSubject.

@Override
protected Object executeWithSubject() throws Exception {
    if (resourceLimit <= 0) {
        printErrorMessage("A limit of " + resourceLimit + " was supplied. The limit must be greater than 0.");
        return null;
    }
    final long start = System.currentTimeMillis();
    int resourceDownloads = 0;
    int downloadErrors = 0;
    int pageCount = 0;
    while (resourceDownloads < resourceLimit) {
        final QueryImpl query = new QueryImpl(getFilter());
        query.setPageSize(resourceLimit);
        query.setStartIndex(pageCount * resourceLimit + 1);
        ++pageCount;
        final QueryRequestImpl queryRequest;
        if (isNotEmpty(sources)) {
            queryRequest = new QueryRequestImpl(query, sources);
        } else {
            queryRequest = new QueryRequestImpl(query, true);
        }
        queryRequest.setProperties(new HashMap<>(CACHE_UPDATE_PROPERTIES));
        final QueryResponse queryResponse = catalogFramework.query(queryRequest);
        if (queryResponse.getResults().isEmpty()) {
            break;
        }
        final List<Entry<? extends ResourceRequest, String>> resourceRequests = queryResponse.getResults().stream().map(Result::getMetacard).filter(isNonCachedResource).map(metacard -> new SimpleEntry<>(new ResourceRequestById(metacard.getId()), metacard.getSourceId())).collect(toList());
        for (Entry<? extends ResourceRequest, String> requestAndSourceId : resourceRequests) {
            final ResourceRequest request = requestAndSourceId.getKey();
            try {
                ResourceResponse response = catalogFramework.getResource(request, requestAndSourceId.getValue());
                ++resourceDownloads;
                EXECUTOR.execute(new ResourceCloseHandler(response));
            } catch (IOException | ResourceNotFoundException | ResourceNotSupportedException e) {
                ++downloadErrors;
                LOGGER.debug("Could not download resource for metacard [id={}]", request.getAttributeValue(), e);
            }
            printProgressAndFlush(start, resourceLimit, resourceDownloads);
            if (resourceDownloads == resourceLimit) {
                break;
            }
        }
    }
    printProgressAndFlush(start, resourceDownloads, resourceDownloads);
    console.println();
    if (downloadErrors > 0) {
        printErrorMessage(downloadErrors + " resource download(s) had errors. Check the logs for details.");
    }
    printSuccessMessage("Done seeding. " + resourceDownloads + " resource download(s) started.");
    return null;
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Command(org.apache.karaf.shell.api.action.Command) Metacard(ddf.catalog.data.Metacard) Map(java.util.Map) ResourceRequest(ddf.catalog.operation.ResourceRequest) SimpleEntry(java.util.AbstractMap.SimpleEntry) Result(ddf.catalog.data.Result) CollectionUtils.isNotEmpty(org.apache.commons.collections.CollectionUtils.isNotEmpty) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) IOException(java.io.IOException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Serializable(java.io.Serializable) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) TimeUnit(java.util.concurrent.TimeUnit) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) QueryResponse(ddf.catalog.operation.QueryResponse) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Attribute(ddf.catalog.data.Attribute) Entry(java.util.Map.Entry) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Option(org.apache.karaf.shell.api.action.Option) Collections(java.util.Collections) InputStream(java.io.InputStream) SimpleEntry(java.util.AbstractMap.SimpleEntry) IOException(java.io.IOException) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SimpleEntry(java.util.AbstractMap.SimpleEntry) Entry(java.util.Map.Entry) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) ResourceRequest(ddf.catalog.operation.ResourceRequest) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Aggregations

SimpleEntry (java.util.AbstractMap.SimpleEntry)47 Test (org.junit.Test)24 ArrayList (java.util.ArrayList)21 HashMap (java.util.HashMap)15 CucumberFeature (cucumber.runtime.model.CucumberFeature)10 Result (gherkin.formatter.model.Result)10 Entry (java.util.Map.Entry)10 HashSet (java.util.HashSet)9 Map (java.util.Map)8 Metacard (ddf.catalog.data.Metacard)7 Serializable (java.io.Serializable)7 HazelcastInstance (com.hazelcast.core.HazelcastInstance)6 List (java.util.List)6 UpdateRequest (ddf.catalog.operation.UpdateRequest)5 Set (java.util.Set)5 Configuration (org.apache.commons.configuration.Configuration)5 ZookeeperConfigurationProvider (com.kixeye.chassis.bootstrap.configuration.zookeeper.ZookeeperConfigurationProvider)4 LinkedList (java.util.LinkedList)4 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)3 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)3