'Store an entity in the result database.
Args:
entity_id: A datastore.Key for the entity.
entity: The entity to store.
Returns:
True if this entities is not already present in the result database.'

def _StoreEntity(self, entity_id, entity):

    assert _RunningInThread(self.secondary_thread)
    assert isinstance(entity_id, datastore.Key), ('expected    a    datastore.Key,    got    a    %s' % entity_id.__class__.__name__)
    key_str = buffer(KeyStr(entity_id).encode('utf-8'))
    self.insert_cursor.execute('select    count(*)    from    result    where    id    =    ?', (key_str,))
    already_present = self.insert_cursor.fetchone()[0]
    result = True
    if already_present:
        result = False
        self.insert_cursor.execute('delete    from    result    where    id    =    ?', (key_str,))
    else:
        self.count += 1
    if self.sort_key_from_entity:
        sort_key = self.sort_key_from_entity(datastore.Entity._FromPb(entity))
    else:
        sort_key = ''
    value = entity.Encode()
    self.insert_cursor.execute('insert    into    result    (id,    value,    sort_key)    values    (?,    ?,    ?)', (key_str, buffer(value), sort_key))
    return result
