<commit_before>/*
 * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.openhft.chronicle.map;

import net.openhft.chronicle.hash.ChronicleHash;
import net.openhft.chronicle.hash.ChronicleHashInstanceConfig;
import net.openhft.chronicle.hash.FindByName;
import net.openhft.chronicle.hash.replication.ReplicationChannel;
import net.openhft.chronicle.hash.replication.ReplicationHub;
import net.openhft.lang.io.Bytes;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;

import static net.openhft.chronicle.map.ChronicleMapBuilder.of;

/**
 * @author <NAME>.
 */
class ReplicationHubFindByName implements FindByName {
    public static final Logger LOG = LoggerFactory.getLogger(ReplicationHubFindByName.class.getName());

    public static final int MAP_BY_NAME_CHANNEL = 1;

    private final AtomicInteger nextFreeChannel = new AtomicInteger(1);
    private final Map<String, ChronicleMapBuilderWithChannelId> map;
    private final ReplicationHub replicationHub;


    public static class ChronicleMapBuilderWithChannelId implements Serializable {
        ChronicleMapBuilder chronicleMapBuilder;
        int channelId;
    }

    /**
     * @throws IOException
     */
    public ReplicationHubFindByName(ReplicationHub replicationHub) throws IOException {


        LOG.info("connecting to replicationHub=" + replicationHub);

        this.replicationHub = replicationHub;
        ReplicationChannel channel = replicationHub.createChannel((short) MAP_BY_NAME_CHANNEL);


        MapEventListener listener = new MapEventListener() {

            @Override
            public void onPut(ChronicleMap map, Bytes entry, int metaDataBytes, boolean added, Object key, Object value, @Nullable Object replacedValue) {
                LOG.info("key=" + key);
            }
        };

        this.map = (Map) of(CharSequence.class, ChronicleMapBuilderWithChannelId.class)
                .entrySize(2500)
                .entries(128)
                .eventListener(listener)
                .instance()
                .replicatedViaChannel(channel)
                .create();
    }


    public <T extends ChronicleHash> T create(ChronicleMapBuilder builder) throws IllegalArgumentException,
            IOException, TimeoutException, InterruptedException {

        int withChannelId = nextFreeChannel.incrementAndGet();
        T result = (T) toReplicatedViaChannel(builder, withChannelId).create();
        ChronicleMapBuilderWithChannelId builderWithChannelId = new
                ChronicleMapBuilderWithChannelId();

        builderWithChannelId.channelId = withChannelId;
        builderWithChannelId.chronicleMapBuilder = builder;
        map.put(builder.name(), builderWithChannelId);<commit_msg>HCOLL-134 more work on node discovery<commit_after>/*
 * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.openhft.chronicle.map;

import net.openhft.chronicle.hash.ChronicleHash;
import net.openhft.chronicle.hash.ChronicleHashInstanceConfig;
import net.openhft.chronicle.hash.FindByName;
import net.openhft.chronicle.hash.replication.ReplicationChannel;
import net.openhft.chronicle.hash.replication.ReplicationHub;
import net.openhft.lang.io.Bytes;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;

import static net.openhft.chronicle.map.ChronicleMapBuilder.of;

/**
 * @author <NAME>.
 */
class ReplicationHubFindByName implements FindByName {
    public static final Logger LOG = LoggerFactory.getLogger(ReplicationHubFindByName.class.getName());

    public static final int MAP_BY_NAME_CHANNEL = 1;

    private final AtomicInteger nextFreeChannel = new AtomicInteger(1);
    private final Map<String, ChronicleMapBuilderWithChannelId> map;
    private final ReplicationHub replicationHub;


    public static class ChronicleMapBuilderWithChannelId implements Serializable {
        ChronicleMapBuilder chronicleMapBuilder;
        int channelId;
    }

    /**
     * @throws IOException
     */
    public ReplicationHubFindByName(ReplicationHub replicationHub) throws IOException {


        LOG.info("connecting to replicationHub=" + replicationHub);

        this.replicationHub = replicationHub;
        ReplicationChannel channel = replicationHub.createChannel((short) MAP_BY_NAME_CHANNEL);

        MapEventListener listener = new MapEventListener() {

            @Override
            public void onPut(ChronicleMap map, Bytes entry, int metaDataBytes, boolean added, Object key, Object value, @Nullable Object replacedValue) {
                LOG.info("key=" + key);
            }
        };

        this.map = (Map) of(CharSequence.class, ChronicleMapBuilderWithChannelId.class)
                .entrySize(2500)
                .entries(128)
                .eventListener(listener)
                .instance()
                .replicatedViaChannel(channel)
                .create();
    }


    public <T extends ChronicleHash> T create(ChronicleMapBuilder builder) throws IllegalArgumentException,
            IOException, TimeoutException, InterruptedException {

        int withChannelId = nextFreeChannel.incrementAndGet();
        T result = (T) toReplicatedViaChannel(builder, withChannelId).create();
        ChronicleMapBuilderWithChannelId builderWithChannelId = new
                ChronicleMapBuilderWithChannelId();

        builderWithChannelId.channelId = withChannelId;
        builderWithChannelId.chronicleMapBuilder = builder;
        map.put(builder.name(), builderWithChannelId);
{'fraction_non_alphanumeric': 0.07912860154602952, 'fraction_numerical': 0.005762473647224174, 'mean_word_length': 3.620779220779221, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3836174', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1813, 'n_words': 492}