Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ObjId.objIdFromByteBuffer() #6845

Merged
merged 2 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import static org.projectnessie.versioned.storage.common.objtypes.RefObj.ref;
import static org.projectnessie.versioned.storage.common.objtypes.StringObj.stringData;
import static org.projectnessie.versioned.storage.common.objtypes.TagObj.tag;
import static org.projectnessie.versioned.storage.common.persist.ObjId.objIdFromByteBuffer;
import static org.projectnessie.versioned.storage.common.persist.ObjId.objIdFromString;
import static org.projectnessie.versioned.storage.common.persist.Reference.reference;

Expand Down Expand Up @@ -624,7 +625,7 @@ CommitObj deserialize(Row row, ObjId id) {
indexStripe(
keyFromString(s.getFirstKey()),
keyFromString(s.getLastKey()),
ObjId.objIdFromByteArray(s.getSegment().toByteArray())))
objIdFromByteBuffer(s.getSegment().asReadOnlyByteBuffer())))
.forEach(b::addReferenceIndexStripes);
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -712,7 +713,7 @@ IndexSegmentsObj deserialize(Row row, ObjId id) {
indexStripe(
keyFromString(s.getFirstKey()),
keyFromString(s.getLastKey()),
ObjId.objIdFromByteArray(s.getSegment().toByteArray())))
objIdFromByteBuffer(s.getSegment().asReadOnlyByteBuffer())))
.collect(Collectors.toList());
return indexSegments(id, stripeList);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ public static ObjId objIdFromString(@Nonnull @jakarta.annotation.Nonnull String
}
}

/**
* Creates an {@link ObjId} from its bytes representation, assuming that all remaining data in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the remaining piece is a bit misleading... ByteString does not have a "pointer" (unlike ByteBuffer), does it?

* {@code bytes} belongs to the object id.
*
* @param bytes the serialized representation of the object id
* @return a {@link ObjId} instance
* @throws NullPointerException if {@code bytes} is {@code null}
*/
public static ObjId objIdFromBytes(ByteString bytes) {
int len = bytes.size();
switch (len) {
Expand All @@ -110,20 +118,43 @@ public static ObjId objIdFromBytes(ByteString bytes) {
}
}

/**
* Creates an {@link ObjId} from its bytes representation, assuming that all remaining data in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the remaining piece is a bit misleading... the byte array is treated whole, is it not? There is no moving pointer 🤔

* {@code bytes} belongs to the object id.
*
* @param bytes the serialized representation of the object id
* @return a {@link ObjId} instance
* @throws NullPointerException if {@code bytes} is {@code null}
*/
public static ObjId objIdFromByteArray(byte[] bytes) {
return fromBytes(bytes.length, ByteBuffer.wrap(bytes));
}

/**
* Creates an {@link ObjId} from its bytes representation, assuming that all remaining data in
* {@code bytes} belongs to the object id.
*
* @param bytes the serialized representation of the object id
* @return a {@link ObjId} instance
* @throws NullPointerException if {@code bytes} is {@code null}
*/
public static ObjId objIdFromByteBuffer(@Nonnull @jakarta.annotation.Nonnull ByteBuffer bytes) {
int len = bytes.remaining();
return fromBytes(len, bytes);
}

public static ObjId randomObjId() {
return ObjId256.random();
}

/**
* Creates a hash instance from its bytes' representation.
* Creates an {@link ObjId} instance from its bytes' representation, deserializing the var-int
* encoded length from {@code bytes} first.
*
* @param bytes the bytes' representation of the hash
* @return a {@code Hash} instance
* @throws NullPointerException if {@code hash} is {@code null}
* @param bytes the serialized representation of the object id, represented by the var-int encoded
* length and the actual object id
* @return a {@link ObjId} instance
* @throws NullPointerException if {@code bytes} is {@code null}
*/
public static ObjId deserializeObjId(@Nonnull @jakarta.annotation.Nonnull ByteBuffer bytes) {
int len = readVarInt(bytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static org.projectnessie.versioned.storage.common.objtypes.RefObj.ref;
import static org.projectnessie.versioned.storage.common.objtypes.StringObj.stringData;
import static org.projectnessie.versioned.storage.common.objtypes.TagObj.tag;
import static org.projectnessie.versioned.storage.common.persist.ObjId.objIdFromByteBuffer;
import static org.projectnessie.versioned.storage.common.persist.ObjId.objIdFromString;
import static org.projectnessie.versioned.storage.common.persist.Reference.reference;
import static org.projectnessie.versioned.storage.dynamodb.DynamoDBBackend.condition;
Expand Down Expand Up @@ -987,7 +988,7 @@ private static void attributeToObjIds(
Map<String, AttributeValue> i, String n, Consumer<ObjId> receiver) {
AttributeValue v = i.get(n);
if (v != null) {
v.l().stream().map(el -> ObjId.objIdFromByteArray(el.b().asByteArray())).forEach(receiver);
v.l().stream().map(el -> objIdFromByteBuffer(el.b().asByteBuffer())).forEach(receiver);
}
}

Expand Down Expand Up @@ -1015,7 +1016,7 @@ private static ObjId attributeToObjId(Map<String, AttributeValue> i, String n) {
}

private static ObjId attributeToObjId(AttributeValue v) {
return v == null ? null : ObjId.objIdFromByteArray(v.b().asByteArray());
return v == null ? null : objIdFromByteBuffer(v.b().asByteBuffer());
}

private static void objIdToAttribute(Map<String, AttributeValue> i, String n, ObjId id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.projectnessie.versioned.storage.common.objtypes.RefObj.ref;
import static org.projectnessie.versioned.storage.common.objtypes.StringObj.stringData;
import static org.projectnessie.versioned.storage.common.objtypes.TagObj.tag;
import static org.projectnessie.versioned.storage.common.persist.ObjId.objIdFromByteBuffer;
import static org.projectnessie.versioned.storage.common.persist.ObjId.objIdFromString;
import static org.projectnessie.versioned.storage.common.persist.Reference.reference;
import static org.projectnessie.versioned.storage.common.util.Closing.closeMultiple;
Expand Down Expand Up @@ -719,7 +720,7 @@ CommitObj deserialize(ResultSet rs, ObjId id) throws SQLException {
indexStripe(
keyFromString(s.getFirstKey()),
keyFromString(s.getLastKey()),
ObjId.objIdFromByteArray(s.getSegment().toByteArray())))
objIdFromByteBuffer(s.getSegment().asReadOnlyByteBuffer())))
.forEach(b::addReferenceIndexStripes);
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -837,7 +838,7 @@ IndexSegmentsObj deserialize(ResultSet rs, ObjId id) throws SQLException {
indexStripe(
keyFromString(s.getFirstKey()),
keyFromString(s.getLastKey()),
ObjId.objIdFromByteArray(s.getSegment().toByteArray())))
objIdFromByteBuffer(s.getSegment().asReadOnlyByteBuffer())))
.collect(Collectors.toList());
return indexSegments(id, stripeList);
} catch (IOException e) {
Expand Down