Move a utility function into allmydata.util.

Signed-off-by: Daira Hopwood <daira@jacaranda.org>
This commit is contained in:
Daira Hopwood 2014-04-04 15:34:20 +01:00
parent e0ffe8bfcd
commit 32547bbd10
4 changed files with 31 additions and 24 deletions

View File

@ -16,6 +16,7 @@ from allmydata.interfaces import IShareBase
from allmydata.util import log from allmydata.util import log
from allmydata.util.assertutil import precondition, _assert from allmydata.util.assertutil import precondition, _assert
from allmydata.util.deferredutil import eventually_callback, eventually_errback, eventual_chain, gatherResults from allmydata.util.deferredutil import eventually_callback, eventually_errback, eventual_chain, gatherResults
from allmydata.util.listutil import concat
from allmydata.storage.common import si_b2a, NUM_RE from allmydata.storage.common import si_b2a, NUM_RE
@ -458,24 +459,6 @@ class CommonContainerMixin:
return self._do_request('DELETE object', self._delete_object, object_name) return self._do_request('DELETE object', self._delete_object, object_name)
def concat(seqs):
"""
O(n), rather than O(n^2), concatenation of list-like things, returning a list.
I can't believe this isn't built in.
"""
total_len = 0
for seq in seqs:
total_len += len(seq)
result = [None]*total_len
i = 0
for seq in seqs:
for x in seq:
result[i] = x
i += 1
_assert(i == total_len, i=i, total_len=total_len)
return result
class ContainerListMixin: class ContainerListMixin:
""" """
S3 has a limitation of 1000 object entries returned on each list (GET Bucket) request. S3 has a limitation of 1000 object entries returned on each list (GET Bucket) request.

View File

@ -1,6 +1,5 @@
import time, os.path, platform, re, simplejson, struct, itertools, urllib import time, os.path, platform, re, simplejson, struct, itertools, urllib
from collections import deque
from cStringIO import StringIO from cStringIO import StringIO
import thread import thread
@ -365,10 +364,6 @@ class Seek(unittest.TestCase, WorkdirMixin):
class CloudCommon(unittest.TestCase, ShouldFailMixin, WorkdirMixin): class CloudCommon(unittest.TestCase, ShouldFailMixin, WorkdirMixin):
def test_concat(self):
x = deque([[1, 2], (), xrange(3, 6)])
self.failUnlessEqual(cloud_common.concat(x), [1, 2, 3, 4, 5])
def test_list_objects_truncated_badly(self): def test_list_objects_truncated_badly(self):
# If a container misbehaves by not producing listings with increasing keys, # If a container misbehaves by not producing listings with increasing keys,
# that should cause an incident. # that should cause an incident.

View File

@ -2,7 +2,9 @@
def foo(): pass # keep the line number constant def foo(): pass # keep the line number constant
import os, time, sys import os, time, sys
from collections import deque
from StringIO import StringIO from StringIO import StringIO
from twisted.trial import unittest from twisted.trial import unittest
from twisted.internet import defer, reactor from twisted.internet import defer, reactor
from twisted.python.failure import Failure from twisted.python.failure import Failure
@ -12,7 +14,7 @@ from pycryptopp.hash.sha256 import SHA256 as _hash
from allmydata.util import base32, idlib, humanreadable, mathutil, hashutil from allmydata.util import base32, idlib, humanreadable, mathutil, hashutil
from allmydata.util import assertutil, fileutil, deferredutil, abbreviate from allmydata.util import assertutil, fileutil, deferredutil, abbreviate
from allmydata.util import limiter, time_format, pollmixin, cachedir from allmydata.util import limiter, time_format, pollmixin, cachedir
from allmydata.util import statistics, dictutil, pipeline from allmydata.util import statistics, dictutil, listutil, pipeline
from allmydata.util import log as tahoe_log from allmydata.util import log as tahoe_log
from allmydata.util.spans import Spans, overlap, DataSpans from allmydata.util.spans import Spans, overlap, DataSpans
@ -1431,6 +1433,13 @@ class DictUtil(unittest.TestCase):
self.failUnlessEqual(d["one"], 1) self.failUnlessEqual(d["one"], 1)
self.failUnlessEqual(d.get_aux("one"), None) self.failUnlessEqual(d.get_aux("one"), None)
class ListUtil(unittest.TestCase):
def test_concat(self):
x = deque([[1, 2], (), xrange(3, 6)])
self.failUnlessEqual(listutil.concat(x), [1, 2, 3, 4, 5])
class Pipeline(unittest.TestCase): class Pipeline(unittest.TestCase):
def pause(self, *args, **kwargs): def pause(self, *args, **kwargs):
d = defer.Deferred() d = defer.Deferred()

View File

@ -0,0 +1,20 @@
from allmydata.util.assertutil import _assert
def concat(seqs):
"""
O(n), rather than O(n^2), concatenation of list-like things, returning a list.
I can't believe this isn't built in.
"""
total_len = 0
for seq in seqs:
total_len += len(seq)
result = [None]*total_len
i = 0
for seq in seqs:
for x in seq:
result[i] = x
i += 1
_assert(i == total_len, i=i, total_len=total_len)
return result