Java memcache Client data operation source code analysis

Source: Internet
Author: User

Java memcache Client data operation source code analysis
Zookeeper

After learning how to use Java_Memcache to operate memcache, I studied the source code of Java_Memcache with great interest. Java_Memcache encapsulates the data operation methods set/add/delete/append/get In The AscIIClient class.

Storage Data set

According to the Memcache command details, we know that the original set command format of memcache is

Set \ R \ n

\ R \ n

In Java_Memcache, the set (String, String, Object, Date, Integer, Long, and flag) method is called at the beginning.

 

 

 

private boolean set(String s, String s1, Object obj, Date date, Integer integer, Long long1, boolean flag){    SchoonerSockIO schoonersockio;    int i;    String s2;    ......    s1 = sanitizeKey(s1);    ......    schoonersockio = pool.getSock(s1, integer);    if(schoonersockio == null)    {        if(errorHandler != null)            errorHandler.handleErrorOnSet(this, new IOException("no socket to server available"), s1);        return false;    }    if(date == null)        date = new Date(0L);    i = NativeHandler.getMarkerFlag(obj);    s2 = s + " " + s1 + " " + i + " " + date.getTime() / 1000L + " ";    boolean flag1;    schoonersockio.writeBuf.clear();    schoonersockio.writeBuf.put(s2.getBytes());    int j = schoonersockio.writeBuf.position();    schoonersockio.writeBuf.put(BLAND_DATA_SIZE);    if(long1.longValue() != 0L)        schoonersockio.writeBuf.put((new StringBuilder()).append(" ").append(long1.toString()).toString().getBytes());    schoonersockio.writeBuf.put(B_RETURN);    SockOutputStream sockoutputstream = new SockOutputStream(schoonersockio);    int k = 0;    if(i != 0)    {        byte abyte0[];        if(flag)            abyte0 = obj.toString().getBytes(defaultEncoding);        else            abyte0 = NativeHandler.encode(obj);        sockoutputstream.write(abyte0);        k = abyte0.length;    } else    {        k = transCoder.encode(sockoutputstream, obj);    }    schoonersockio.writeBuf.put(B_RETURN);    byte abyte1[] = (new Integer(k)).toString().getBytes();    int l = schoonersockio.writeBuf.position();    schoonersockio.writeBuf.position(j);    schoonersockio.writeBuf.put(abyte1);    schoonersockio.writeBuf.position(l);    schoonersockio.flush();    String s3 = (new SockInputStream(schoonersockio, 2147483647)).getLine();    if(!"STORED\r\n".equals(s3))        break MISSING_BLOCK_LABEL_538;    ......    return false;}

 

 

When we use MemCacheClient. set ("name", "abcdef"), by comparing the above code, first I = NativeHandler. the value of getMarkerFlag ("abcdef") is 32, then the string s2 = "set name 32 0" is spliced, s2 is put into ByteBuffer, and then the system calls the schonersockio. writeBuf. put (BLAND_DATA_SIZE), while BLADN_DATA_SIZE is "". getBytes () is actually "set name 32 0", and then the final schonersockio. writeBuf. put (B _RETURN) is input \ r \ n, and then pass transCoder. encode () writes value.

 

public void encode(OutputStream outputstream, Object obj)throws IOException{    ObjectOutputStream objectoutputstream = new ObjectOutputStream(outputstream);    objectoutputstream.writeObject(obj);    objectoutputstream.close();}

 

 

Actually, the object is serialized. Therefore, all custom objects operated by Java_Memcache must implement the Serializable interface.

However, if we run "set name 32 0 \ r \ n" directly, an error message is displayed.

I am confused about the last How are parameters handled ????

Is the decompiled code incomplete or is the decompiled Code incorrect?

Data extraction get

Similarly, the original get command of memcache is very simple, and its basic format is:

Get \ R \ n

In Java_Memcache, The AscIIClient is processed as follows:

 

public Object get(String s){    return get(s, null);}public Object get(String s, Integer integer){    return get("get", s, integer, false);}
Private Object get (String s, String s1, Integer integer, boolean flag) {schonersockio; String s2 ;.... s1 = sanitizeKey (s1); schonersockio = pool. getSock (s1, integer); s2 = (new StringBuilder ()). append (s ). append (""). append (s1 ). toString (); int I; int j; SockInputStream sockinputstream; boolean flag1; StringBuffer stringbuffer; int l; schonersockio. writeBuf. clear (); schonersockio. WriteBuf. put (s2.getBytes (); schonersockio. writeBuf. put (B _RETURN); schonersockio. flush (); I = 0; j = 0; sockinputstream = new SockInputStream (schonersockio, 2147483647); flag1 = false; stringbuffer = new StringBuffer (); l = 0; // The first part is to build the string "get name \ r \ n", that is, to send the get command _ L5: int k to the Memcache server; if (flag1) break MISSING_BLOCK_LABEL_365; k = sockinputstream. read (); if (k! = 32 & k! = 13) break MISSING_BLOCK_LABEL_353; // always read to the carriage return or line feed l; jvm instr tableswitch 0 3: default 322 goto _ L1 _ L2 _ L1 _ L3 _ L4 // here is a while loop that receives the message character _ L1: break returned from the memcache server; /* Loop/switch isn' t completed */_ L2: Object obj; if (! "END \ r \ n ". startsWith (stringbuffer. toString () break;/* Loop/switch isn't completed * // if it is not an END \ r row, continue to process it, otherwise, null obj = null is returned. if (schonersockio! = Null) {schonersockio. close (); schonersockio = null;} return obj; // when there is no corresponding key value in memcache, no value is obtained, after the get command is called, END/r/n is obtained directly, and null _ L3: j = Integer is returned. parseInt (stringbuffer. toString (); break;/* Loop/switch isn't completed */_ L4: I = Integer. parseInt (stringbuffer. toString (); l ++; stringbuffer = new StringBuffer (); if (k = 13) {sockinputstream. read (); flag1 = true;} goto _ L5 stringbuffer. append (char) k); Goto _ L5 Object obj3; Object obj1 = null; sockinputstream. willRead (I); if (I> 0) if (NativeHandler. isHandled (j) {byte abyte0 [] = sockinputstream. getBuffer (); if (j & 2) = 2) {GZIPInputStream gzipinputstream = new GZIPInputStream (new feature (abyte0); ByteArrayOutputStream bytearrayoutputstream = new feature (abyte0.length ); byte abyte1 [] = new byte [2048]; int i1; whi Le (i1 = gzipinputstream. read (abyte1 ))! =-1) bytearrayoutputstream. write (abyte1, 0, i1); abyte0 = bytearrayoutputstream. toByteArray (); // read data to the gzipinputstream In the byte array. close ();} if (primitiveAsString | flag) obj1 = new String (abyte0, defaultEncoding); else obj1 = NativeHandler. decode (abyte0, j); // finally call decodeString to decode the string} else if (transCoder! = Null) {Object obj2 = sockinputstream; if (j & 2) = 2) obj2 = new GZIPInputStream (java. io. inputStream) (obj2); if (classLoader = null) obj1 = transCoder. decode (java. io. inputStream) (obj2); else obj1 = (ObjectTransCoder) transCoder ). decode (java. io. inputStream) (obj2), classLoader);} sockinputstream. willRead (2147483647); sockinputstream. getLine (); sockinputstream. getLine (); obj3 = obj1; If (schonersockio! = Null) {schonersockio. close (); schonersockio = null;} return obj3 ;}

 

 

We can compare the input and output operations using the memcache command, that is, after the get command is input, the character sequence is parsed and encoded into actual data.


 

It may be difficult to understand the decompiled code. You can view the original code of the Java_Memcache client to learn more easily.

Some versions of the original code are directly get/set data in MemcacheClient.

 

This code analysis is easier for Memcache to process data.

Summary

So far, we have introduced how to install and operate Memcache in windows, analyzed the distributed principle of Memcache, discussed general Hash algorithms, consistent Hash algorithms, and how to operate data. We have a deep understanding of Java_Memcache Client as a whole. The main idea is to maintain a set of distributed Server information and maintain Socket connections for each Server, map Hash data to a Server based on the operation data, and finally build a memcache command to operate and parse the data.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.