X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=auth%2Fauth-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faaf%2Fauth%2Flocal%2FTextIndex.java;h=d35ac4af13a747064fecbe60479b7a16c8317ed5;hb=6f28795822d50e03bda5d0937d7ab4eb4f37a80d;hp=6ef6a769d1bf059bbefb070a78c3de653c2958f2;hpb=ceda6e8bc270202bcb24340b86617110289c902e;p=aaf%2Fauthz.git diff --git a/auth/auth-core/src/main/java/org/onap/aaf/auth/local/TextIndex.java b/auth/auth-core/src/main/java/org/onap/aaf/auth/local/TextIndex.java index 6ef6a769..d35ac4af 100644 --- a/auth/auth-core/src/main/java/org/onap/aaf/auth/local/TextIndex.java +++ b/auth/auth-core/src/main/java/org/onap/aaf/auth/local/TextIndex.java @@ -7,9 +7,9 @@ * 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. @@ -39,223 +39,224 @@ import org.onap.aaf.misc.env.TimeTaken; import org.onap.aaf.misc.env.Trans; public class TextIndex { - private static final int REC_SIZE=8; - - private File file; - private DataFile dataFile=null; - - public TextIndex(File theFile) { - file = theFile; - } - - public void open() throws IOException { - dataFile = new DataFile(file,"r"); - dataFile.open(); - } - - public void close() throws IOException { - if(dataFile!=null) { - dataFile.close(); - dataFile=null; - } - } + private static final int REC_SIZE=8; + + private File file; + private DataFile dataFile=null; + + public TextIndex(File theFile) { + file = theFile; + } + + public void open() throws IOException { + dataFile = new DataFile(file,"r"); + dataFile.open(); + } + + public void close() throws IOException { + if (dataFile!=null) { + dataFile.close(); + dataFile=null; + } + } + + public int find(Object key, AbsData.Reuse reuse, int offset) throws IOException { + return find(key,reuse.tokenData,reuse.getFieldData(),offset); + } + + public int find(Object key, DataFile.Token dtok, Field df, int offset) throws IOException { + if (dataFile==null) { + throw new IOException("File not opened"); + } + long hash = hashToLong(key.hashCode()); + int min=0, max = (int)(dataFile.size()/REC_SIZE); + Token ttok = dataFile.new Token(REC_SIZE); + IntBuffer tib = ttok.getIntBuffer(); + long lhash; + int curr; + while ((max-min)>100) { + curr=(min+(max-min)/2); + ttok.pos(curr*REC_SIZE); + tib.rewind(); + lhash = hashToLong(tib.get()); + if (lhashhash) { + max=curr-1; + } else { + min=curr-40; + max=curr+40; + break; + } + } + + List entries = new ArrayList<>(); + for (int i=min;i<=max;++i) { + ttok.pos(i*REC_SIZE); + tib.rewind(); + lhash = hashToLong(tib.get()); + if (lhash==hash) { + entries.add(tib.get()); + } else if (lhash>hash) { + break; + } + } + + for (Integer i : entries) { + dtok.pos(i); + if (df.at(offset).equals(key)) { + return i; + } + } + return -1; + } + + + /* + * Have to change Bytes into a Long, to avoid the inevitable signs in the Hash + */ + private static long hashToLong(int hash) { + long rv; + if (hash<0) { + rv = 0xFFFFFFFFL & hash; + } else { + rv = hash; + } + return rv; + } + + public void create(final Trans trans,final DataFile data, int maxLine, char delim, int fieldOffset, int skipLines) throws IOException { + FileChannel fos; + + List list = new LinkedList<>(); // Some hashcodes will double... DO NOT make a set + TimeTaken tt2 = trans.start("Open Files", Env.SUB); + RandomAccessFile raf=null; + try { + try { + raf = new RandomAccessFile(file,"rw"); + raf.setLength(0L); + fos = raf.getChannel(); + } finally { + tt2.done(); + } + + try { + + Token t = data.new Token(maxLine); + Field f = t.new Field(delim); + + int count = 0; + if (skipLines>0) { + trans.info().log("Skipping",skipLines,"line"+(skipLines==1?" in":"s in"),data.file().getName()); + } + for (int i=0;i100) { - ttok.pos((curr=(min+(max-min)/2))*REC_SIZE); - tib.rewind(); - lhash = hashToLong(tib.get()); - if(lhashhash) { - max=curr-1; - } else { - min=curr-40; - max=curr+40; - break; - } - } - - List entries = new ArrayList(); - for(int i=min;i<=max;++i) { - ttok.pos(i*REC_SIZE); - tib.rewind(); - lhash = hashToLong(tib.get()); - if(lhash==hash) { - entries.add(tib.get()); - } else if(lhash>hash) { - break; - } - } - - for(Integer i : entries) { - dtok.pos(i); - if(df.at(offset).equals(key)) { - return i; - } - } - return -1; - } - + public Iter() { + try { + idx = 0; + end = dataFile.size(); + t = dataFile.new Token(REC_SIZE); + ib = t.getIntBuffer(); - /* - * Have to change Bytes into a Long, to avoid the inevitable signs in the Hash - */ - private static long hashToLong(int hash) { - long rv; - if(hash<0) { - rv = 0xFFFFFFFFL & hash; - } else { - rv = hash; - } - return rv; - } - - public void create(final Trans trans,final DataFile data, int maxLine, char delim, int fieldOffset, int skipLines) throws IOException { - FileChannel fos; - - List list = new LinkedList(); // Some hashcodes will double... DO NOT make a set - TimeTaken tt2 = trans.start("Open Files", Env.SUB); - RandomAccessFile raf=null; - try { - try { - raf = new RandomAccessFile(file,"rw"); - raf.setLength(0L); - fos = raf.getChannel(); - } finally { - tt2.done(); - } - - try { - - Token t = data.new Token(maxLine); - Field f = t.new Field(delim); - - int count = 0; - if(skipLines>0) { - trans.info().log("Skipping",skipLines,"line"+(skipLines==1?" in":"s in"),data.file().getName()); - } - for(int i=0;i { + public int hash, pos; + public Idx(Object obj, int pos) { + hash = obj.hashCode(); + this.pos = pos; + } - public boolean hasNext() { - return idx { - public int hash, pos; - public Idx(Object obj, int pos) { - hash = obj.hashCode(); - this.pos = pos; - } - - @Override - public int compareTo(Idx ib) { - long a = hashToLong(hash); - long b = hashToLong(ib.hash); - return a>b?1:ab?1:a