jpayne@68: package kmer; jpayne@68: jpayne@68: import java.util.concurrent.atomic.AtomicLong; jpayne@68: jpayne@68: import fileIO.ByteStreamWriter; jpayne@68: import fileIO.TextStreamWriter; jpayne@68: import structures.ByteBuilder; jpayne@68: import structures.SuperLongList; jpayne@68: jpayne@68: /** jpayne@68: * @author Brian Bushnell jpayne@68: * @date Nov 22, 2013 jpayne@68: * jpayne@68: */ jpayne@68: public class HashBuffer extends AbstractKmerTable { jpayne@68: jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: /*---------------- Initialization ----------------*/ jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: jpayne@68: public HashBuffer(AbstractKmerTable[] tables_, int buflen_, int k_, boolean initValues, boolean setIfNotPresent_){ jpayne@68: tables=tables_; jpayne@68: buflen=buflen_; jpayne@68: halflen=(int)Math.ceil(buflen*0.5); jpayne@68: ways=tables.length; jpayne@68: buffers=new KmerBuffer[ways]; jpayne@68: setIfNotPresent=setIfNotPresent_; jpayne@68: useValues=initValues; jpayne@68: coreMask=(AbstractKmerTableSet.MASK_CORE ? ~(((-1L)<<(2*(k_-1)))|3) : -1L); jpayne@68: for(int i=0; i=halflen && (size>=buflen || (size&SIZEMASK)==0)){ jpayne@68: return dumpBuffer(way, size>=buflen); jpayne@68: } jpayne@68: return 0; jpayne@68: } jpayne@68: jpayne@68: @Override jpayne@68: public final long flush(){ jpayne@68: long added=0; jpayne@68: for(int i=0; i=halflen && (size>=buflen || (size&SIZEMASK)==0)){ jpayne@68: return dumpBuffer(way, size>=buflen); jpayne@68: } jpayne@68: return 0; jpayne@68: } jpayne@68: jpayne@68: @Override jpayne@68: public int set(long kmer, int[] vals, int vlen) { jpayne@68: throw new RuntimeException("Unimplemented method; this class lacks value buffers"); jpayne@68: } jpayne@68: jpayne@68: @Override jpayne@68: public int setIfNotPresent(long kmer, int value) { jpayne@68: throw new RuntimeException("Unimplemented method; this class lacks value buffers"); jpayne@68: } jpayne@68: jpayne@68: @Override jpayne@68: public int getValue(long kmer) { jpayne@68: final int way=kmerToWay(kmer); jpayne@68: return tables[way].getValue(kmer); jpayne@68: } jpayne@68: jpayne@68: @Override jpayne@68: public int[] getValues(long kmer, int[] singleton){ jpayne@68: final int way=kmerToWay(kmer); jpayne@68: return tables[way].getValues(kmer, singleton); jpayne@68: } jpayne@68: jpayne@68: @Override jpayne@68: public boolean contains(long kmer) { jpayne@68: final int way=kmerToWay(kmer); jpayne@68: return tables[way].contains(kmer); jpayne@68: } jpayne@68: jpayne@68: jpayne@68: jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: /*---------------- Ownership ----------------*/ jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: jpayne@68: @Override jpayne@68: public final void initializeOwnership(){ jpayne@68: for(AbstractKmerTable t : tables){t.initializeOwnership();} jpayne@68: } jpayne@68: jpayne@68: @Override jpayne@68: public final void clearOwnership(){ jpayne@68: for(AbstractKmerTable t : tables){t.clearOwnership();} jpayne@68: } jpayne@68: jpayne@68: @Override jpayne@68: public final int setOwner(final long kmer, final int newOwner){ jpayne@68: final int way=kmerToWay(kmer); jpayne@68: return tables[way].setOwner(kmer, newOwner); jpayne@68: } jpayne@68: jpayne@68: @Override jpayne@68: public final boolean clearOwner(final long kmer, final int owner){ jpayne@68: final int way=kmerToWay(kmer); jpayne@68: return tables[way].clearOwner(kmer, owner); jpayne@68: } jpayne@68: jpayne@68: @Override jpayne@68: public final int getOwner(final long kmer){ jpayne@68: final int way=kmerToWay(kmer); jpayne@68: return tables[way].getOwner(kmer); jpayne@68: } jpayne@68: jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: /*---------------- Nonpublic Methods ----------------*/ jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: jpayne@68: @Override jpayne@68: Object get(long kmer) { jpayne@68: final int way=kmerToWay(kmer); jpayne@68: return tables[way].get(kmer); jpayne@68: } jpayne@68: jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: /*---------------- Private Methods ----------------*/ jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: jpayne@68: private int dumpBuffer(final int way, boolean force){ jpayne@68: final KmerBuffer buffer=buffers[way]; jpayne@68: final AbstractKmerTable table=tables[way]; jpayne@68: final int lim=buffer.size(); jpayne@68: if(lim<0){return 0;} jpayne@68: jpayne@68: if(force){table.lock();} jpayne@68: else if(!table.tryLock()){return 0;} jpayne@68: jpayne@68: if(SORT_BUFFERS && buffer.values==null){//Can go before or after lock; neither helps much jpayne@68: buffer.kmers.sortSerial(); jpayne@68: } jpayne@68: jpayne@68: final int x=dumpBuffer_inner(way); jpayne@68: table.unlock(); jpayne@68: return x; jpayne@68: } jpayne@68: jpayne@68: private int dumpBuffer_inner(final int way){ jpayne@68: if(verbose){System.err.println("Dumping buffer for way "+way+" of "+ways);} jpayne@68: final KmerBuffer buffer=buffers[way]; jpayne@68: final int lim=buffer.size(); jpayne@68: if(lim<1){return 0;} jpayne@68: final long[] kmers=buffer.kmers.array; jpayne@68: final int[] values=(buffer.values==null ? null : buffer.values.array); jpayne@68: if(lim<1){return 0;} jpayne@68: int added=0; jpayne@68: final AbstractKmerTable table=tables[way]; jpayne@68: // synchronized(table){ jpayne@68: if(values==null){ jpayne@68: // Arrays.sort(kmers, 0, lim); //Makes it slower jpayne@68: if(SORT_BUFFERS){ jpayne@68: long prev=-1; jpayne@68: int sum=0; jpayne@68: for(int i=0; i0){added+=table.incrementAndReturnNumCreated(prev, sum);} jpayne@68: prev=kmer; jpayne@68: sum=1; jpayne@68: } jpayne@68: } jpayne@68: if(sum>0){added+=table.incrementAndReturnNumCreated(prev, sum);} jpayne@68: }else{ jpayne@68: for(int i=0; i