Java Code Examples for java.nio.MappedByteBuffer#getLong()

The following examples show how to use java.nio.MappedByteBuffer#getLong() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Slopbucket.java    From jelectrum with MIT License 5 votes vote down vote up
private long getCurrentWriteLocation()
{
  MappedByteBuffer mbb = getBufferMap(0);
  long v;
  v = mbb.getLong((int)LOCATION_NEXT_FREE);
  if (v == 0) return LOCATION_FIRST_FREE;
  return v;

}
 
Example 2
Source File: Slopbucket.java    From jelectrum with MIT License 5 votes vote down vote up
protected Map<String, Integer> loadTroughMap()
{
  TreeMap<String,Integer> map = new TreeMap<>();

  MappedByteBuffer mbb = getBufferMap(0);
  synchronized(mbb)
  {
    for(int i=0; i<MAX_TROUGHS; i++)
    {
      mbb.position( (int)(LOCATION_TROUGH_TABLE_START + (8 + MAX_TROUGH_NAME_LEN) * i));
      long ptr = mbb.getLong();
      byte[] name = new byte[MAX_TROUGH_NAME_LEN];
      mbb.get(name);
      int len =0;
      for(int j=0; (j<MAX_TROUGH_NAME_LEN) && (name[j] != 0); j++)
      {
        len++;
      }
      if (len > 0)
      {
        String name_str = new String(name, 0, len);
        map.put(name_str, i);
      }
      else
      {
        map.put("__FREE", i);
      }
    }
  }
  return map;
}
 
Example 3
Source File: Slopbucket.java    From jelectrum with MIT License 5 votes vote down vote up
public long getTroughPtr(String name)
{
  Map<String, Integer> troughs = getTroughMap();
  if (!troughs.containsKey(name))
  {
    throw new RuntimeException("Unable to find trough: " + name);
  }
  int idx=troughs.get(name);
  MappedByteBuffer mbb = getBufferMap(0);
  synchronized(mbb)
  {
    mbb.position( (int) (LOCATION_TROUGH_TABLE_START + (8 + MAX_TROUGH_NAME_LEN) * idx) );
    return mbb.getLong();
  }
}
 
Example 4
Source File: ReadWriteMemMap.java    From sparkey-java with Apache License 2.0 5 votes vote down vote up
@Override
public long readLittleEndianLong() throws IOException {
  MappedByteBuffer curChunk = getCurChunk();
  if (curChunk.remaining() >= 8) {
    return curChunk.getLong();
  }

  // Value is on the chunk boundary - edge case so it is ok if it's a bit slower.
  return Util.readLittleEndianLongSlowly(this);
}
 
Example 5
Source File: ReadOnlyMemMap.java    From sparkey-java with Apache License 2.0 5 votes vote down vote up
@Override
public long readLittleEndianLong() throws IOException {
  MappedByteBuffer curChunk = getCurChunk();
  if (curChunk.remaining() >= 8) {
    return curChunk.getLong();
  }

  // Value is on the chunk boundary - edge case so it is ok if it's a bit slower.
  return Util.readLittleEndianLongSlowly(this);
}
 
Example 6
Source File: TestFileMap.java    From mynlp with Apache License 2.0 4 votes vote down vote up
public static void main2(String[] args) throws Exception {
        //FileChannel.open()

        RandomAccessFile f = new RandomAccessFile("testdata/float.martix", "rw");
        FileChannel fileChannel = f.getChannel();


        MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, fileChannel.size());

        buffer.getInt();
        System.out.println(buffer);
        buffer.getLong();
        System.out.println(buffer);
        buffer.getChar();
        System.out.println(buffer);


        float[] floats = new float[300];

        buffer.asFloatBuffer().get(floats);
        System.out.println(Arrays.toString(floats));

        System.out.println(buffer);

//        FloatBuffer floatBuffer = buffer.asFloatBuffer();
//
//
//        float[] floats = new float[300];
//
//        floatBuffer.position(0);
//        floatBuffer.get(floats);
//        System.out.println(floatBuffer);
//        System.out.println(Arrays.toString(floats));
//        System.out.println(buffer);
//
//
//
//        System.out.println("----"+buffer.asLongBuffer());
//        System.out.println(buffer.asFloatBuffer());
//        System.out.println(buffer.position(1200));
//        System.out.println(buffer.slice().get(0));

//        Random random = new Random();
//
//        long t1 = System.currentTimeMillis();
//        for (int i = 0; i < 1000000; i++) {
//            floatBuffer.position(random.nextInt(1000000)*300);
//            floatBuffer.get(floats);
//        }
//        long t2 = System.currentTimeMillis();
//
//        System.out.println(t2-t1);

        //TimeUnit.SECONDS.sleep(10);


        fileChannel.close();


    }
 
Example 7
Source File: Zip.java    From turbine with Apache License 2.0 4 votes vote down vote up
public ZipIterable(Path path) throws IOException {
  this.path = path;
  this.chan = FileChannel.open(path, StandardOpenOption.READ);
  // Locate the EOCD
  long size = chan.size();
  if (size < ENDHDR) {
    throw new ZipException("invalid zip archive");
  }
  long eocdOffset = size - ENDHDR;
  MappedByteBuffer eocd = chan.map(MapMode.READ_ONLY, eocdOffset, ENDHDR);
  eocd.order(ByteOrder.LITTLE_ENDIAN);
  int index = 0;
  int commentSize = 0;
  if (!isSignature(eocd, 0, 5, 6)) {
    // The archive may contain a zip file comment; keep looking for the EOCD.
    long start = Math.max(0, size - ENDHDR - 0xFFFF);
    eocd = chan.map(MapMode.READ_ONLY, start, (size - start));
    eocd.order(ByteOrder.LITTLE_ENDIAN);
    index = (int) ((size - start) - ENDHDR);
    while (index > 0) {
      index--;
      eocd.position(index);
      if (isSignature(eocd, index, 5, 6)) {
        commentSize = (int) ((size - start) - ENDHDR) - index;
        eocdOffset = start + index;
        break;
      }
    }
  }
  checkSignature(path, eocd, index, 5, 6, "ENDSIG");
  int totalEntries = eocd.getChar(index + ENDTOT);
  long cdsize = UnsignedInts.toLong(eocd.getInt(index + ENDSIZ));
  int actualCommentSize = eocd.getChar(index + ENDCOM);
  if (commentSize != actualCommentSize) {
    throw new ZipException(
        String.format(
            "zip file comment length was %d, expected %d", commentSize, actualCommentSize));
  }
  // If the number of entries is 0xffff, check if the archive has a zip64 EOCD locator.
  if (totalEntries == ZIP64_MAGICCOUNT) {
    // Assume the zip64 EOCD has the usual size; we don't support zip64 extensible data sectors.
    long zip64eocdOffset = size - ENDHDR - ZIP64_LOCHDR - ZIP64_ENDHDR;
    MappedByteBuffer zip64eocd = chan.map(MapMode.READ_ONLY, zip64eocdOffset, ZIP64_ENDHDR);
    zip64eocd.order(ByteOrder.LITTLE_ENDIAN);
    // Note that zip reading is necessarily best-effort, since an archive could contain 0xFFFF
    // entries and the last entry's data could contain a ZIP64_ENDSIG. Some implementations
    // read the full EOCD records and compare them.
    if (zip64eocd.getInt(0) == ZIP64_ENDSIG) {
      cdsize = zip64eocd.getLong(ZIP64_ENDSIZ);
      eocdOffset = zip64eocdOffset;
    }
  }
  this.cd = chan.map(MapMode.READ_ONLY, eocdOffset - cdsize, cdsize);
  cd.order(ByteOrder.LITTLE_ENDIAN);
}
 
Example 8
Source File: Slopbucket.java    From jelectrum with MIT License 4 votes vote down vote up
protected RecordEntry getKeyValueTable(long table_pos, ByteString key)
{
  int hash_file = (int) (table_pos / SEGMENT_FILE_SIZE);
  MappedByteBuffer hash_mbb = getBufferMap(hash_file);
  int file_offset = (int) (table_pos % SEGMENT_FILE_SIZE);


  int max;
  int items;
  long next_ptr;

  synchronized(hash_mbb)
  {
    hash_mbb.position(file_offset + (int) LOCATION_HASH_MAX); 
    max = hash_mbb.getInt();
    items = hash_mbb.getInt();
    next_ptr = hash_mbb.getLong();
  }

  Assert.assertTrue("Max " + max + " items " + items + " table at " + table_pos + " file " + hash_file + " file offset " + file_offset ,max > items);
  Assert.assertTrue(max > 4);
  Assert.assertTrue(items >= 0);

  int hash = key.hashCode();
  int loc = Math.abs(hash % max);
  if (loc < 0) loc = 0;
  //DeterministicStream det_stream = new DeterministicStream(key);
  //int loc = det_stream.nextInt(max);

  while(true)
  {
    Assert.assertTrue(loc >= 0);
    Assert.assertTrue(loc < max);
    synchronized(hash_mbb)
    {
      hash_mbb.position(file_offset + LOCATION_HASH_START + loc * 8);
      long ptr = hash_mbb.getLong();
    

      if (ptr != 0)
      {
        RecordEntry re = new RecordEntry(ptr);
        if (re.getKey().equals(key)) return re;
      }
      if (ptr == 0)
      {
        if (next_ptr != 0)
        {
          return getKeyValueTable(next_ptr, key); 
        }
        else
        {
          return null;
        }
      }
    }
  
    //loc = det_stream.nextInt(max);
    loc = (loc + 131 ) % max;
  }

}
 
Example 9
Source File: Slopbucket.java    From jelectrum with MIT License 4 votes vote down vote up
protected void putKeyValueTable(long table_pos, RecordEntry put_re)
{
  long t1=System.nanoTime();
  int hash_file = (int) (table_pos / SEGMENT_FILE_SIZE);
  MappedByteBuffer hash_mbb = getBufferMap(hash_file);
  int file_offset = (int) (table_pos % SEGMENT_FILE_SIZE);
  

  int max;
  int items;
  long next_ptr;

  synchronized(hash_mbb)
  {
    hash_mbb.position(file_offset + (int) LOCATION_HASH_MAX); 
    max = hash_mbb.getInt();
    items = hash_mbb.getInt();
    next_ptr = hash_mbb.getLong();
  }

  Assert.assertTrue("Max " + max + " items " + items + " table at " + table_pos + " file " + hash_file + " file offset " + file_offset ,max > items);
  Assert.assertTrue(max > 4);
  Assert.assertTrue(items >= 0);

  //DeterministicStream det_stream = new DeterministicStream(key);
  //int loc = det_stream.nextInt(max);
  int hash = put_re.getKey().hashCode();
  int loc = Math.abs(hash % max);
  if (loc < 0) loc = 0;

  double full = (double) items / (double) max;



  while(true)
  {
    Assert.assertTrue(loc >= 0);
    Assert.assertTrue(loc < max);
    synchronized(hash_mbb)
    {
      long t1_check = System.nanoTime();
      hash_mbb.position(file_offset + LOCATION_HASH_START + loc * 8);
      long ptr = hash_mbb.getLong();
      TimeRecord.record(t1_check, "slop_get_ptr");

      if ((ptr == 0) && (full >= HASH_FULL))
      { 
        // It isn't here and the hash is full, move on to next table

        if (next_ptr == 0)
        {
          next_ptr = makeNewHashTable(max * HASH_MULTIPLCATION);
          hash_mbb.position(file_offset + (int) LOCATION_HASH_NEXT);
          hash_mbb.putLong(next_ptr);
        }
        TimeRecord.record(t1, "slop_put_key_value_table_rec");
        putKeyValueTable(next_ptr, put_re);
        return;
   
      }
      RecordEntry re = null;
      if (ptr != 0)
      {
        re = new RecordEntry(ptr);
        if (!re.getKey().equals(put_re.getKey()))
        {
          re = null;
        }
      }
      if ((ptr == 0) || (re!=null))
      {
        //If we have an empty or a key match
        long data_loc = put_re.storeItem(ptr);

        hash_mbb.position(file_offset + LOCATION_HASH_START + loc * 8);
        hash_mbb.putLong(data_loc);

        if (ptr == 0)
        {
          hash_mbb.position(file_offset + LOCATION_HASH_ITEMS);
          items++;
          hash_mbb.putInt(items);
        }
        TimeRecord.record(t1, "slop_put_key_value_table_add");
        return;
      }
    }
    
    //loc = det_stream.nextInt(max);
    loc = (loc + 131 ) % max;
  }

}
 
Example 10
Source File: Slopbucket.java    From jelectrum with MIT License 4 votes vote down vote up
private void getTableStats(long table_pos, Map<String, Long> map)
{
  map.put("tables", map.get("tables") + 1);

  int hash_file = (int) (table_pos / SEGMENT_FILE_SIZE);
  MappedByteBuffer hash_mbb = getBufferMap(hash_file);
  int file_offset = (int) (table_pos % SEGMENT_FILE_SIZE);

  int max;
  int items;
  long next_ptr;

  synchronized(hash_mbb)
  {
    hash_mbb.position(file_offset + (int) LOCATION_HASH_MAX); 
    max = hash_mbb.getInt();
    items = hash_mbb.getInt();
    next_ptr = hash_mbb.getLong();

    hash_mbb.position(file_offset + (int) LOCATION_HASH_START);
    for(int i=0; i<max; i++)
    {
      long ptr = hash_mbb.getLong(file_offset + LOCATION_HASH_START + i*8);
      if (ptr != 0)
      {
        RecordEntry re = new RecordEntry(ptr);
        ByteString key = re.getKey();
        ByteString value = re.getValue();
        map.put("key_size", map.get("key_size") + key.size());
        map.put("data_size", map.get("data_size") + value.size());
        
      }
    }

  }
  map.put("items", map.get("items") + items);

  if (next_ptr != 0)
  {
    getTableStats(next_ptr, map);
  }


}