~bzr-pqm/bzr/bzr.dev

2617.4.2 by Robert Collins
Add FileCollection support class.
1
# Copyright (C) 2007 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""A collection of file names which is persisted on a transport."""
18
19
from bzrlib.lazy_import import lazy_import
20
lazy_import(globals(), """
21
from bzrlib import (
22
        errors,
23
        )
24
""")
25
26
2617.4.4 by Robert Collins
Rename to FileNames as per review.
27
class FileNames(object):
2617.4.2 by Robert Collins
Add FileCollection support class.
28
    """A collection of file names.
29
30
    The file names are persisted to a file on a transport, and cand be
31
    added, removed and initialised.
32
33
    The set of names are stored in a flat file, one name per line.
34
    New names are allocated sequentially.
35
    Initialisation creates an empty file.
36
37
    This is intended to support management of modest numbers of files in 
38
    write-locked environments which may be read from unlistable transports.
39
    
40
    The save method must be called to cause the state to be saved to the
41
    transport.
42
43
    Finally, load is used to obtain a previously saved set.
44
    """
45
46
    def __init__(self, transport, index_name):
2617.4.7 by Robert Collins
Tweak docstrings after over-aggressive search n replace.
47
        """Create a collection on transport called index_name."""
2617.4.2 by Robert Collins
Add FileCollection support class.
48
        self._transport = transport
49
        self._index_name = index_name
50
        self._names = None
51
        self._cap = 10000
52
53
    def allocate(self):
54
        for number in xrange(self._cap):
55
            if str(number) not in self._names:
56
                self._names.add(str(number))
57
                return str(number)
58
        raise errors.BzrError('too many files')
59
60
    def initialise(self):
2617.4.7 by Robert Collins
Tweak docstrings after over-aggressive search n replace.
61
        """Initialise the collection."""
2617.4.2 by Robert Collins
Add FileCollection support class.
62
        self._names = set()
63
64
    def load(self):
65
        """Load the names from the transport."""
66
        self._names = set(self._transport.get_bytes(
67
            self._index_name).split('\n'))
2617.4.6 by Robert Collins
self._names.discard wins over conditional remove - thanks Aaron.
68
        self._names.discard('')
2617.4.2 by Robert Collins
Add FileCollection support class.
69
70
    def names(self):
2617.4.7 by Robert Collins
Tweak docstrings after over-aggressive search n replace.
71
        """What are the names in this collection?"""
2617.4.2 by Robert Collins
Add FileCollection support class.
72
        return frozenset(self._names)
73
2617.4.3 by Robert Collins
Check loading empty collections and support removing items.
74
    def remove(self, name):
2617.4.7 by Robert Collins
Tweak docstrings after over-aggressive search n replace.
75
        """Remove name from the collection."""
2617.4.3 by Robert Collins
Check loading empty collections and support removing items.
76
        self._names.remove(name)
77
2617.4.2 by Robert Collins
Add FileCollection support class.
78
    def save(self):
79
        """Save the set of names."""
80
        self._transport.put_bytes(self._index_name, '\n'.join(self._names))
81