Browse Source

htable: internal function to update item expire along with the value

Daniel-Constantin Mierla 7 years ago
parent
commit
7a02461169
2 changed files with 55 additions and 16 deletions
  1. 52 16
      src/modules/htable/ht_api.c
  2. 3 0
      src/modules/htable/ht_api.h

+ 52 - 16
src/modules/htable/ht_api.c

@@ -455,7 +455,8 @@ int ht_destroy(void)
 }
 
 
-int ht_set_cell(ht_t *ht, str *name, int type, int_str *val, int mode)
+int ht_set_cell_ex(ht_t *ht, str *name, int type, int_str *val, int mode,
+		int exv)
 {
 	unsigned int idx;
 	unsigned int hid;
@@ -497,8 +498,13 @@ int ht_set_cell(ht_t *ht, str *name, int type, int_str *val, int mode)
 						memcpy(it->value.s.s, val->s.s, val->s.len);
 						it->value.s.s[it->value.s.len] = '\0';
 
-						if(ht->updateexpire)
-							it->expire = now + ht->htexpire;
+						if(exv<=0) {
+							if(ht->updateexpire) {
+								it->expire = now + ht->htexpire;
+							}
+						} else {
+							it->expire = now + exv;
+						}
 					} else {
 						/* new */
 						cell = ht_cell_new(name, type, val, hid);
@@ -510,10 +516,15 @@ int ht_set_cell(ht_t *ht, str *name, int type, int_str *val, int mode)
 						}
 						cell->next = it->next;
 						cell->prev = it->prev;
-						if(ht->updateexpire)
-							cell->expire = now + ht->htexpire;
-						else
-							cell->expire = it->expire;
+						if(exv<=0) {
+							if(ht->updateexpire) {
+								cell->expire = now + ht->htexpire;
+							} else {
+								cell->expire = it->expire;
+							}
+						} else {
+							it->expire = now + exv;
+						}
 						if(it->prev)
 							it->prev->next = cell;
 						else
@@ -526,8 +537,13 @@ int ht_set_cell(ht_t *ht, str *name, int type, int_str *val, int mode)
 					it->flags &= ~AVP_VAL_STR;
 					it->value.n = val->n;
 
-					if(ht->updateexpire)
-						it->expire = now + ht->htexpire;
+					if(exv<=0) {
+						if(ht->updateexpire) {
+							it->expire = now + ht->htexpire;
+						}
+					} else {
+						it->expire = now + exv;
+					}
 				}
 				if(mode) ht_slot_unlock(ht, idx);
 				return 0;
@@ -542,10 +558,16 @@ int ht_set_cell(ht_t *ht, str *name, int type, int_str *val, int mode)
 						if(mode) ht_slot_unlock(ht, idx);
 						return -1;
 					}
-					if(ht->updateexpire)
-						cell->expire = now + ht->htexpire;
-					else
-						cell->expire = it->expire;
+					if(exv<=0) {
+						if(ht->updateexpire) {
+							cell->expire = now + ht->htexpire;
+						} else {
+							cell->expire = it->expire;
+						}
+					} else {
+						it->expire = now + exv;
+					}
+
 					cell->next = it->next;
 					cell->prev = it->prev;
 					if(it->prev)
@@ -558,8 +580,13 @@ int ht_set_cell(ht_t *ht, str *name, int type, int_str *val, int mode)
 				} else {
 					it->value.n = val->n;
 
-					if(ht->updateexpire)
-						it->expire = now + ht->htexpire;
+					if(exv<=0) {
+						if(ht->updateexpire) {
+							it->expire = now + ht->htexpire;
+						}
+					} else {
+						it->expire = now + exv;
+					}
 				}
 				if(mode) ht_slot_unlock(ht, idx);
 				return 0;
@@ -576,7 +603,11 @@ int ht_set_cell(ht_t *ht, str *name, int type, int_str *val, int mode)
 		if(mode) ht_slot_unlock(ht, idx);
 		return -1;
 	}
-	cell->expire = now + ht->htexpire;
+	if(exv<=0) {
+		cell->expire = now + ht->htexpire;
+	} else {
+		it->expire = now + exv;
+	}
 	if(prev==NULL)
 	{
 		if(ht->entries[idx].first!=NULL)
@@ -597,6 +628,11 @@ int ht_set_cell(ht_t *ht, str *name, int type, int_str *val, int mode)
 	return 0;
 }
 
+int ht_set_cell(ht_t *ht, str *name, int type, int_str *val, int mode)
+{
+	return ht_set_cell_ex(ht, name, type, val, mode, 0);
+}
+
 int ht_del_cell(ht_t *ht, str *name)
 {
 	unsigned int idx;

+ 3 - 0
src/modules/htable/ht_api.h

@@ -109,6 +109,9 @@ void ht_handle_expired_record(ht_t *ht, ht_cell_t *cell);
 int ht_set_cell_expire(ht_t *ht, str *name, int type, int_str *val);
 int ht_get_cell_expire(ht_t *ht, str *name, unsigned int *val);
 
+int ht_set_cell_ex(ht_t *ht, str *name, int type, int_str *val, int mode,
+		int exv);
+
 int ht_rm_cell_re(str *sre, ht_t *ht, int mode);
 int ht_count_cells_re(str *sre, ht_t *ht, int mode);
 ht_t *ht_get_root(void);