Exemplo n.º 1
0
    def test_incomplete_routing_map(self):
        crm = _CollectionRoutingMap.CompleteRoutingMap([
            ({
                'id': "2",
                'minInclusive': "",
                'maxExclusive': "0000000030"
            }, 2),
            ({
                'id': "3",
                'minInclusive': "0000000031",
                'maxExclusive': "FF"
            }, 2),
        ], "")
        self.assertIsNone(crm)

        crm = _CollectionRoutingMap.CompleteRoutingMap([
            ({
                'id': "2",
                'minInclusive': "",
                'maxExclusive': "0000000030"
            }, 2),
            ({
                'id': "2",
                'minInclusive': "0000000030",
                'maxExclusive': "FF"
            }, 2),
        ], "")

        self.assertIsNotNone(crm)
    def get_overlapping_ranges(self, collection_link, partition_key_ranges):
        '''
        Given a partition key range and a collection, 
        returns the list of overlapping partition key ranges
        
        :Parameters:
            `collection_link` (str): the name of the collection
            `partition_key_range` (list): List of partition key range
        
        :Returns:
            list. List of overlapping partition key ranges
        '''
        cl = self._documentClient

        collection_id = base.GetResourceIdOrFullNameFromLink(collection_link)

        collection_routing_map = self._collection_routing_map_by_item.get(
            collection_id)
        if collection_routing_map is None:
            partitionKeyRanges = list(
                cl._ReadPartitionKeyRanges(collection_link))
            collection_routing_map = _CollectionRoutingMap.CompleteRoutingMap(
                [(r, True) for r in partitionKeyRanges], collection_id)
            self._collection_routing_map_by_item[
                collection_id] = collection_routing_map
        return collection_routing_map.get_overlapping_ranges(
            partition_key_ranges)
Exemplo n.º 3
0
    def test_advanced(self):
        partition_key_ranges = [{
            u'id': u'0',
            u'minInclusive': u'',
            u'maxExclusive': u'05C1C9CD673398'
        }, {
            u'id': u'1',
            u'minInclusive': u'05C1C9CD673398',
            u'maxExclusive': u'05C1D9CD673398'
        }, {
            u'id': u'2',
            u'minInclusive': u'05C1D9CD673398',
            u'maxExclusive': u'05C1E399CD6732'
        }, {
            u'id': u'3',
            u'minInclusive': u'05C1E399CD6732',
            u'maxExclusive': u'05C1E9CD673398'
        }, {
            u'id': u'4',
            u'minInclusive': u'05C1E9CD673398',
            u'maxExclusive': u'FF'
        }]
        partitionRangeWithInfo = [(r, True) for r in partition_key_ranges]

        pkRange = routing_range._Range("", "FF", True, False)
        collection_routing_map = _CollectionRoutingMap.CompleteRoutingMap(
            partitionRangeWithInfo, 'sample collection id')
        overlapping_partition_key_ranges = collection_routing_map.get_overlapping_ranges(
            pkRange)

        self.assertEqual(len(overlapping_partition_key_ranges),
                         len(partition_key_ranges))
        self.assertEqual(overlapping_partition_key_ranges,
                         partition_key_ranges)
Exemplo n.º 4
0
    def get_overlapping_ranges(self, collection_link, partition_key_ranges):
        '''
        Given a partition key range and a collection, 
        returns the list of overlapping partition key ranges
        
        :param str collection_link:
            The name of the collection.
        :param list partition_key_range: 
            List of partition key range.
        
        :return:
            List of overlapping partition key ranges.
        :rtype: list
        '''
        cl = self._documentClient

        collection_id = base.GetResourceIdOrFullNameFromLink(collection_link)

        collection_routing_map = self._collection_routing_map_by_item.get(
            collection_id)
        if collection_routing_map is None:
            collection_pk_ranges = list(
                cl._ReadPartitionKeyRanges(collection_link))
            # for large collections, a split may complete between the read partition key ranges query page responses,
            # causing the partitionKeyRanges to have both the children ranges and their parents. Therefore, we need
            # to discard the parent ranges to have a valid routing map.
            collection_pk_ranges = _PartitionKeyRangeCache._discard_parent_ranges(
                collection_pk_ranges)
            collection_routing_map = _CollectionRoutingMap.CompleteRoutingMap(
                [(r, True) for r in collection_pk_ranges], collection_id)
            self._collection_routing_map_by_item[
                collection_id] = collection_routing_map
        return collection_routing_map.get_overlapping_ranges(
            partition_key_ranges)
Exemplo n.º 5
0
    def test_collection_routing_map(self):

        Id = 'id'
        MinInclusive = 'minInclusive'
        MaxExclusive = 'maxExclusive'

        partitionKeyRanges = \
                    [
                        ({Id : "2",
                        MinInclusive : "0000000050",
                        MaxExclusive : "0000000070"},
                        2),
                        ({Id : "0",
                        MinInclusive : "",
                        MaxExclusive : "0000000030"},
                        0),
                        ({Id : "1",
                        MinInclusive : "0000000030",
                        MaxExclusive : "0000000050"},
                        1),
                        ({Id : "3",
                        MinInclusive : "0000000070",
                        MaxExclusive : "FF"},
                        3)
                    ]

        crm = _CollectionRoutingMap.CompleteRoutingMap(partitionKeyRanges, "")

        self.assertEqual("0", crm._orderedPartitionKeyRanges[0][Id])
        self.assertEqual("1", crm._orderedPartitionKeyRanges[1][Id])
        self.assertEqual("2", crm._orderedPartitionKeyRanges[2][Id])
        self.assertEqual("3", crm._orderedPartitionKeyRanges[3][Id])

        self.assertEqual(0, crm._orderedPartitionInfo[0])
        self.assertEqual(1, crm._orderedPartitionInfo[1])
        self.assertEqual(2, crm._orderedPartitionInfo[2])
        self.assertEqual(3, crm._orderedPartitionInfo[3])

        self.assertEqual("0", crm.get_range_by_effective_partition_key("")[Id])
        self.assertEqual(
            "0",
            crm.get_range_by_effective_partition_key("0000000000")[Id])
        self.assertEqual(
            "1",
            crm.get_range_by_effective_partition_key("0000000030")[Id])
        self.assertEqual(
            "1",
            crm.get_range_by_effective_partition_key("0000000031")[Id])
        self.assertEqual(
            "3",
            crm.get_range_by_effective_partition_key("0000000071")[Id])

        self.assertEqual("0", crm.get_range_by_partition_key_range_id("0")[Id])
        self.assertEqual("1", crm.get_range_by_partition_key_range_id("1")[Id])

        fullRangeMinToMaxRange = routing_range._Range(
            _CollectionRoutingMap.MinimumInclusiveEffectivePartitionKey,
            _CollectionRoutingMap.MaximumExclusiveEffectivePartitionKey, True,
            False)
        overlappingRanges = crm.get_overlapping_ranges(
            [fullRangeMinToMaxRange])
        self.assertEqual(4, len(overlappingRanges))

        onlyParitionRanges = [item[0] for item in partitionKeyRanges]

        def getKey(r):
            return r['id']

        onlyParitionRanges.sort(key=getKey)
        self.assertEqual(overlappingRanges, onlyParitionRanges)

        noPoint = routing_range._Range(
            _CollectionRoutingMap.MinimumInclusiveEffectivePartitionKey,
            _CollectionRoutingMap.MinimumInclusiveEffectivePartitionKey, False,
            False)
        self.assertEqual(0, len(crm.get_overlapping_ranges([noPoint])))

        onePoint = routing_range._Range("0000000040", "0000000040", True, True)
        overlappingPartitionKeyRanges = crm.get_overlapping_ranges([onePoint])
        self.assertEqual(1, len(overlappingPartitionKeyRanges))
        self.assertEqual("1", overlappingPartitionKeyRanges[0][Id])

        ranges = [
            routing_range._Range("0000000040", "0000000045", True, True),
            routing_range._Range("0000000045", "0000000046", True, True),
            routing_range._Range("0000000046", "0000000050", True, True)
        ]
        overlappingPartitionKeyRanges = crm.get_overlapping_ranges(ranges)

        self.assertEqual(2, len(overlappingPartitionKeyRanges))
        self.assertEqual("1", overlappingPartitionKeyRanges[0][Id])
        self.assertEqual("2", overlappingPartitionKeyRanges[1][Id])
Exemplo n.º 6
0
 def createRoutingMap():
     _CollectionRoutingMap.CompleteRoutingMap(partitionKeyRanges,
                                              collectionUniqueId)