Modyfy Splitter: Additional constructors. Now only k and m are necessary
[splitter-ng] / Splitter-ng-plugin-jerasure / src / jerasureplugin / CodecLiberation.java
1 package jerasureplugin;
2
3
4
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.Map;
8 import java.util.Map.Entry;
9
10 import splitterng.plugin.CodecDescription;
11 import splitterng.plugin.interfaces.SplitterCodec;
12 import eu.vandertil.jerasure.jni.Jerasure;
13 import eu.vandertil.jerasure.jni.Liberation;
14
15 public class CodecLiberation implements SplitterCodec {
16
17         public final static String codecID = "JerasureLiberation";
18         public final static String codecName = "Jerasure Liberation";
19         public final static String codecDescriptionText = "This Codec implements Liberation coding with Jerasure."
20                         + "Requirements: k<=w , m=2 , w>=3 and prim";
21
22         public final static Map <String, Integer []> codecParameterMinMax;
23         public final static Map <String, Integer> codecParameterDefaults;
24
25         private int k,m,w,packetsize;
26         
27         static {
28                 Map <String, Integer []> tmpMap = new HashMap<String, Integer[]>();
29                 tmpMap.put("k", new Integer[] {new Integer(1),new Integer(Integer.MAX_VALUE)});
30                 tmpMap.put("m", new Integer[] {new Integer(2),new Integer(2)});
31                 tmpMap.put("w", new Integer[] {new Integer(3),new Integer(Integer.MAX_VALUE)});
32                 tmpMap.put("packetsize", new Integer[] {new Integer(1),new Integer(Integer.MAX_VALUE)});
33                 
34                 codecParameterMinMax  = Collections.unmodifiableMap(tmpMap);                            
35
36                 Map <String, Integer> tmpMap2 = new HashMap<String, Integer>();
37                 tmpMap2.put("w", new Integer(7));
38                 tmpMap2.put("packetsize", new Integer(32));
39                 
40                 codecParameterDefaults  = Collections.unmodifiableMap(tmpMap2);                         
41         }
42         
43         
44         private static final CodecDescription codecDescription = new CodecDescription(
45         codecID, codecName, codecDescriptionText, codecParameterMinMax, codecParameterDefaults);
46         
47         private int[] matrix;
48         private int[] bitmatrix;
49         private byte[][] coding_ptrs;
50         private byte[][] data_ptrs;
51         private byte[][] dataAndCoding_ptrs;
52  
53         private int [] erasures;
54         private int numerased=0;
55
56         
57         public static final CodecDescription getCodecDescription(){
58                 //System.out.println("JerasureCodecParity: getCodecDescription: "+JerasureCodecParity.codecDescription);
59                 return CodecLiberation.codecDescription;
60         }
61         
62         /**
63          * @param codecParameter
64          */
65         public CodecLiberation(Map<String, Integer> codecParameter) {
66                 super();
67                 
68                 //Test CodecParameter
69                 for (Entry<String, Integer[]> requiredEntry : codecParameterMinMax
70                                 .entrySet()) {
71                         if (codecParameter.containsKey(requiredEntry.getKey())) {
72                                 // Test ob Parameter vorhanden und in den gültigen grenzen
73                                 // liegen
74                                 //System.out.println("Test" +codecParameter.get(requiredEntry.getKey()) +" "+  requiredEntry.getValue()[0]);
75                                 if (codecParameter.get(requiredEntry.getKey()) < requiredEntry.getValue()[0])
76                                         throw new IllegalArgumentException(
77                                                         "unzulässiger Wert für Parameter: "
78                                                                         + requiredEntry.getKey() + " < "
79                                                                         + requiredEntry.getValue()[0]);
80                                 if (codecParameter.get(requiredEntry.getKey()) > requiredEntry.getValue()[1])
81                                         throw new IllegalArgumentException(
82                                                         "unzulässiger Wert für Parameter: "
83                                                                         + requiredEntry.getKey() + " > "
84                                                                         + requiredEntry.getValue()[1]);
85                         } else if (codecParameterDefaults.containsKey(requiredEntry.getKey())) {
86                                 codecParameter.put(requiredEntry.getKey(), codecParameterDefaults.get(requiredEntry.getKey()));
87                         } else {
88                                 throw new IllegalArgumentException("Missing Codec Parameter: "
89                                                 + requiredEntry.getKey());
90                         }
91                 }
92                 
93                 k=(int)codecParameter.get("k");
94                 m=(int)codecParameter.get("m");
95                 w=(int)codecParameter.get("w");
96                 packetsize=(int)codecParameter.get("packetsize");
97                 bitmatrix = Liberation.liberation_coding_bitmatrix(k, w);
98                 erasures = new int [k+m+1];
99                 
100         }
101
102         @Override
103         public byte[][] decode(byte[][] dataAndCoding_ptrs, boolean [] erased){
104                 //TODO Array Testen ob zulässig? eventuell nicht, da es hier bremsen könnte
105                 int fragmentsize = dataAndCoding_ptrs[0].length;
106                 
107                 if (data_ptrs == null || (data_ptrs.length != k) || (data_ptrs[0].length != fragmentsize)){
108                         data_ptrs = new byte[k][fragmentsize];
109                 }                       
110                 
111                 if (coding_ptrs == null || (coding_ptrs.length != m) || (coding_ptrs[0].length != fragmentsize)){
112                         coding_ptrs = new byte[m][fragmentsize];
113                 }
114                 
115                 for (int i=0; i<k ; i++){
116                         data_ptrs[i]=dataAndCoding_ptrs[i];
117                 }
118                 for (int i=k; i<k+m ; i++){
119                         coding_ptrs[i-k]=dataAndCoding_ptrs[i];
120                 }
121                 
122                 //Preparing Erasures
123                 numerased=0;
124                 for (int i=0; i<m+k; i++){
125                         if (erased[i]==true){
126                                 erasures[numerased]=i;
127                                 numerased++;
128                         }
129                 }
130                 erasures[numerased]=-1;
131                 
132
133                 //Jerasure.jerasure_matrix_encode(k, m, w, matrix, data_ptrs, coding_ptrs, blocksize);
134                 Jerasure.jerasure_bitmatrix_decode(k, m, w, bitmatrix, false, erasures, data_ptrs, coding_ptrs, fragmentsize, packetsize);
135
136                 return data_ptrs;
137         }
138
139         @Override
140         public byte[][] encode(byte[][] data_ptrs){
141                 //TODO Array Testen ob zulässig? eventuell nicht, da es hier bremsen könnte
142                 // buffersize%(sizeof(int)*w*k) == 0
143                 int fragmentsize = data_ptrs[0].length;
144                 
145                 if (coding_ptrs == null || (coding_ptrs.length != m) || (coding_ptrs[0].length != fragmentsize)){
146                         coding_ptrs = new byte[m][fragmentsize];
147                 }
148                 
149                 //Jerasure.jerasure_matrix_encode(k, m, w, matrix, data_ptrs, coding_ptrs, blocksize);
150                 Jerasure.jerasure_bitmatrix_encode(k, m, w, bitmatrix, data_ptrs, coding_ptrs, fragmentsize, packetsize);
151                 
152                 if (dataAndCoding_ptrs == null || (dataAndCoding_ptrs.length != k+m) || (dataAndCoding_ptrs[0].length != fragmentsize)){
153                         dataAndCoding_ptrs = new byte[k+m][fragmentsize];
154                 }
155                 //Data Blocks
156                 for (int i=0; i<k ; i++){
157                         dataAndCoding_ptrs[i]=data_ptrs[i];
158                 }
159                 for (int i=k; i<k+m ; i++){
160                         dataAndCoding_ptrs[i]=coding_ptrs[i-k];
161                 }
162                                 
163                 return dataAndCoding_ptrs;
164         }
165
166
167 }