1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
12 * * http://www.apache.org/licenses/LICENSE-2.0
\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
21 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 ******************************************************************************/
\r
24 package com.att.dao;
\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
38 import com.datastax.driver.core.Row;
\r
40 public abstract class Loader<DATA> {
\r
41 private int keylimit;
\r
42 public Loader(int keylimit) {
\r
43 this.keylimit = keylimit;
\r
46 public int keylimit() {
\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
54 public final Object[] extract(DATA data, int size, CassDAOImpl.CRUD type) {
\r
58 rv = new Object[keylimit()];
\r
62 rv = new Object[size];
\r
64 int body = size-keylimit();
\r
70 rv = new Object[size];
\r
72 if(size>keylimit()) {
\r
73 body(data,keylimit(),rv);
\r
80 public static void writeString(DataOutputStream os, String s) throws IOException {
\r
84 switch(s.length()) {
\r
89 byte[] bytes = s.getBytes();
\r
90 os.writeInt(bytes.length);
\r
97 * We use bytes here to set a Maximum
\r
102 * @throws IOException
\r
104 public static String readString(DataInputStream is, byte[] _buff) throws IOException {
\r
105 int l = is.readInt();
\r
106 byte[] buff = _buff;
\r
108 case -1: return null;
\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
116 return new String(buff,0,l);
\r
121 * Write a set with proper sizing
\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
128 * @throws IOException
\r
130 public static void writeStringSet(DataOutputStream os, Collection<String> set) throws IOException {
\r
134 os.writeInt(set.size());
\r
135 for(String s : set) {
\r
136 writeString(os, s);
\r
142 public static Set<String> readStringSet(DataInputStream is, byte[] buff) throws IOException {
\r
143 int l = is.readInt();
\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
154 public static List<String> readStringList(DataInputStream is, byte[] buff) throws IOException {
\r
155 int l = is.readInt();
\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
170 * @throws IOException
\r
172 public static void writeStringMap(DataOutputStream os, Map<String,String> map) throws IOException {
\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
186 public static Map<String,String> readStringMap(DataInputStream is, byte[] buff) throws IOException {
\r
187 int l = is.readInt();
\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
198 public static void writeHeader(DataOutputStream os, int magic, int version) throws IOException {
\r
199 os.writeInt(magic);
\r
200 os.writeInt(version);
\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
207 int v = is.readInt();
\r
208 if(version<0 || v>version) {
\r
209 throw new IOException("Unsupported Data Version: " + v);
\r