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