com.splout.db.common.ReplicationMap Maven / Gradle / Ivy
Show all versions of splout-commons Show documentation
package com.splout.db.common;
/*
* #%L
* Splout SQL commons
* %%
* Copyright (C) 2012 Datasalt Systems S.L.
* %%
* 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.
* #L%
*/
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* A Splout's replication map definition.
*
* A replication map is made up by a list of {@link ReplicationEntry}. Each of them specifies the list of addresses
* (nodes) that hold the data for a shard. It is understood that if one {@link ReplicationEntry} has more than one node
* then they will all hold the same data.
*/
@SuppressWarnings("serial")
public class ReplicationMap implements Serializable {
public String toString() {
return replicationEntries.toString();
}
List replicationEntries;
public ReplicationMap() {
}
public ReplicationMap(List replicationMap) {
this.replicationEntries = replicationMap;
}
public static ReplicationMap roundRobinMap(int nPartitions, int repFactor, String... hosts) {
List repEntries = new ArrayList();
for(int i = 0; i < nPartitions; i++) {
int toNode = i % hosts.length;
ReplicationEntry repEntry = new ReplicationEntry();
repEntry.setShard(i);
List shardDNodes = new ArrayList();
for(int j = 0; j < repFactor; j++) {
toNode = (toNode + j) % hosts.length;
shardDNodes.add(hosts[toNode]);
}
repEntry.setNodes(shardDNodes);
repEntries.add(repEntry);
}
return new ReplicationMap(repEntries);
}
/**
* Returns a one to one replication map with consecutive shard Ids from a list of hosts. This method is useful for
* quickly having an instance of ReplicationMap that has no replication (1 shard -> 1 host).
*/
public static ReplicationMap oneToOneMap(String... hosts) {
List replicationMap = new ArrayList();
int shard = 0;
for(String host : hosts) {
ReplicationEntry entry = new ReplicationEntry();
entry.setNodes(Arrays.asList(new String[] { host }));
entry.setShard(shard);
replicationMap.add(entry);
shard++;
}
return new ReplicationMap(replicationMap);
}
public List getReplicationEntries() {
return replicationEntries;
}
public void setReplicationEntries(List replicationEntries) {
this.replicationEntries = replicationEntries;
}
}