comparison src/org/tmatesoft/hg/internal/Transaction.java @ 617:65c01508f002

Rollback support for commands that modify repository. Strategy to keep complete copy of a file being changed
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 15 May 2013 20:10:09 +0200
parents
children 99ad1e3a4e4d
comparison
equal deleted inserted replaced
616:5e0313485eef 617:65c01508f002
1 /*
2 * Copyright (c) 2013 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
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 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.internal;
18
19 import java.io.File;
20 import java.io.IOException;
21
22 import org.tmatesoft.hg.core.HgIOException;
23 import org.tmatesoft.hg.core.SessionContext;
24 import org.tmatesoft.hg.repo.HgInvalidStateException;
25
26 /**
27 * Implementation strategies possible:<ul>
28 * <li> Get a copy, write changes to origin, keep copy as backup till #commit
29 * <p>(-) doesn't break hard links
30 * <li> Get a copy, write changes to a copy, on commit rename copy to origin.
31 * <p>(-) What if we read newly written data (won't find it);
32 * <p>(-) complex #commit
33 * <p>(+) simple rollback
34 * <li> Get a copy, rename origin to backup (breaks hard links), rename copy to origin, write changes
35 * <p>(+) Modified file is in place right away;
36 * <p>(+) easy #commit
37 * <li> Do not copy, just record file size, truncate to that size on rollback
38 * <li> ...?
39 * </ul>
40 * @author Artem Tikhomirov
41 * @author TMate Software Ltd.
42 */
43 public abstract class Transaction {
44 /**
45 * Record the file is going to be modified during this transaction, obtain actual
46 * destination to write to.
47 */
48 public abstract File prepare(File f) throws HgIOException;
49 /**
50 * overwrites backup if exists, backup is kept after successful {@link #commit()}
51 */
52 public abstract File prepare(File origin, File backup) throws HgIOException;
53 /**
54 * Tell that file was successfully processed
55 */
56 public abstract void done(File f) throws HgIOException;
57 /**
58 * optional?
59 */
60 public abstract void failure(File f, IOException ex);
61 /**
62 * Complete the transaction
63 */
64 public abstract void commit() throws HgIOException;
65 /**
66 * Undo all the changes
67 */
68 public abstract void rollback() throws HgIOException;
69
70 public interface Factory {
71 public Transaction create(SessionContext.Source ctxSource);
72 }
73
74 public static class NoRollback extends Transaction {
75
76 @Override
77 public File prepare(File f) throws HgIOException {
78 return f;
79 }
80
81 @Override
82 public File prepare(File origin, File backup) throws HgIOException {
83 return origin;
84 }
85
86 @Override
87 public void done(File f) throws HgIOException {
88 // no-op
89 }
90
91 @Override
92 public void failure(File f, IOException ex) {
93 // no-op
94 }
95
96 @Override
97 public void commit() throws HgIOException {
98 // no-op
99 }
100
101 @Override
102 public void rollback() throws HgIOException {
103 throw new HgInvalidStateException("This transaction doesn't support rollback");
104 }
105 }
106 }