{"id":4814,"date":"2020-02-23T17:43:48","date_gmt":"2020-02-23T09:43:48","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=4814"},"modified":"2020-02-23T17:43:48","modified_gmt":"2020-02-23T09:43:48","slug":"%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9aredis%e7%9a%84sds%e5%92%8cc%e4%b8%ad%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9b%b8%e6%af%94%e6%9c%89%e4%bb%80%e4%b9%88%e4%bc%98%e5%8a%bf%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4814","title":{"rendered":"\u9762\u8bd5\u9898\uff1aRedis\u7684SDS\u548cC\u4e2d\u5b57\u7b26\u4e32\u76f8\u6bd4\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u4f17\u6240\u5468\u77e5\uff0cRedis \u662f\u73b0\u5728\u6700\u706b\u7684 nosql \u7cfb\u7edf\u3002\u9762\u8bd5\u5fc5\u5907\uff0c\u9762\u8bd5\u5fc5\u4f1a\u3002\u4eca\u5e74\u60c5\u51b5\u7279\u522b\u7279\u6b8a\uff0c\u56f0\u96be\u91cd\u91cd\uff0c\u524d\u6bb5\u65f6\u95f4\u7fa4\u91cc\u522e\u8d77\u4e86\u4e00\u9635\u5b66\u4e60 Redis \u6e90\u7801\u7684\u98ce\u3002\u521a\u597d\u6700\u8fd1\u6709\u7f51\u53cb\u9762\u8bd5\u9047\u5230\u4e86\uff0cRedis \u7684 SDS \u548c C \u4e2d\u5b57\u7b26\u4e32\u76f8\u6bd4\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f\u672c\u6587\u6765\u7b80\u5355\u7684\u8bf4\u4e00\u8bf4\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 C \u8bed\u8a00\u4e2d\u4f7f\u7528 N + 1 \u957f\u5ea6\u7684\u5b57\u7b26\u6570\u7ec4\u6765\u8868\u793a\u5b57\u7b26\u4e32\uff0c\u5c3e\u90e8\u4f7f\u7528&#8217;\\0&#8217;\u4f5c\u4e3a\u7ed3\u5c3e\u6807\u5fd7\uff0c\u5bf9\u4e8e\u6b64\u79cd\u5b9e\u73b0\u65e0\u6cd5\u6ee1\u8db3 Redis \u5bf9\u4e8e\u5b89\u5168\u6027\u3001\u6548\u7387\u3001\u4e30\u5bcc\u7684\u529f\u80fd\u7684\u8981\u6c42\uff0c\u56e0\u6b64 Redis \u5355\u72ec\u5c01\u88c5\u4e86 SDS \u7b80\u5355\u52a8\u6001\u5b57\u7b26\u4e32\u7ed3\u6784\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u7406\u89e3 SDS \u7684\u4f18\u52bf\u4e4b\u524d\u9700\u8981\u5148\u770b\u4e0b SDS \u7684\u5b9e\u73b0\u7ec6\u8282\uff0c\u627e\u4e86 github \u6700\u65b0\u7684 src\/sds.h \u7684\u5b9a\u4e49\u770b\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef char *sds;\n\n\/*\u8fd9\u4e2a\u7528\u4e0d\u5230 \u5ffd\u7565\u5373\u53ef*\/\nstruct __attribute__ ((__packed__)) sdshdr5 {\n    unsigned char flags; \/* 3 lsb of type, and 5 msb of string length *\/\n    char buf[];\n};\n\n\/*\u4e0d\u540c\u957f\u5ea6\u7684header 8 16 32 64\u51714\u79cd \u90fd\u7ed9\u51fa\u4e86\u56db\u4e2a\u6210\u5458\nlen\uff1a\u5f53\u524d\u4f7f\u7528\u7684\u7a7a\u95f4\u5927\u5c0f\uff1balloc\u53bb\u6389header\u548c\u7ed3\u5c3e\u7a7a\u5b57\u7b26\u7684\u6700\u5927\u7a7a\u95f4\u5927\u5c0f\nflags:8\u4f4d\u7684\u6807\u8bb0 \u4e0b\u9762\u5173\u4e8eSDS_TYPE_x\u7684\u5b8f\u5b9a\u4e49\u53ea\u67095\u79cd 3bit\u8db3\u591f\u4e86 5bit\u6ca1\u6709\u7528\nbuf:\u8fd9\u4e2a\u8ddfC\u8bed\u8a00\u4e2d\u7684\u5b57\u7b26\u6570\u7ec4\u662f\u4e00\u6837\u7684\uff0c\u4ecetypedef char* sds\u53ef\u4ee5\u77e5\u9053\u5c31\u662f\u8fd9\u6837\u7684\u3002\nbuf\u7684\u6700\u5927\u957f\u5ea6\u662f2^n \u5176\u4e2dn\u4e3asdshdr\u7684\u7c7b\u578b\uff0c\u5982\u5f53\u9009\u62e9sdshdr16\uff0cbuf_max=2^16\u3002\n*\/\nstruct __attribute__ ((__packed__)) sdshdr8 {\n    uint8_t len; \/* used *\/\n    uint8_t alloc; \/* excluding the header and null terminator *\/\n    unsigned char flags; \/* 3 lsb of type, 5 unused bits *\/\n    char buf[];\n};\nstruct __attribute__ ((__packed__)) sdshdr16 {\n    uint16_t len; \/* used *\/\n    uint16_t alloc; \/* excluding the header and null terminator *\/\n    unsigned char flags; \/* 3 lsb of type, 5 unused bits *\/\n    char buf[];\n};\nstruct __attribute__ ((__packed__)) sdshdr32 {\n    uint32_t len; \/* used *\/\n    uint32_t alloc; \/* excluding the header and null terminator *\/\n    unsigned char flags; \/* 3 lsb of type, 5 unused bits *\/\n    char buf[];\n};\nstruct __attribute__ ((__packed__)) sdshdr64 {\n    uint64_t len; \/* used *\/\n    uint64_t alloc; \/* excluding the header and null terminator *\/\n    unsigned char flags; \/* 3 lsb of type, 5 unused bits *\/\n    char buf[];\n};\n\n#define SDS_TYPE_5  0\n#define SDS_TYPE_8  1\n#define SDS_TYPE_16 2\n#define SDS_TYPE_32 3\n#define SDS_TYPE_64 4\n#define SDS_TYPE_MASK 7\n#define SDS_TYPE_BITS 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u770b\u4e86\u524d\u9762\u7684\u5b9a\u4e49\uff0c\u7b14\u8005\u753b\u4e86\u4e2a\u56fe\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/gitee.com\/xmtblog\/img\/raw\/master\/1582450617426.jpg\" alt=\"\" \/><figcaption>Redis \u7b80\u5355\u52a8\u6001\u6570\u7ec4 SDS \u7ed3\u6784<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u56fe\u4e2d\u53ef\u4ee5\u77e5\u9053 sds \u672c\u8d28\u5206\u4e3a\u4e09\u90e8\u5206\uff1aheader\u3001buf\u3001null \u7ed3\u5c3e\u7b26\uff0c\u5176\u4e2d header \u53ef\u4ee5\u8ba4\u4e3a\u662f\u6574\u4e2a sds \u7684\u6307\u5f15\u90e8\u5206\uff0c\u7ed9\u5b9a\u4e86\u4f7f\u7528\u7684\u7a7a\u95f4\u5927\u5c0f\u3001\u6700\u5927\u5206\u914d\u5927\u5c0f\u7b49\u4fe1\u606f\uff0c\u518d\u7528\u4e00\u5f20\u7f51\u4e0a\u7684\u56fe\u6765\u6e05\u6670\u770b\u4e0b sdshdr8 \u7684\u5b9e\u4f8b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/gitee.com\/xmtblog\/img\/raw\/master\/1582450632179.jpg\" alt=\"\" \/><figcaption>Redis sds \u5b9e\u4f8b<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 sds.h\/sds.c \u6e90\u7801\u4e2d\u53ef\u6e05\u695a\u5730\u770b\u5230sds\u5b8c\u6574\u7684\u5b9e\u73b0\u7ec6\u8282\uff0c\u672c\u6587\u5c31\u4e0d\u5c55\u5f00\u4e86\u8981\u4e0d\u7136\u7bc7\u5e45\u5c31\u8fc7\u957f\u4e86\uff0c\u5feb\u901f\u8fdb\u5165\u4e3b\u9898\u8bf4\u4e0b sds \u7684\u4f18\u52bf\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">null<li>O(1) \u83b7\u53d6\u957f\u5ea6: C \u5b57\u7b26\u4e32\u9700\u8981\u904d\u5386\u800c sds \u4e2d\u6709 len \u53ef\u4ee5\u76f4\u63a5\u83b7\u5f97\uff1b<\/li><li>\u9632\u6b62\u7f13\u51b2\u533a\u6ea2\u51fa bufferoverflow: \u5f53 sds \u9700\u8981\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u4fee\u6539\u65f6\uff0c\u9996\u5148\u501f\u52a9\u4e8e len \u548c alloc \u68c0\u67e5\u7a7a\u95f4\u662f\u5426\u6ee1\u8db3\u4fee\u6539\u6240\u9700\u7684\u8981\u6c42\uff0c\u5982\u679c\u7a7a\u95f4\u4e0d\u591f\u7684\u8bdd\uff0cSDS \u4f1a\u81ea\u52a8\u6269\u5c55\u7a7a\u95f4\uff0c\u907f\u514d\u4e86\u50cf C \u5b57\u7b26\u4e32\u64cd\u4f5c\u4e2d\u7684\u8986\u76d6\u60c5\u51b5\uff1b<\/li><li>\u6709\u6548\u964d\u4f4e\u5185\u5b58\u5206\u914d\u6b21\u6570\uff1aC \u5b57\u7b26\u4e32\u5728\u6d89\u53ca\u589e\u52a0\u6216\u8005\u6e05\u9664\u64cd\u4f5c\u65f6\u4f1a\u6539\u53d8\u5e95\u5c42\u6570\u7ec4\u7684\u5927\u5c0f\u9020\u6210\u91cd\u65b0\u5206\u914d\u3001sds \u4f7f\u7528\u4e86\u7a7a\u95f4\u9884\u5206\u914d\u548c\u60f0\u6027\u7a7a\u95f4\u91ca\u653e\u673a\u5236\uff0c\u8bf4\u767d\u4e86\u5c31\u662f\u6bcf\u6b21\u5728\u6269\u5c55\u65f6\u662f\u6210\u500d\u7684\u591a\u5206\u914d\u7684\uff0c\u5728\u7f29\u5bb9\u662f\u4e5f\u662f\u5148\u7559\u7740\u5e76\u4e0d\u6b63\u5f0f\u5f52\u8fd8\u7ed9 OS\uff0c\u8fd9\u4e24\u4e2a\u673a\u5236\u4e5f\u662f\u6bd4\u8f83\u597d\u7406\u89e3\u7684\uff1b<\/li><li>\u4e8c\u8fdb\u5236\u5b89\u5168\uff1aC \u8bed\u8a00\u5b57\u7b26\u4e32\u53ea\u80fd\u4fdd\u5b58 ascii \u7801\uff0c\u5bf9\u4e8e\u56fe\u7247\u3001\u97f3\u9891\u7b49\u4fe1\u606f\u65e0\u6cd5\u4fdd\u5b58\uff0csds \u662f\u4e8c\u8fdb\u5236\u5b89\u5168\u7684\uff0c\u5199\u5165\u4ec0\u4e48\u8bfb\u53d6\u5c31\u662f\u4ec0\u4e48\uff0c\u4e0d\u505a\u4efb\u4f55\u8fc7\u6ee4\u548c\u9650\u5236\uff1b<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8001\u89c4\u77e9\u4e0a\u4e00\u5f20\u9ec4\u5065\u5b8f\u5927\u795e\u603b\u7ed3\u597d\u7684\u56fe\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/gitee.com\/xmtblog\/img\/raw\/master\/1582450662069.jpg\" alt=\"\" \/><figcaption>Redis \u5b57\u7b26\u4e32\u4e0e C \u5b57\u7b26\u4e32\u7684\u533a\u522b<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u591f\u627e\u5230\u8bda\u5fc3\u5982\u610f\u7684\u597d\u5de5\u4f5c\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=4814\">\u9762\u8bd5\u9898\uff1aRedis\u7684SDS\u548cC\u4e2d\u5b57\u7b26\u4e32\u76f8\u6bd4\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u4f17\u6240\u5468\u77e5\uff0cRedis \u662f\u73b0\u5728\u6700\u706b\u7684 nosql \u7cfb\u7edf\u3002\u9762\u8bd5\u5fc5\u5907\uff0c\u9762\u8bd5\u5fc5\u4f1a\u3002\u4eca\u5e74\u60c5\u51b5\u7279\u522b\u7279\u6b8a\uff0c\u56f0\u96be\u91cd\u91cd\uff0c\u524d\u6bb5\u65f6\u95f4 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[672,1876,1878,1879,1877],"class_list":["post-4814","post","type-post","status-publish","format-standard","hentry","category-java","tag-redis-cluster","tag-redis-hgetall-","tag-redis-hgetall"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4814","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4814"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4814\/revisions"}],"predecessor-version":[{"id":4815,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4814\/revisions\/4815"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}