关键词搜索

源码搜索 ×
×

漫话Redis源码之二十一

发布2021-12-05浏览517次

详情内容

这个时候关于log的,不影响核心逻辑的理解:

  1. /* Log a fixed message without printf-alike capabilities, in a way that is
  2. * safe to call from a signal handler.
  3. *
  4. * We actually use this only for signals that are not fatal from the point
  5. * of view of Redis. Signals that are going to kill the server anyway and
  6. * where we need printf-alike features are served by serverLog(). */
  7. void serverLogFromHandler(int level, const char *msg) {
  8. int fd;
  9. int log_to_stdout = server.logfile[0] == '\0';
  10. char buf[64];
  11. if ((level&0xff) < server.verbosity || (log_to_stdout && server.daemonize))
  12. return;
  13. fd = log_to_stdout ? STDOUT_FILENO :
  14. open(server.logfile, O_APPEND|O_CREAT|O_WRONLY, 0644);
  15. if (fd == -1) return;
  16. ll2string(buf,sizeof(buf),getpid());
  17. if (write(fd,buf,strlen(buf)) == -1) goto err;
  18. if (write(fd,":signal-handler (",17) == -1) goto err;
  19. ll2string(buf,sizeof(buf),time(NULL));
  20. if (write(fd,buf,strlen(buf)) == -1) goto err;
  21. if (write(fd,") ",2) == -1) goto err;
  22. if (write(fd,msg,strlen(msg)) == -1) goto err;
  23. if (write(fd,"\n",1) == -1) goto err;
  24. err:
  25. if (!log_to_stdout) close(fd);
  26. }
  27. /* Return the UNIX time in microseconds */
  28. long long ustime(void) {
  29. struct timeval tv;
  30. long long ust;
  31. gettimeofday(&tv, NULL);
  32. ust = ((long long)tv.tv_sec)*1000000;
  33. ust += tv.tv_usec;
  34. return ust;
  35. }
  36. /* Return the UNIX time in milliseconds */
  37. mstime_t mstime(void) {
  38. return ustime()/1000;
  39. }
  40. /* After an RDB dump or AOF rewrite we exit from children using _exit() instead of
  41. * exit(), because the latter may interact with the same file objects used by
  42. * the parent process. However if we are testing the coverage normal exit() is
  43. * used in order to obtain the right coverage information. */
  44. void exitFromChild(int retcode) {
  45. #ifdef COVERAGE_TEST
  46. exit(retcode);
  47. #else
  48. _exit(retcode);
  49. #endif
  50. }
  51. /*====================== Hash table type implementation ==================== */
  52. /* This is a hash table type that uses the SDS dynamic strings library as
  53. * keys and redis objects as values (objects can hold SDS strings,
  54. * lists, sets). */
  55. void dictVanillaFree(void *privdata, void *val)
  56. {
  57. DICT_NOTUSED(privdata);
  58. zfree(val);
  59. }
  60. void dictListDestructor(void *privdata, void *val)
  61. {
  62. DICT_NOTUSED(privdata);
  63. listRelease((list*)val);
  64. }
  65. int dictSdsKeyCompare(void *privdata, const void *key1,
  66. const void *key2)
  67. {
  68. int l1,l2;
  69. DICT_NOTUSED(privdata);
  70. l1 = sdslen((sds)key1);
  71. l2 = sdslen((sds)key2);
  72. if (l1 != l2) return 0;
  73. return memcmp(key1, key2, l1) == 0;
  74. }
  75. /* A case insensitive version used for the command lookup table and other
  76. * places where case insensitive non binary-safe comparison is needed. */
  77. int dictSdsKeyCaseCompare(void *privdata, const void *key1,
  78. const void *key2)
  79. {
  80. DICT_NOTUSED(privdata);
  81. return strcasecmp(key1, key2) == 0;
  82. }
  83. void dictObjectDestructor(void *privdata, void *val)
  84. {
  85. DICT_NOTUSED(privdata);
  86. if (val == NULL) return; /* Lazy freeing will set value to NULL. */
  87. decrRefCount(val);
  88. }
  89. void dictSdsDestructor(void *privdata, void *val)
  90. {
  91. DICT_NOTUSED(privdata);
  92. sdsfree(val);
  93. }
  94. int dictObjKeyCompare(void *privdata, const void *key1,
  95. const void *key2)
  96. {
  97. const robj *o1 = key1, *o2 = key2;
  98. return dictSdsKeyCompare(privdata,o1->ptr,o2->ptr);
  99. }
  100. uint64_t dictObjHash(const void *key) {
  101. const robj *o = key;
  102. return dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
  103. }
  104. uint64_t dictSdsHash(const void *key) {
  105. return dictGenHashFunction((unsigned char*)key, sdslen((char*)key));
  106. }
  107. uint64_t dictSdsCaseHash(const void *key) {
  108. return dictGenCaseHashFunction((unsigned char*)key, sdslen((char*)key));
  109. }
  110. int dictEncObjKeyCompare(void *privdata, const void *key1,
  111. const void *key2)
  112. {
  113. robj *o1 = (robj*) key1, *o2 = (robj*) key2;
  114. int cmp;
  115. if (o1->encoding == OBJ_ENCODING_INT &&
  116. o2->encoding == OBJ_ENCODING_INT)
  117. return o1->ptr == o2->ptr;
  118. /* Due to OBJ_STATIC_REFCOUNT, we avoid calling getDecodedObject() without
  119. * good reasons, because it would incrRefCount() the object, which
  120. * is invalid. So we check to make sure dictFind() works with static
  121. * objects as well. */
  122. if (o1->refcount != OBJ_STATIC_REFCOUNT) o1 = getDecodedObject(o1);
  123. if (o2->refcount != OBJ_STATIC_REFCOUNT) o2 = getDecodedObject(o2);
  124. cmp = dictSdsKeyCompare(privdata,o1->ptr,o2->ptr);
  125. if (o1->refcount != OBJ_STATIC_REFCOUNT) decrRefCount(o1);
  126. if (o2->refcount != OBJ_STATIC_REFCOUNT) decrRefCount(o2);
  127. return cmp;
  128. }

相关技术文章

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

提示信息

×

选择支付方式

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