From 83f054498549f8860d809a786ad90d27c43d54c9 Mon Sep 17 00:00:00 2001 From: Sunjeet Singh Date: Sat, 7 Oct 2023 14:01:36 -0700 Subject: [PATCH] Cleanup --- .../object/HollowObjectTypeReadState.java | 100 ++++++++++-------- 1 file changed, 56 insertions(+), 44 deletions(-) diff --git a/hollow/src/main/java/com/netflix/hollow/core/read/engine/object/HollowObjectTypeReadState.java b/hollow/src/main/java/com/netflix/hollow/core/read/engine/object/HollowObjectTypeReadState.java index f8cf6ceb82..abf8515369 100644 --- a/hollow/src/main/java/com/netflix/hollow/core/read/engine/object/HollowObjectTypeReadState.java +++ b/hollow/src/main/java/com/netflix/hollow/core/read/engine/object/HollowObjectTypeReadState.java @@ -46,27 +46,29 @@ public class HollowObjectTypeReadState extends HollowTypeReadState implements Ho private final HollowObjectSchema unfilteredSchema; private final HollowObjectSampler sampler; - static class ShardsHolder { + private static class ShardsHolder { final HollowObjectTypeReadStateShard shards[]; final int shardNumberMask; - public ShardsHolder(int numShards) { - this.shards = new HollowObjectTypeReadStateShard[numShards]; + private ShardsHolder(HollowSchema schema, int numShards) { + HollowObjectTypeReadStateShard[] shards = new HollowObjectTypeReadStateShard[numShards]; + int shardOrdinalShift = 31 - Integer.numberOfLeadingZeros(numShards); + for(int i=0; i shardOrdinalShift = 2. Ordinal 4 = 100, shardOrdinal = 100 >> 2 == 1 (in shard 0). Ordinal 10 = 1010, shardOrdinal = 1010 >> 2 = 2 (in shard 2) + int shardOrdinalShift = 31 - Integer.numberOfLeadingZeros(numShards); if(numShards < 1 || 1 << shardOrdinalShift != numShards) throw new IllegalArgumentException("Number of shards must be a power of 2!"); - this.shardsVolatile = new ShardsHolder(numShards); - for(int i=0;i 1) maxOrdinal = VarInt.readVInt(in); - for(int i = 0; i< shardsVolatile.shards.length; i++) { + for(int i=0; i 1) maxOrdinal = VarInt.readVInt(in); - for(int i = 0; i< shardsVolatile.shards.length; i++) { + for(int i=0; i> shard.shardOrdinalShift, fieldIndex); + HollowObjectTypeReadState.ShardsHolder shardsHolder; + boolean result; + + do { + shardsHolder = this.shardsVolatile; // this read can return a stale shardHolder with greater or lesser than current + // num shards but maxOrdinal remains same across stale vs current. So given this + // atomic assignment below operations on a stale shards holder will be legal + HollowObjectTypeReadStateShard shard = shardsHolder.shards[ordinal & shardsHolder.shardNumberMask]; + result = shard.isNull(ordinal >> shard.shardOrdinalShift, fieldIndex); + } while(readWasUnsafe(shardsHolder)); + return result; } @Override public int readOrdinal(int ordinal, int fieldIndex) { sampler.recordFieldAccess(fieldIndex); - HollowObjectTypeReadStateShard shard = shardsVolatile.shards[ordinal & shardsVolatile.shardNumberMask]; - return shard.readOrdinal(ordinal >> shard.shardOrdinalShift, fieldIndex); + HollowObjectTypeReadState.ShardsHolder shardsHolder; + int result; + + do { + shardsHolder = this.shardsVolatile; + HollowObjectTypeReadStateShard shard = shardsHolder.shards[ordinal & shardsHolder.shardNumberMask]; + result = shard.readOrdinal(ordinal >> shard.shardOrdinalShift, fieldIndex); + } while(readWasUnsafe(shardsHolder)); + return result; } @Override @@ -365,9 +378,7 @@ public int readInt(int ordinal, int fieldIndex) { int result; do { - shardsHolder = this.shardsVolatile; // this read can return a stale shardHolder with greater or lesser than current - // num shards but maxOrdinal remains same across stale vs current. So given this - // atomic assignment below operations on a stale shards holder will be legal + shardsHolder = this.shardsVolatile; HollowObjectTypeReadStateShard shard = shardsHolder.shards[ordinal & shardsHolder.shardNumberMask]; result = shard.readInt(ordinal >> shard.shardOrdinalShift, fieldIndex); } while(readWasUnsafe(shardsHolder)); @@ -497,7 +508,7 @@ private boolean readWasUnsafe(ShardsHolder shardsHolder) { * @return the number of bits required for the field */ public int bitsRequiredForField(String fieldName) { - HollowObjectTypeReadStateShard[] shards = this.shardsVolatile.shards; + final HollowObjectTypeReadStateShard[] shards = this.shardsVolatile.shards; int maxBitsRequiredForField = shards[0].bitsRequiredForField(fieldName); for(int i=1;i