/** * To {@code Blob} instances are considered equal iff they have the * same SHA-256 hash code are equal. * @param other */ @Override public boolean equals(Object other) { if (other == this) { return true; } if (other instanceof AbstractBlob) { AbstractBlob that = (AbstractBlob) other; // optimize the comparison if both this and the other blob // already have pre-computed SHA-256 hash codes synchronized (this) { if (hashCode != null) { synchronized (that) { if (that.hashCode != null) { return hashCode.equals(that.hashCode); } } } } } return other instanceof Blob && equal(this, (Blob) other); }
/** * To {@code Blob} instances are considered equal iff they have the * same SHA-256 hash code are equal. * @param other */ @Override public boolean equals(Object other) { if (other == this) { return true; } if (other instanceof AbstractBlob) { AbstractBlob that = (AbstractBlob) other; // optimize the comparison if both this and the other blob // already have pre-computed SHA-256 hash codes synchronized (this) { if (hashCode != null) { synchronized (that) { if (that.hashCode != null) { return hashCode.equals(that.hashCode); } } } } } return other instanceof Blob && equal(this, (Blob) other); }
@Override public boolean equals(Object object) { if (Record.fastEquals(this, object)) { return true; } if (object instanceof SegmentBlob) { SegmentBlob that = (SegmentBlob) object; if (this.length() != that.length()) { return false; } List<RecordId> bulkIds = this.getBulkRecordIds(); if (bulkIds != null && bulkIds.equals(that.getBulkRecordIds())) { return true; } } return object instanceof Blob && AbstractBlob.equal(this, (Blob) object); }
@Test public void inMemoryBlob() throws Exception{ Blob b = new ArrayBasedBlob("hello world".getBytes()); String value = serializer.serialize(b); Blob b2 = serializer.deserialize(value); assertTrue(AbstractBlob.equal(b, b2)); }
@Test public void blobs() throws Exception{ int maxInlineSize = 100; FSBlobSerializer serializer = new FSBlobSerializer(folder.getRoot(), maxInlineSize); String data = Strings.repeat("x", maxInlineSize * 10); Blob b = new ArrayBasedBlob(data.getBytes(UTF_8)); String id = serializer.serialize(b); Blob b2 = serializer.deserialize(id); assertTrue(AbstractBlob.equal(b, b2)); }
@Test public void blobComparisonBasedOnContentIdentity() throws Exception { byte[] data = bytes(100); Blob a = new TestBlob(data, "id1", false); Blob b = new TestBlob(data, "id1", false); assertTrue(AbstractBlob.equal(a, b)); Blob a2 = new TestBlob(data, "id1", true); Blob b2 = new TestBlob(data, "id2", true); assertTrue("Blobs with different id but same content should match", AbstractBlob.equal(a2, b2)); }
@Test public void blobComparisonBasedOnLength() throws Exception { Blob a = new TestBlob(bytes(100), null, false); Blob b = new TestBlob(bytes(50), null, false); assertFalse("Blob comparison should not fallback on content if lengths not same", AbstractBlob.equal(a, b)); }
@Test public void blobStoreBlob() throws Exception{ Blob b = new ArrayBasedBlob("hello world".getBytes()); String value = blobStore.writeBlob(b.getNewStream()); String serValue = serializer.serialize(new BlobStoreBlob(blobStore, value)); Blob b2 = serializer.deserialize(serValue); assertTrue(AbstractBlob.equal(b, b2)); }
private Blob testCreateAndRead(Blob blob) throws Exception { NodeState state = nodeStore.getRoot().getChildNode("hello"); if (!state.exists()) { NodeBuilder builder = nodeStore.getRoot().builder(); builder.child("hello"); nodeStore.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY); } NodeBuilder builder = nodeStore.getRoot().builder(); builder.getChildNode("hello").setProperty("world", blob); nodeStore.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY); state = nodeStore.getRoot().getChildNode("hello"); blob = state.getProperty("world").getValue(Type.BINARY); assertTrue("Blob written and read must be equal", AbstractBlob.equal(blob, blob)); return blob; }