Ticket #12581: 12581.diff

File 12581.diff, 3.0 KB (added by Rob Hudson, 15 years ago)

RingBuffer, based on connections.deque (Python 2.4+)

  • django/db/__init__.py

    diff --git a/django/db/__init__.py b/django/db/__init__.py
    index ea353e4..21bf58d 100644
    a b from django.conf import settings  
    22from django.core import signals
    33from django.core.exceptions import ImproperlyConfigured
    44from django.db.utils import ConnectionHandler, load_backend
     5from django.utils.datastructures import RingBuffer
    56from django.utils.functional import curry
    67
    78__all__ = ('backend', 'connection', 'connections', 'DatabaseError',
    signals.request_finished.connect(close_connection)  
    8788# when a Django request is started.
    8889def reset_queries(**kwargs):
    8990    for conn in connections.all():
    90         conn.queries = []
     91        conn.queries = RingBuffer(100)
    9192signals.request_started.connect(reset_queries)
    9293
    9394# Register an event that rolls back the connections
  • django/db/backends/__init__.py

    diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
    index bf3e9db..7cb242a 100644
    a b from threading import local  
    44from django.db import DEFAULT_DB_ALIAS
    55from django.db.backends import util
    66from django.utils import datetime_safe
     7from django.utils.datastructures import RingBuffer
    78from django.utils.importlib import import_module
    89
    910class BaseDatabaseWrapper(local):
    class BaseDatabaseWrapper(local):  
    1718        # NAME, USER, etc. It's called `settings_dict` instead of `settings`
    1819        # to disambiguate it from Django settings modules.
    1920        self.connection = None
    20         self.queries = []
     21        self.queries = RingBuffer(100)
    2122        self.settings_dict = settings_dict
    2223        self.alias = alias
    2324
  • django/utils/datastructures.py

    diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py
    index d6fc9dc..5c37313 100644
    a b  
     1import collections
    12from types import GeneratorType
    23
    34from django.utils.copycompat import deepcopy
    class DictWrapper(dict):  
    447448            return self.func(value)
    448449        return value
    449450
     451class RingBuffer(collections.deque):
     452    def __init__(self, size):
     453        collections.deque.__init__(self)
     454        self.size = size
     455
     456    def append(self, item):
     457        collections.deque.append(self, item)
     458        if len(self) > self.size:
     459            self.popleft()
     460
     461    def get(self):
     462        return list(self)
     463
  • tests/regressiontests/datastructures/tests.py

    diff --git a/tests/regressiontests/datastructures/tests.py b/tests/regressiontests/datastructures/tests.py
    index b7885e6..7315d4b 100644
    a b AttributeError: Object is immutable!  
    159159>>> "Normal: %(a)s. Modified: %(xx_a)s" % d
    160160'Normal: a. Modified: *a'
    161161
     162### RingBuffer ##############################################################
     163
     164>>> r = RingBuffer(3)
     165>>> r.append(1)
     166>>> r.get()
     167[1]
     168>>> r.append(2)
     169>>> r.append(3)
     170>>> r.get()
     171[1, 2, 3]
     172>>> r.append(4)
     173>>> r.get()
     174[2, 3, 4]
     175
    162176"""
Back to Top