You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by ju...@apache.org on 2003/02/25 17:30:06 UTC

cvs commit: jakarta-slide/src/share/org/apache/slide/lock NodeLock.java

juergen     2003/02/25 08:30:06

  Modified:    src/share/org/apache/slide/lock NodeLock.java
  Log:
  faster nodelock token computation. Thanks to Sven Steiniger [Sven.Steiniger@newtron.net]
  
  Revision  Changes    Path
  1.12      +65 -12    jakarta-slide/src/share/org/apache/slide/lock/NodeLock.java
  
  Index: NodeLock.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/share/org/apache/slide/lock/NodeLock.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- NodeLock.java	14 Feb 2003 14:34:34 -0000	1.11
  +++ NodeLock.java	25 Feb 2003 16:30:06 -0000	1.12
  @@ -189,16 +189,69 @@
           this.typeUri = typeUri;
           this.expirationDate = expirationDate;
           this.inheritance = inheritance;
  -        this.lockId = objectUri.hashCode() + "_" + subjectUri.hashCode() + "_"
  -            + typeUri.hashCode() + "_" + expirationDate.getTime() + "_"
  -            + (new Date()).getTime();
  -        this.lockId =
  -            md5Encoder.encode(md5Helper.digest(this.lockId.getBytes()));
  +        this.lockId = generateLockID(objectUri.hashCode(), subjectUri.hashCode(),
  +                                     typeUri.hashCode(),
  +                                     (expirationDate==null) ? 0 : expirationDate.getTime());
           this.exclusive = exclusive;
           this.ownerInfo = ownerInfo;
       }
  -    
  -    
  +
  +    /**
  +     * Utility method for safely generating an lock id.
  +     * @param object The hascode of the object-uri of the lock.
  +     * @param subject The hashcode of the subject-uri of the lock.
  +     * @param type The hashcode of the type-uri of the lock.
  +     * @param expires The time in milliseconds of the expires date.
  +     * @return The generated lock-id.
  +     */
  +    private static final String generateLockID(int object, int subject, int type, long expires)
  +    {
  +      long current=System.currentTimeMillis();
  +      byte[] input=new byte[4+4+4+8+8];
  +      encode(input,  0, object);
  +      encode(input,  4, subject);
  +      encode(input,  8, type);
  +      if(expires!=0) encode(input, 12, expires);
  +      encode(input, 20, current);
  +      // maybe replace this encoder with something faster?
  +      return md5Encoder.encode(md5Helper.digest(input));
  +
  +      // old
  +      /*String ret=object + "_" + subject + "_"
  +          + type + "_" + expires + "_"
  +          + current;
  +      return md5Encoder.encode(md5Helper.digest(ret.getBytes()));*/
  +    }
  +
  +    /**
  +     * Encodes the given integer into the buffer at given position.
  +     * It will always append 4 bytes.
  +     */
  +    private static final void encode(byte[] buf, int offset, int number)
  +    {
  +      buf[offset+0]=(byte)((number    ) & 0xff);
  +      buf[offset+1]=(byte)((number>> 8) & 0xff);
  +      buf[offset+2]=(byte)((number>>16) & 0xff);
  +      buf[offset+3]=(byte)((number>>24) & 0xff);
  +    }
  +
  +    /**
  +     * Encodes the given long into the buffer at given position.
  +     * It will always append 8 bytes.
  +     */
  +    private static final void encode(byte[] buf, int offset, long number)
  +    {
  +      buf[offset+0]=(byte)((number    ) & 0xff);
  +      buf[offset+1]=(byte)((number>> 8) & 0xff);
  +      buf[offset+2]=(byte)((number>>16) & 0xff);
  +      buf[offset+3]=(byte)((number>>24) & 0xff);
  +      buf[offset+4]=(byte)((number>>32) & 0xff);
  +      buf[offset+5]=(byte)((number>>40) & 0xff);
  +      buf[offset+6]=(byte)((number>>48) & 0xff);
  +      buf[offset+7]=(byte)((number>>56) & 0xff);
  +    }
  +
  +
       /**
        * Constructor.
        * Creates a new lock linked to another given lock.
  @@ -419,7 +472,7 @@
        * @return boolean True if this lock has expired
        */
       public boolean hasExpired() {
  -        Date currentTime = new Date();
  +        //Date currentTime = new Date();
           return (expirationDate.before(new Date()));
       }
       
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: slide-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: slide-dev-help@jakarta.apache.org