Skip to content

Commit 4e44da4

Browse files
committed
tweak: code cleanup
1 parent 62b2cf4 commit 4e44da4

File tree

1 file changed

+80
-77
lines changed

1 file changed

+80
-77
lines changed

src/com/danga/MemCached/MemCachedClient.java

Lines changed: 80 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -320,60 +320,62 @@ public boolean delete(String key, Date expiry) {
320320
* @param expiry when to expire the record.
321321
* @return <code>true</code>, if the data was deleted successfully
322322
*/
323-
public boolean delete(String key, Integer hashCode, Date expiry) {
323+
public boolean delete( String key, Integer hashCode, Date expiry ) {
324324

325325
// get SockIO obj from hash or from key
326-
SockIOPool.SockIO sock = SockIOPool.getInstance( poolName ).getSock(key, hashCode);
326+
SockIOPool.SockIO sock = SockIOPool.getInstance( poolName ).getSock( key, hashCode );
327327

328328
// return false if unable to get SockIO obj
329-
if (sock == null)
329+
if ( sock == null )
330330
return false;
331331

332332
// build command
333-
StringBuffer command = new StringBuffer("delete " + key);
334-
if (expiry != null) {
335-
command.append(" " + expiry.getTime() / 1000);
336-
}
337-
command.append("\r\n");
333+
StringBuffer command = new StringBuffer( "delete " ).append( key );
334+
if ( expiry != null )
335+
command.append( " " + expiry.getTime() / 1000 );
336+
337+
command.append( "\r\n" );
338338

339339
try {
340-
sock.write(command.toString().getBytes());
340+
sock.write( command.toString().getBytes() );
341341
sock.flush();
342342

343343
// if we get appropriate response back, then we return true
344344
String line = sock.readLine();
345-
if (DELETED.equals(line)) {
346-
log.info("++++ deletion of key: " + key + " from cache was a success");
345+
if ( DELETED.equals( line ) ) {
346+
log.info( "++++ deletion of key: " + key + " from cache was a success" );
347347

348348
// return sock to pool and bail here
349349
sock.close();
350+
sock = null;
350351
return true;
351-
352-
} else if (NOTFOUND.equals(line)) {
353-
log.info("++++ deletion of key: " + key + " from cache failed as the key was not found");
354-
355-
} else {
356-
log.error("++++ error deleting key: " + key);
357-
log.error(line);
352+
}
353+
else if ( NOTFOUND.equals( line ) ) {
354+
log.info( "++++ deletion of key: " + key + " from cache failed as the key was not found" );
355+
}
356+
else {
357+
log.error( "++++ error deleting key: " + key );
358+
log.error( line );
358359
}
359360
}
360-
catch (IOException e) {
361+
catch ( IOException e ) {
361362
// exception thrown
362-
log.error("++++ exception thrown while writing bytes to server on delete");
363-
log.error(e.getMessage(), e);
363+
log.error( "++++ exception thrown while writing bytes to server on delete" );
364+
log.error( e.getMessage(), e );
364365

365366
try {
366367
sock.trueClose();
367368
}
368-
catch(IOException ioe) {
369-
log.error("++++ failed to close socket : " + sock.toString());
369+
catch ( IOException ioe ) {
370+
log.error( "++++ failed to close socket : " + sock.toString() );
370371
}
371372

372373
sock = null;
373374
}
374375

375-
if (sock != null)
376+
if ( sock != null )
376377
sock.close();
378+
377379
return false;
378380
}
379381

@@ -384,8 +386,8 @@ public boolean delete(String key, Integer hashCode, Date expiry) {
384386
* @param value value to store
385387
* @return true, if the data was successfully stored
386388
*/
387-
public boolean set(String key, Object value) {
388-
return set("set", key, value, null, null, primitiveAsString);
389+
public boolean set( String key, Object value ) {
390+
return set( "set", key, value, null, null, primitiveAsString );
389391
}
390392

391393
/**
@@ -396,8 +398,8 @@ public boolean set(String key, Object value) {
396398
* @param hashCode if not null, then the int hashcode to use
397399
* @return true, if the data was successfully stored
398400
*/
399-
public boolean set(String key, Object value, Integer hashCode) {
400-
return set("set", key, value, null, hashCode, primitiveAsString);
401+
public boolean set( String key, Object value, Integer hashCode ) {
402+
return set( "set", key, value, null, hashCode, primitiveAsString );
401403
}
402404

403405
/**
@@ -408,8 +410,8 @@ public boolean set(String key, Object value, Integer hashCode) {
408410
* @param expiry when to expire the record
409411
* @return true, if the data was successfully stored
410412
*/
411-
public boolean set(String key, Object value, Date expiry) {
412-
return set("set", key, value, expiry, null, primitiveAsString);
413+
public boolean set( String key, Object value, Date expiry ) {
414+
return set( "set", key, value, expiry, null, primitiveAsString );
413415
}
414416

415417
/**
@@ -541,15 +543,15 @@ public boolean replace(String key, Object value, Date expiry, Integer hashCode)
541543
* @param asString store this object as a string?
542544
* @return true/false indicating success
543545
*/
544-
private boolean set(String cmdname, String key, Object value, Date expiry, Integer hashCode, boolean asString) {
546+
private boolean set( String cmdname, String key, Object value, Date expiry, Integer hashCode, boolean asString ) {
545547

546548
// get SockIO obj
547-
SockIOPool.SockIO sock = SockIOPool.getInstance( poolName ).getSock(key, hashCode);
549+
SockIOPool.SockIO sock = SockIOPool.getInstance( poolName ).getSock( key, hashCode );
548550

549-
if (sock == null)
551+
if ( sock == null )
550552
return false;
551553

552-
if (expiry == null)
554+
if ( expiry == null )
553555
expiry = new Date(0);
554556

555557
// store flags
@@ -563,13 +565,14 @@ private boolean set(String cmdname, String key, Object value, Date expiry, Integ
563565
if ( asString ) {
564566
// useful for sharing data between java and non-java
565567
// and also for storing ints for the increment method
566-
log.info("++++ storing data as a string for key: " + key + " for class: " + value.getClass().getName());
568+
log.info( "++++ storing data as a string for key: " + key + " for class: " + value.getClass().getName() );
567569
try {
568-
val = value.toString().getBytes(defaultEncoding);
570+
val = value.toString().getBytes( defaultEncoding );
569571
}
570-
catch (UnsupportedEncodingException ue) {
571-
log.error("invalid encoding type used: " + defaultEncoding);
572+
catch ( UnsupportedEncodingException ue ) {
573+
log.error( "invalid encoding type used: " + defaultEncoding );
572574
sock.close();
575+
sock = null;
573576
return false;
574577
}
575578
}
@@ -579,104 +582,104 @@ private boolean set(String cmdname, String key, Object value, Date expiry, Integ
579582
try {
580583
val = NativeHandler.encode( value );
581584
}
582-
catch (Exception e) {
583-
log.error("Failed to native handle obj", e);
585+
catch ( Exception e ) {
586+
log.error( "Failed to native handle obj", e );
584587

585588
sock.close();
589+
sock = null;
586590
return false;
587591
}
588592
}
589593
}
590594
else {
591595
// always serialize for non-primitive types
592-
log.info("++++ serializing for key: " + key + " for class: " + value.getClass().getName());
596+
log.info( "++++ serializing for key: " + key + " for class: " + value.getClass().getName() );
593597
try {
594598
ByteArrayOutputStream bos = new ByteArrayOutputStream();
595-
(new ObjectOutputStream(bos)).writeObject(value);
599+
(new ObjectOutputStream( bos )).writeObject( value );
596600
val = bos.toByteArray();
597601
flags |= F_SERIALIZED;
598602
}
599-
catch (IOException e) {
603+
catch ( IOException e ) {
600604
// if we fail to serialize, then
601605
// we bail
602-
log.error("failed to serialize obj", e);
603-
log.error(value.toString());
606+
log.error( "failed to serialize obj", e );
607+
log.error( value.toString() );
604608

605609
// return socket to pool and bail
606610
sock.close();
611+
sock = null;
607612
return false;
608613
}
609614
}
610615

611616
// now try to compress if we want to
612617
// and if the length is over the threshold
613-
if (compressEnable && val.length > compressThreshold) {
614-
log.info("++++ trying to compress data");
615-
log.info("++++ size prior to compression: " + val.length);
618+
if ( compressEnable && val.length > compressThreshold ) {
619+
log.info( "++++ trying to compress data" );
620+
log.info( "++++ size prior to compression: " + val.length );
616621

617622
try {
618-
ByteArrayOutputStream bos = new ByteArrayOutputStream(val.length);
619-
GZIPOutputStream gos = new GZIPOutputStream(bos);
620-
gos.write(val, 0, val.length);
623+
ByteArrayOutputStream bos = new ByteArrayOutputStream( val.length );
624+
GZIPOutputStream gos = new GZIPOutputStream( bos );
625+
gos.write( val, 0, val.length );
621626
gos.finish();
622627

623628
// store it and set compression flag
624629
val = bos.toByteArray();
625630
flags |= F_COMPRESSED;
626631

627-
log.info("++++ compression succeeded, size after: " + val.length);
628-
632+
log.info( "++++ compression succeeded, size after: " + val.length );
629633
}
630634
catch (IOException e) {
631-
log.error("IOException while compressing stream: " + e.getMessage());
632-
log.error("storing data uncompressed");
635+
log.error( "IOException while compressing stream: " + e.getMessage() );
636+
log.error( "storing data uncompressed" );
633637
}
634638
}
635639

636640
// now write the data to the cache server
637641
try {
638642
String cmd = cmdname + " " + key + " " + flags + " "
639643
+ expiry.getTime() / 1000 + " " + val.length + "\r\n";
640-
sock.write(cmd.getBytes());
641-
sock.write(val);
642-
sock.write("\r\n".getBytes());
644+
sock.write( cmd.getBytes() );
645+
sock.write( val );
646+
sock.write( "\r\n".getBytes() );
643647
sock.flush();
644648

645649
// get result code
646650
String line = sock.readLine();
647-
log.info("++++ memcache cmd (result code): " + cmd + " (" + line + ")");
651+
log.info( "++++ memcache cmd (result code): " + cmd + " (" + line + ")" );
648652

649-
if (STORED.equals(line)) {
650-
log.info("++++ data successfully stored for key: " + key);
653+
if ( STORED.equals( line ) ) {
654+
log.info("++++ data successfully stored for key: " + key );
651655
sock.close();
656+
sock = null;
652657
return true;
653-
654658
}
655-
else if (NOTSTORED.equals(line)) {
656-
log.info("++++ data not stored in cache for key: " + key);
657-
659+
else if ( NOTSTORED.equals( line ) ) {
660+
log.info( "++++ data not stored in cache for key: " + key );
658661
}
659662
else {
660-
log.error("++++ error storing data in cache for key: " + key + " -- length: " + val.length);
661-
log.error(line);
663+
log.error( "++++ error storing data in cache for key: " + key + " -- length: " + val.length );
664+
log.error( line );
662665
}
663666
}
664-
catch (IOException e) {
667+
catch ( IOException e ) {
665668
// exception thrown
666-
log.error("++++ exception thrown while writing bytes to server on delete");
667-
log.error(e.getMessage(), e);
669+
log.error( "++++ exception thrown while writing bytes to server on delete" );
670+
log.error( e.getMessage(), e );
668671

669672
try {
670673
sock.trueClose();
671674
}
672-
catch (IOException ioe) {
673-
log.error("++++ failed to close socket : " + sock.toString());
675+
catch ( IOException ioe ) {
676+
log.error( "++++ failed to close socket : " + sock.toString() );
674677
}
675678

676679
sock = null;
677680
}
678681

679-
if (sock != null)
682+
if ( sock != null )
680683
sock.close();
681684

682685
return false;
@@ -689,8 +692,8 @@ else if (NOTSTORED.equals(line)) {
689692
* @param counter number to store
690693
* @return true/false indicating success
691694
*/
692-
public boolean storeCounter(String key, long counter) {
693-
return set("set", key, new Long(counter), null, null, true);
695+
public boolean storeCounter( String key, long counter ) {
696+
return set( "set", key, new Long(counter), null, null, true );
694697
}
695698

696699
/**
@@ -700,8 +703,8 @@ public boolean storeCounter(String key, long counter) {
700703
* @param counter number to store
701704
* @return true/false indicating success
702705
*/
703-
public boolean storeCounter(String key, Long counter) {
704-
return set("set", key, counter, null, null, true);
706+
public boolean storeCounter( String key, Long counter ) {
707+
return set( "set", key, counter, null, null, true );
705708
}
706709

707710
/**

0 commit comments

Comments
 (0)