summaryrefslogtreecommitdiffstats
path: root/authz-cass/src/main/java/com/att/dao/Loader.java
blob: 6b09df9620a69ff7bb192c93dcfa6645a36ad5ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aai
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * Copyright © 2017 Amdocs
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package com.att.dao;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.datastax.driver.core.Row;

public abstract class Loader<DATA> {
	private int keylimit;
	public Loader(int keylimit) {
		this.keylimit = keylimit;
	}
	
	public int keylimit() {
		return keylimit;
	}
	
	protected abstract DATA load(DATA data, Row row);
	protected abstract void key(DATA data, int idx, Object[] obj);
	protected abstract void body(DATA data, int idx, Object[] obj);

	public final Object[] extract(DATA data, int size, CassDAOImpl.CRUD type) {
		Object[] rv=null;
		switch(type) {
			case delete:
				rv = new Object[keylimit()];
				key(data,0,rv);
				break;
			case update:
				rv = new Object[size];
				body(data,0,rv);
				int body = size-keylimit();
				if(body>0) {
				    key(data,body,rv);
				}
				break;
			default:
				rv = new Object[size];
				key(data,0,rv);
				if(size>keylimit()) {
				    body(data,keylimit(),rv);
				}
				break;
		}
		return rv;
	}
	
	public static void writeString(DataOutputStream os, String s) throws IOException {
		if(s==null) {
			os.writeInt(-1);
		} else {
			switch(s.length()) {
				case 0:
					os.writeInt(0);
					break;
				default:
					byte[] bytes = s.getBytes();
					os.writeInt(bytes.length);
					os.write(bytes);
			}
		}
	}
	
	/**
	 * We use bytes here to set a Maximum
	 * 
	 * @param is
	 * @param MAX
	 * @return
	 * @throws IOException
	 */
	public static String readString(DataInputStream is, byte[] _buff) throws IOException {
		int l = is.readInt();
		byte[] buff = _buff;
		switch(l) {
			case -1: return null;
			case  0: return "";
			default:
				// Cover case where there is a large string, without always allocating a large buffer.
				if(l>buff.length) {
				    buff = new byte[l];
				}
				is.read(buff,0,l);
				return new String(buff,0,l);
		}
	}

	/**
	 * Write a set with proper sizing
	 * 
	 * Note: at the moment, this is just String.  Probably can develop system where types
	 * are supported too... but not now.
	 * 
	 * @param os
	 * @param set
	 * @throws IOException
	 */
	public static void writeStringSet(DataOutputStream os, Collection<String> set) throws IOException {
		if(set==null) {
			os.writeInt(-1);
		} else {
			os.writeInt(set.size());
			for(String s : set) {
				writeString(os, s);
			}
		}

	}
	
	public static Set<String> readStringSet(DataInputStream is, byte[] buff) throws IOException {
		int l = is.readInt();
		if(l<0) {
		    return null;
		}
		Set<String> set = new HashSet<String>(l);
		for(int i=0;i<l;++i) {
			set.add(readString(is,buff));
		}
		return set;
	}
	
	public static List<String> readStringList(DataInputStream is, byte[] buff) throws IOException {
		int l = is.readInt();
		if(l<0) {
		    return null;
		}
		List<String> list = new ArrayList<String>(l);
		for(int i=0;i<l;++i) {
			list.add(Loader.readString(is,buff));
		}
		return list;
	}

	/** 
	 * Write a map
	 * @param os
	 * @param map
	 * @throws IOException
	 */
	public static void writeStringMap(DataOutputStream os, Map<String,String> map) throws IOException {
		if(map==null) {
			os.writeInt(-1);
		} else {
			Set<Entry<String, String>> es = map.entrySet();
			os.writeInt(es.size());
			for(Entry<String,String> e : es) {
				writeString(os, e.getKey());
				writeString(os, e.getValue());
			}
		}

	}

	public static Map<String,String> readStringMap(DataInputStream is, byte[] buff) throws IOException {
		int l = is.readInt();
		if(l<0) {
		    return null;
		}
		Map<String,String> map = new HashMap<String,String>(l);
		for(int i=0;i<l;++i) {
			String key = readString(is,buff);
			map.put(key,readString(is,buff));
		}
		return map;
	}
	public static void writeHeader(DataOutputStream os, int magic, int version) throws IOException {
		os.writeInt(magic);
		os.writeInt(version);
	}
	
	public static int readHeader(DataInputStream is, final int magic, final int version) throws IOException {
		if(is.readInt()!=magic) {
		    throw new IOException("Corrupted Data Stream");
		}
		int v = is.readInt();
		if(version<0 || v>version) {
		    throw new IOException("Unsupported Data Version: " + v);
		}
		return v;
	}

}