Advertisement
Guest User

code 1

a guest
Nov 24th, 2012
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.core;
  20.  
  21. import crdt.*;
  22. import crdt.simulator.IncorrectTraceException;
  23. import java.io.Serializable;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26.  
  27. /**
  28.  * Squeleton of mergeAlgorithm
  29.  * @author urso
  30.  */
  31. public abstract class MergeAlgorithm extends CRDT<String> implements Serializable{
  32.  
  33.     private static final boolean DEBUG = false;
  34.    
  35.     // Supported Document
  36.     final private Document doc;
  37.        
  38.     final private List<String> states  = new ArrayList<String>(1);  
  39.    
  40.   /*  private void applyOneRemote(CommutativeMessage mess){
  41.        
  42.     }*/
  43.     /*
  44.      * Constructor
  45.      */
  46.     /**
  47.      *
  48.      * @param doc Document of this merge algorithm
  49.      * @param siteId SiteID or replicat number
  50.      */
  51.     public MergeAlgorithm(Document doc, int siteId) {
  52.         this.doc = doc;
  53.         this.setReplicaNumber(siteId);
  54.     }
  55.  
  56.     public MergeAlgorithm(Document doc) {
  57.         this.doc = doc;
  58.     }
  59.  
  60.     /**
  61.      * Integrate remote message from another replicas
  62.      * To be define by the concrete merge algorithm
  63.      * @param message from another replicas
  64.      * @throws IncorrectTraceException
  65.      */
  66.     protected abstract void integrateRemote(SequenceMessage message) throws IncorrectTraceException;
  67.            
  68.     /**
  69.      *
  70.      * @return the document
  71.      */
  72.     public Document getDoc() {
  73.         return doc;
  74.     }
  75.  
  76.     /**
  77.      * This the mergeAlgorithm return in initial state
  78.      */
  79.     public void reset() {
  80.     }
  81.  
  82.     /**
  83.      * Integreate local modification of the document.
  84.      * This function to stay compatible with causal dispatcher
  85.      * localmodification is performed and a sequence message is transformed to CRDTMessage.
  86.      * @param op local operation
  87.      * @return CRDT message will be sent to another
  88.      * @throws PreconditionException
  89.      */
  90.     @Override
  91.     public CRDTMessage applyLocal(LocalOperation op) throws PreconditionException {
  92.         if (!(op instanceof SequenceOperation)) {
  93.             throw new PreconditionException("Not a sequenceOperation : " + op);
  94.         }
  95.         SequenceOperation opt = (SequenceOperation) op;
  96.         List<SequenceMessage> l;
  97.         switch (opt.getType()) {
  98.         case ins:
  99.             l = localInsert(opt);
  100.             break;
  101.         case del:
  102.             l =  localDelete(opt);
  103.             break;
  104.         case update:
  105.             l = localUpdate(opt);
  106.             break;
  107.         case noop:
  108.             return CRDTMessage.emptyMessage;
  109.         default:
  110.             throw new IncorrectTraceException("Unsupported operation : " + opt);
  111.         }
  112.        
  113.         // FIXME: when l is an empty list, a NullPointerException is thrown...
  114.        
  115.         CRDTMessage m = null;
  116.         for (SequenceMessage n : l) {
  117.             if (m == null) {
  118.                 m = new OperationBasedOneMessage(n);
  119.             } else {
  120.                 m=m.concat(new OperationBasedOneMessage(n));
  121.             }
  122.         }        
  123.         if (DEBUG) states.add(doc.view());
  124.         return m;
  125.     }
  126.  
  127.     final public CRDTMessage insert(int position, String content) throws PreconditionException {
  128.         return applyLocal(SequenceOperation.insert( position, content));
  129.     }
  130.  
  131.     final public CRDTMessage remove(int position, int offset) throws PreconditionException {
  132.         return applyLocal(SequenceOperation.delete(position, offset));
  133.     }
  134.    
  135.     /**
  136.      * integrate remote operations.
  137.      * @param msg message generated by another replicas
  138.      */
  139.     /*@Override
  140.     public void applyRemote(CRDTMessage msg) {
  141.         ((CommutativeMessage)msg).execute(this);
  142.        
  143.         if (DEBUG) states.add(doc.view());
  144.     }*/
  145.     @Override
  146.     public void applyOneRemote(CRDTMessage mess){
  147.          try {            
  148.              integrateRemote(CRDTMessage2SequenceMessage(mess));
  149.          }catch (IncorrectTraceException ex) {
  150.             throw new IllegalStateException(ex);
  151.         }
  152.     }
  153.    
  154.     public static SequenceMessage CRDTMessage2SequenceMessage(CRDTMessage mess){
  155.         return (SequenceMessage) ((OperationBasedOneMessage) mess).getOperation();
  156.     }
  157.     /**
  158.      * return document
  159.      * @return
  160.      */
  161.     @Override
  162.     public String lookup() {
  163.         return doc.view();
  164.     }
  165.  
  166.     /**
  167.      * The defaut behavior of localInsert and localDelete is to call generateLocal.
  168.      * Either localXXXs or generateLocal should be overrriden.
  169.      * @param opt local modification
  170.      * @return Messages generated by replication algorithm will sent to another replicas
  171.      * @throws IncorrectTraceException by default
  172.      */
  173.     abstract protected List<SequenceMessage> localInsert(SequenceOperation opt) throws IncorrectTraceException;
  174.  
  175.     abstract protected List<SequenceMessage> localDelete(SequenceOperation opt) throws IncorrectTraceException;
  176.    
  177.     /**
  178.      * Default behavior of localUpdate is to throw IncorrectTraceException.
  179.      */
  180.     protected List<SequenceMessage> localUpdate(SequenceOperation opt) throws IncorrectTraceException {
  181.         List<SequenceMessage> lop = localDelete(opt);
  182.         lop.addAll(localInsert(opt));
  183.         return lop;
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement