"""
Setup for the API
"""

import api

log = api.logger.use(__name__)


def index_mongo():
    """
    Ensure the mongo collections are indexed.
    """

    db = api.common.get_conn()

    log.debug("Ensuring mongo is indexed.")

    db.users.ensure_index("uid", unique=True, name="unique uid")
    db.users.ensure_index("username", unique=True, name="unique username")

    db.groups.ensure_index("gid", unique=True, name="unique gid")
    db.problems.ensure_index("pid", unique=True, name="unique pid")

    db.submissions.ensure_index("tid", name="submission tids")
    db.ssh.ensure_index("tid", unique=True, name="unique ssh tid")
    db.teams.ensure_index("team_name", unique=True, name="unique team names")

    db.shell_servers.ensure_index("name", unique=True, name="unique shell name")
    db.shell_servers.ensure_index("sid", unique=True, name="unique shell sid")

    db.cache.ensure_index("expireAt", expireAfterSeconds=0)
    db.cache.ensure_index("kwargs", name="kwargs")
    db.cache.ensure_index("kwargs.uid", name="kwargs.uid")
    db.cache.ensure_index("kwargs.tid", name="kwargs.tid")
    db.cache.ensure_index("args", name="args")

    db.shell_servers.ensure_index(
        "sid", unique=True, name="unique shell server id")

@@ -363,16 +363,49 @@
 ername%22)
+%0A    db.users.ensure_index(%22tid%22)
 %0A%0A    db
@@ -460,24 +460,25 @@
 nique gid%22)%0A
+%0A
     db.probl
@@ -570,49 +570,130 @@
 dex(
+%5B(
 %22tid%22, 
-name=%22submission tids%22)%0A    db.ssh
+1), (%22uid%22, 1), (%22correct%22, 1)%5D)%0A    db.submissions.ensure_index(%5B(%22uid%22, 1), (%22correct%22, 1)%5D)%0A    db.submissions
 .ens
@@ -706,51 +706,179 @@
 dex(
+%5B(
 %22tid%22, 
-unique=True, name=%22unique ssh 
+1), (%22correct%22, 1)%5D)%0A    db.submissions.ensure_index(%5B(%22pid%22, 1), (%22correct%22, 1)%5D)%0A    db.submissions.ensure_index(%22uid%22)%0A    db.submissions.ensure_index(%22
 tid%22)%0A
+%0A
     
@@ -950,16 +950,53 @@
  names%22)
+%0A    db.teams.ensure_index(%22country%22)
 %0A%0A    db
@@ -1291,98 +1291,48 @@
 dex(
-%22kwargs.uid%22, name=%22kwargs.uid%22)%0A    db.cache.ensure_index(%22kwargs.tid%22, name=%22kwargs.tid%22
+%5B(%22function%22, 1), (%22ordered_kwargs%22, 1)%5D
 )%0A  

Update mongodb indexes based on high-frequency query usage