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.local;
25 import java.io.IOException;
26 import java.io.RandomAccessFile;
27 import java.nio.ByteBuffer;
28 import java.nio.IntBuffer;
29 import java.nio.channels.FileChannel;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.LinkedList;
33 import java.util.List;
35 import org.onap.aaf.auth.local.DataFile.Token;
36 import org.onap.aaf.auth.local.DataFile.Token.Field;
37 import org.onap.aaf.misc.env.Env;
38 import org.onap.aaf.misc.env.TimeTaken;
39 import org.onap.aaf.misc.env.Trans;
41 public class TextIndex {
42 private static final int REC_SIZE=8;
45 private DataFile dataFile=null;
47 public TextIndex(File theFile) {
51 public void open() throws IOException {
52 dataFile = new DataFile(file,"r");
56 public void close() throws IOException {
63 public int find(Object key, AbsData.Reuse reuse, int offset) throws IOException {
64 return find(key,reuse.tokenData,reuse.getFieldData(),offset);
67 public int find(Object key, DataFile.Token dtok, Field df, int offset) throws IOException {
69 throw new IOException("File not opened");
71 long hash = hashToLong(key.hashCode());
72 int min=0, max = (int)(dataFile.size()/REC_SIZE);
73 Token ttok = dataFile.new Token(REC_SIZE);
74 IntBuffer tib = ttok.getIntBuffer();
77 while ((max-min)>100) {
78 curr=(min+(max-min)/2);
79 ttok.pos(curr*REC_SIZE);
81 lhash = hashToLong(tib.get());
84 } else if (lhash>hash) {
93 List<Integer> entries = new ArrayList<>();
94 for (int i=min;i<=max;++i) {
97 lhash = hashToLong(tib.get());
99 entries.add(tib.get());
100 } else if (lhash>hash) {
105 for (Integer i : entries) {
107 if (df.at(offset).equals(key)) {
116 * Have to change Bytes into a Long, to avoid the inevitable signs in the Hash
118 private static long hashToLong(int hash) {
121 rv = 0xFFFFFFFFL & hash;
128 public void create(final Trans trans,final DataFile data, int maxLine, char delim, int fieldOffset, int skipLines) throws IOException {
131 List<Idx> list = new LinkedList<>(); // Some hashcodes will double... DO NOT make a set
132 TimeTaken tt2 = trans.start("Open Files", Env.SUB);
133 RandomAccessFile raf=null;
136 raf = new RandomAccessFile(file,"rw");
138 fos = raf.getChannel();
145 Token t = data.new Token(maxLine);
146 Field f = t.new Field(delim);
150 trans.info().log("Skipping",skipLines,"line"+(skipLines==1?" in":"s in"),data.file().getName());
152 for (int i=0;i<skipLines;++i) {
155 tt2 = trans.start("Read", Env.SUB);
157 while (t.nextLine()) {
158 list.add(new Idx(f.at(fieldOffset),t.pos()));
164 trans.checkpoint(" Read " + count + " records");
165 tt2 = trans.start("Sort List", Env.SUB);
166 Collections.sort(list);
168 tt2 = trans.start("Write Idx", Env.SUB);
170 ByteBuffer bb = ByteBuffer.allocate(8*1024);
171 IntBuffer ib = bb.asIntBuffer();
172 for (Idx idx : list) {
173 if (!ib.hasRemaining()) {
181 bb.limit(4*ib.position());
191 raf.close(); // closed by fos
200 private IntBuffer ib;
206 end = dataFile.size();
207 t = dataFile.new Token(REC_SIZE);
208 ib = t.getIntBuffer();
210 } catch (IOException e) {
224 public boolean hasNext() {
229 private static class Idx implements Comparable<Idx> {
230 public int hash, pos;
231 public Idx(Object obj, int pos) {
232 hash = obj.hashCode();
237 public int compareTo(Idx ib) {
238 long a = hashToLong(hash);
239 long b = hashToLong(ib.hash);
240 return a>b?1:a<b?-1:0;
244 * @see java.lang.Object#equals(java.lang.Object)
247 public boolean equals(Object o) {
248 if (o!=null && o instanceof Idx) {
249 return hash == ((Idx)o).hash;
255 * @see java.lang.Object#hashCode()
258 public int hashCode() {