[AAF-21] Initial code import
[aaf/authz.git] / authz-cass / src / main / java / com / att / dao / Loader.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.dao;\r
25 \r
26 import java.io.DataInputStream;\r
27 import java.io.DataOutputStream;\r
28 import java.io.IOException;\r
29 import java.util.ArrayList;\r
30 import java.util.Collection;\r
31 import java.util.HashMap;\r
32 import java.util.HashSet;\r
33 import java.util.List;\r
34 import java.util.Map;\r
35 import java.util.Map.Entry;\r
36 import java.util.Set;\r
37 \r
38 import com.datastax.driver.core.Row;\r
39 \r
40 public abstract class Loader<DATA> {\r
41         private int keylimit;\r
42         public Loader(int keylimit) {\r
43                 this.keylimit = keylimit;\r
44         }\r
45         \r
46         public int keylimit() {\r
47                 return keylimit;\r
48         }\r
49         \r
50         protected abstract DATA load(DATA data, Row row);\r
51         protected abstract void key(DATA data, int idx, Object[] obj);\r
52         protected abstract void body(DATA data, int idx, Object[] obj);\r
53 \r
54         public final Object[] extract(DATA data, int size, CassDAOImpl.CRUD type) {\r
55                 Object[] rv=null;\r
56                 switch(type) {\r
57                         case delete:\r
58                                 rv = new Object[keylimit()];\r
59                                 key(data,0,rv);\r
60                                 break;\r
61                         case update:\r
62                                 rv = new Object[size];\r
63                                 body(data,0,rv);\r
64                                 int body = size-keylimit();\r
65                                 if(body>0) {\r
66                                     key(data,body,rv);\r
67                                 }\r
68                                 break;\r
69                         default:\r
70                                 rv = new Object[size];\r
71                                 key(data,0,rv);\r
72                                 if(size>keylimit()) {\r
73                                     body(data,keylimit(),rv);\r
74                                 }\r
75                                 break;\r
76                 }\r
77                 return rv;\r
78         }\r
79         \r
80         public static void writeString(DataOutputStream os, String s) throws IOException {\r
81                 if(s==null) {\r
82                         os.writeInt(-1);\r
83                 } else {\r
84                         switch(s.length()) {\r
85                                 case 0:\r
86                                         os.writeInt(0);\r
87                                         break;\r
88                                 default:\r
89                                         byte[] bytes = s.getBytes();\r
90                                         os.writeInt(bytes.length);\r
91                                         os.write(bytes);\r
92                         }\r
93                 }\r
94         }\r
95         \r
96         /**\r
97          * We use bytes here to set a Maximum\r
98          * \r
99          * @param is\r
100          * @param MAX\r
101          * @return\r
102          * @throws IOException\r
103          */\r
104         public static String readString(DataInputStream is, byte[] _buff) throws IOException {\r
105                 int l = is.readInt();\r
106                 byte[] buff = _buff;\r
107                 switch(l) {\r
108                         case -1: return null;\r
109                         case  0: return "";\r
110                         default:\r
111                                 // Cover case where there is a large string, without always allocating a large buffer.\r
112                                 if(l>buff.length) {\r
113                                     buff = new byte[l];\r
114                                 }\r
115                                 is.read(buff,0,l);\r
116                                 return new String(buff,0,l);\r
117                 }\r
118         }\r
119 \r
120         /**\r
121          * Write a set with proper sizing\r
122          * \r
123          * Note: at the moment, this is just String.  Probably can develop system where types\r
124          * are supported too... but not now.\r
125          * \r
126          * @param os\r
127          * @param set\r
128          * @throws IOException\r
129          */\r
130         public static void writeStringSet(DataOutputStream os, Collection<String> set) throws IOException {\r
131                 if(set==null) {\r
132                         os.writeInt(-1);\r
133                 } else {\r
134                         os.writeInt(set.size());\r
135                         for(String s : set) {\r
136                                 writeString(os, s);\r
137                         }\r
138                 }\r
139 \r
140         }\r
141         \r
142         public static Set<String> readStringSet(DataInputStream is, byte[] buff) throws IOException {\r
143                 int l = is.readInt();\r
144                 if(l<0) {\r
145                     return null;\r
146                 }\r
147                 Set<String> set = new HashSet<String>(l);\r
148                 for(int i=0;i<l;++i) {\r
149                         set.add(readString(is,buff));\r
150                 }\r
151                 return set;\r
152         }\r
153         \r
154         public static List<String> readStringList(DataInputStream is, byte[] buff) throws IOException {\r
155                 int l = is.readInt();\r
156                 if(l<0) {\r
157                     return null;\r
158                 }\r
159                 List<String> list = new ArrayList<String>(l);\r
160                 for(int i=0;i<l;++i) {\r
161                         list.add(Loader.readString(is,buff));\r
162                 }\r
163                 return list;\r
164         }\r
165 \r
166         /** \r
167          * Write a map\r
168          * @param os\r
169          * @param map\r
170          * @throws IOException\r
171          */\r
172         public static void writeStringMap(DataOutputStream os, Map<String,String> map) throws IOException {\r
173                 if(map==null) {\r
174                         os.writeInt(-1);\r
175                 } else {\r
176                         Set<Entry<String, String>> es = map.entrySet();\r
177                         os.writeInt(es.size());\r
178                         for(Entry<String,String> e : es) {\r
179                                 writeString(os, e.getKey());\r
180                                 writeString(os, e.getValue());\r
181                         }\r
182                 }\r
183 \r
184         }\r
185 \r
186         public static Map<String,String> readStringMap(DataInputStream is, byte[] buff) throws IOException {\r
187                 int l = is.readInt();\r
188                 if(l<0) {\r
189                     return null;\r
190                 }\r
191                 Map<String,String> map = new HashMap<String,String>(l);\r
192                 for(int i=0;i<l;++i) {\r
193                         String key = readString(is,buff);\r
194                         map.put(key,readString(is,buff));\r
195                 }\r
196                 return map;\r
197         }\r
198         public static void writeHeader(DataOutputStream os, int magic, int version) throws IOException {\r
199                 os.writeInt(magic);\r
200                 os.writeInt(version);\r
201         }\r
202         \r
203         public static int readHeader(DataInputStream is, final int magic, final int version) throws IOException {\r
204                 if(is.readInt()!=magic) {\r
205                     throw new IOException("Corrupted Data Stream");\r
206                 }\r
207                 int v = is.readInt();\r
208                 if(version<0 || v>version) {\r
209                     throw new IOException("Unsupported Data Version: " + v);\r
210                 }\r
211                 return v;\r
212         }\r
213 \r
214 }\r
215 \r