Rev 4691: Start introducing a depth parameter, this allows us to have keys which are in http://bazaar.launchpad.net/~jameinel/bzr/2.1-memory-consumption
John Arbash Meinel
john at arbash-meinel.com
Wed Sep 9 22:23:22 BST 2009
At http://bazaar.launchpad.net/~jameinel/bzr/2.1-memory-consumption
------------------------------------------------------------
revno: 4691
revision-id: john at arbash-meinel.com-20090909212308-wxyu8hwuc4izhcaa
parent: john at arbash-meinel.com-20090909210251-x22c3hj4uvaplxzu
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1-memory-consumption
timestamp: Wed 2009-09-09 16:23:08 -0500
message:
Start introducing a depth parameter, this allows us to have keys which are
('key', 'one')
as well as
(('key', 'one'), ('key', 'two'))
-------------- next part --------------
=== modified file 'bzrlib/_keys_type_c.c'
--- a/bzrlib/_keys_type_c.c 2009-09-09 21:00:40 +0000
+++ b/bzrlib/_keys_type_c.c 2009-09-09 21:23:08 +0000
@@ -68,6 +68,7 @@
Py_ssize_t i;
long key_width;
long num_keys;
+ long depth;
long num_key_bits;
PyObject *obj= NULL;
Keys *self;
@@ -82,9 +83,9 @@
return NULL;
}
num_args = PyTuple_GET_SIZE(args);
- if (num_args < 1) {
- PyErr_SetString(PyExc_TypeError, "Keys.__init__(width, ...) takes at"
- " least one argument.");
+ if (num_args < 2) {
+ PyErr_SetString(PyExc_TypeError, "Keys.__init__(width, depth, ...)"
+ " takes at least two arguments.");
return NULL;
}
key_width = PyInt_AsLong(PyTuple_GET_ITEM(args, 0));
@@ -92,17 +93,36 @@
return NULL;
}
if (key_width <= 0) {
- PyErr_SetString(PyExc_ValueError, "Keys.__init__(width, ...), width"
- " should be a positive integer.");
+ PyErr_SetString(PyExc_ValueError, "Keys.__init__(width, depth, ...)"
+ " width should be a positive integer.");
return NULL;
}
if (key_width > 256) {
- PyErr_SetString(PyExc_ValueError, "Keys.__init__(width, ...), width"
- " must be <= 256");
+ PyErr_SetString(PyExc_ValueError, "Keys.__init__(width, depth, ...)"
+ " width must be <= 256");
+ return NULL;
+ }
+ depth = PyInt_AsLong(PyTuple_GET_ITEM(args, 1));
+ if (depth == -1 && PyErr_Occurred()) {
+ return NULL;
+ }
+ if (depth <= 0) {
+ PyErr_SetString(PyExc_ValueError, "Keys.__init__(width, depth, ...)"
+ " depth must be > 0");
+ return NULL;
+ }
+ if (depth > 256) {
+ PyErr_SetString(PyExc_ValueError, "Keys.__init__(width, depth, ...)"
+ " depth must be <= 256");
+ return NULL;
+ }
+ if (depth != 2) {
+ PyErr_SetString(PyExc_ValueError, "Keys.__init__(width, depth, ...)"
+ " only depth == 2 currently supported");
return NULL;
}
/* First arg is the key width, the rest are the actual key items */
- num_key_bits = num_args - 1;
+ num_key_bits = num_args - 2;
num_keys = num_key_bits / key_width;
if (num_keys * key_width != num_key_bits) {
PyErr_SetString(PyExc_ValueError, "Keys.__init__(width, ...), was"
@@ -119,7 +139,7 @@
self->key_width = (unsigned char)key_width;
self->ob_size = (unsigned char)num_keys;
for (i = 0; i < num_key_bits; i++) {
- obj = PyTuple_GET_ITEM(args, i + 1);
+ obj = PyTuple_GET_ITEM(args, i + 2);
if (!PyString_CheckExact(obj)) {
PyErr_SetString(PyExc_TypeError, "Keys.__init__(width, ...)"
" requires that all key bits are strings.");
=== modified file 'bzrlib/_keys_type_py.py'
--- a/bzrlib/_keys_type_py.py 2009-09-08 21:35:01 +0000
+++ b/bzrlib/_keys_type_py.py 2009-09-09 21:23:08 +0000
@@ -21,11 +21,15 @@
"""
-def Keys(width, *args):
+def Keys(width, depth, *args):
if not isinstance(width, int):
raise TypeError('width must be an integer.')
+ if not isinstance(depth, int):
+ raise TypeError('depth must be an integer.')
if width <= 0 or width > 256:
raise ValueError('width must be in the range 1 => 256')
+ if depth <= 0 or depth > 256:
+ raise ValueError('depth must be in the range 1 => 256')
num_keys = len(args) // width
if (num_keys * width != len(args)):
raise ValueError('number of entries not a multiple of width')
=== modified file 'bzrlib/tests/test__keys_type.py'
--- a/bzrlib/tests/test__keys_type.py 2009-09-09 21:00:40 +0000
+++ b/bzrlib/tests/test__keys_type.py 2009-09-09 21:23:08 +0000
@@ -62,32 +62,36 @@
class TestKeysType(tests.TestCase):
def test_create(self):
- k = self.module.Keys(1, 'foo', 'bar')
+ #k = self.module.Keys(1, 1, 'foo')
+ k = self.module.Keys(1, 2, 'foo', 'bar')
+ #k = self.module.Keys(2, 1, 'foo', 'bar')
def test_create_bad_args(self):
self.assertRaises(TypeError, self.module.Keys)
self.assertRaises(TypeError, self.module.Keys, 'foo')
- self.assertRaises(ValueError, self.module.Keys, 0)
- self.assertRaises(ValueError, self.module.Keys, -1)
- self.assertRaises(ValueError, self.module.Keys, -200)
- self.assertRaises(ValueError, self.module.Keys, 2, 'foo')
- self.assertRaises(ValueError, self.module.Keys, 257)
+ self.assertRaises(TypeError, self.module.Keys, 0)
+ self.assertRaises(ValueError, self.module.Keys, 0, 2)
+ self.assertRaises(ValueError, self.module.Keys, 1, 0)
+ self.assertRaises(ValueError, self.module.Keys, -1, 2)
+ self.assertRaises(ValueError, self.module.Keys, -200, 2)
+ self.assertRaises(ValueError, self.module.Keys, 2, 2, 'foo')
+ self.assertRaises(ValueError, self.module.Keys, 257, 2)
lots_of_args = ['a']*300
# too many args
- self.assertRaises(ValueError, self.module.Keys, 1, *lots_of_args)
- self.assertRaises(TypeError, self.module.Keys, 1, 'foo', 10)
+ self.assertRaises(ValueError, self.module.Keys, 1, 2, *lots_of_args)
+ self.assertRaises(TypeError, self.module.Keys, 1, 2, 'foo', 10)
def test_create_and_del_correct_refcount(self):
s = 'my custom' + ' foo bar'
n_ref = sys.getrefcount(s)
- k = self.module.Keys(1, s)
+ k = self.module.Keys(1, 2, s)
self.assertEqual(n_ref + 1, sys.getrefcount(s))
del k
self.assertEqual(n_ref, sys.getrefcount(s))
def test_get_item(self):
f = 'fo' + 'o'
- k = self.module.Keys(1, f, 'bar')
+ k = self.module.Keys(1, 2, f, 'bar')
self.assertEqual(('foo',), k[0])
self.assertEqual(('bar',), k[1])
self.assertRaises(IndexError, k.__getitem__, 2)
@@ -103,14 +107,14 @@
self.assertEqual(2, len(k))
def test_get_wide_key(self):
- k = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing')
+ k = self.module.Keys(2, 2, 'foo', 'bar', 'baz', 'bing')
self.assertEqual(('foo', 'bar'), k[0])
self.assertEqual(('baz', 'bing'), k[1])
self.assertRaises(IndexError, k.__getitem__, 2)
self.assertEqual(2, len(k))
def test_as_tuple(self):
- k = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing')
+ k = self.module.Keys(2, 2, 'foo', 'bar', 'baz', 'bing')
if getattr(k, 'as_tuples', None) is not None:
t = k.as_tuples()
else:
@@ -118,14 +122,14 @@
self.assertEqual((('foo', 'bar'), ('baz', 'bing')), t)
def test_repr(self):
- k = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing')
+ k = self.module.Keys(2, 2, 'foo', 'bar', 'baz', 'bing')
self.assertEqual("(('foo', 'bar'), ('baz', 'bing'))", repr(k))
def test_compare(self):
- k1 = self.module.Keys(2, 'foo', 'bar')
- k2 = self.module.Keys(2, 'baz', 'bing')
- k3 = self.module.Keys(2, 'foo', 'zzz')
- k4 = self.module.Keys(2, 'foo', 'bar')
+ k1 = self.module.Keys(2, 2, 'foo', 'bar')
+ k2 = self.module.Keys(2, 2, 'baz', 'bing')
+ k3 = self.module.Keys(2, 2, 'foo', 'zzz')
+ k4 = self.module.Keys(2, 2, 'foo', 'bar')
# Comparison should be done on the keys themselves, and not based on
# object id, etc.
self.assertTrue(k1 == k1)
@@ -138,18 +142,18 @@
self.assertTrue(k1 == (('foo', 'bar'),))
def test_sorted(self):
- k1 = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing', 'foo', 'zzz')
+ k1 = self.module.Keys(2, 2, 'foo', 'bar', 'baz', 'bing', 'foo', 'zzz')
self.assertEqual([('baz', 'bing'), ('foo', 'bar'), ('foo', 'zzz')],
sorted(k1))
- k1 = self.module.Keys(2, 'foo', 'bar')
- k2 = self.module.Keys(2, 'baz', 'bing')
- k3 = self.module.Keys(2, 'foo', 'zzz')
+ k1 = self.module.Keys(2, 2, 'foo', 'bar')
+ k2 = self.module.Keys(2, 2, 'baz', 'bing')
+ k3 = self.module.Keys(2, 2, 'foo', 'zzz')
self.assertEqual([(('baz', 'bing'),), (('foo', 'bar'),),
(('foo', 'zzz'),)], sorted([k1, k2, k3]))
def test_hash(self):
- k1 = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing', 'foo', 'zzz')
+ k1 = self.module.Keys(2, 2, 'foo', 'bar', 'baz', 'bing', 'foo', 'zzz')
as_tuple =(('foo', 'bar'), ('baz', 'bing'), ('foo', 'zzz'))
self.assertEqual(hash(k1), hash(as_tuple))
x = {k1: 'foo'}
More information about the bazaar-commits
mailing list