accumulo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dhutchis <...@git.apache.org>
Subject [GitHub] accumulo issue #145: ACCUMULO-4376 add KeyBuilder
Date Fri, 02 Sep 2016 04:07:46 GMT
Github user dhutchis commented on the issue:

    https://github.com/apache/accumulo/pull/145
  
    @melrief Here is a user scenario for building a Key from ByteBuffers. The main advantage
is being able to create keys from the middle of existing arrays given their offsets and lengths.
    
    Sadly I realized after writing the example that the `Key.copyIfNeeded()` method will copy
all of these anyway, so the efficiency gained by the code may just be an illusion.  If it
is an illusion, it would be simpler for a user to use `ByteBufferUtil.toArray(bbRow)` and
so forth. Changing the Key class to use segments of arrays is a big change.
    
    ```java
    // These byte[]s are obtained from user code.
    byte[] bigArray = new byte[500], bigArray2 = new byte[80], EMPTY_BYTES = new byte[0];
    
    // Suppose that we want to create a key whose row is in the bigArray from bytes 105 to
115,
    // whose column family is in the bigArray2 from bytes 200 to 207,
    // and whose column qualifier is in the bigArray2 from bytes 60 to 69.
    ByteBuffer bbRow = ByteBuffer.wrap(bigArray, 105, 10);
    ByteBuffer bbFam = ByteBuffer.wrap(bigArray, 200, 7);
    ByteBuffer bbQual = ByteBuffer.wrap(bigArray2, 60, 9);
    
    // We expect hasArray() to be true for all of these.
    // When hasArray() is true, we can avoid copying out the portion of the ByteBuffer to
a new byte[].
    // I include the case for ByteBuffers that do not have a backing array for completeness.
    final byte[] bRow, bFam, bQual;
    final int offRow, offFam, offQual, lenRow, lenFam, lenQual;
    if (bbRow.hasArray()) {
      bRow = bbRow.array(); offRow = bbRow.arrayOffset() + bbRow.position(); lenRow = bbRow.remaining();
    } else {
      // fallback to copy case
      bRow = ByteBufferUtil.toBytes(bbRow);
      // The ByteBufferUtil.toBytes method will have the following effect:
      //bRow = new byte[bbRow.remaining()];
      //bbRow.duplicate().get(bRow);
      offRow = 0; lenRow = bRow.length;
    }
    // Repeat for family and qualifier
    if (bbFam.hasArray()) {
      bFam = bbFam.array(); offFam = bbFam.arrayOffset() + bbFam.position(); lenFam = bbFam.remaining();
    } else {
      // fallback to copy case
      bFam = ByteBufferUtil.toBytes(bbFam);
      offFam = 0; lenFam = bFam.length;
    }
    if (bbQual.hasArray()) {
      bQual = bbQual.array(); offQual = bbQual.arrayOffset() + bbQual.position(); lenQual
= bbQual.remaining();
    } else {
      // fallback to copy case
      bQual = ByteBufferUtil.toBytes(bbQual);
      offQual = 0; lenQual = bQual.length;
    }
    
    // Construct key from all byte[]s with offsets and lengths.
    // Note that this old API method copies the data. It would be nice to not copy the data,
    // but this would bigger changes to the Key class.
    Key k = new Key(bRow, offRow, lenRow, bFam, offFam, lenFam, bQual, offQual, lenQual,
    	EMPTY_BYTES, 0, 0, Long.MAX_VALUE);
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message