Skip to content

Commit

Permalink
Add ObjId.objIdFromByteBuffer() (#6845)
Browse files Browse the repository at this point in the history
  • Loading branch information
snazy committed May 17, 2023
1 parent 573feee commit edd1f6c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
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 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 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 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 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

0 comments on commit edd1f6c

Please sign in to comment.