public final RangeMapping merge()

in elastic-db-tools/src/main/java/com/microsoft/azure/elasticdb/shard/mapper/RangeShardMapper.java [315:365]


    public final RangeMapping merge(RangeMapping left,
            RangeMapping right,
            UUID leftLockOwnerId,
            UUID rightLockOwnerId) {
        this.ensureMappingBelongsToShardMap(left, "Merge", "left");
        this.ensureMappingBelongsToShardMap(right, "Merge", "right");

        if (!left.getShard().getLocation().equals(right.getShard().getLocation())) {
            throw new IllegalArgumentException(StringUtilsLocal.formatInvariant(Errors._ShardMapping_MergeDifferentShards,
                    this.getShardMap().getName(), left.getShard().getLocation(), right.getShard().getLocation()));
        }

        if (left.getRange().intersects(right.getRange()) || ShardKey.opInequality(left.getRange().getHigh(), right.getRange().getLow())) {
            throw new IllegalArgumentException(Errors._ShardMapping_MergeNotAdjacent);
        }

        if (left.getStatus() != right.getStatus()) {
            throw new IllegalArgumentException(StringUtilsLocal.formatInvariant(Errors._ShardMapping_DifferentStatus, this.getShardMap().getName()));
        }

        StoreShard ss = left.getShard().getStoreShard();
        StoreShard newShard = new StoreShard(ss.getId(), UUID.randomUUID(), ss.getShardMapId(), ss.getLocation(), ss.getStatus());

        StoreMapping leftMap = left.getStoreMapping();
        StoreMapping mappingToRemoveLeft = new StoreMapping(leftMap.getId(), leftMap.getShardMapId(), leftMap.getMinValue(), leftMap.getMaxValue(),
                leftMap.getStatus(), leftMap.getLockOwnerId(), newShard);

        StoreMapping rightMap = right.getStoreMapping();
        StoreMapping mappingToRemoveRight = new StoreMapping(rightMap.getId(), rightMap.getShardMapId(), rightMap.getMinValue(),
                rightMap.getMaxValue(), rightMap.getStatus(), rightMap.getLockOwnerId(), newShard);

        StoreMapping mappingToAdd = new StoreMapping(UUID.randomUUID(), newShard.getShardMapId(), left.getRange().getLow().getRawValue(),
                right.getRange().getHigh().getRawValue(), left.getStatus().getValue(), leftLockOwnerId, newShard);

        List<Pair<StoreMapping, UUID>> listPairRemove = new ArrayList<>();
        listPairRemove.add(new ImmutablePair<>(mappingToRemoveLeft, leftLockOwnerId));
        listPairRemove.add(new ImmutablePair<>(mappingToRemoveRight, rightLockOwnerId));

        List<Pair<StoreMapping, UUID>> listPairAdd = new ArrayList<>();
        listPairAdd.add(new ImmutablePair<>(mappingToAdd, leftLockOwnerId));

        try (IStoreOperation op = this.shardMapManager.getStoreOperationFactory().createReplaceMappingsOperation(this.shardMapManager,
                StoreOperationCode.MergeMappings, this.shardMap.getStoreShardMap(), listPairRemove, listPairAdd)) {
            op.doOperation();
        }
        catch (Exception e) {
            ExceptionUtils.throwStronglyTypedException(e);
        }

        return new RangeMapping(this.shardMapManager, this.shardMap, mappingToAdd);
    }