All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vlingo.lattice.grid.hashring.MD5ListHashRing Maven / Gradle / Ivy

Go to download

Tooling for reactive Domain-Driven Design projects that are highly concurrent. Includes compute grid, actor caching, spaces, cross-node cluster messaging, CQRS, and Event Sourcing support.

There is a newer version: 1.7.5
Show newest version
// Copyright © 2012-2020 VLINGO LABS. All rights reserved.
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL
// was not distributed with this file, You can obtain
// one at https://mozilla.org/MPL/2.0/.

package io.vlingo.lattice.grid.hashring;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ListIterator;
import java.util.function.BiFunction;

public class MD5ListHashRing extends MD5HashRing {
  private final List> hashedNodePoints;

  public MD5ListHashRing(final int pointsPerNode, final BiFunction> factory) throws Exception {
    super(pointsPerNode, factory);

    this.hashedNodePoints = new ArrayList<>();
  }

  @Override
  public void dump() {
    System.out.println("NODES: " + hashedNodePoints.size());
    for (final HashedNodePoint hashedNodePoint : hashedNodePoints) {
      System.out.println("NODE: " + hashedNodePoint);
    }
  }

  @Override
  public HashRing excludeNode(final T nodeIdentifier) {
    final ListIterator> iterator = hashedNodePoints.listIterator();
    int element = 0;
    int hash = hashed(nodeIdentifier.toString() + element);
    while (iterator.hasNext()) {
      final HashedNodePoint hashedNodePoint = iterator.next();
      if (hashedNodePoint.hash() == hash) {
        hash = hashed(nodeIdentifier.toString() + (++element));
      } else {
        iterator.remove();
        hashedNodePoint.excluded();
      }
    }
    return this;
  }

  @Override
  public HashRing includeNode(final T nodeIdentifier) {
    for (int element = 0; element < pointsPerNode; ++element) {
      hasher.reset();
      hasher.update(StandardCharsets.UTF_8.encode(nodeIdentifier.toString() + element));
      final int hash = Arrays.hashCode(hasher.digest());
      final HashedNodePoint hashedNodePoint = factory.apply(hash, nodeIdentifier);
      hashedNodePoints.add(hashedNodePoint);
      hashedNodePoint.included();
    }
    Collections.sort(hashedNodePoints, Comparator.comparingInt(HashedIdentity::hash));

    return this;
  }

  @Override
  public T nodeOf(final Object id) {
    final HashedNodePoint hashedNodePoint = hashedNodePointOf(id);
    int index = Collections.binarySearch(hashedNodePoints, hashedNodePoint, Comparator.comparingInt(HashedIdentity::hash));
    if (index < 0) {
      index = -index;
      if (index >= hashedNodePoints.size()) {
        index = 0;
      }
    }
    return hashedNodePoints.get(index).nodeIdentifier;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy