Skip to content

Commit c79a24d

Browse files
committed
some code refactoring
1 parent 700f5e8 commit c79a24d

File tree

1 file changed

+45
-57
lines changed

1 file changed

+45
-57
lines changed

src/com/danga/MemCached/SockIOPool.java

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -731,48 +731,15 @@ public SockIO getSock( String key, Integer hashCode ) {
731731
}
732732

733733
// from here on, we are working w/ multiple servers
734-
int tries = 0;
735-
736-
// generate hashcode
737-
int hv;
738-
if ( hashCode != null ) {
739-
hv = hashCode.intValue();
740-
}
741-
else {
742-
743-
// NATIVE_HASH = 0
744-
// OLD_COMPAT_HASH = 1
745-
// NEW_COMPAT_HASH = 2
746-
switch ( hashingAlg ) {
747-
case NATIVE_HASH:
748-
hv = key.hashCode();
749-
break;
750-
751-
case OLD_COMPAT_HASH:
752-
hv = origCompatHashingAlg( key );
753-
break;
754-
755-
case NEW_COMPAT_HASH:
756-
hv = newCompatHashingAlg( key );
757-
break;
758-
759-
default:
760-
// use the native hash as a default
761-
hv = key.hashCode();
762-
hashingAlg = NATIVE_HASH;
763-
break;
764-
}
765-
}
766-
767734
// keep trying different servers until we find one
768735
int bucketSize = buckets.size();
769736
boolean[] triedBucket = new boolean[ bucketSize ];
770737
Arrays.fill( triedBucket, false );
771738

772739
// get initial bucket
773-
int bucket = hv % bucketSize;
774-
if ( bucket < 0 ) bucket *= -1;
740+
int bucket = getBucket( key, hashCode );
775741

742+
int tries = 0;
776743
while ( tries++ < bucketSize ) {
777744

778745
// try to get socket from bucket
@@ -820,43 +787,64 @@ public SockIO getSock( String key, Integer hashCode ) {
820787
// if we failed to get a socket from this server
821788
// then we try again by adding an incrementer to the
822789
// current key and then rehashing
790+
log.debug( "we need to rehash as we want to failover and we still have servers to try" );
823791
int rehashTries = 0;
824792
while ( triedBucket[ bucket ] ) {
825793

826794
int keyTry = tries + rehashTries;
827795
String newKey = String.format( "%s%s", keyTry, key );
828796

829797
log.debug( "rehashing with: " + keyTry );
830-
switch ( hashingAlg ) {
831-
case NATIVE_HASH:
832-
hv = newKey.hashCode();
833-
break;
798+
bucket = getBucket( newKey, null );
834799

835-
case OLD_COMPAT_HASH:
836-
hv = origCompatHashingAlg( newKey );
837-
break;
800+
rehashTries++;
801+
}
802+
}
803+
}
838804

839-
case NEW_COMPAT_HASH:
840-
hv = newCompatHashingAlg( newKey );
841-
break;
805+
return null;
806+
}
842807

843-
default:
844-
// use the native hash as a default
845-
hv = newKey.hashCode();
846-
hashingAlg = NATIVE_HASH;
847-
break;
848-
}
808+
/**
809+
* Returns a bucket to check for a given key.
810+
*
811+
* @param key String key cache is stored under
812+
* @return int bucket
813+
*/
814+
public int getBucket( String key, Integer hashCode ) {
849815

850-
rehashTries++;
816+
int hc;
851817

852-
// new bucket
853-
bucket = hv % bucketSize;
854-
if ( bucket < 0 ) bucket *= -1;
855-
}
818+
if ( hashCode != null ) {
819+
hc = hashCode.intValue();
820+
}
821+
else {
822+
switch ( hashingAlg ) {
823+
case NATIVE_HASH:
824+
hc = key.hashCode();
825+
break;
826+
827+
case OLD_COMPAT_HASH:
828+
hc = origCompatHashingAlg( key );
829+
break;
830+
831+
case NEW_COMPAT_HASH:
832+
hc = newCompatHashingAlg( key );
833+
break;
834+
835+
default:
836+
// use the native hash as a default
837+
hc = key.hashCode();
838+
hashingAlg = NATIVE_HASH;
839+
break;
856840
}
857841
}
858842

859-
return null;
843+
// new bucket
844+
int bucket = hc % buckets.size();
845+
if ( bucket < 0 ) bucket *= -1;
846+
847+
return bucket;
860848
}
861849

862850
/**

0 commit comments

Comments
 (0)