关键词搜索

源码搜索 ×
×

漫话Redis源码之七

发布2021-11-21浏览541次

详情内容

getrangeCommand这个函数没看的太明白,但大致知道意思:

  1. void getrangeCommand(client *c) {
  2. robj *o;
  3. long long start, end;
  4. char *str, llbuf[32];
  5. size_t strlen;
  6. if (getLongLongFromObjectOrReply(c,c->argv[2],&start,NULL) != C_OK)
  7. return;
  8. if (getLongLongFromObjectOrReply(c,c->argv[3],&end,NULL) != C_OK)
  9. return;
  10. if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptybulk)) == NULL ||
  11. checkType(c,o,OBJ_STRING)) return;
  12. if (o->encoding == OBJ_ENCODING_INT) {
  13. str = llbuf;
  14. strlen = ll2string(llbuf,sizeof(llbuf),(long)o->ptr);
  15. } else {
  16. str = o->ptr;
  17. strlen = sdslen(str);
  18. }
  19. /* Convert negative indexes */
  20. if (start < 0 && end < 0 && start > end) {
  21. addReply(c,shared.emptybulk);
  22. return;
  23. }
  24. if (start < 0) start = strlen+start;
  25. if (end < 0) end = strlen+end;
  26. if (start < 0) start = 0;
  27. if (end < 0) end = 0;
  28. if ((unsigned long long)end >= strlen) end = strlen-1;
  29. /* Precondition: end >= 0 && end < strlen, so the only condition where
  30. * nothing can be returned is: start > end. */
  31. if (start > end || strlen == 0) {
  32. addReply(c,shared.emptybulk);
  33. } else {
  34. addReplyBulkCBuffer(c,(char*)str+start,end-start+1);
  35. }
  36. }
  37. void mgetCommand(client *c) {
  38. int j;
  39. addReplyArrayLen(c,c->argc-1);
  40. for (j = 1; j < c->argc; j++) {
  41. robj *o = lookupKeyRead(c->db,c->argv[j]);
  42. if (o == NULL) {
  43. addReplyNull(c);
  44. } else {
  45. if (o->type != OBJ_STRING) {
  46. addReplyNull(c);
  47. } else {
  48. addReplyBulk(c,o);
  49. }
  50. }
  51. }
  52. }
  53. void msetGenericCommand(client *c, int nx) {
  54. int j;
  55. if ((c->argc % 2) == 0) {
  56. addReplyError(c,"wrong number of arguments for MSET");
  57. return;
  58. }
  59. /* Handle the NX flag. The MSETNX semantic is to return zero and don't
  60. * set anything if at least one key already exists. */
  61. if (nx) {
  62. for (j = 1; j < c->argc; j += 2) {
  63. if (lookupKeyWrite(c->db,c->argv[j]) != NULL) {
  64. addReply(c, shared.czero);
  65. return;
  66. }
  67. }
  68. }
  69. for (j = 1; j < c->argc; j += 2) {
  70. c->argv[j+1] = tryObjectEncoding(c->argv[j+1]);
  71. setKey(c,c->db,c->argv[j],c->argv[j+1]);
  72. notifyKeyspaceEvent(NOTIFY_STRING,"set",c->argv[j],c->db->id);
  73. }
  74. server.dirty += (c->argc-1)/2;
  75. addReply(c, nx ? shared.cone : shared.ok);
  76. }
  77. void msetCommand(client *c) {
  78. msetGenericCommand(c,0);
  79. }
  80. void msetnxCommand(client *c) {
  81. msetGenericCommand(c,1);
  82. }
  83. void incrDecrCommand(client *c, long long incr) {
  84. long long value, oldvalue;
  85. robj *o, *new;
  86. o = lookupKeyWrite(c->db,c->argv[1]);
  87. if (checkType(c,o,OBJ_STRING)) return;
  88. if (getLongLongFromObjectOrReply(c,o,&value,NULL) != C_OK) return;
  89. oldvalue = value;
  90. if ((incr < 0 && oldvalue < 0 && incr < (LLONG_MIN-oldvalue)) ||
  91. (incr > 0 && oldvalue > 0 && incr > (LLONG_MAX-oldvalue))) {
  92. addReplyError(c,"increment or decrement would overflow");
  93. return;
  94. }
  95. value += incr;
  96. if (o && o->refcount == 1 && o->encoding == OBJ_ENCODING_INT &&
  97. (value < 0 || value >= OBJ_SHARED_INTEGERS) &&
  98. value >= LONG_MIN && value <= LONG_MAX)
  99. {
  100. new = o;
  101. o->ptr = (void*)((long)value);
  102. } else {
  103. new = createStringObjectFromLongLongForValue(value);
  104. if (o) {
  105. dbOverwrite(c->db,c->argv[1],new);
  106. } else {
  107. dbAdd(c->db,c->argv[1],new);
  108. }
  109. }
  110. signalModifiedKey(c,c->db,c->argv[1]);
  111. notifyKeyspaceEvent(NOTIFY_STRING,"incrby",c->argv[1],c->db->id);
  112. server.dirty++;
  113. addReply(c,shared.colon);
  114. addReply(c,new);
  115. addReply(c,shared.crlf);
  116. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载