Redis study notes (Part I): string and list

The study in addition to the basic content of the main thinking three questions: why (Why), what (What is the principle), which (What kind of something like that, what is the difference compared to).

Since I am more familiar with java, java and also has strings and lists. So Benpian temporarily take redis strings and list the java comparison.

String

Look at a few questions:

  • redis have not used C language as a string array of characters? The answer: no
  • Then the array of characters C language has limitations that what java and redis are redefining their string it?
  • What is the definition of a string structure redis? java it?
  • Java string structure and redis What is the difference? Why would the formation of such a distinction? Both Which is better?

Limitations of C arrays

  1. Inconvenient to take the string length, c language character array is not stored length of the string, it is necessary to traverse. Time (time complexity): O (n)
  2. Unsafe, two character arrays may cause a buffer overflow when splicing, resulting in damage to other data.
  3. Every string additions and deletions will lead to relocate or reclaim memory, affect efficiency.
  4. With \ 0 determines whether the end, can only store text data.
  5. Compares two strings are equal, the cycle requires comparing each character, Time: O (n).

Since the array of characters C language has more limitations, so redis and java have redefined own string structure.

What is the definition of a string structure redis? java it?

redis strings are constructed own abstract type identifier called simple dynamic string (SDS) is. Its specific structure is as follows:

struct sdshdr {
    //字符串长度
    int len;
    // buf中未使用的字节数
    int free;
    // 字节数组,用于保存字符串
    char buf[];
}

redis this data structure, a good solution to the limitations of 1,2,3,4-point C language array of characters. Also worth noting that in sdshdr buf array, is to "\ 0" at the end, the total length of buf is: len + free + 1. So why waste this one byte of it? Mainly want to reuse C language strings for some functions, such as connection, output and the like.

There is a point to note what is not redis string stored coding, storage and retrieval are directly transmitted via binary, so in theory as long as the client encoding and decoding way, is not garbled of. This is also the reason for the comment above buf's write byte array.

java string it? It is directly engaged into a string constant. as follows:

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];

    /** Cache the hash code for the string */
    private int hash; // Default to 0
    ......
}

java string also solves the first point c language 1/2/3/4 array of characters.

  • Since the java stored directly in the array length, so to avoid the 1/4 point C arrays limitations.
  • Because String is a constant, so the 2/3 point c language limitations of the character array does not exist.
  • To address the limitations of point 5, the character string is java introduced the concept of hashCode.
    It is because in the string, so only set to a constant string hash stored directly (otherwise, change every time the value of the string, you need to recalculate the value of hashcode, a waste of efficiency). Because the string is constant, so only the variable string StringBuilder, StringBuffer's. . . . . .

Now we come back to take a look redis structure in strings, there is an array, the array has a length of stored data. Think of what? Java, StringBuilder, StringBuffer, ArrayList is not all that structure? Do they have in common? Is not all variable array (StringBuilder, StringBuffer can be seen as a variable array of characters)?

Java string comparison and the redis

  1. redis string array of characters (buf) is "\ 0" at the end, Java is not. why?
    Because redis the beginning of the design is efficient, fast running cache. So will choose in C language (because c is all structured languages run the fastest), and c of the array of characters and the same end, so that it can be used directly without repeating a function of c-create the wheel.
    java the beginning of the design is a cross-platform language, so all function strings are their own implementation, so no additional waste a byte of space.
  2. redis defines a string sds, but also based on the array of characters C. And c java directly to redefine the array (the array C is not directly available length of the array).
  3. About string comparison for equality issues:
    the Java has hashcode concept to enhance the speed of string comparisons for equality. redis in is how to do it? For example, when the get key, how to locate a specific key, and get the value of it? This problem is not yet known.

The reasons for these differences, mainly its positioning is different. redis cache location is required quickly. java position is language, the most important thing is cross-platform and scalability.

List

Also look at three questions:

  1. Why is the list generated?
  2. What is the principle and other data structures again a contrast.
  3. Java linked list and what is the difference?

Why is the list generated?

The list is generated mainly because of the limitations of the array.
Such as: insert, delete slow. You can not dynamically add elements.

What is the principle

redis linked list structure is as follows:

typedef struct listNode {
    struct listNode *prev; //前驱节点,如果是list的头结点,则prev指向NULL
    struct listNode *next;//后继节点,如果是list尾部结点,则next指向NULL
    void *value;            //万能指针,能够存放任何信息
} listNode;

typedef struct list {
    listNode *head;     //链表头结点指针
    listNode *tail;     //链表尾结点指针

    //下面的三个函数指针就像类中的成员函数一样
    void *(*dup)(void *ptr);    //复制链表节点保存的值
    void (*free)(void *ptr);    //释放链表节点保存的值
    int (*match)(void *ptr, void *key); //比较链表节点所保存的节点值和另一个输入的值是否相等
    unsigned long len;      //链表长度计数器
} list;

java linked list structure is as follows:

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable{
    transient int size = 0;

    /**
    * Pointer to first node.
    * Invariant: (first == null && last == null) ||
    *            (first.prev == null && first.item != null)
    */
    transient Node<E> first;

    /**
    * Pointer to last node.
    * Invariant: (first == null && last == null) ||
    *            (last.next == null && last.item != null)
    */
    transient Node<E> last;
    .......
}

What redis java linked list and the list are not the same?

Basically the same, are not two-way circular list.

Two-way advantage is that you can traverse forward, it can also traverse backwards. The disadvantage is that each node needs to waste a pointer to point to the previous node.

Guess you like

Origin www.cnblogs.com/wind-snow/p/11019260.html