1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.dao;
\r
25 import java.io.DataInputStream;
\r
26 import java.io.DataOutputStream;
\r
27 import java.io.IOException;
\r
28 import java.util.ArrayList;
\r
29 import java.util.Collection;
\r
30 import java.util.HashMap;
\r
31 import java.util.HashSet;
\r
32 import java.util.List;
\r
33 import java.util.Map;
\r
34 import java.util.Map.Entry;
\r
35 import java.util.Set;
\r
37 import com.datastax.driver.core.Row;
\r
39 public abstract class Loader<DATA> {
\r
40 private int keylimit;
\r
41 public Loader(int keylimit) {
\r
42 this.keylimit = keylimit;
\r
45 public int keylimit() {
\r
49 protected abstract DATA load(DATA data, Row row);
\r
50 protected abstract void key(DATA data, int idx, Object[] obj);
\r
51 protected abstract void body(DATA data, int idx, Object[] obj);
\r
53 public final Object[] extract(DATA data, int size, CassDAOImpl.CRUD type) {
\r
57 rv = new Object[keylimit()];
\r
61 rv = new Object[size];
\r
63 int body = size-keylimit();
\r
69 rv = new Object[size];
\r
71 if(size>keylimit()) {
\r
72 body(data,keylimit(),rv);
\r
79 public static void writeString(DataOutputStream os, String s) throws IOException {
\r
83 switch(s.length()) {
\r
88 byte[] bytes = s.getBytes();
\r
89 os.writeInt(bytes.length);
\r
96 * We use bytes here to set a Maximum
\r
101 * @throws IOException
\r
103 public static String readString(DataInputStream is, byte[] _buff) throws IOException {
\r
104 int l = is.readInt();
\r
105 byte[] buff = _buff;
\r
107 case -1: return null;
\r
110 // Cover case where there is a large string, without always allocating a large buffer.
\r
111 if(l>buff.length) {
\r
112 buff = new byte[l];
\r
115 return new String(buff,0,l);
\r
120 * Write a set with proper sizing
\r
122 * Note: at the moment, this is just String. Probably can develop system where types
\r
123 * are supported too... but not now.
\r
127 * @throws IOException
\r
129 public static void writeStringSet(DataOutputStream os, Collection<String> set) throws IOException {
\r
133 os.writeInt(set.size());
\r
134 for(String s : set) {
\r
135 writeString(os, s);
\r
141 public static Set<String> readStringSet(DataInputStream is, byte[] buff) throws IOException {
\r
142 int l = is.readInt();
\r
146 Set<String> set = new HashSet<String>(l);
\r
147 for(int i=0;i<l;++i) {
\r
148 set.add(readString(is,buff));
\r
153 public static List<String> readStringList(DataInputStream is, byte[] buff) throws IOException {
\r
154 int l = is.readInt();
\r
158 List<String> list = new ArrayList<String>(l);
\r
159 for(int i=0;i<l;++i) {
\r
160 list.add(Loader.readString(is,buff));
\r
169 * @throws IOException
\r
171 public static void writeStringMap(DataOutputStream os, Map<String,String> map) throws IOException {
\r
175 Set<Entry<String, String>> es = map.entrySet();
\r
176 os.writeInt(es.size());
\r
177 for(Entry<String,String> e : es) {
\r
178 writeString(os, e.getKey());
\r
179 writeString(os, e.getValue());
\r
185 public static Map<String,String> readStringMap(DataInputStream is, byte[] buff) throws IOException {
\r
186 int l = is.readInt();
\r
190 Map<String,String> map = new HashMap<String,String>(l);
\r
191 for(int i=0;i<l;++i) {
\r
192 String key = readString(is,buff);
\r
193 map.put(key,readString(is,buff));
\r
197 public static void writeHeader(DataOutputStream os, int magic, int version) throws IOException {
\r
198 os.writeInt(magic);
\r
199 os.writeInt(version);
\r
202 public static int readHeader(DataInputStream is, final int magic, final int version) throws IOException {
\r
203 if(is.readInt()!=magic) {
\r
204 throw new IOException("Corrupted Data Stream");
\r
206 int v = is.readInt();
\r
207 if(version<0 || v>version) {
\r
208 throw new IOException("Unsupported Data Version: " + v);
\r