예제 #1
0
    def generateDesignCandidates(self, collections, isShardingEnabled=True, isIndexesEnabled=True, isDenormalizationEnabled=True):

        dc = DesignCandidates()
        valid_collection = set()
        for col_info in collections.itervalues():

            shardKeys = []
            indexKeys = []
            denorm = []

            interesting = col_info['interesting']
            valid_collection.add(col_info['name'])
            
            interesting = self.__remove_heuristicaly_bad_key__(col_info, interesting)
            # Make sure that none of our interesting fields start with
            # the character that we used to convert $ commands
            for key in interesting:
                assert not key.startswith(constants.REPLACE_KEY_DOLLAR_PREFIX), \
                    "Unexpected candidate key '%s.%s'" % (col_info["name"], key)

            if constants.SKIP_MONGODB_ID_FIELD and "_id" in interesting:
                interesting = interesting[:]
                interesting.remove("_id")

            # deal with shards
            if isShardingEnabled:
                LOG.debug("Sharding is enabled")
                shardKeys = interesting

            # deal with indexes
            if isIndexesEnabled:
                LOG.debug("Indexes is enabled")
                for o in xrange(1, len(interesting) + 1) :
                    if o > constants.MAX_INDEX_SIZE: break
                    for i in itertools.permutations(interesting, o):
                        indexKeys.append(i)
                    ## FOR
                ## FOR
            # deal with de-normalization
            if len(indexKeys) > 10:
                LOG.warn("Too many index keys: %s", len(indexKeys))
            if isDenormalizationEnabled:
                LOG.debug("Denormalization is enabled")
                for k,v in col_info['fields'].iteritems() :
                    if v['parent_col'] <> None and v['parent_col'] not in denorm and v['parent_col'] in valid_collection:
                        denorm.append(v['parent_col'])
            
            dc.addCollection(col_info['name'], indexKeys, shardKeys, denorm)
            ## FOR

        return dc