-
Notifications
You must be signed in to change notification settings - Fork 9
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 tomestone flag bit into OptOutEntry to avoid major changes #57
Open
scong-ttd
wants to merge
1
commit into
IABTechLab:main
Choose a base branch
from
scong-ttd:uid2-751-optin-flag-as-optout-tombstone
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+84
−12
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,8 @@ | |
// Total: 72 bytes | ||
// | ||
// Metadata format: | ||
// Lower 6 bits -- identity type | ||
// Lower 5 bits -- identity type | ||
// Middle 1 bit -- tombstone | ||
// Higher 2 bits -- record version | ||
// Total: 1 byte | ||
// | ||
|
@@ -23,18 +24,33 @@ public final class OptOutEntry { | |
public final byte[] identityHash; | ||
public final byte[] advertisingId; | ||
public final long timestamp; | ||
public final boolean isTombstone; | ||
|
||
private static final long timestampMask = 0xFFFFFFFFFFFFFFl; | ||
private static final byte adsIdTypeMask = 0x3F; | ||
private static final long timestampMask = 0xFFFFFFFFFFFFFFL; | ||
private static final byte adsIdTypeMask = 0x1F; | ||
private static final int adsIdVersionShift = 6; | ||
private static final byte tombstoneMask = 0x1; | ||
private static final int tombstoneShift = 5; | ||
|
||
@Deprecated | ||
public OptOutEntry(byte[] identityHash, byte[] advertisingId, long ts) { | ||
assert identityHash.length == OptOutConst.Sha256Bytes; | ||
assert advertisingId.length == OptOutConst.Sha256Bytes || advertisingId.length == OptOutConst.Sha256Bytes + 1; | ||
// assert ts >= 0; | ||
this.identityHash = identityHash; | ||
this.advertisingId = advertisingId; | ||
this.timestamp = ts; | ||
this.isTombstone = false; | ||
} | ||
|
||
public OptOutEntry(byte[] identityHash, byte[] advertisingId, long ts, boolean isTombstone) { | ||
assert identityHash.length == OptOutConst.Sha256Bytes; | ||
assert advertisingId.length == OptOutConst.Sha256Bytes || advertisingId.length == OptOutConst.Sha256Bytes + 1; | ||
// assert ts >= 0; | ||
this.identityHash = identityHash; | ||
this.advertisingId = advertisingId; | ||
this.timestamp = ts; | ||
this.isTombstone = isTombstone; | ||
} | ||
|
||
public static OptOutEntry parse(byte[] buffer, int bufferIndex) { | ||
|
@@ -43,6 +59,7 @@ public static OptOutEntry parse(byte[] buffer, int bufferIndex) { | |
final byte metadata = buffer[bufferIndex + OptOutConst.EntrySize - 1]; | ||
final byte adsIdVersion = (byte) (metadata >> adsIdVersionShift); | ||
final byte identityType = (byte) (metadata & adsIdTypeMask); | ||
final boolean isTombstone = (((byte) (metadata >> tombstoneShift)) & tombstoneMask) == 0x1; | ||
|
||
final byte[] idHash = Arrays.copyOfRange(buffer, bufferIndex, bufferIndex + OptOutConst.Sha256Bytes); | ||
bufferIndex += OptOutConst.Sha256Bytes; | ||
|
@@ -54,7 +71,7 @@ public static OptOutEntry parse(byte[] buffer, int bufferIndex) { | |
|
||
final long ts = ByteBuffer.wrap(buffer, bufferIndex, Long.BYTES).order(ByteOrder.LITTLE_ENDIAN).getLong() & timestampMask; | ||
|
||
return new OptOutEntry(idHash, adsId, ts); | ||
return new OptOutEntry(idHash, adsId, ts, isTombstone); | ||
} | ||
|
||
private static byte[] parseAdsIdV3(byte[] buffer, int bufferIndex, byte identityType) { | ||
|
@@ -70,6 +87,12 @@ public static long parseTimestamp(byte[] buffer, int bufferIndexForEntry) { | |
.order(ByteOrder.LITTLE_ENDIAN).getLong() & timestampMask; | ||
} | ||
|
||
public static boolean parseTombstone(byte[] buffer, int bufferIndexForEntry) { | ||
assert bufferIndexForEntry + OptOutConst.EntrySize <= buffer.length; | ||
byte metadata = buffer[bufferIndexForEntry + (OptOutConst.Sha256Bytes << 1) + 7]; | ||
return ((byte)(metadata >> tombstoneShift) & tombstoneMask) == (byte)0x1; | ||
} | ||
|
||
public static void setTimestamp(byte[] buffer, int bufferIndexForEntry, long timestamp) { | ||
assert bufferIndexForEntry + OptOutConst.EntrySize <= buffer.length; | ||
final byte metadata = buffer[bufferIndexForEntry + OptOutConst.EntrySize - 1]; | ||
|
@@ -109,7 +132,7 @@ public static OptOutEntry newRandom() { | |
public static OptOutEntry newTestEntry(long idHash, long timestamp) { | ||
// for test, using the same value for identity_hash and advertising_id | ||
byte[] id = idHashFromLong(idHash); | ||
return new OptOutEntry(id, id, timestamp); | ||
return new OptOutEntry(id, id, timestamp, false); | ||
} | ||
|
||
// Overriding equals() to compare two OptOutEntry objects | ||
|
@@ -131,16 +154,19 @@ public int hashCode() { | |
return (int) (timestamp + Arrays.hashCode(identityHash) + Arrays.hashCode(advertisingId)); | ||
} | ||
|
||
private static byte calcMetadata(byte[] advertisingId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to leave the old function in for backward compatibility? |
||
return (byte) (advertisingId.length == OptOutConst.Sha256Bytes ? 0 : ((1 << adsIdVersionShift) | advertisingId[0])); | ||
private static byte calcMetadata(byte[] advertisingId, boolean isTombstone) { | ||
return (byte) ( | ||
(advertisingId.length == OptOutConst.Sha256Bytes ? 0 : ((1 << adsIdVersionShift) | advertisingId[0])) | ||
| (isTombstone ? (1 << tombstoneShift) : 0) | ||
); | ||
} | ||
|
||
public void copyToByteArray(byte[] bytes, int offset) { | ||
// copy identity hash | ||
System.arraycopy(this.identityHash, 0, bytes, offset, OptOutConst.Sha256Bytes); | ||
offset += OptOutConst.Sha256Bytes; | ||
|
||
final byte metadata = calcMetadata(this.advertisingId); | ||
final byte metadata = calcMetadata(this.advertisingId, isTombstone); | ||
|
||
// copy advertising id | ||
System.arraycopy(this.advertisingId, metadata == 0 ? 0 : 1, bytes, offset, OptOutConst.Sha256Bytes); | ||
|
@@ -153,11 +179,16 @@ public void copyToByteArray(byte[] bytes, int offset) { | |
bytes[offset + Long.BYTES - 1] = metadata; | ||
} | ||
|
||
@Deprecated | ||
public static void writeTo(ByteBuffer buffer, byte[] identityHash, byte[] advertisingId, long timestamp) { | ||
writeTo(buffer, identityHash, advertisingId, timestamp, false); | ||
} | ||
|
||
public static void writeTo(ByteBuffer buffer, byte[] identityHash, byte[] advertisingId, long timestamp, boolean isTombstone) { | ||
assert identityHash.length == OptOutConst.Sha256Bytes; | ||
assert advertisingId.length == OptOutConst.Sha256Bytes || advertisingId.length == OptOutConst.Sha256Bytes + 1; | ||
|
||
final byte metadata = calcMetadata(advertisingId); | ||
final byte metadata = calcMetadata(advertisingId, isTombstone); | ||
final byte[] timestampBytes = OptOutUtils.toByteArray(timestamp); | ||
timestampBytes[timestampBytes.length - 1] = metadata; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you call the constructor below instead?