关键词搜索

源码搜索 ×
×

漫话Redis源码之十三

发布2021-11-28浏览479次

详情内容

sds是一个基础的数据的结构,这两个函数没有看得很透彻,先记录学习一下,后面调到了,再具体看看:

  1. /* Render part of a sequence, so that render_sequence() call call this function
  2. * with different parts in order to create the full output without overflowing
  3. * the current terminal columns. */
  4. sds sparklineRenderRange(sds output, struct sequence *seq, int rows, int offset, int len, int flags) {
  5. int j;
  6. double relmax = seq->max - seq->min;
  7. int steps = charset_len*rows;
  8. int row = 0;
  9. char *chars = zmalloc(len);
  10. int loop = 1;
  11. int opt_fill = flags & SPARKLINE_FILL;
  12. int opt_log = flags & SPARKLINE_LOG_SCALE;
  13. if (opt_log) {
  14. relmax = log(relmax+1);
  15. } else if (relmax == 0) {
  16. relmax = 1;
  17. }
  18. while(loop) {
  19. loop = 0;
  20. memset(chars,' ',len);
  21. for (j = 0; j < len; j++) {
  22. struct sample *s = &seq->samples[j+offset];
  23. double relval = s->value - seq->min;
  24. int step;
  25. if (opt_log) relval = log(relval+1);
  26. step = (int) (relval*steps)/relmax;
  27. if (step < 0) step = 0;
  28. if (step >= steps) step = steps-1;
  29. if (row < rows) {
  30. /* Print the character needed to create the sparkline */
  31. int charidx = step-((rows-row-1)*charset_len);
  32. loop = 1;
  33. if (charidx >= 0 && charidx < charset_len) {
  34. chars[j] = opt_fill ? charset_fill[charidx] :
  35. charset[charidx];
  36. } else if(opt_fill && charidx >= charset_len) {
  37. chars[j] = '|';
  38. }
  39. } else {
  40. /* Labels spacing */
  41. if (seq->labels && row-rows < label_margin_top) {
  42. loop = 1;
  43. break;
  44. }
  45. /* Print the label if needed. */
  46. if (s->label) {
  47. int label_len = strlen(s->label);
  48. int label_char = row - rows - label_margin_top;
  49. if (label_len > label_char) {
  50. loop = 1;
  51. chars[j] = s->label[label_char];
  52. }
  53. }
  54. }
  55. }
  56. if (loop) {
  57. row++;
  58. output = sdscatlen(output,chars,len);
  59. output = sdscatlen(output,"\n",1);
  60. }
  61. }
  62. zfree(chars);
  63. return output;
  64. }
  65. /* Turn a sequence into its ASCII representation */
  66. sds sparklineRender(sds output, struct sequence *seq, int columns, int rows, int flags) {
  67. int j;
  68. for (j = 0; j < seq->length; j += columns) {
  69. int sublen = (seq->length-j) < columns ? (seq->length-j) : columns;
  70. if (j != 0) output = sdscatlen(output,"\n",1);
  71. output = sparklineRenderRange(output, seq, rows, j, sublen, flags);
  72. }
  73. return output;
  74. }

相关技术文章

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

提示信息

×

选择支付方式

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