def map(self, entity):
        ''' Lowercase and add/clean the tags field,
        and create the MacroTags objects for this macro.
        '''
        # Add the classes to the tags, lower, dedup, and sort the tags
        # prior to saving.
        tags = list(set([t.lower() for t in entity.tags + entity.classes]))
        tags.sort()

        # Update the entity and add for writing back.
        entity.tags = tags

        # Create tags for the macro.
        save_macro_tags(tags)
        return ([entity], [])
    def save_macro(self, macro, notes, title, name, classes, tags, version, server=''):
        ''' Create a new macro entry in the datastore.
        This function assumes data has already been validated.
        
        Note that this function involves two writes to the datastore
        in order to have a serialized count across all macro entites.
        This sucks donkey balls but ensures a) that we can page through
        macros in search results, and b) we can generate unique ids
        for each macro.
        
        This is a classmethod function, can use without needing a
        SavedMacroOps object.
        
        Parameters:
        Correspond with the params in SavedMacro
        
        '''
        saved_macro = None
        
        # Add the classes to the tags, lower, dedup, and sort the tags
        # prior to saving.
        tags = list(set([t.lower() for t in tags + classes]))
        tags.sort()

        # Get a serialized macro count.
        # Need this in order to get unique encoded links.
        # Only needs to be done once on save.
        def index_txn():
            macro_index = MacroIndex.get_by_key_name(_MACRO_COUNTER)
            if macro_index is None:
                macro_index = MacroIndex(key_name=_MACRO_COUNTER)
            new_index = macro_index.curr_index
            macro_index.curr_index += 1
            macro_index.put()    
            return new_index

        # Get macro index for this macro.
        new_index = db.run_in_transaction(index_txn)
        
        # Create a new entity for this macro.
        def txn():
            # Create the macro.  Since macros are never edited, this
            # means the only locking is when a new macro is created,
            # Which hopefully is << the number of times a macro is read.
            enc_index = _MACRO_KEY % encode(int(new_index))
            saved_macro = SavedMacro(key_name = enc_index,
                                     notes    = notes,
                                     macro    = macro,
                                     title    = title,
                                     name     = name,
                                     server   = server,
                                     version  = version,
                                     classes  = classes,
                                     tags     = tags,
                                     index    = new_index,
                                     link_id  = enc_index)
            saved_macro.put()
            return saved_macro
    
        # Create and save, both in datastore and memcache
        saved_macro = db.run_in_transaction(txn)
        memcache.add(cache_key(saved_macro.link_id), saved_macro,
                     _MEMCACHED_SAVED_MACRO)

        # Create the tag entries.
        #logging.debug(tags)
        save_macro_tags(tags)

        # Init the memcache counters.
        init_count(saved_macro.link_id, "views")

        return saved_macro.link_id