redis相關知識(六)

來源:互聯網
上載者:User

1.7.1.3 SkipList(跳躍表)操作

1) An empty SkipList

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/21010L0T-0.jpg" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;text-align:center;background-color:#f3f3f3;padding:4px 5px 5px;margin:10px;" alt="Image011810.jpg" />

2) Finding an element with key x

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/21010L328-1.jpg" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;text-align:center;background-color:#f3f3f3;padding:4px 5px 5px;margin:10px;" alt="Image011820.jpg" />

12345678910111213 p=topWhile(1){while (p->next->key < x ) p=p->next;If (p->down == NULL ) return p->nextp=p->down ;}

Observe that we return x, if exists, or succ(x) if x is not in the SkipList

3) Inserting new element X

Determine k the number of levels in which x participates (explained later)

Do find(x), and insert x to the appropriate places in the lowest k levels. (after the elements at which the search path turns down or terminates)

Example – inserting 119. k=2

If k is larger than the current number of levels, add new levels (and update top)

Example – inser(119) when k=4

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/21010J252-2.jpg" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;text-align:center;background-color:#f3f3f3;padding:4px 5px 5px;margin:10px;" alt="Image011840.jpg" />

Determining k

k – the number of levels at which an element x participate.

Use a random function OurRnd() — returns 1 or 0 (True/False) with equal probability.

k=1 ;

While( OurRnd() ) k++ ;

Deleteing a key x

Find x in all the levels it participates, and delete it using the standard ‘delete from a linked list’ method.

If one or more of the upper levels are empty, remove them (and update top)

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/21010L217-3.jpg" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;text-align:center;background-color:#f3f3f3;padding:4px 5px 5px;margin:10px;" alt="Image011850.jpg" />

Facts about SkipList

The expected number of levels is O( log n )

(here n is the numer of elements)

The expected time for insert/delete/find is O( log n )

The expected size (number of cells) is O(n )

1.7.2 redis SkipList 實現

/* ZSETs use a specialized version of Skiplists */

12345678910111213141516171819202122232425262728293031323334353637383940414243 typedef struct zskiplistNode{robj *obj;double score;struct zskiplistNode *backward;struct zskiplistLevel{struct zskiplistNode *forward;unsigned int span;} level[];} zskiplistNode;typedef struct zskiplist{struct zskiplistNode *header, *tail;unsigned long length;int level;} zskiplist;typedef struct zset{dict *dict;zskiplist *zsl;} zset;

本文出自 “螞蟻窩” 部落格,請務必保留此出處http://feihan21.blog.51cto.com/1364153/1300026

相關文章

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.