void appendInstance(final Bytes bytes, final V value) {
   bytes.clear();
   if (generatedValueType) ((BytesMarshallable) value).writeMarshallable(bytes);
   else bytes.writeInstance(vClass, value);
   bytes.flip();
   appendValue(bytes);
 }
Example #2
0
 @Override
 public void readExternal(ObjectInput in) throws IOException {
   Bytes b = (Bytes) in;
   this.x = b.readStopBit();
   if (this.y == null) this.y = new StringBuilder();
   b.readUTFΔ(this.y);
 }
 @Override
 public DirectBitSet andNot(long longIndex, long value) {
   long l = bytes.readLong(longIndex << 3);
   long l2 = l & ~value;
   bytes.writeLong(longIndex << 3, l2);
   return this;
 }
 @Override
 public long nextClearLong(long fromLongIndex) {
   if (fromLongIndex < 0) throw new IndexOutOfBoundsException();
   if (fromLongIndex >= longLength) return NOT_FOUND;
   if (bytes.readLong(fromLongIndex << 3) != ~0L) return fromLongIndex;
   for (long i = fromLongIndex + 1; i < longLength; i++) {
     if (bytes.readLong(i << 3) != ~0L) return i;
   }
   return NOT_FOUND;
 }
 public void write(Bytes bytes, C c) {
   if (c == null) {
     bytes.writeStopBit(NULL_LENGTH);
     return;
   }
   bytes.writeStopBit(c.size());
   for (E e : c) {
     eBytesMarshaller.write(bytes, e);
   }
 }
  public static void main(String[] args) throws MPIException {
    args = MPI.Init(args);
    Intracomm worldProcComm = MPI.COMM_WORLD;
    int worldProcRank = worldProcComm.getRank();
    int worldProcsCount = worldProcComm.getSize();

    int size = Integer.parseInt(args[0]);
    Range[] ranges = RangePartitioner.partition(size, worldProcsCount);
    Range myRange = ranges[worldProcRank];

    String fname = "/dev/shm/mmap.tmp";
    try (FileChannel fc =
        FileChannel.open(
            Paths.get(fname),
            StandardOpenOption.CREATE,
            StandardOpenOption.WRITE,
            StandardOpenOption.READ)) {
      int extent = size * Double.BYTES;
      bytes = ByteBufferBytes.wrap(fc.map(FileChannel.MapMode.READ_WRITE, 0L, extent));
      byteBuffer = bytes.sliceAsByteBuffer(byteBuffer);
      byteSlice =
          bytes.slice(myRange.getStartIndex() * Double.BYTES, myRange.getLength() * Double.BYTES);
      byteBufferSlice = byteSlice.sliceAsByteBuffer(byteBufferSlice);

      Win win = new Win(byteBuffer, extent, Double.BYTES, MPI.INFO_NULL, worldProcComm);

      for (int i = 0; i < myRange.getLength(); ++i) {
        byteSlice.writeDouble(i * Double.BYTES, worldProcRank);
      }
      win.fence(0);
      if (worldProcRank != 0) {
        win.put(
            byteBufferSlice,
            myRange.getLength(),
            MPI.DOUBLE,
            0,
            myRange.getStartIndex(),
            myRange.getLength(),
            MPI.DOUBLE);
      }
      win.fence(0);

      worldProcComm.barrier();
      if (worldProcRank == 0) {
        for (int i = 0; i < size; ++i) {
          System.out.println(bytes.readDouble(i * Double.BYTES));
        }
      }
      worldProcComm.barrier();
    } catch (IOException e) {
      e.printStackTrace();
    }

    MPI.Finalize();
  }
 void appendValue(final Bytes value) {
   if (value.remaining() + 4 > tmpBytes.remaining())
     throw new IllegalArgumentException(
         "Value too large for entry was "
             + (value.remaining() + 4)
             + ", remaining: "
             + tmpBytes.remaining());
   tmpBytes.writeStopBit(value.remaining());
   tmpBytes.position(align(tmpBytes.position()));
   tmpBytes.write(value);
 }
 @Override
 public DirectBitSet flip(long bitIndex) {
   long longIndex = bitIndex >> 6;
   if (bitIndex < 0 || longIndex >= longLength) throw new IndexOutOfBoundsException();
   long byteIndex = longIndex << 3;
   // only 6 lowest-order bits used, JLS 15.19
   long mask = 1L << bitIndex;
   long l = bytes.readLong(byteIndex);
   long l2 = l ^ mask;
   bytes.writeLong(byteIndex, l2);
   return this;
 }
 private long longHashCode(Bytes bytes) {
   long h = 0;
   int i = 0;
   long limit = bytes.limit(); // clustering.
   for (; i < limit - 7; i += 8) h = 10191 * h + bytes.readLong(i);
   //        for (; i < bytes.limit() - 3; i += 2)
   //            h = 10191 * h + bytes.readInt(i);
   for (; i < limit; i++) h = 57 * h + bytes.readByte(i);
   h ^= (h >>> 31) + (h << 31);
   h += (h >>> 21) + (h >>> 11);
   return h;
 }
 @Override
 public DirectBitSet clear(long bitIndex) {
   long longIndex = bitIndex >> 6;
   if (bitIndex < 0 || longIndex >= longLength) throw new IndexOutOfBoundsException();
   long byteIndex = longIndex << 3;
   long mask = 1L << bitIndex;
   long l = bytes.readLong(byteIndex);
   if ((l & mask) == 0) return this;
   long l2 = l & ~mask;
   bytes.writeLong(byteIndex, l2);
   return this;
 }
 @Override
 public boolean setIfClear(long bitIndex) {
   long longIndex = bitIndex >> 6;
   if (bitIndex < 0 || longIndex >= longLength) throw new IndexOutOfBoundsException();
   long byteIndex = longIndex << 3;
   long mask = 1L << bitIndex;
   long l = bytes.readLong(byteIndex);
   long l2 = l | mask;
   if (l == l2) return false;
   bytes.writeLong(byteIndex, l2);
   return true;
 }
 @Override
 public long previousClearLong(long fromLongIndex) {
   if (fromLongIndex < 0) {
     if (fromLongIndex == NOT_FOUND) return NOT_FOUND;
     throw new IndexOutOfBoundsException();
   }
   if (fromLongIndex >= longLength) fromLongIndex = longLength - 1;
   if (bytes.readLong(fromLongIndex << 3) != ~0L) return fromLongIndex;
   for (long i = fromLongIndex - 1; i >= 0; i--) {
     if (bytes.readLong(i << 3) != ~0L) return i;
   }
   return NOT_FOUND;
 }
Example #13
0
  public static void main(String... ignored) {
    ByteBuffer wrap = ByteBuffer.allocate(1024);
    Bytes bufferBytes = ByteBufferBytes.wrap(wrap);
    byte[] bytes = "BAC,12.32,12.54,12.56,232443".getBytes();

    int runs = 10000000;
    long start = System.nanoTime();
    for (int i = 0; i < runs; i++) {
      bufferBytes.clear();
      // read the next message.
      bufferBytes.write(bytes);
      bufferBytes.position(0);
      // decode message
      String word = bufferBytes.parseUtf8(StopCharTesters.COMMA_STOP);
      double low = bufferBytes.parseDouble();
      double curr = bufferBytes.parseDouble();
      double high = bufferBytes.parseDouble();
      long sequence = bufferBytes.parseLong();
      if (i == 0) {
        assertEquals("BAC", word);
        assertEquals(12.32, low, 0.0);
        assertEquals(12.54, curr, 0.0);
        assertEquals(12.56, high, 0.0);
        assertEquals(232443, sequence);
      }
    }
    long time = System.nanoTime() - start;
    System.out.println("Average time was " + time / runs + " nano-seconds");
  }
  @Test
  @Ignore
  public void NullPointerException() {
    final Bytes bytes =
        ByteBufferBytes.wrap(ByteBuffer.allocate(1024).order(ByteOrder.nativeOrder()));

    NullPointerException expected = new NullPointerException("test");
    bytes.writeObject(expected);

    bytes.position(0);
    NullPointerException actual = (NullPointerException) bytes.readObject();

    Assert.assertEquals(expected, actual);
  }
 @Override
 public long nextClearBit(long fromIndex) {
   if (fromIndex < 0) throw new IndexOutOfBoundsException();
   long fromLongIndex = fromIndex >> 6;
   if (fromLongIndex >= longLength) return NOT_FOUND;
   long l = (~bytes.readLong(fromLongIndex << 3)) >>> fromIndex;
   if (l != 0) {
     return fromIndex + Long.numberOfTrailingZeros(l);
   }
   for (long i = fromLongIndex + 1; i < longLength; i++) {
     l = ~bytes.readLong(i << 3);
     if (l != 0) return (i << 6) + Long.numberOfTrailingZeros(l);
   }
   return NOT_FOUND;
 }
    void directPut(final Bytes key, final Bytes value, int hash2) {
      lock();
      try {
        hash2 = hashLookup.startSearch(hash2);
        while (true) {
          final int pos = hashLookup.nextPos();
          if (pos < 0) {
            directPutEntry(key, value, hash2);

            return;

          } else {
            final long offset = entriesOffset + pos * entrySize;
            tmpBytes.storePositionAndSize(bytes, offset, entrySize);
            if (!keyEquals(key, tmpBytes)) continue;
            final long keyLength = key.remaining();
            tmpBytes.skip(keyLength);
            appendValue(value);
            return;
          }
        }
      } finally {
        unlock();
      }
    }
 @Override
 public boolean get(long bitIndex) {
   long longIndex = bitIndex >> 6;
   if (bitIndex < 0 || longIndex >= longLength) throw new IndexOutOfBoundsException();
   long l = bytes.readLong(longIndex << 3);
   return (l & (1L << bitIndex)) != 0;
 }
 @Override
 public DirectBitSet setAll() {
   for (long i = 0; i < longLength; i++) {
     bytes.writeLong(i << 3, ~0L);
   }
   return this;
 }
    void directRemove(final Bytes keyBytes, int hash2) {
      lock();
      try {
        hash2 = hashLookup.startSearch(hash2);
        while (true) {

          final int pos = hashLookup.nextPos();
          if (pos < 0) {
            return;

          } else {
            final long offset = entriesOffset + pos * entrySize;
            tmpBytes.storePositionAndSize(bytes, offset, entrySize);
            if (!keyEquals(keyBytes, tmpBytes)) continue;
            final long keyLength =
                align(keyBytes.remaining() + tmpBytes.position()); // includes the stop bit length.
            tmpBytes.position(keyLength);

            hashLookup.remove(hash2, pos);
            decrementSize();

            freeList.clear(pos);
            if (pos < nextSet) nextSet = pos;

            return;
          }
        }
      } finally {
        unlock();
      }
    }
 @Override
 public long cardinality() {
   long count = 0;
   for (long i = 0; i < longLength; i++) {
     count += Long.bitCount(bytes.readLong(i << 3));
   }
   return count;
 }
  @Override
  public DirectBitSet flip(long fromIndex, long exclusiveToIndex) {
    long fromLongIndex = fromIndex >> 6;
    long toIndex = exclusiveToIndex - 1;
    long toLongIndex = toIndex >> 6;
    if (fromIndex < 0 || fromIndex > exclusiveToIndex || toLongIndex >= longLength)
      throw new IndexOutOfBoundsException();

    if (fromLongIndex != toLongIndex) {
      long firstFullLongIndex = fromLongIndex;
      if ((fromIndex & 0x3F) != 0) {
        long fromByteIndex = fromLongIndex << 3;
        long mask = (~0L) << fromIndex;
        long l = bytes.readLong(fromByteIndex);
        long l2 = l ^ mask;
        bytes.writeLong(fromByteIndex, l2);
        firstFullLongIndex++;
      }

      if ((exclusiveToIndex & 0x3F) == 0) {
        for (long i = firstFullLongIndex; i <= toLongIndex; i++) {
          bytes.writeLong(i << 3, ~bytes.readLong(i << 3));
        }
      } else {
        for (long i = firstFullLongIndex; i < toLongIndex; i++) {
          bytes.writeLong(i << 3, ~bytes.readLong(i << 3));
        }

        long toByteIndex = toLongIndex << 3;
        // >>> ~toIndex === >>> (63 - (toIndex & 0x3F))
        long mask = (~0L) >>> ~toIndex;
        long l = bytes.readLong(toByteIndex);
        long l2 = l ^ mask;
        bytes.writeLong(toByteIndex, l2);
      }
    } else {
      long byteIndex = fromLongIndex << 3;
      long mask = ((~0L) << fromIndex) & ((~0L) >>> ~toIndex);
      long l = bytes.readLong(byteIndex);
      long l2 = l ^ mask;
      bytes.writeLong(byteIndex, l2);
    }
    return this;
  }
 @Override
 public long previousClearBit(long fromIndex) {
   if (fromIndex < 0) {
     if (fromIndex == NOT_FOUND) return NOT_FOUND;
     throw new IndexOutOfBoundsException();
   }
   long fromLongIndex = fromIndex >> 6;
   if (fromLongIndex >= longLength) {
     fromLongIndex = longLength - 1;
     fromIndex = size() - 1;
   }
   long l = (~bytes.readLong(fromLongIndex << 3)) << ~fromIndex;
   if (l != 0) return fromIndex - Long.numberOfLeadingZeros(l);
   for (long i = fromLongIndex - 1; i >= 0; i--) {
     l = ~bytes.readLong(i << 3);
     if (l != 0) return (i << 6) + 63 - Long.numberOfLeadingZeros(l);
   }
   return NOT_FOUND;
 }
 @Override
 public long previousSetBit(long fromIndex) {
   if (fromIndex < 0) {
     if (fromIndex == NOT_FOUND) return NOT_FOUND;
     throw new IndexOutOfBoundsException();
   }
   long fromLongIndex = fromIndex >> 6;
   if (fromLongIndex >= longLength) {
     // the same policy for this "index out of bounds" situation
     // as in j.u.BitSet
     fromLongIndex = longLength - 1;
     fromIndex = size() - 1;
   }
   // << ~fromIndex === << (63 - (fromIndex & 0x3F))
   long l = bytes.readLong(fromLongIndex << 3) << ~fromIndex;
   if (l != 0) return fromIndex - Long.numberOfLeadingZeros(l);
   for (long i = fromLongIndex - 1; i >= 0; i--) {
     l = bytes.readLong(i << 3);
     if (l != 0) return (i << 6) + 63 - Long.numberOfLeadingZeros(l);
   }
   return NOT_FOUND;
 }
  public C read(Bytes bytes, @Nullable C c) {
    long length = bytes.readStopBit();

    if (length == 0 && c != null) {
      c.clear();
      return c;
    }

    if (length < NULL_LENGTH || length > Integer.MAX_VALUE)
      throw new IllegalStateException("Invalid length: " + length);

    if (length == NULL_LENGTH) return null;

    if (c == null) c = newCollection();

    return readCollection(bytes, c, (int) length);
  }
 public SingleThreadedDirectBitSet(Bytes bytes) {
   this.bytes = bytes;
   longLength = bytes.capacity() >> 3;
 }
 @Override
 public void release() {
   bytes.release();
 }
 @Override
 public int refCount() {
   return bytes.refCount();
 }
 @Override
 public void reserve() {
   bytes.reserve();
 }
 private void writeKey(Bytes keyBytes, long offset) {
   tmpBytes.storePositionAndSize(bytes, offset, entrySize);
   long keyLength = keyBytes.remaining();
   tmpBytes.writeStopBit(keyLength);
   tmpBytes.write(keyBytes);
 }
 boolean keyEquals(Bytes keyBytes, MultiStoreBytes tmpBytes) {
   // check the length is the same.
   long keyLength = tmpBytes.readStopBit();
   return keyLength == keyBytes.remaining() && tmpBytes.startsWith(keyBytes);
 }