private ByteBuffer _getRecord(final long offset,final int nbytes) throws IllegalStateException, InterruptedException {
  ByteBuffer tmp=_readFromCache(offset,nbytes);
  if (tmp != null) {
    if (tmp.remaining() == 0)     throw new AssertionError();
    return tmp;
  }
  final boolean largeRecord=nbytes > capacity;
  final boolean directRead=largeRecord || this.readListSize == 0;
  if (directRead) {
    final ByteBuffer ret=_readFromLocalDiskIntoNewHeapByteBuffer(offset,nbytes);
    if (ret != null && ret.remaining() == 0)     throw new AssertionError();
    return ret;
  }
  ReadCache theCache=null;
  ByteBuffer bb=null;
  boolean willInstall=false;
  try {
synchronized (readCache) {
      theCache=readCache.get();
      if (theCache != null) {
        assert theCache.getReferenceCount() > 0;
        bb=theCache.allocate(nbytes);
        if (bb != null) {
          theCache.incrementReferenceCount();
          willInstall=true;
        }
 else {
          readCache.set(null);
          if (theCache.decrementReferenceCount() == 0) {
            readList.add(theCache);
          }
        }
      }
      if (bb == null) {
        assert readCache.get() == null;
        final ReadCache newCache=getDirectReadCache();
        if (newCache != null) {
          assert newCache.getReferenceCount() == 0;
{
            newCache.incrementReferenceCount();
            readCache.set(newCache);
          }
          bb=newCache.allocate(nbytes);
          theCache=newCache;
{
            theCache.incrementReferenceCount();
            willInstall=true;
          }
        }
      }
    }
    if (bb == null) {
      assert willInstall == false;
      return _readFromLocalDiskIntoNewHeapByteBuffer(offset,nbytes);
    }
    final int pos=bb.position();
    final ByteBuffer ret=reader.readRaw(offset,bb);
    final byte[] b=new byte[nbytes - 4];
    ret.get(b);
{
      final int datalen=nbytes - 4;
      final int chk=ret.getInt(pos + datalen);
      if (chk != ChecksumUtility.threadChk.get().checksum(b,0,datalen)) {
        throw new ChecksumError();
      }
    }
    theCache.commitToMap(offset,pos,nbytes);
    serviceMap.put(offset,theCache);
    return ByteBuffer.wrap(b);
  }
 catch (  Throwable t) {
    t.printStackTrace(System.err);
    throw new RuntimeException(t);
  }
 finally {
    if (willInstall && theCache.decrementReferenceCount() == 0) {
      readList.add(theCache);
      if (theCache == readCache.get())       throw new AssertionError();
    }
  }
}

private ByteBuffer _getRecord ( final long offset , final int nbytes ) throws IllegalStateException , InterruptedException { ByteBuffer tmp = _readFromCache ( offset , nbytes ) ; if ( tmp != null ) { if ( tmp . remaining ( ) == 0 ) throw new AssertionError ( ) ; return tmp ; } final boolean largeRecord = nbytes > capacity ; final boolean directRead = largeRecord || this . readListSize == 0 ; if ( directRead ) { final ByteBuffer ret = _readFromLocalDiskIntoNewHeapByteBuffer ( offset , nbytes ) ; if ( ret != null && ret . remaining ( ) == 0 ) throw new AssertionError ( ) ; return ret ; } ReadCache theCache = null ; ByteBuffer bb = null ; boolean willInstall = false ; try { synchronized ( readCache ) { theCache = readCache . get ( ) ; if ( theCache != null ) { assert theCache . getReferenceCount ( ) > 0 ; bb = theCache . allocate ( nbytes ) ; if ( bb != null ) { theCache . incrementReferenceCount ( ) ; willInstall = true ; } else { readCache . set ( null ) ; if ( theCache . decrementReferenceCount ( ) == 0 ) { readList . add ( theCache ) ; } } } if ( bb == null ) { assert readCache . get ( ) == null ; final ReadCache newCache = getDirectReadCache ( ) ; if ( newCache != null ) { assert newCache . getReferenceCount ( ) == 0 ; { newCache . incrementReferenceCount ( ) ; readCache . set ( newCache ) ; } bb = newCache . allocate ( nbytes ) ; theCache = newCache ; { theCache . incrementReferenceCount ( ) ; willInstall = true ; } } } } if ( bb == null ) { assert willInstall == false ; return _readFromLocalDiskIntoNewHeapByteBuffer ( offset , nbytes ) ; } final int pos = bb . position ( ) ; final ByteBuffer ret = reader . readRaw ( offset , bb ) ; final byte [ ] b = new byte [ nbytes - 4 ] ; ret . get ( b ) ; { final int datalen = nbytes - 4 ; final int chk = ret . getInt ( pos + datalen ) ; if ( chk != ChecksumUtility . threadChk . get ( ) . checksum ( b , 0 , datalen ) ) { throw new ChecksumError ( ) ; } } theCache . commitToMap ( offset , pos , nbytes ) ; serviceMap . put ( offset , theCache ) ; return ByteBuffer . wrap ( b ) ; } catch ( Throwable t ) { t . printStackTrace ( System . err ) ; throw new RuntimeException ( t ) ; } finally { if ( willInstall && theCache . decrementReferenceCount ( ) == 0 ) { readList . add ( theCache ) ; if ( theCache == readCache . get ( ) ) throw new AssertionError ( ) ; } } }
method invoked from within the memoizer pattern to read the record from the backing store and install it into the cache . the method must first verify that the record is not in the cache .
