Skip to content

Commit a99ae03

Browse files
committed
refactor size array and rebase
Signed-off-by: hwware <wen.hui.ware@gmail.com>
1 parent 2ca20e6 commit a99ae03

File tree

7 files changed

+206
-205
lines changed

7 files changed

+206
-205
lines changed

src/server.c

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,211 @@ void increaseDataTypeArrayCurrentValue(keysizeInfo *keysize_array, int low, int
944944
}
945945
}
946946

947+
void scaleHashKeySizeArray(client *c, long value) {
948+
int length = c->db->hashes_array_length;
949+
int high_bound = c->db->hashes_array[length - 1].element_size;
950+
int base = high_bound;
951+
int count = 0;
952+
while (high_bound < value) {
953+
count++;
954+
high_bound = high_bound * 2;
955+
}
956+
keysizeInfo *new_array = zmalloc(sizeof(keysizeInfo) * (count + length));
957+
for (int i = 0; i < length; i++) {
958+
new_array[i].element_size = c->db->hashes_array[i].element_size;
959+
new_array[i].num = c->db->hashes_array[i].num;
960+
}
961+
for (int i = length; i < (count + length); i++) {
962+
base *= 2;
963+
new_array[i].element_size = base;
964+
new_array[i].num = 0;
965+
}
966+
keysizeInfo *old_array = c->db->hashes_array;
967+
zfree(old_array);
968+
c->db->hashes_array = new_array;
969+
c->db->hashes_array_length = count + length;
970+
}
971+
972+
void updateHashKeySizeArray(client *c, long previous, long curr) {
973+
int low = 0;
974+
int high = c->db->hashes_array_length - 1;
975+
if (curr > c->db->hashes_array[high].element_size) {
976+
scaleHashKeySizeArray(c, curr);
977+
}
978+
979+
high = c->db->hashes_array_length - 1;
980+
if (previous != 0) {
981+
decreaseDataTypeArrayPreviousValue(c->db->hashes_array, low, high, previous);
982+
}
983+
if (curr != 0) {
984+
increaseDataTypeArrayCurrentValue(c->db->hashes_array, low, high, curr);
985+
}
986+
}
987+
988+
void scaleListKeySizeArray(client *c, long value) {
989+
int length = c->db->lists_array_length;
990+
int high_bound = c->db->lists_array[length - 1].element_size;
991+
int base = high_bound;
992+
int count = 0;
993+
while (high_bound < value) {
994+
count++;
995+
high_bound = high_bound * 2;
996+
}
997+
keysizeInfo *new_array = zmalloc(sizeof(keysizeInfo) * (count + length));
998+
for (int i = 0; i < length; i++) {
999+
new_array[i].element_size = c->db->lists_array[i].element_size;
1000+
new_array[i].num = c->db->lists_array[i].num;
1001+
}
1002+
for (int i = length; i < (count + length); i++) {
1003+
base *= 2;
1004+
new_array[i].element_size = base;
1005+
new_array[i].num = 0;
1006+
}
1007+
keysizeInfo *old_array = c->db->lists_array;
1008+
zfree(old_array);
1009+
c->db->lists_array = new_array;
1010+
c->db->lists_array_length = count + length;
1011+
}
1012+
1013+
void updateListKeySizeArray(client *c, long previous, long curr) {
1014+
int low = 0;
1015+
int high = c->db->lists_array_length - 1;
1016+
if (curr > c->db->lists_array[high].element_size) {
1017+
scaleListKeySizeArray(c, curr);
1018+
}
1019+
1020+
high = c->db->lists_array_length - 1;
1021+
if (previous != 0) {
1022+
decreaseDataTypeArrayPreviousValue(c->db->lists_array, low, high, previous);
1023+
}
1024+
if (curr != 0) {
1025+
increaseDataTypeArrayCurrentValue(c->db->lists_array, low, high, curr);
1026+
}
1027+
}
1028+
1029+
void scaleSetKeySizeArray(client *c, long value) {
1030+
int length = c->db->sets_array_length;
1031+
int high_bound = c->db->sets_array[length - 1].element_size;
1032+
int base = high_bound;
1033+
int count = 0;
1034+
while (high_bound < value) {
1035+
count++;
1036+
high_bound = high_bound * 2;
1037+
}
1038+
keysizeInfo *new_array = zmalloc(sizeof(keysizeInfo) * (count + length));
1039+
for (int i = 0; i < length; i++) {
1040+
new_array[i].element_size = c->db->sets_array[i].element_size;
1041+
new_array[i].num = c->db->sets_array[i].num;
1042+
}
1043+
for (int i = length; i < (count + length); i++) {
1044+
base *= 2;
1045+
new_array[i].element_size = base;
1046+
new_array[i].num = 0;
1047+
}
1048+
keysizeInfo *old_array = c->db->sets_array;
1049+
zfree(old_array);
1050+
c->db->sets_array = new_array;
1051+
c->db->sets_array_length = count + length;
1052+
}
1053+
1054+
void updateSetKeySizeArray(client *c, long previous, long curr) {
1055+
int low = 0;
1056+
int high = c->db->sets_array_length - 1;
1057+
if (curr > c->db->sets_array[high].element_size) {
1058+
scaleSetKeySizeArray(c, curr);
1059+
}
1060+
1061+
high = c->db->sets_array_length - 1;
1062+
if (previous != 0) {
1063+
decreaseDataTypeArrayPreviousValue(c->db->sets_array, low, high, previous);
1064+
}
1065+
if (curr != 0) {
1066+
increaseDataTypeArrayCurrentValue(c->db->sets_array, low, high, curr);
1067+
}
1068+
}
1069+
1070+
void scaleStringKeySizeArray(client *c, long value) {
1071+
int length = c->db->strings_array_length;
1072+
int high_bound = c->db->strings_array[length - 1].element_size;
1073+
int base = high_bound;
1074+
int count = 0;
1075+
while (high_bound < value) {
1076+
count++;
1077+
high_bound = high_bound * 2;
1078+
}
1079+
keysizeInfo *new_array = zmalloc(sizeof(keysizeInfo) * (count + length));
1080+
for (int i = 0; i < length; i++) {
1081+
new_array[i].element_size = c->db->strings_array[i].element_size;
1082+
new_array[i].num = c->db->strings_array[i].num;
1083+
}
1084+
for (int i = length; i < (count + length); i++) {
1085+
base *= 2;
1086+
new_array[i].element_size = base;
1087+
new_array[i].num = 0;
1088+
}
1089+
keysizeInfo *old_array = c->db->strings_array;
1090+
zfree(old_array);
1091+
c->db->strings_array = new_array;
1092+
c->db->strings_array_length = count + length;
1093+
}
1094+
1095+
void updateStringKeySizeArray(client *c, long previous, long curr) {
1096+
int low = 0;
1097+
int high = c->db->strings_array_length - 1;
1098+
if (curr > c->db->strings_array[high].element_size) {
1099+
scaleStringKeySizeArray(c, curr);
1100+
}
1101+
1102+
high = c->db->strings_array_length - 1;
1103+
if (previous != 0) {
1104+
decreaseDataTypeArrayPreviousValue(c->db->strings_array, low, high, previous);
1105+
}
1106+
if (curr != 0) {
1107+
increaseDataTypeArrayCurrentValue(c->db->strings_array, low, high, curr);
1108+
}
1109+
}
1110+
1111+
void scaleZsetKeySizeArray(client *c, long value) {
1112+
int length = c->db->zsets_array_length;
1113+
int high_bound = c->db->zsets_array[length - 1].element_size;
1114+
int base = high_bound;
1115+
int count = 0;
1116+
while (high_bound < value) {
1117+
count++;
1118+
high_bound = high_bound * 2;
1119+
}
1120+
keysizeInfo *new_array = zmalloc(sizeof(keysizeInfo) * (count + length));
1121+
for (int i = 0; i < length; i++) {
1122+
new_array[i].element_size = c->db->zsets_array[i].element_size;
1123+
new_array[i].num = c->db->zsets_array[i].num;
1124+
}
1125+
for (int i = length; i < (count + length); i++) {
1126+
base *= 2;
1127+
new_array[i].element_size = base;
1128+
new_array[i].num = 0;
1129+
}
1130+
keysizeInfo *old_array = c->db->zsets_array;
1131+
zfree(old_array);
1132+
c->db->zsets_array = new_array;
1133+
c->db->zsets_array_length = count + length;
1134+
}
1135+
1136+
void updateZsetKeySizeArray(client *c, long previous, long curr) {
1137+
int low = 0;
1138+
int high = c->db->zsets_array_length - 1;
1139+
if (curr > c->db->zsets_array[high].element_size) {
1140+
scaleZsetKeySizeArray(c, curr);
1141+
}
1142+
1143+
high = c->db->lists_array_length - 1;
1144+
if (previous != 0) {
1145+
decreaseDataTypeArrayPreviousValue(c->db->zsets_array, low, high, previous);
1146+
}
1147+
if (curr != 0) {
1148+
increaseDataTypeArrayCurrentValue(c->db->zsets_array, low, high, curr);
1149+
}
1150+
}
1151+
9471152
/* Return the mean of all the samples. */
9481153
long long getInstantaneousMetric(int metric) {
9491154
int j;

src/server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3258,6 +3258,7 @@ void updateHashKeySizeArray(client *c, long previous, long curr);
32583258
void updateStringKeySizeArray(client *c, long previous, long curr);
32593259
void updateListKeySizeArray(client *c, long previous, long curr);
32603260
void updateZsetKeySizeArray(client *c, long previous, long curr);
3261+
void updateSetKeySizeArray(client *c, long previous, long curr);
32613262

32623263
#define RESTART_SERVER_NONE 0
32633264
#define RESTART_SERVER_GRACEFULLY (1 << 0) /* Do proper shutdown. */

src/t_hash.c

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -788,47 +788,6 @@ static void hashTypeRandomElement(robj *hashobj, unsigned long hashsize, listpac
788788
}
789789
}
790790

791-
void scaleHashKeySizeArray(client *c, long value) {
792-
int length = c->db->hashes_array_length;
793-
int high_bound = c->db->hashes_array[length - 1].element_size;
794-
int base = high_bound;
795-
int count = 0;
796-
while (high_bound < value) {
797-
count++;
798-
high_bound = high_bound * 2;
799-
}
800-
keysizeInfo *new_array = zmalloc(sizeof(keysizeInfo) * (count + length));
801-
for (int i = 0; i < length; i++) {
802-
new_array[i].element_size = c->db->hashes_array[i].element_size;
803-
new_array[i].num = c->db->hashes_array[i].num;
804-
}
805-
for (int i = length; i < (count + length); i++) {
806-
base *= 2;
807-
new_array[i].element_size = base;
808-
new_array[i].num = 0;
809-
}
810-
keysizeInfo *old_array = c->db->hashes_array;
811-
zfree(old_array);
812-
c->db->hashes_array = new_array;
813-
c->db->hashes_array_length = count + length;
814-
}
815-
816-
void updateHashKeySizeArray(client *c, long previous, long curr) {
817-
int low = 0;
818-
int high = c->db->hashes_array_length - 1;
819-
if (curr > c->db->hashes_array[high].element_size) {
820-
scaleHashKeySizeArray(c, curr);
821-
}
822-
823-
high = c->db->hashes_array_length - 1;
824-
if (previous != 0) {
825-
decreaseDataTypeArrayPreviousValue(c->db->hashes_array, low, high, previous);
826-
}
827-
if (curr != 0) {
828-
increaseDataTypeArrayCurrentValue(c->db->hashes_array, low, high, curr);
829-
}
830-
}
831-
832791
/*-----------------------------------------------------------------------------
833792
* Hash type commands
834793
*----------------------------------------------------------------------------*/

src/t_list.c

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -453,47 +453,6 @@ void listTypeDelRange(robj *subject, long start, long count) {
453453
}
454454
}
455455

456-
void scaleListKeySizeArray(client *c, long value) {
457-
int length = c->db->lists_array_length;
458-
int high_bound = c->db->lists_array[length - 1].element_size;
459-
int base = high_bound;
460-
int count = 0;
461-
while (high_bound < value) {
462-
count++;
463-
high_bound = high_bound * 2;
464-
}
465-
keysizeInfo *new_array = zmalloc(sizeof(keysizeInfo) * (count + length));
466-
for (int i = 0; i < length; i++) {
467-
new_array[i].element_size = c->db->lists_array[i].element_size;
468-
new_array[i].num = c->db->lists_array[i].num;
469-
}
470-
for (int i = length; i < (count + length); i++) {
471-
base *= 2;
472-
new_array[i].element_size = base;
473-
new_array[i].num = 0;
474-
}
475-
keysizeInfo *old_array = c->db->lists_array;
476-
zfree(old_array);
477-
c->db->lists_array = new_array;
478-
c->db->lists_array_length = count + length;
479-
}
480-
481-
void updateListKeySizeArray(client *c, long previous, long curr) {
482-
int low = 0;
483-
int high = c->db->lists_array_length - 1;
484-
if (curr > c->db->lists_array[high].element_size) {
485-
scaleListKeySizeArray(c, curr);
486-
}
487-
488-
high = c->db->lists_array_length - 1;
489-
if (previous != 0) {
490-
decreaseDataTypeArrayPreviousValue(c->db->lists_array, low, high, previous);
491-
}
492-
if (curr != 0) {
493-
increaseDataTypeArrayCurrentValue(c->db->lists_array, low, high, curr);
494-
}
495-
}
496-
497456
/*-----------------------------------------------------------------------------
498457
* List Commands
499458
*----------------------------------------------------------------------------*/

src/t_set.c

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -36,47 +36,6 @@
3636
#include "hashtable.h"
3737
#include "intset.h" /* Compact integer set structure */
3838

39-
void scaleSetKeySizeArray(client *c, long value) {
40-
int length = c->db->sets_array_length;
41-
int high_bound = c->db->sets_array[length - 1].element_size;
42-
int base = high_bound;
43-
int count = 0;
44-
while (high_bound < value) {
45-
count++;
46-
high_bound = high_bound * 2;
47-
}
48-
keysizeInfo *new_array = zmalloc(sizeof(keysizeInfo) * (count + length));
49-
for (int i = 0; i < length; i++) {
50-
new_array[i].element_size = c->db->sets_array[i].element_size;
51-
new_array[i].num = c->db->sets_array[i].num;
52-
}
53-
for (int i = length; i < (count + length); i++) {
54-
base *= 2;
55-
new_array[i].element_size = base;
56-
new_array[i].num = 0;
57-
}
58-
keysizeInfo *old_array = c->db->sets_array;
59-
zfree(old_array);
60-
c->db->sets_array = new_array;
61-
c->db->sets_array_length = count + length;
62-
}
63-
64-
void updateSetKeySizeArray(client *c, long previous, long curr) {
65-
int low = 0;
66-
int high = c->db->sets_array_length - 1;
67-
if (curr > c->db->sets_array[high].element_size) {
68-
scaleSetKeySizeArray(c, curr);
69-
}
70-
71-
high = c->db->sets_array_length - 1;
72-
if (previous != 0) {
73-
decreaseDataTypeArrayPreviousValue(c->db->sets_array, low, high, previous);
74-
}
75-
if (curr != 0) {
76-
increaseDataTypeArrayCurrentValue(c->db->sets_array, low, high, curr);
77-
}
78-
}
79-
8039
/*-----------------------------------------------------------------------------
8140
* Set Commands
8241
*----------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)