use of com.alipay.sofa.jraft.rhea.metadata.Region in project sofa-jraft by sofastack.
the class RegionRouteTable method findRegionsByKeys.
/**
* Returns the list of regions to which the keys belongs.
*/
public Map<Region, List<byte[]>> findRegionsByKeys(final List<byte[]> keys) {
Requires.requireNonNull(keys, "keys");
final Map<Region, List<byte[]>> regionMap = Maps.newHashMap();
final StampedLock stampedLock = this.stampedLock;
final long stamp = stampedLock.readLock();
try {
for (final byte[] key : keys) {
final Region region = findRegionByKeyWithoutLock(key);
regionMap.computeIfAbsent(region, k -> Lists.newArrayList()).add(key);
}
return regionMap;
} finally {
stampedLock.unlockRead(stamp);
}
}
use of com.alipay.sofa.jraft.rhea.metadata.Region in project sofa-jraft by sofastack.
the class RegionRouteTable method getRegionById.
public Region getRegionById(final long regionId) {
final StampedLock stampedLock = this.stampedLock;
long stamp = stampedLock.tryOptimisticRead();
// validate() emit a load-fence, but no store-fence. So you should only have
// load instructions inside a block of tryOptimisticRead() / validate(),
// because it is meant to the a read-only operation, and therefore, it is fine
// to use the loadFence() function to avoid re-ordering.
Region region = safeCopy(this.regionTable.get(regionId));
if (!stampedLock.validate(stamp)) {
stamp = stampedLock.readLock();
try {
region = safeCopy(this.regionTable.get(regionId));
} finally {
stampedLock.unlockRead(stamp);
}
}
return region;
}
use of com.alipay.sofa.jraft.rhea.metadata.Region in project sofa-jraft by sofastack.
the class RegionRouteTable method findRegionsByKeyRange.
/**
* Returns the list of regions covered by startKey and endKey.
*/
public List<Region> findRegionsByKeyRange(final byte[] startKey, final byte[] endKey) {
final StampedLock stampedLock = this.stampedLock;
final long stamp = stampedLock.readLock();
try {
final byte[] realStartKey = BytesUtil.nullToEmpty(startKey);
final NavigableMap<byte[], Long> subRegionMap;
if (endKey == null) {
subRegionMap = this.rangeTable.tailMap(realStartKey, false);
} else {
subRegionMap = this.rangeTable.subMap(realStartKey, false, endKey, true);
}
final List<Region> regionList = Lists.newArrayListWithCapacity(subRegionMap.size() + 1);
final Map.Entry<byte[], Long> headEntry = this.rangeTable.floorEntry(realStartKey);
if (headEntry == null) {
reportFail(startKey);
throw reject(startKey, "fail to find region by startKey");
}
regionList.add(safeCopy(this.regionTable.get(headEntry.getValue())));
for (final Long regionId : subRegionMap.values()) {
regionList.add(safeCopy(this.regionTable.get(regionId)));
}
return regionList;
} finally {
stampedLock.unlockRead(stamp);
}
}
use of com.alipay.sofa.jraft.rhea.metadata.Region in project sofa-jraft by sofastack.
the class RegionRouteTable method removeRegion.
public boolean removeRegion(final long regionId) {
final StampedLock stampedLock = this.stampedLock;
final long stamp = stampedLock.writeLock();
try {
final Region region = this.regionTable.remove(regionId);
if (region != null) {
final byte[] startKey = BytesUtil.nullToEmpty(region.getStartKey());
return this.rangeTable.remove(startKey) != null;
}
} finally {
stampedLock.unlockWrite(stamp);
}
return false;
}
use of com.alipay.sofa.jraft.rhea.metadata.Region in project sofa-jraft by sofastack.
the class RegionRouteTable method findRegionsByKvEntries.
/**
* Returns the list of regions to which the keys belongs.
*/
public Map<Region, List<KVEntry>> findRegionsByKvEntries(final List<KVEntry> kvEntries) {
Requires.requireNonNull(kvEntries, "kvEntries");
final Map<Region, List<KVEntry>> regionMap = Maps.newHashMap();
final StampedLock stampedLock = this.stampedLock;
final long stamp = stampedLock.readLock();
try {
for (final KVEntry kvEntry : kvEntries) {
final Region region = findRegionByKeyWithoutLock(kvEntry.getKey());
regionMap.computeIfAbsent(region, k -> Lists.newArrayList()).add(kvEntry);
}
return regionMap;
} finally {
stampedLock.unlockRead(stamp);
}
}
Aggregations