Re-update to rcsparse r2495 to get all the goodness of the new update script.
[cvs2svn.git] / cvs2svn_lib / persistence_manager.py
blob90c7ace58269e3f3078196e8e5cbae217d2e1a92
1 # (Be in -*- python -*- mode.)
3 # ====================================================================
4 # Copyright (c) 2000-2008 CollabNet. All rights reserved.
6 # This software is licensed as described in the file COPYING, which
7 # you should have received as part of this distribution. The terms
8 # are also available at http://subversion.tigris.org/license-1.html.
9 # If newer versions of this license are posted there, you may use a
10 # newer version instead, at your option.
12 # This software consists of voluntary contributions made by many
13 # individuals. For exact contribution history, see the revision
14 # history and logs, available at http://cvs2svn.tigris.org/.
15 # ====================================================================
17 """This module contains class PersistenceManager."""
20 from cvs2svn_lib import config
21 from cvs2svn_lib.common import DB_OPEN_NEW
22 from cvs2svn_lib.common import DB_OPEN_READ
23 from cvs2svn_lib.common import SVN_INVALID_REVNUM
24 from cvs2svn_lib.artifact_manager import artifact_manager
25 from cvs2svn_lib.record_table import SignedIntegerPacker
26 from cvs2svn_lib.record_table import RecordTable
27 from cvs2svn_lib.serializer import PrimedPickleSerializer
28 from cvs2svn_lib.indexed_database import IndexedDatabase
29 from cvs2svn_lib.svn_commit import SVNRevisionCommit
30 from cvs2svn_lib.svn_commit import SVNInitialProjectCommit
31 from cvs2svn_lib.svn_commit import SVNPrimaryCommit
32 from cvs2svn_lib.svn_commit import SVNBranchCommit
33 from cvs2svn_lib.svn_commit import SVNTagCommit
34 from cvs2svn_lib.svn_commit import SVNPostCommit
37 class PersistenceManager:
38 """The PersistenceManager allows us to effectively store SVNCommits
39 to disk and retrieve them later using only their subversion revision
40 number as the key. It also returns the subversion revision number
41 for a given CVSRevision's unique key.
43 All information pertinent to each SVNCommit is stored in a series of
44 on-disk databases so that SVNCommits can be retrieved on-demand.
46 MODE is one of the constants DB_OPEN_NEW or DB_OPEN_READ.
47 In 'new' mode, PersistenceManager will initialize a new set of on-disk
48 databases and be fully-featured.
49 In 'read' mode, PersistenceManager will open existing on-disk databases
50 and the set_* methods will be unavailable."""
52 def __init__(self, mode):
53 self.mode = mode
54 if mode not in (DB_OPEN_NEW, DB_OPEN_READ):
55 raise RuntimeError("Invalid 'mode' argument to PersistenceManager")
56 primer = (
57 SVNInitialProjectCommit,
58 SVNPrimaryCommit,
59 SVNPostCommit,
60 SVNBranchCommit,
61 SVNTagCommit,
63 serializer = PrimedPickleSerializer(primer)
64 self.svn_commit_db = IndexedDatabase(
65 artifact_manager.get_temp_file(config.SVN_COMMITS_INDEX_TABLE),
66 artifact_manager.get_temp_file(config.SVN_COMMITS_STORE),
67 mode, serializer)
68 self.cvs2svn_db = RecordTable(
69 artifact_manager.get_temp_file(config.CVS_REVS_TO_SVN_REVNUMS),
70 mode, SignedIntegerPacker(SVN_INVALID_REVNUM))
72 def get_svn_revnum(self, cvs_rev_id):
73 """Return the Subversion revision number in which CVS_REV_ID was
74 committed, or SVN_INVALID_REVNUM if there is no mapping for
75 CVS_REV_ID."""
77 return self.cvs2svn_db.get(cvs_rev_id, SVN_INVALID_REVNUM)
79 def get_svn_commit(self, svn_revnum):
80 """Return an SVNCommit that corresponds to SVN_REVNUM.
82 If no SVNCommit exists for revnum SVN_REVNUM, then return None."""
84 return self.svn_commit_db.get(svn_revnum, None)
86 def put_svn_commit(self, svn_commit):
87 """Record the bidirectional mapping between SVN_REVNUM and
88 CVS_REVS and record associated attributes."""
90 if self.mode == DB_OPEN_READ:
91 raise RuntimeError(
92 'Write operation attempted on read-only PersistenceManager'
95 self.svn_commit_db[svn_commit.revnum] = svn_commit
97 if isinstance(svn_commit, SVNRevisionCommit):
98 for cvs_rev in svn_commit.cvs_revs:
99 self.cvs2svn_db[cvs_rev.id] = svn_commit.revnum
101 def close(self):
102 self.cvs2svn_db.close()
103 self.cvs2svn_db = None
104 self.svn_commit_db.close()
105 self.svn_commit_db = None