~bzr/bzr/bzr.0.90

« back to all changes in this revision

Viewing changes to bzrlib/file_names.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-08-08 05:55:16 UTC
  • mfrom: (2683.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20070808055516-ml9ucjsb4idmpmww
(robertc) Remove the new FileNames class as it is redundant with a trivial no-refs, no-value index. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
27
 
class FileNames(object):
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):
47
 
        """Create a collection on transport called index_name."""
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):
61
 
        """Initialise the collection."""
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'))
68
 
        self._names.discard('')
69
 
 
70
 
    def names(self):
71
 
        """What are the names in this collection?"""
72
 
        return frozenset(self._names)
73
 
 
74
 
    def remove(self, name):
75
 
        """Remove name from the collection."""
76
 
        self._names.remove(name)
77
 
 
78
 
    def save(self):
79
 
        """Save the set of names."""
80
 
        self._transport.put_bytes(self._index_name, '\n'.join(self._names))
81