def createCF():
    # Create column families for Events
    try:
        yield db.system_drop_column_family("userAgenda")
    except: pass
    userAgenda = ttypes.CfDef(KEYSPACE, "userAgenda", "Standard", "TimeUUIDType",
                              None, "Time sorted list of events for a user")
    yield db.system_add_column_family(userAgenda)

    try:
        yield db.system_drop_column_family("userAgendaMap")
    except: pass
    userAgendaMap = ttypes.CfDef(KEYSPACE, "userAgendaMap", "Standard", "TimeUUIDType",
                                 None, "Reverse map of user:event to agenda entry")
    yield db.system_add_column_family(userAgendaMap)

    try:
        yield db.system_drop_column_family("eventResponses")
    except: pass
    eventResponses = ttypes.CfDef(KEYSPACE, "eventResponses", "Standard", "UTF8Type",
                                  None, "List of RSVP responses to an event")
    yield db.system_add_column_family(eventResponses)
def createCF():
    # Create column families required for file listing
    #    drop user_files CF
    #    create user_files entityFeed_files
    #
    yield db.system_drop_column_family('user_files')
    user_files = ttypes.CfDef(KEYSPACE, 'user_files', 'Standard',
                             'TimeUUIDType', None,
                             "List of files owned by the user")
    yield db.system_add_column_family(user_files)

    entityFeed_files = ttypes.CfDef(KEYSPACE, 'entityFeed_files',
                                'Standard', 'TimeUUIDType', None,
                                "List of files that appeared in entity's feed")
    yield db.system_add_column_family(entityFeed_files)

    #Remove User from network
    userSessionsMap = ttypes.CfDef(KEYSPACE, 'userSessionsMap', 'Standard',
                            'BytesType', None, 'userId-Session Map')
    yield db.system_add_column_family(userSessionsMap)

    deletedUsers = ttypes.CfDef(KEYSPACE, "deletedUsers", "Standard", "UTF8Type", None,
                         "List of users removed from the networks by admins.")
    yield db.system_add_column_family(deletedUsers)

    #Preset Tags

    orgPresetTags = ttypes.CfDef(KEYSPACE, "orgPresetTags", "Standard", "UTF8Type",
                          None, "List of preset tags. Only admin can create"
                          "or delete these tags. unlike normal tags these tags"
                          "will not be deleted automatically. On deletion it"
                          "behaves like a normal tag")
    yield db.system_add_column_family(orgPresetTags)

    #Keyword Monitoring
    keywords = ttypes.CfDef(KEYSPACE, 'keywords', 'Standard', 'UTF8Type',
                            None, "list of keywords to be monitored")
    yield db.system_add_column_family(keywords)

    originalKeywords = ttypes.CfDef(KEYSPACE, 'originalKeywords', 'Standard', 'UTF8Type',
                                    None, 'list of original keywords')
    yield db.system_add_column_family(originalKeywords)

    keywordItems = ttypes.CfDef(KEYSPACE, "keywordItems", "Standard", "TimeUUIDType",
                                None, "list of items which have a keyword monitored by admins")
    yield db.system_add_column_family(keywordItems)

    #API
    apps = ttypes.CfDef(KEYSPACE, "apps", "Super", "UTF8Type", "UTF8Type",
                 "Details of Applications registered for API Access")
    yield db.system_add_column_family(apps)

    appsByOwner = ttypes.CfDef(KEYSPACE, "appsByOwner", "Standard", "UTF8Type",
                        None, "List of applications registered by a User")
    yield db.system_add_column_family(appsByOwner)

    oAuthData = ttypes.CfDef(KEYSPACE, "oAuthData", "Standard", "UTF8Type",
                      None, "List of access, refresh tokens and auth codes")
    yield db.system_add_column_family(oAuthData)

    #
    # Create column families for poll indexing (in feeds and userItems)
    #
    userItemsType = ttypes.CfDef(KEYSPACE, 'userItems_poll', 'Standard',
                          'TimeUUIDType', None,
                          'poll items posted by a given user')
    yield db.system_add_column_family(userItemsType)

    feedType = ttypes.CfDef(KEYSPACE, 'feed_poll', 'Standard', 'TimeUUIDType',
                     None, 'Feed of poll items')
    yield db.system_add_column_family(feedType)

    #
    # Remove unused feed and userItems index
    #
    try:
        yield db.system_drop_column_family('feed_document')
    except: pass
    try:
        yield db.system_drop_column_family('userItems_document')
    except: pass
def migrateFriendsToFollowers():
    # Migrate all friends to followers/subscriptions.
    connectionRows = yield db.get_range_slice('connections', count=10000)
    for connectionRow in connectionRows:
        userId = connectionRow.key
        friends = [x.super_column.name for x in connectionRow.columns]
        yield db.batch_insert(userId, "followers", dict([(x, '') for x in friends]))
        yield db.batch_mutate(dict([(x, {'subscriptions': {userId: ''}}) for x in friends]))
    log.msg('>>>>>>>> Converted all connections to following.')

    # Remove name indices of friends
    entityRows = yield db.get_range_slice('entities', count=10000, names=['basic'])
    entities = dict([(x.key, utils.supercolumnsToDict(x.columns)) for x in entityRows])
    userIds = [x for x in entities.keys() if entities[x]['basic']['type'] == 'user']
    for userId in userIds:
        yield db.remove(userId, 'displayNameIndex')
        yield db.remove(userId, 'nameIndex')
    log.msg('>>>>>>>> Removed name indices for friends.')

    # Convert all "connection" activity to "follow".
    # We already have two separate items, so subtype conversion should be good.
    itemRows = yield db.get_range_slice('items', count=10000, names=['meta'])
    items = dict([(x.key, utils.supercolumnsToDict(x.columns)) for x in itemRows])
    connectionItems = [x for x in items.keys()\
                       if items[x]['meta'].get('type', '') == 'activity'\
                          and items[x]['meta']['subType'] == 'connection']
    yield db.batch_mutate(dict([(x, {'items':{'meta':{'subType':'following'}}}) for x in connectionItems]))
    log.msg('>>>>>>>> All connection items converted to following.')

    # Remove all friend requests from pendingConnections
    pendingRows = yield db.get_range_slice('pendingConnections', count=10000)
    for pendingRow in pendingRows:
        userId = pendingRow.key
        pendingFriendRequestIds = [x.column.name for x in pendingRow.columns \
                                   if not x.column.name.startswith('G')]
        if pendingFriendRequestIds:
            yield db.batch_remove({'pendingConnections': [userId]}, names=pendingFriendRequestIds)
    log.msg('>>>>>>>> Removed pending friend requests.')

    # Remove all friend requests from latest
    yield db.batch_remove({'latest': userIds}, names='people')
    log.msg('>>>>>>>> Removed friend requests from latest.')

    # Remove all friend-request-accepted notifications
    notifyMutations = {}
    for userId in userIds:
        items = yield db.get_slice(userId, "notificationItems", super_column=':FA')
        if items:
            names = [col.column.name for col in items]
            colmap = dict([(x, None) for x in names])
            deletion = Deletion(time.time() * 1000000, 'notifications',
                                SlicePredicate(column_names=names))
            notifyMutations[userId] = {'notifications': colmap, 'latest': [deletion]}
            yield db.remove(userId, 'notificationItems', super_column=':FA')
    if notifyMutations:
        yield db.batch_mutate(notifyMutations)
    log.msg('>>>>>>>> Removed friend notifications from notifications and latest.')

    # Finally, remove the connections column family.
    yield db.system_drop_column_family('connections')
    yield db.system_drop_column_family('connectionsByTag')
    log.msg('>>>>>>>> Removed the connections column family.')