Mercurial > urweb
comparison src/c/urweb.c @ 2240:88cc0f44c940
Rename C functions and remove functors nested inside modules.
author | Ziv Scully <ziv@mit.edu> |
---|---|
date | Sun, 19 Jul 2015 19:03:11 -0700 |
parents | 2f7ed04332a0 |
children | 2b1af5dc6dee |
comparison
equal
deleted
inserted
replaced
2239:f70a91f7810d | 2240:88cc0f44c940 |
---|---|
4498 } | 4498 } |
4499 | 4499 |
4500 | 4500 |
4501 // Sqlcache | 4501 // Sqlcache |
4502 | 4502 |
4503 void listDelete(CacheList *list, CacheEntry *entry) { | 4503 void uw_sqlcache_listDelete(uw_sqlcache_CacheList *list, uw_sqlcache_CacheEntry *entry) { |
4504 if (list->first == entry) { | 4504 if (list->first == entry) { |
4505 list->first = entry->next; | 4505 list->first = entry->next; |
4506 } | 4506 } |
4507 if (list->last == entry) { | 4507 if (list->last == entry) { |
4508 list->last = entry->prev; | 4508 list->last = entry->prev; |
4516 entry->prev = NULL; | 4516 entry->prev = NULL; |
4517 entry->next = NULL; | 4517 entry->next = NULL; |
4518 --(list->size); | 4518 --(list->size); |
4519 } | 4519 } |
4520 | 4520 |
4521 void listAdd(CacheList *list, CacheEntry *entry) { | 4521 void uw_sqlcache_listAdd(uw_sqlcache_CacheList *list, uw_sqlcache_CacheEntry *entry) { |
4522 if (list->last) { | 4522 if (list->last) { |
4523 list->last->next = entry; | 4523 list->last->next = entry; |
4524 entry->prev = list->last; | 4524 entry->prev = list->last; |
4525 list->last = entry; | 4525 list->last = entry; |
4526 } else { | 4526 } else { |
4528 list->last = entry; | 4528 list->last = entry; |
4529 } | 4529 } |
4530 ++(list->size); | 4530 ++(list->size); |
4531 } | 4531 } |
4532 | 4532 |
4533 void listBump(CacheList *list, CacheEntry *entry) { | 4533 void uw_sqlcache_listBump(uw_sqlcache_CacheList *list, uw_sqlcache_CacheEntry *entry) { |
4534 listDelete(list, entry); | 4534 uw_sqlcache_listDelete(list, entry); |
4535 listAdd(list, entry); | 4535 uw_sqlcache_listAdd(list, entry); |
4536 } | 4536 } |
4537 | 4537 |
4538 // TODO: deal with time properly. | 4538 // TODO: deal with time properly. |
4539 | 4539 |
4540 time_t getTimeNow() { | 4540 time_t uw_sqlcache_getTimeNow() { |
4541 return time(NULL); | 4541 return time(NULL); |
4542 } | 4542 } |
4543 | 4543 |
4544 time_t timeMax(time_t x, time_t y) { | 4544 time_t uw_sqlcache_timeMax(time_t x, time_t y) { |
4545 return difftime(x, y) > 0 ? x : y; | 4545 return difftime(x, y) > 0 ? x : y; |
4546 } | 4546 } |
4547 | 4547 |
4548 void freeCacheValue(CacheValue *value) { | 4548 void uw_sqlcache_freeuw_sqlcache_CacheValue(uw_sqlcache_CacheValue *value) { |
4549 if (value) { | 4549 if (value) { |
4550 free(value->result); | 4550 free(value->result); |
4551 free(value->output); | 4551 free(value->output); |
4552 free(value); | 4552 free(value); |
4553 } | 4553 } |
4554 } | 4554 } |
4555 | 4555 |
4556 void delete(Cache *cache, CacheEntry* entry) { | 4556 void uw_sqlcache_delete(uw_sqlcache_Cache *cache, uw_sqlcache_CacheEntry* entry) { |
4557 //listDelete(cache->lru, entry); | 4557 //uw_sqlcache_listUw_Sqlcache_Delete(cache->lru, entry); |
4558 HASH_DELETE(hh, cache->table, entry); | 4558 HASH_DELETE(hh, cache->table, entry); |
4559 freeCacheValue(entry->value); | 4559 uw_sqlcache_freeuw_sqlcache_CacheValue(entry->value); |
4560 free(entry->key); | 4560 free(entry->key); |
4561 free(entry); | 4561 free(entry); |
4562 } | 4562 } |
4563 | 4563 |
4564 CacheValue *checkHelper(Cache *cache, char **keys, int timeInvalid) { | 4564 uw_sqlcache_CacheValue *uw_sqlcache_checkHelper(uw_sqlcache_Cache *cache, char **keys, int timeInvalid) { |
4565 char *key = keys[cache->height]; | 4565 char *key = keys[cache->height]; |
4566 CacheEntry *entry; | 4566 uw_sqlcache_CacheEntry *entry; |
4567 HASH_FIND(hh, cache->table, key, strlen(key), entry); | 4567 HASH_FIND(hh, cache->table, key, strlen(key), entry); |
4568 timeInvalid = timeMax(timeInvalid, cache->timeInvalid); | 4568 timeInvalid = uw_sqlcache_timeMax(timeInvalid, cache->timeInvalid); |
4569 if (entry && difftime(entry->timeValid, timeInvalid) > 0) { | 4569 if (entry && difftime(entry->timeValid, timeInvalid) > 0) { |
4570 if (cache->height == 0) { | 4570 if (cache->height == 0) { |
4571 // At height 0, entry->value is the desired value. | 4571 // At height 0, entry->value is the desired value. |
4572 //listBump(cache->lru, entry); | 4572 //uw_sqlcache_listBump(cache->lru, entry); |
4573 return entry->value; | 4573 return entry->value; |
4574 } else { | 4574 } else { |
4575 // At height n+1, entry->value is a pointer to a cache at heignt n. | 4575 // At height n+1, entry->value is a pointer to a cache at heignt n. |
4576 return checkHelper(entry->value, keys, timeInvalid); | 4576 return uw_sqlcache_checkHelper(entry->value, keys, timeInvalid); |
4577 } | 4577 } |
4578 } else { | 4578 } else { |
4579 return NULL; | 4579 return NULL; |
4580 } | 4580 } |
4581 } | 4581 } |
4582 | 4582 |
4583 CacheValue *check(Cache *cache, char **keys) { | 4583 uw_sqlcache_CacheValue *uw_sqlcache_check(uw_sqlcache_Cache *cache, char **keys) { |
4584 return checkHelper(cache, keys, 0); | 4584 return uw_sqlcache_checkHelper(cache, keys, 0); |
4585 } | 4585 } |
4586 | 4586 |
4587 void storeHelper(Cache *cache, char **keys, CacheValue *value, int timeNow) { | 4587 void uw_sqlcache_storeHelper(uw_sqlcache_Cache *cache, char **keys, uw_sqlcache_CacheValue *value, int timeNow) { |
4588 CacheEntry *entry; | 4588 uw_sqlcache_CacheEntry *entry; |
4589 char *key = keys[cache->height]; | 4589 char *key = keys[cache->height]; |
4590 HASH_FIND(hh, cache->table, key, strlen(key), entry); | 4590 HASH_FIND(hh, cache->table, key, strlen(key), entry); |
4591 if (!entry) { | 4591 if (!entry) { |
4592 entry = malloc(sizeof(CacheEntry)); | 4592 entry = malloc(sizeof(uw_sqlcache_CacheEntry)); |
4593 entry->key = strdup(key); | 4593 entry->key = strdup(key); |
4594 entry->value = NULL; | 4594 entry->value = NULL; |
4595 HASH_ADD_KEYPTR(hh, cache->table, entry->key, strlen(entry->key), entry); | 4595 HASH_ADD_KEYPTR(hh, cache->table, entry->key, strlen(entry->key), entry); |
4596 } | 4596 } |
4597 entry->timeValid = timeNow; | 4597 entry->timeValid = timeNow; |
4598 if (cache->height == 0) { | 4598 if (cache->height == 0) { |
4599 //listAdd(cache->lru, entry); | 4599 //uw_sqlcache_listAdd(cache->lru, entry); |
4600 freeCacheValue(entry->value); | 4600 uw_sqlcache_freeuw_sqlcache_CacheValue(entry->value); |
4601 entry->value = value; | 4601 entry->value = value; |
4602 //if (cache->lru->size > MAX_SIZE) { | 4602 //if (cache->lru->size > MAX_SIZE) { |
4603 //delete(cache, cache->lru->first); | 4603 //uw_sqlcache_delete(cache, cache->lru->first); |
4604 // TODO: return flushed value. | 4604 // TODO: return flushed value. |
4605 //} | 4605 //} |
4606 } else { | 4606 } else { |
4607 if (!entry->value) { | 4607 if (!entry->value) { |
4608 Cache *newCache = malloc(sizeof(Cache)); | 4608 uw_sqlcache_Cache *newuw_sqlcache_Cache = malloc(sizeof(uw_sqlcache_Cache)); |
4609 newCache->table = NULL; | 4609 newuw_sqlcache_Cache->table = NULL; |
4610 newCache->timeInvalid = timeNow; | 4610 newuw_sqlcache_Cache->timeInvalid = timeNow; |
4611 newCache->lru = cache->lru; | 4611 newuw_sqlcache_Cache->lru = cache->lru; |
4612 newCache->height = cache->height - 1; | 4612 newuw_sqlcache_Cache->height = cache->height - 1; |
4613 entry->value = newCache; | 4613 entry->value = newuw_sqlcache_Cache; |
4614 } | 4614 } |
4615 storeHelper(entry->value, keys, value, timeNow); | 4615 uw_sqlcache_storeHelper(entry->value, keys, value, timeNow); |
4616 } | 4616 } |
4617 } | 4617 } |
4618 | 4618 |
4619 void store(Cache *cache, char **keys, CacheValue *value) { | 4619 void uw_sqlcache_store(uw_sqlcache_Cache *cache, char **keys, uw_sqlcache_CacheValue *value) { |
4620 storeHelper(cache, keys, value, getTimeNow()); | 4620 uw_sqlcache_storeHelper(cache, keys, value, uw_sqlcache_getTimeNow()); |
4621 } | 4621 } |
4622 | 4622 |
4623 void flushHelper(Cache *cache, char **keys, int timeNow) { | 4623 void uw_sqlcache_flushHelper(uw_sqlcache_Cache *cache, char **keys, int timeNow) { |
4624 CacheEntry *entry; | 4624 uw_sqlcache_CacheEntry *entry; |
4625 char *key = keys[cache->height]; | 4625 char *key = keys[cache->height]; |
4626 if (key) { | 4626 if (key) { |
4627 HASH_FIND(hh, cache->table, key, strlen(key), entry); | 4627 HASH_FIND(hh, cache->table, key, strlen(key), entry); |
4628 if (entry) { | 4628 if (entry) { |
4629 if (cache->height == 0) { | 4629 if (cache->height == 0) { |
4630 delete(cache, entry); | 4630 uw_sqlcache_delete(cache, entry); |
4631 } else { | 4631 } else { |
4632 flushHelper(entry->value, keys, timeNow); | 4632 uw_sqlcache_flushHelper(entry->value, keys, timeNow); |
4633 } | 4633 } |
4634 } | 4634 } |
4635 } else { | 4635 } else { |
4636 // Null key means invalidate the entire subtree. | 4636 // Null key means invalidate the entire subtree. |
4637 cache->timeInvalid = timeNow; | 4637 cache->timeInvalid = timeNow; |
4638 } | 4638 } |
4639 } | 4639 } |
4640 | 4640 |
4641 void flush(Cache *cache, char **keys) { | 4641 void uw_sqlcache_flush(uw_sqlcache_Cache *cache, char **keys) { |
4642 flushHelper(cache, keys, getTimeNow()); | 4642 uw_sqlcache_flushHelper(cache, keys, uw_sqlcache_getTimeNow()); |
4643 } | 4643 } |