Advertisement
Guest User

code 2

a guest
Nov 24th, 2012
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1. /**
  2.  * Replication Benchmarker
  3.  * https://github.com/score-team/replication-benchmarker/
  4.  * Copyright (C) 2012 LORIA / Inria / SCORE Team
  5.  *
  6.  * This program is free software: you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation, either version 3 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  */
  19. package jbenchmarker.rga;
  20.  
  21. import crdt.CRDT;
  22. import java.util.List;
  23. import java.util.ArrayList;
  24. import collect.VectorClock;
  25. import jbenchmarker.core.Document;
  26. import jbenchmarker.core.MergeAlgorithm;
  27. import jbenchmarker.core.SequenceMessage;
  28. import crdt.simulator.IncorrectTraceException;
  29. import jbenchmarker.core.SequenceOperation;
  30.  
  31. /**
  32. *
  33. * @author Roh
  34. */
  35. public class RGAMerge extends MergeAlgorithm {
  36.  
  37.     private VectorClock siteVC;
  38. //  private RGAPurger   purger;
  39.    
  40.     public RGAMerge(Document doc, int r){
  41.         super(doc, r);
  42.         siteVC = new VectorClock();
  43. //      purger = new RGAPurger((RGADocument)this.getDoc());
  44.     }
  45.    
  46.     @Override
  47.     protected void integrateRemote(SequenceMessage op) throws IncorrectTraceException {
  48.         RGAOperation rgaop  = (RGAOperation) op;
  49.         RGADocument  rgadoc = (RGADocument)(this.getDoc());
  50.         this.siteVC.inc(rgaop.getReplica());
  51.         rgadoc.apply(rgaop);
  52. //      purger.setLastVC(rgaop.getS4VTms().sid, rgaop.getOriginalOp().getVectorClock());
  53.         //RGANode tau = purger.tryPurge();
  54.         //if(tau != null) rgadoc.purge(tau);
  55.     }
  56.  
  57.     @Override
  58.     protected List<SequenceMessage> localInsert(SequenceOperation opt) throws IncorrectTraceException {
  59.         List<SequenceMessage> lop       = new ArrayList<SequenceMessage>();
  60.         RGADocument     rgadoc  = (RGADocument)(this.getDoc());
  61.         RGAS4Vector     s4vtms, s4vpos = null;
  62.         RGAOperation    rgaop;     
  63.         RGANode     target = null; 
  64.                
  65.         int  p  = opt.getPosition();
  66.         int offset;    
  67.        
  68.                 offset = opt.getContent().size();
  69.         if(p==0) s4vpos = null;
  70.         else s4vpos = rgadoc.getVisibleS4V(p); // if head, s4vpos = null; if after tail, s4vpos= the last one.     
  71.            
  72.         for(int i=0; i < offset ; i++) {
  73.             this.siteVC.inc(this.getReplicaNumber());
  74.             s4vtms = new RGAS4Vector(this.getReplicaNumber(), this.siteVC);
  75.             rgaop = new RGAOperation(opt, p+i, s4vpos, opt.getContent().get(i), s4vtms);
  76.             s4vpos = s4vtms; // The s4v of the current insert becomes the s4vpos of next insert.
  77.             lop.add(rgaop);
  78.             rgadoc.apply(rgaop);
  79.            
  80. //          purger.setLastVC(this.getReplicaNumber(),this.siteVC);
  81.         }
  82.  
  83.         return lop;
  84.     }
  85.        
  86.         @Override
  87.     protected List<SequenceMessage> localDelete(SequenceOperation opt) throws IncorrectTraceException {
  88.         List<SequenceMessage> lop       = new ArrayList<SequenceMessage>();
  89.         RGADocument     rgadoc  = (RGADocument)(this.getDoc());
  90.         RGAS4Vector     s4vtms, s4vpos = null;
  91.         RGAOperation    rgaop;     
  92.         RGANode         target = null;
  93.        
  94.         int  p          = opt.getPosition();
  95.         int offset;    
  96.        
  97.         offset = opt.getLenghOfADel();
  98.         target = rgadoc.getVisibleNode(p+1);       
  99.        
  100.         for(int i=0; i < offset ; i++) {
  101.             this.siteVC.inc(this.getReplicaNumber());
  102.             s4vtms = new RGAS4Vector(this.getReplicaNumber(), this.siteVC);
  103.             rgaop = new RGAOperation(opt, p+1, target.getKey(), s4vtms);
  104.             target = target.getNextVisible();
  105.             lop.add(rgaop);
  106.             rgadoc.apply(rgaop);
  107.            
  108. //          purger.setLastVC(this.getReplicaNumber(),this.siteVC);
  109.         }
  110.  
  111.         return lop;
  112.     }
  113.  
  114.     @Override
  115.     public CRDT<String> create() {
  116.         return new RGAMerge(new RGADocument(), 0);
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement