jiajunwang commented on a change in pull request #718: Implement Helix 
nonblocking lock
URL: https://github.com/apache/helix/pull/718#discussion_r379657934
 
 

 ##########
 File path: helix-lock/src/main/java/org/apache/helix/lock/LockInfo.java
 ##########
 @@ -19,28 +19,162 @@
 
 package org.apache.helix.lock;
 
-import java.util.Map;
+import org.apache.helix.HelixProperty;
+import org.apache.helix.ZNRecord;
 
 
 /**
- * Generic interface for a map contains the Helix lock information
- * @param <T> The type of the LockInfo value
+ * Structure represents a lock node information, implemented using ZNRecord
  */
-public interface LockInfo<T> {
+public class LockInfo extends HelixProperty {
 
-  //TODO: add specific setter and getter for any field that is determined to 
be universal for all implementations of HelixLock
+  // Default values for each attribute if there are no current values set by 
user
+  public static final String DEFAULT_OWNER_TEXT = "";
+  public static final String DEFAULT_MESSAGE_TEXT = "";
+  public static final long DEFAULT_TIMEOUT_LONG = -1L;
+
+  // default lock info represents the status of a unlocked lock
+  public static final LockInfo defaultLockInfo = new LockInfo("");
+
+  /**
+   * The keys to lock information
+   */
+  public enum LockInfoAttribute {
+    OWNER, MESSAGE, TIMEOUT
+  }
+
+  /**
+   * Initialize a LockInfo with a ZNRecord id, set all info fields to default 
data
+   */
+  public LockInfo(String id) {
+    super(id);
+    resetLockInfo();
+  }
+
+  /**
+   * Initialize a LockInfo with a ZNRecord, set all info fields to default data
+   * @param znRecord The ZNRecord contains lock node data that used to 
initialize the LockInfo
+   */
+  public LockInfo(ZNRecord znRecord) {
+    super(znRecord);
+    setNullLockInfoFieldsToDefault();
+  }
+
+  /**
+   * Initialize a LockInfo with data for each field, set all null info fields 
to default data
+   * @param ownerId value of OWNER attribute
+   * @param message value of MESSAGE attribute
+   * @param timeout value of TIMEOUT attribute
+   */
+  public LockInfo(String ownerId, String message, long timeout) {
+    this(ownerId);
+    setLockInfoFields(ownerId, message, timeout);
+  }
+
+  /**
+   * Build a LOCKINFO instance that represents an unlocked lock states
+   * @return the unlocked lock node LockInfo instance
+   */
+  public static LockInfo buildUnlockedLockInfo() {
+    return new LockInfo("");
+  }
+
+  /**
+   * Set each field of lock info to user provided values if the values are not 
null, null values are set to default values
+   * @param ownerId value of OWNER attribute
+   * @param message value of MESSAGE attribute
+   * @param timeout value of TIMEOUT attribute
+   */
+  public void setLockInfoFields(String ownerId, String message, Long timeout) {
+    _record.setSimpleField(LockInfoAttribute.OWNER.name(),
+        ownerId == null ? DEFAULT_OWNER_TEXT : ownerId);
+    _record.setSimpleField(LockInfoAttribute.MESSAGE.name(),
+        message == null ? DEFAULT_MESSAGE_TEXT : message);
+    _record.setLongField(LockInfoAttribute.TIMEOUT.name(),
+        timeout == null ? DEFAULT_TIMEOUT_LONG : timeout);
+  }
+
+  /**
+   * Set all null values to default values in LockInfo, keep non-null values
+   */
+  private void setNullLockInfoFieldsToDefault() {
+    setLockInfoFields(getOwner(), getMessage(), getTimeout());
+  }
+
+  /**
+   * Reset the lock info to unlocked lock state
+   */
+  public void resetLockInfo() {
+    setLockInfoFields(DEFAULT_OWNER_TEXT, DEFAULT_MESSAGE_TEXT, 
DEFAULT_TIMEOUT_LONG);
+  }
+
+  /**
+   * Get the value for OWNER attribute of the lock
+   * @return the owner id of the lock, empty string if there is no owner id set
+   */
+  public String getOwner() {
+    String owner = _record.getSimpleField(LockInfoAttribute.OWNER.name());
+    return owner == null ? DEFAULT_OWNER_TEXT : owner;
+  }
+
+  /**
+   * Get the value for MESSAGE attribute of the lock
+   * @return the message of the lock, empty string if there is no message set
+   */
+  public String getMessage() {
+    String message = _record.getSimpleField(LockInfoAttribute.MESSAGE.name());
+    return message == null ? DEFAULT_MESSAGE_TEXT : message;
+  }
+
+  /**
+   * Get the value for TIMEOUT attribute of the lock
+   * @return the expiring time of the lock, -1 if there is no timeout set
+   */
+  public Long getTimeout() {
+    return _record.getLongField(LockInfoAttribute.TIMEOUT.name(), 
DEFAULT_TIMEOUT_LONG);
+  }
+
+  /**
+   * Get the value for OWNER attribute of the lock from a ZNRecord
+   * @return the owner id of the lock, empty string if there is no owner id set
+   */
+  public static String getOwner(ZNRecord znRecord) {
 
 Review comment:
   And then you can avoid the duplicate code by calling the instance method.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@helix.apache.org
For additional commands, e-mail: reviews-h...@helix.apache.org

Reply via email to