com.alachisoft.ncache.client.internal.communication.DistributedPartitioningStrategy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ncache-professional-client Show documentation
Show all versions of ncache-professional-client Show documentation
NCache Professional client for java.
package com.alachisoft.ncache.client.internal.communication;
import Alachisoft.NCache.Common.Extensibility.Client.RPC.Partition;
import Alachisoft.NCache.Common.Extensibility.Client.RPC.PartitioningStrategy;
import Alachisoft.NCache.Common.Net.Address;
class DistributedPartitioningStrategy extends PartitioningStrategy {
private Broker _broker;
private java.util.HashMap _partitionMap = new java.util.HashMap();
public DistributedPartitioningStrategy(Broker broker) {
this._broker = broker;
}
@Override
public Iterable GetAllPartitions() {
_broker._hashMapStatus.WaitForAny(HashMapStatus.INITIALIZE);
return _broker.getPool().getPartitins();
}
@Override
public Partition GetDestinationPartition(String cacheKey) {
_broker._hashMapStatus.WaitForAny(HashMapStatus.INITIALIZE);
Address server = _broker.getPool().GetIp(cacheKey);
Partition partition = null;
//we are caching partition to avoid new object creation. This map does not represent actual current distribution and
//servers as a cache only
if (!((partition = _partitionMap.get(server)) != null)) {
partition.setAddress(server);
synchronized (_partitionMap) {
if (!_partitionMap.containsKey(server)) {
_partitionMap.put(server, partition);
}
}
}
return partition;
}
@Override
public boolean PartitionExists(Partition partition) {
java.util.ArrayList pool = _broker.getPool().getPartitins();
for (int i = 0; i < pool.size(); i++) {
if (pool.get(i).getAddress().getIpAddress().toString().equals(partition.getAddress().getIpAddress().toString())) {
return true;
}
}
return false;
}
}