Mercurial > hg > RemoteEditor > REPSessionManager
comparison rep/handler/Translator.java @ 427:622a8e15ff40
Merge Worked ?
author | one |
---|---|
date | Sat, 02 Jan 2010 03:20:23 +0900 |
parents | 1acc3dfde5d3 |
children | 1bb59652d89c |
comparison
equal
deleted
inserted
replaced
426:1acc3dfde5d3 | 427:622a8e15ff40 |
---|---|
1 package rep.handler; | 1 package rep.handler; |
2 | 2 |
3 import java.util.Collection; | |
4 import java.util.Comparator; | 3 import java.util.Comparator; |
5 import java.util.LinkedList; | 4 import java.util.LinkedList; |
6 import java.util.List; | 5 import java.util.List; |
7 import java.util.TreeSet; | 6 import java.util.TreeSet; |
8 | 7 |
53 * stage my writeQueue is empty, our editor is waiting for me. | 52 * stage my writeQueue is empty, our editor is waiting for me. |
54 * Start merge process. | 53 * Start merge process. |
55 * @param cmd | 54 * @param cmd |
56 */ | 55 */ |
57 public boolean catchOwnCommand(REPNode editor){ | 56 public boolean catchOwnCommand(REPNode editor){ |
58 logger.writeLog("beforeMarge:"+unMergedCmds); | 57 logger.writeLog("beforeMerge:"+unMergedCmds); |
59 LinkedList<REPCommand> output = new LinkedList<REPCommand>(); | 58 LinkedList<REPCommand> output = new LinkedList<REPCommand>(); |
60 LinkedList<REPCommand> cmds = new LinkedList<REPCommand>(); | 59 TreeSet<REPCommand> cmds = new TreeSet<REPCommand>(new REPCommandComparator()); |
61 //スタック上にあるコマンドを全部undoコマンドにする | 60 // merge queue上にあるコマンドを全部undoコマンドするのと同時に |
62 while ( !unMergedCmds.isEmpty() ){ | 61 // sort したコマンド列を生成する |
63 REPCommand cmd0 = unMergedCmds.removeLast(); | 62 for( REPCommand cmd0 : unMergedCmds) { |
64 output.add( createUndo(cmd0) ); | 63 output.add( createUndo(cmd0) ); |
65 cmds.add(cmd0); | 64 cmds.add(cmd0); |
66 } | 65 } |
67 | |
68 /* 必要な分だけソートして返却用のリストに追加 */ | |
69 output.addAll( sortCmds(cmds) ); | |
70 | |
71 /* 残ったコマンドも再び実行させるが、まだマージされてないのでunMergedにも入れる */ | |
72 output.addAll(cmds); | 66 output.addAll(cmds); |
73 unMergedCmds.addAll(cmds); | 67 // ACKが来たものは必ず先頭 |
68 unMergedCmds.removeFirst(); | |
74 logger.writeLog("outputMerge:"+output); | 69 logger.writeLog("outputMerge:"+output); |
75 logger.writeLog("afterMerge:"+unMergedCmds); | 70 logger.writeLog("afterMerge:"+unMergedCmds); |
76 return optimizedSend(editor,output); | 71 return optimizedSend(editor,output); |
77 } | 72 } |
78 | 73 |
107 } | 102 } |
108 | 103 |
109 class REPCommandComparator implements Comparator<REPCommand>{ | 104 class REPCommandComparator implements Comparator<REPCommand>{ |
110 | 105 |
111 public int compare(REPCommand o1, REPCommand o2) { | 106 public int compare(REPCommand o1, REPCommand o2) { |
112 | 107 if ( o1.eid<o2.eid ) return -1; |
113 if ( o2.lineno > o1.lineno ) return 1; | 108 if ( o1.eid>o2.eid ) return 1; |
114 else if ( o2.lineno < o1.lineno | 109 if ( o1.seq<o2.seq ) return -1; |
115 || o2.eid > o1.eid ) | 110 if ( o1.seq>o2.seq ) return 1; |
116 return -1; | 111 assert(false); |
117 | 112 return 0; |
118 return 1; | |
119 } | 113 } |
120 | |
121 } | 114 } |
122 | 115 |
123 private Collection<REPCommand> sortCmds(LinkedList<REPCommand> cmds) { | |
124 TreeSet<REPCommand> sortedCmds1 = new TreeSet<REPCommand>(new REPCommandComparator()); | |
125 int top; | |
126 int prevEid=-1; | |
127 while ( -1 != (top=getPrecedence(cmds, prevEid+1)) ){ | |
128 REPCommand tmp = cmds.remove(top); | |
129 sortedCmds1.add(tmp); | |
130 prevEid = tmp.eid; | |
131 } | |
132 | |
133 return sortedCmds1; | |
134 } | |
135 | |
136 /* search cmd. ordering by min EID that is lower lowEid and min SEQ. */ | |
137 private int getPrecedence(LinkedList<REPCommand> cmds, int lowEid) { | |
138 int cEid, cSeq; | |
139 cEid=cSeq=Integer.MAX_VALUE; | |
140 int ret=-1; | |
141 for (int i=0; i<cmds.size(); i++){ | |
142 REPCommand c = cmds.get(i); | |
143 if ( c.eid<lowEid ) continue; | |
144 else if ( c.eid>cEid ) continue; | |
145 else if ( c.eid==cEid ) { | |
146 if ( c.seq>cSeq ) continue; | |
147 cSeq=c.seq; | |
148 ret = i; | |
149 } else { /* tmp.eid<cEid */ | |
150 cEid = c.eid; | |
151 cSeq = c.seq; | |
152 ret = i; | |
153 } | |
154 } | |
155 return ret; | |
156 } | |
157 | |
158 /** | 116 /** |
159 * Translate Command cmd that was received from SeMa. | 117 * Translate Command cmd that was received from SeMa. |
160 * @param cmd the command to be translated. | 118 * @param cmd the command to be translated. |
161 * @return translated commannd. | 119 * @return translated commannd. |
162 */ | 120 */ |
223 public boolean isMerging() { | 181 public boolean isMerging() { |
224 return merge_mode; | 182 return merge_mode; |
225 } | 183 } |
226 | 184 |
227 public void startMerge(REPCommand cmd) { | 185 public void startMerge(REPCommand cmd) { |
186 logger.writeLog("START MERGE command ="+cmd+" and top of unMergedCmds = "+ unMergedCmds.getFirst()); | |
228 merge_mode = true; | 187 merge_mode = true; |
229 } | 188 } |
230 | 189 |
231 public void mergeAck() { | 190 public void mergeAck() { |
232 } | 191 } |