Package org.apache.flink.core.memory

Examples of org.apache.flink.core.memory.MemorySegment


    final int posHashCode = hashCode % this.numBuckets;
   
    // get the bucket for the given hash code
    final int bucketArrayPos = posHashCode >> this.bucketsPerSegmentBits;
    final int bucketInSegmentPos = (posHashCode & this.bucketsPerSegmentMask) << NUM_INTRA_BUCKET_BITS;
    final MemorySegment bucket = this.buckets[bucketArrayPos];
   
    // get the basic characteristics of the bucket
    final int partitionNumber = bucket.get(bucketInSegmentPos + HEADER_PARTITION_OFFSET);
   
    // get the partition descriptor for the bucket
    if (partitionNumber < 0 || partitionNumber >= this.partitionsBeingBuilt.size()) {
      throw new RuntimeException("Error: Hash structures in Hash-Join are corrupt. Invalid partition number for bucket.");
    }
View Full Code Here


      if (originalForwardPointer != BUCKET_FORWARD_POINTER_NOT_SET) {
       
        // forward pointer set
        final int overflowSegNum = (int) (originalForwardPointer >>> 32);
        final int segOffset = (int) (originalForwardPointer & 0xffffffff);
        final MemorySegment seg = p.overflowSegments[overflowSegNum];
       
        final short obCount = seg.getShort(segOffset + HEADER_COUNT_OFFSET);
       
        // check if there is space in this overflow bucket
        if (obCount < NUM_ENTRIES_PER_BUCKET) {
          // space in this bucket and we are done
          seg.putInt(segOffset + BUCKET_HEADER_LENGTH + (obCount * HASH_CODE_LEN), hashCode)// hash code
          seg.putLong(segOffset + BUCKET_POINTER_START_OFFSET + (obCount * POINTER_LEN), pointer); // pointer
          seg.putShort(segOffset + HEADER_COUNT_OFFSET, (short) (obCount + 1)); // update count
          return;
        }
        else {
          // no space here, we need a new bucket. this current overflow bucket will be the
          // target of the new overflow bucket
          forwardForNewBucket = originalForwardPointer;
        }
      }
      else {
        // no overflow bucket yet, so we need a first one
        forwardForNewBucket = BUCKET_FORWARD_POINTER_NOT_SET;
      }
     
      // we need a new overflow bucket
      MemorySegment overflowSeg;
      final int overflowBucketNum;
      final int overflowBucketOffset;
     
     
      // first, see if there is space for an overflow bucket remaining in the last overflow segment
      if (p.nextOverflowBucket == 0) {
        // no space left in last bucket, or no bucket yet, so create an overflow segment
        overflowSeg = getNextBuffer();
        if (overflowSeg == null) {
          // no memory available to create overflow bucket. we need to spill a partition
          final int spilledPart = spillPartition();
          if (spilledPart == p.getPartitionNumber()) {
            // this bucket is no longer in-memory
            return;
          }
          overflowSeg = getNextBuffer();
          if (overflowSeg == null) {
            throw new RuntimeException("Bug in HybridHashJoin: No memory became available after spilling a partition.");
          }
        }
        overflowBucketOffset = 0;
        overflowBucketNum = p.numOverflowSegments;
       
        // add the new overflow segment
        if (p.overflowSegments.length <= p.numOverflowSegments) {
          MemorySegment[] newSegsArray = new MemorySegment[p.overflowSegments.length * 2];
          System.arraycopy(p.overflowSegments, 0, newSegsArray, 0, p.overflowSegments.length);
          p.overflowSegments = newSegsArray;
        }
        p.overflowSegments[p.numOverflowSegments] = overflowSeg;
        p.numOverflowSegments++;
      }
      else {
        // there is space in the last overflow bucket
        overflowBucketNum = p.numOverflowSegments - 1;
        overflowSeg = p.overflowSegments[overflowBucketNum];
        overflowBucketOffset = p.nextOverflowBucket << NUM_INTRA_BUCKET_BITS;
      }
     
      // next overflow bucket is one ahead. if the segment is full, the next will be at the beginning
      // of a new segment
      p.nextOverflowBucket = (p.nextOverflowBucket == this.bucketsPerSegmentMask ? 0 : p.nextOverflowBucket + 1);
     
      // insert the new overflow bucket in the chain of buckets
      // 1) set the old forward pointer
      // 2) let the bucket in the main table point to this one
      overflowSeg.putLong(overflowBucketOffset + HEADER_FORWARD_OFFSET, forwardForNewBucket);
      final long pointerToNewBucket = (((long) overflowBucketNum) << 32) | ((long) overflowBucketOffset);
      bucket.putLong(bucketInSegmentPos + HEADER_FORWARD_OFFSET, pointerToNewBucket);
     
      // finally, insert the values into the overflow buckets
      overflowSeg.putInt(overflowBucketOffset + BUCKET_HEADER_LENGTH, hashCode)// hash code
      overflowSeg.putLong(overflowBucketOffset + BUCKET_POINTER_START_OFFSET, pointer); // pointer
     
      // set the count to one
      overflowSeg.putShort(overflowBucketOffset + HEADER_COUNT_OFFSET, (short) 1);
    }
  }
View Full Code Here

   
    ensureNumBuffersReturned(numSegs);
   
    // go over all segments that are part of the table
    for (int i = 0, bucket = 0; i < numSegs && bucket < numBuckets; i++) {
      final MemorySegment seg = getNextBuffer();
     
      // go over all buckets in the segment
      for (int k = 0; k < bucketsPerSegment && bucket < numBuckets; k++, bucket++) {
        final int bucketOffset = k * HASH_BUCKET_SIZE; 
       
        // compute the partition that the bucket corresponds to
        final byte partition = assignPartition(bucket, numPartitions);
       
        // initialize the header fields
        seg.put(bucketOffset + HEADER_PARTITION_OFFSET, partition);
        seg.put(bucketOffset + HEADER_STATUS_OFFSET, BUCKET_STATUS_IN_MEMORY);
        seg.putShort(bucketOffset + HEADER_COUNT_OFFSET, (short) 0);
        seg.putLong(bucketOffset + HEADER_FORWARD_OFFSET, BUCKET_FORWARD_POINTER_NOT_SET);
      }
     
      table[i] = seg;
    }
    this.buckets = table;
View Full Code Here

    // spill the partition
    int numBuffersFreed = p.spillPartition(this.availableMemory, this.ioManager,
                    this.currentEnumerator.next(), this.writeBehindBuffers);
    this.writeBehindBuffersAvailable += numBuffersFreed;
    // grab as many buffers as are available directly
    MemorySegment currBuff = null;
    while (this.writeBehindBuffersAvailable > 0 && (currBuff = this.writeBehindBuffers.poll()) != null) {
      this.availableMemory.add(currBuff);
      this.writeBehindBuffersAvailable--;
    }
    return largestPartNum;
View Full Code Here

    }
   
    // check if there are write behind buffers that actually are to be used for the hash table
    if (this.writeBehindBuffersAvailable > 0) {
      // grab at least one, no matter what
      MemorySegment toReturn;
      try {
        toReturn = this.writeBehindBuffers.take();
      }
      catch (InterruptedException iex) {
        throw new RuntimeException("Hybrid Hash Join was interrupted while taking a buffer.");
      }
      this.writeBehindBuffersAvailable--;
     
      // grab as many more buffers as are available directly
      MemorySegment currBuff = null;
      while (this.writeBehindBuffersAvailable > 0 && (currBuff = this.writeBehindBuffers.poll()) != null) {
        this.availableMemory.add(currBuff);
        this.writeBehindBuffersAvailable--;
      }
      return toReturn;
View Full Code Here

  }
 

  @Override
  public MemorySegment nextSegment() {
    final MemorySegment seg = getNextBuffer();
    if (seg == null) {
      try {
        spillPartition();
      } catch (IOException ioex) {
        throw new RuntimeException("Error spilling Hash Join Partition" + (ioex.getMessage() == null ?
          "." : ": " + ioex.getMessage()), ioex);
      }
     
      MemorySegment fromSpill = getNextBuffer();
      if (fromSpill == null) {
        throw new RuntimeException("BUG in Hybrid Hash Join: Spilling did not free a buffer.");
      } else {
        return fromSpill;
      }
View Full Code Here

TOP

Related Classes of org.apache.flink.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.