Package eu.stratosphere.core.memory

Examples of eu.stratosphere.core.memory.MemorySegment


  @Test
  public void testWrapAsByteBuffer() {
    SerializationTestType randomInt = Util.randomRecord(SerializationTestTypeFactory.INT);

    DataOutputSerializer serializer = new DataOutputSerializer(randomInt.length());
    MemorySegment segment = new MemorySegment(new byte[randomInt.length()]);

    try {
      // empty buffer, read buffer should be empty
      ByteBuffer wrapper = serializer.wrapAsByteBuffer();

      Assert.assertEquals(0, wrapper.position());
      Assert.assertEquals(0, wrapper.limit());

      // write to data output, read buffer should still be empty
      randomInt.write(serializer);

      Assert.assertEquals(0, wrapper.position());
      Assert.assertEquals(0, wrapper.limit());

      // get updated read buffer, read buffer should contain written data
      wrapper = serializer.wrapAsByteBuffer();

      Assert.assertEquals(0, wrapper.position());
      Assert.assertEquals(randomInt.length(), wrapper.limit());

      // clear data output, read buffer should still contain written data
      serializer.clear();

      Assert.assertEquals(0, wrapper.position());
      Assert.assertEquals(randomInt.length(), wrapper.limit());

      // get updated read buffer, should be empty
      wrapper = serializer.wrapAsByteBuffer();

      Assert.assertEquals(0, wrapper.position());
      Assert.assertEquals(0, wrapper.limit());

      // write to data output and read back to memory
      randomInt.write(serializer);
      wrapper = serializer.wrapAsByteBuffer();

      segment.put(0, wrapper, randomInt.length());

      Assert.assertEquals(randomInt.length(), wrapper.position());
      Assert.assertEquals(randomInt.length(), wrapper.limit());
    } catch (IOException e) {
      e.printStackTrace();
View Full Code Here


   
    try {
      final Channel.ID channelID = this.ioManager.createChannel();
      final BlockChannelWriter writer = this.ioManager.createBlockChannelWriter(channelID);
     
      MemorySegment memSeg = this.memoryManager.allocatePages(new DummyInvokable(), 1).get(0);
     
      for (int i = 0; i < NUM_IOS; i++) {
        for (int pos = 0; pos < memSeg.size(); pos += 4) {
          memSeg.putInt(pos, i);
        }
       
        writer.writeBlock(memSeg);
        memSeg = writer.getNextReturnedSegment();
      }
     
      writer.close();
     
      final BlockChannelReader reader = this.ioManager.createBlockChannelReader(channelID);
      for (int i = 0; i < NUM_IOS; i++) {
        reader.readBlock(memSeg);
        memSeg = reader.getNextReturnedSegment();
       
        for (int pos = 0; pos < memSeg.size(); pos += 4) {
          if (memSeg.getInt(pos) != i) {
            Assert.fail("Read memory segment contains invalid data.");
          }
        }
      }
     
View Full Code Here

      final List<MemorySegment> memSegs = this.memoryManager.allocatePages(new DummyInvokable(), NUM_SEGS);
      final Channel.ID channelID = this.ioManager.createChannel();
      final BlockChannelWriter writer = this.ioManager.createBlockChannelWriter(channelID);
     
      for (int i = 0; i < NUM_IOS; i++) {
        final MemorySegment memSeg = memSegs.isEmpty() ? writer.getNextReturnedSegment() : memSegs.remove(0);
       
        for (int pos = 0; pos < memSeg.size(); pos += 4) {
          memSeg.putInt(pos, i);
        }
       
        writer.writeBlock(memSeg);
      }
      writer.close();
     
      // get back the memory
      while (memSegs.size() < NUM_SEGS) {
        memSegs.add(writer.getNextReturnedSegment());
      }
     
      final BlockChannelReader reader = this.ioManager.createBlockChannelReader(channelID);
      while(!memSegs.isEmpty()) {
        reader.readBlock(memSegs.remove(0));
      }
     
      for (int i = 0; i < NUM_IOS; i++) {
        final MemorySegment memSeg = reader.getNextReturnedSegment();
       
        for (int pos = 0; pos < memSeg.size(); pos += 4) {
          if (memSeg.getInt(pos) != i) {
            Assert.fail("Read memory segment contains invalid data.");
          }
        }
        reader.readBlock(memSeg);
      }
View Full Code Here

  private static final int MAX_BUFFER_SIZE = 32768;

  @Test
  public void testEncodedSizeAndBufferRecycling() {
    final ByteBuffer events = ByteBuffer.allocate(MAX_EVENTS_SIZE);
    final MemorySegment segment = new MemorySegment(new byte[MAX_BUFFER_SIZE]);

    final Buffer buffer = mock(Buffer.class);
    when(buffer.getMemorySegment()).thenReturn(segment);

    final EmbeddedChannel channel = new EmbeddedChannel(new OutboundEnvelopeEncoder());
View Full Code Here

    private final List<SegmentWithPosition> segments = new ArrayList<SegmentWithPosition>();

    private final int segmentSize;

    private TestOutputView(int segmentSize) {
      super(new MemorySegment(new byte[segmentSize]), segmentSize, 0);

      this.segmentSize = segmentSize;
    }
View Full Code Here

    }

    @Override
    protected MemorySegment nextSegment(MemorySegment current, int positionInCurrent) throws IOException {
      segments.add(new SegmentWithPosition(current, positionInCurrent));
      return new MemorySegment(new byte[segmentSize]);
    }
View Full Code Here

  @Test
  public void testHasData() {
    final int SEGMENT_SIZE = 16;

    final SpanningRecordSerializer<SerializationTestType> serializer = new SpanningRecordSerializer<SerializationTestType>();
    final Buffer buffer = new Buffer(new MemorySegment(new byte[SEGMENT_SIZE]), SEGMENT_SIZE, null);
    final SerializationTestType randomIntRecord = Util.randomRecord(SerializationTestTypeFactory.INT);

    Assert.assertFalse(serializer.hasData());

    try {
View Full Code Here

  @Test
  public void testEmptyRecords() {
    final int SEGMENT_SIZE = 11;

    final SpanningRecordSerializer<SerializationTestType> serializer = new SpanningRecordSerializer<SerializationTestType>();
    final Buffer buffer = new Buffer(new MemorySegment(new byte[SEGMENT_SIZE]), SEGMENT_SIZE, null);

    try {
      Assert.assertEquals(SerializationResult.FULL_RECORD, serializer.setNextBuffer(buffer));
    } catch (IOException e) {
      e.printStackTrace();
View Full Code Here

   */
  private void test(Util.MockRecords records, int segmentSize) throws Exception {
    final int SERIALIZATION_OVERHEAD = 4; // length encoding

    final SpanningRecordSerializer<SerializationTestType> serializer = new SpanningRecordSerializer<SerializationTestType>();
    final Buffer buffer = new Buffer(new MemorySegment(new byte[segmentSize]), segmentSize, null);

    // -------------------------------------------------------------------------------------------------------------

    serializer.setNextBuffer(buffer);

View Full Code Here

  private static Buffer allocBuffer() {
    return allocBuffer(MAX_BUFFER_SIZE);
  }

  private static Buffer allocBuffer(int bufferSize) {
    return spy(new Buffer(new MemorySegment(new byte[bufferSize]), bufferSize, RECYCLER));
  }
View Full Code Here

TOP

Related Classes of eu.stratosphere.core.memory.MemorySegment

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.