- import of splitter-NG SVN repository from : https://obelinux.inf.tu-dresden.de...
[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
24         private int k,m,w,packetsize;
25         
26         static {
27                 Map <String, Integer []> tmpMap = new HashMap<String, Integer[]>();
28                 tmpMap.put("k", new Integer[] {new Integer(1),new Integer(Integer.MAX_VALUE)});
29                 tmpMap.put("m", new Integer[] {new Integer(2),new Integer(2)});
30                 tmpMap.put("w", new Integer[] {new Integer(3),new Integer(Integer.MAX_VALUE)});
31                 tmpMap.put("packetsize", new Integer[] {new Integer(1),new Integer(Integer.MAX_VALUE)});
32                 
33                 codecParameterMinMax  = Collections.unmodifiableMap(tmpMap);                            
34         }
35         
36         
37         private static final CodecDescription codecDescription = new CodecDescription(
38         codecID, codecName, codecDescriptionText, codecParameterMinMax);
39         
40         private int[] matrix;
41         private int[] bitmatrix;
42         private byte[][] coding_ptrs;
43         private byte[][] data_ptrs;
44         private byte[][] dataAndCoding_ptrs;
45  
46         private int [] erasures;
47         private int numerased=0;
48
49         
50         public static final CodecDescription getCodecDescription(){
51                 //System.out.println("JerasureCodecParity: getCodecDescription: "+JerasureCodecParity.codecDescription);
52                 return CodecLiberation.codecDescription;
53         }
54         
55         /**
56          * @param codecParameter
57          */
58         public CodecLiberation(Map<String, Integer> codecParameter) {
59                 super();
60                 
61                 //Test CodecParameter
62                 for (Entry<String, Integer[]> requiredEntry : CodecLiberation.codecParameterMinMax
63                                 .entrySet()) {
64                         if (codecParameter.containsKey(requiredEntry.getKey())) {
65                                 // Test ob Parameter vorhanden und in den gültigen grenzen
66                                 // liegen
67                                 //System.out.println("Test" +codecParameter.get(requiredEntry.getKey()) +" "+  requiredEntry.getValue()[0]);
68                                 if (codecParameter.get(requiredEntry.getKey()) < requiredEntry.getValue()[0])
69                                         throw new IllegalArgumentException(
70                                                         "unzulässiger Wert für Parameter: "
71                                                                         + requiredEntry.getKey() + " < "
72                                                                         + requiredEntry.getValue()[0]);
73                                 if (codecParameter.get(requiredEntry.getKey()) > requiredEntry.getValue()[1])
74                                         throw new IllegalArgumentException(
75                                                         "unzulässiger Wert für Parameter: "
76                                                                         + requiredEntry.getKey() + " > "
77                                                                         + requiredEntry.getValue()[1]);
78                         } else {
79                                 throw new IllegalArgumentException("Missing Codec Parameter: "
80                                                 + requiredEntry.getKey());
81                         }
82                 }
83                 
84                 k=(int)codecParameter.get("k");
85                 m=(int)codecParameter.get("m");
86                 w=(int)codecParameter.get("w");
87                 packetsize=(int)codecParameter.get("packetsize");
88                 bitmatrix = Liberation.liberation_coding_bitmatrix(k, w);
89                 erasures = new int [k+m+1];
90                 
91         }
92
93         @Override
94         public byte[][] decode(byte[][] dataAndCoding_ptrs, boolean [] erased){
95                 //TODO Array Testen ob zulässig? eventuell nicht, da es hier bremsen könnte
96                 int fragmentsize = dataAndCoding_ptrs[0].length;
97                 
98                 if (data_ptrs == null || (data_ptrs.length != k) || (data_ptrs[0].length != fragmentsize)){
99                         data_ptrs = new byte[k][fragmentsize];
100                 }                       
101                 
102                 if (coding_ptrs == null || (coding_ptrs.length != m) || (coding_ptrs[0].length != fragmentsize)){
103                         coding_ptrs = new byte[m][fragmentsize];
104                 }
105                 
106                 for (int i=0; i<k ; i++){
107                         data_ptrs[i]=dataAndCoding_ptrs[i];
108                 }
109                 for (int i=k; i<k+m ; i++){
110                         coding_ptrs[i-k]=dataAndCoding_ptrs[i];
111                 }
112                 
113                 //Preparing Erasures
114                 numerased=0;
115                 for (int i=0; i<m+k; i++){
116                         if (erased[i]==true){
117                                 erasures[numerased]=i;
118                                 numerased++;
119                         }
120                 }
121                 erasures[numerased]=-1;
122                 
123
124                 //Jerasure.jerasure_matrix_encode(k, m, w, matrix, data_ptrs, coding_ptrs, blocksize);
125                 Jerasure.jerasure_bitmatrix_decode(k, m, w, bitmatrix, false, erasures, data_ptrs, coding_ptrs, fragmentsize, packetsize);
126
127                 return data_ptrs;
128         }
129
130         @Override
131         public byte[][] encode(byte[][] data_ptrs){
132                 //TODO Array Testen ob zulässig? eventuell nicht, da es hier bremsen könnte
133                 // buffersize%(sizeof(int)*w*k) == 0
134                 int fragmentsize = data_ptrs[0].length;
135                 
136                 if (coding_ptrs == null || (coding_ptrs.length != m) || (coding_ptrs[0].length != fragmentsize)){
137                         coding_ptrs = new byte[m][fragmentsize];
138                 }
139                 
140                 //Jerasure.jerasure_matrix_encode(k, m, w, matrix, data_ptrs, coding_ptrs, blocksize);
141                 Jerasure.jerasure_bitmatrix_encode(k, m, w, bitmatrix, data_ptrs, coding_ptrs, fragmentsize, packetsize);
142                 
143                 if (dataAndCoding_ptrs == null || (dataAndCoding_ptrs.length != k+m) || (dataAndCoding_ptrs[0].length != fragmentsize)){
144                         dataAndCoding_ptrs = new byte[k+m][fragmentsize];
145                 }
146                 //Data Blocks
147                 for (int i=0; i<k ; i++){
148                         dataAndCoding_ptrs[i]=data_ptrs[i];
149                 }
150                 for (int i=k; i<k+m ; i++){
151                         dataAndCoding_ptrs[i]=coding_ptrs[i-k];
152                 }
153                                 
154                 return dataAndCoding_ptrs;
155         }
156
157
158 }