kadirozde commented on code in PR #1855:
URL: https://github.com/apache/phoenix/pull/1855#discussion_r1544061014


##########
phoenix-core/src/main/java/org/apache/phoenix/util/matcher/RowKeyMatcher.java:
##########
@@ -0,0 +1,144 @@
+package org.apache.phoenix.util.matcher;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.StampedLock;
+
+/**
+ * This class holds the index, mapping row-key matcher patterns to tableIds.
+ * Assumes byte[] are UTF-8 encoded.
+ * This class is thread safe.
+ */
+public class RowKeyMatcher {
+       private static final Logger LOGGER = 
LoggerFactory.getLogger(RowKeyMatcher.class);
+
+       public static final int R = 256;
+       private TrieNode root = new TrieNode();
+       private final AtomicInteger numEntries = new AtomicInteger(0);
+
+       // Basic Trie node implementation
+       class TrieNode {
+               private Integer tableId = null;
+               TrieNode[] next = new TrieNode[R];
+           private final StampedLock sl = new StampedLock();
+               private TrieNode tryOptimisticGet(int pos) {
+                       long stamp = sl.tryOptimisticRead();
+                       TrieNode nextNode = this.next[pos];
+                       if (!sl.validate(stamp)) {
+                               stamp = sl.readLock();
+                               try {
+                                       nextNode = this.next[pos];
+                               } finally {
+                                       sl.unlockRead(stamp);
+                               }
+                       }
+                       return nextNode;
+               }
+
+               protected void put(int pos, byte[] key, int val, int depth) {
+                       long stamp = sl.writeLock();
+                       try {
+                               this.next[pos] = 
RowKeyMatcher.this.put(this.next[pos], key, val, depth, true);
+                       }
+                       finally {
+                               sl.unlock(stamp);
+                       }
+                       
+               }
+
+               protected void registerTableId(int tableId) {
+                       long stamp = sl.writeLock();
+                       try {
+                               if (this.tableId == null) {
+                                       this.tableId = tableId;
+                                       numEntries.incrementAndGet();
+                               }
+                       }
+                       finally {
+                               sl.unlock(stamp);
+                       }
+               }
+       }
+
+       // return the number of prefixes that this index has.
+       public int getNumEntries() {
+               return numEntries.get();
+       }
+
+       // return the Id associated with the rowkey.
+       public Integer match(byte[] rowkey, int offset) {
+               return get(rowkey, offset);
+       }
+
+       public Integer get(byte[] key, int offset) {
+               TrieNode node = get(root, key, offset);
+               if (node == null)
+                       return null;
+               return node.tableId;
+       }
+       private TrieNode get(TrieNode node, byte[] key, int depth) {
+               if (node == null) {
+                       return null;
+               }
+
+               if (node.tableId != null) {
+                       return node;
+               }
+               if (key.length == depth) {
+                       return node;
+               }
+
+               int index = (int) key[depth] % (int) R;

Review Comment:
   Would the following be sufficient to convert byte to unsigned int and 
simplify the code?
   int index = key[depth] & 0xFF;
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to