00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 """Different managers to handle when cursors are killed after they are closed.
00016
00017 New cursor managers should be defined as subclasses of CursorManager and can be
00018 installed on a connection by calling
00019 `pymongo.connection.Connection.set_cursor_manager`."""
00020
00021
00022 class CursorManager(object):
00023 """The default cursor manager.
00024
00025 This manager will kill cursors one at a time as they are closed.
00026 """
00027
00028 def __init__(self, connection):
00029 """Instantiate the manager.
00030
00031 :Parameters:
00032 - `connection`: a Mongo Connection
00033 """
00034 self.__connection = connection
00035
00036 def close(self, cursor_id):
00037 """Close a cursor by killing it immediately.
00038
00039 Raises TypeError if cursor_id is not an instance of (int, long).
00040
00041 :Parameters:
00042 - `cursor_id`: cursor id to close
00043 """
00044 if not isinstance(cursor_id, (int, long)):
00045 raise TypeError("cursor_id must be an instance of (int, long)")
00046
00047 self.__connection.kill_cursors([cursor_id])
00048
00049
00050 class BatchCursorManager(CursorManager):
00051 """A cursor manager that kills cursors in batches.
00052 """
00053
00054 def __init__(self, connection):
00055 """Instantiate the manager.
00056
00057 :Parameters:
00058 - `connection`: a Mongo Connection
00059 """
00060 self.__dying_cursors = []
00061 self.__max_dying_cursors = 20
00062 self.__connection = connection
00063
00064 CursorManager.__init__(self, connection)
00065
00066 def __del__(self):
00067 """Cleanup - be sure to kill any outstanding cursors.
00068 """
00069 self.__connection.kill_cursors(self.__dying_cursors)
00070
00071 def close(self, cursor_id):
00072 """Close a cursor by killing it in a batch.
00073
00074 Raises TypeError if cursor_id is not an instance of (int, long).
00075
00076 :Parameters:
00077 - `cursor_id`: cursor id to close
00078 """
00079 if not isinstance(cursor_id, (int, long)):
00080 raise TypeError("cursor_id must be an instance of (int, long)")
00081
00082 self.__dying_cursors.append(cursor_id)
00083
00084 if len(self.__dying_cursors) > self.__max_dying_cursors:
00085 self.__connection.kill_cursors(self.__dying_cursors)
00086 self.__dying_cursors = []