Example #1
0
bool testCacheExample() {
  char* empty_string = "";
  char* hello = "Hello world!";
  char* helloAgain = "Hello world!";
  char* goodbye = "Goodbye!";
  char* goodDay = "Good day!";

  Cache cache = cacheCreate(256, freeString, copyString, compareStrings, getFirstLetter);
  ASSERT_TEST(cache != NULL);
  ASSERT_TEST(cacheIsIn(cache, hello) == false);
  ASSERT_TEST(cachePush(cache, hello) == CACHE_SUCCESS);
  ASSERT_TEST(cacheIsIn(cache, helloAgain) == true);
  ASSERT_TEST(cachePush(cache, hello) == CACHE_ITEM_ALREADY_EXISTS);

  ASSERT_TEST(cachePush(cache, goodbye) == CACHE_SUCCESS);
  ASSERT_TEST(cachePush(cache, goodDay) == CACHE_SUCCESS);
  ASSERT_TEST(cachePush(cache, empty_string) == CACHE_SUCCESS);

  ASSERT_TEST(cacheIsIn(cache, empty_string) == true);
  ASSERT_TEST(cacheIsIn(cache, goodbye) == true);
  
  char* temp_ptr;
  temp_ptr = cacheExtractElementByKey(cache, 0);
  ASSERT_TEST(strcmp(temp_ptr, "") == 0);
  ASSERT_TEST(cacheIsIn(cache, empty_string) == false);
  ASSERT_TEST(cacheFreeElement(cache, goodbye) == CACHE_SUCCESS);
  ASSERT_TEST(cacheIsIn(cache, goodbye) == false);

  ASSERT_TEST(cacheClear(cache) == CACHE_SUCCESS);
  ASSERT_TEST(setGetFirst(cacheGetFirst(cache)) == NULL);

  free(temp_ptr);
  cacheDestroy(cache);
  return true;
}
Example #2
0
static bool testCacheExtractElementByKey() {
	char * andykaufman = "andykaufman";
	char * agonist = "agonist";
	char * nightwish = "nightwish";

	Cache cache = cacheCreate(256, freeString, copyString, compareStrings, getFirstLetter);
	ASSERT_TEST(cache != NULL);

	ASSERT_TEST(cachePush(cache, nightwish) == CACHE_SUCCESS);
	ASSERT_TEST(cachePush(cache, nightwish) == CACHE_ITEM_ALREADY_EXISTS);
	ASSERT_TEST(cacheIsIn(cache, nightwish));
	ASSERT_TEST(cachePush(cache, andykaufman) == CACHE_SUCCESS);
	ASSERT_TEST(cachePush(cache, andykaufman) == CACHE_ITEM_ALREADY_EXISTS);
	ASSERT_TEST(cacheIsIn(cache, andykaufman));
	ASSERT_TEST(cachePush(cache, agonist) == CACHE_SUCCESS);
	ASSERT_TEST(cachePush(cache, agonist) == CACHE_ITEM_ALREADY_EXISTS);
	ASSERT_TEST(cacheIsIn(cache, agonist));

	const int SIZE = 3;
	char * elements[SIZE];
	int i = 0;
	CACHE_FOREACH(set, cache) {
		SET_FOREACH(char *, str, set) {
			elements[i++] = str;
		}
	}
Example #3
0
static bool testCacheFreeElement(void) {
	Cache cache = cacheCreate(255, freeString, copyString, compareStrings, getFirstLetter);

	char outOfRange[2] = { (char)255, '\0' };
	char *notInCache = "Rock";
	char *doubled = "Louna";
	char * elements[] = {
			"Ramones",
			"Lumen",
			"Louna",
			"Arch Enemy"
			"Linkin park",
	};
	const int ELEMENTS_SIZE = sizeof(elements) / sizeof(*elements);

	for (int i = 0; i < ELEMENTS_SIZE; ++i) {
		ASSERT_TEST(cachePush(cache, elements[i]) == CACHE_SUCCESS);
	}
	ASSERT_TEST(cachePush(cache, outOfRange) == CACHE_OUT_OF_RANGE);
	ASSERT_TEST(cachePush(cache, doubled) == CACHE_ITEM_ALREADY_EXISTS);

	ASSERT_TEST(cacheFreeElement(cache, outOfRange) == CACHE_ITEM_DOES_NOT_EXIST);
	ASSERT_TEST(cacheFreeElement(cache, notInCache) == CACHE_ITEM_DOES_NOT_EXIST);
	for (int i = ELEMENTS_SIZE-1; i >= 0; --i) {
		ASSERT_TEST(cacheFreeElement(cache, elements[i]) == CACHE_SUCCESS);
	}
	ASSERT_TEST(cacheFreeElement(cache, doubled) == CACHE_ITEM_DOES_NOT_EXIST);
	ASSERT_TEST(cacheFreeElement(NULL, doubled) == CACHE_NULL_ARGUMENT);
	ASSERT_TEST(cacheFreeElement(NULL, NULL) == CACHE_NULL_ARGUMENT);

	cacheDestroy(cache);
	return true;
}
Example #4
0
static bool testCachePush() {
	//strings
	char * toFirstCell = "";
	char * ramones = "Ramones";
	char toLastCell[] = {(char)254, '\0'};
	char outOfRange[] = {(char)255, '\0'};

	Cache cache = cacheCreate(255, freeString, copyString, compareStrings, getFirstLetter);
	ASSERT_TEST(cache != NULL);

	ASSERT_TEST(cachePush(NULL, toFirstCell) == CACHE_NULL_ARGUMENT);
	ASSERT_TEST(cachePush(cache, NULL) == CACHE_NULL_ARGUMENT);
	ASSERT_TEST(cachePush(cache, toFirstCell) == CACHE_SUCCESS);
	ASSERT_TEST(cacheIsIn(cache, toFirstCell));
	ASSERT_TEST(cacheIsIn(cache, toFirstCell));
	ASSERT_TEST(cachePush(cache, ramones) == CACHE_SUCCESS);
	ASSERT_TEST(cacheIsIn(cache, ramones));
	ASSERT_TEST(cachePush(cache, toLastCell) == CACHE_SUCCESS);
	ASSERT_TEST(cacheIsIn(cache, toLastCell));
	ASSERT_TEST(cachePush(cache, outOfRange) == CACHE_OUT_OF_RANGE);
	ASSERT_TEST(!cacheIsIn(cache, outOfRange));
	ASSERT_TEST(cachePush(cache, ramones) == CACHE_ITEM_ALREADY_EXISTS);
	ASSERT_TEST(cachePush(cache, toFirstCell) == CACHE_ITEM_ALREADY_EXISTS);
	ASSERT_TEST(cachePush(cache, toLastCell) == CACHE_ITEM_ALREADY_EXISTS);
	ASSERT_TEST(cachePush(cache, outOfRange) == CACHE_OUT_OF_RANGE);

	char * toFirstCellCopy = cacheExtractElementByKey(cache, getFirstLetter(toFirstCell));
	char * ramonesCopy = cacheExtractElementByKey(cache, getFirstLetter(ramones));
	char * toLastCellCopy = cacheExtractElementByKey(cache, getFirstLetter(toLastCell));
	char * outOfRangeCopy = cacheExtractElementByKey(cache, getFirstLetter(outOfRange));

	ASSERT_TEST(!strcmp(toFirstCell, toFirstCellCopy));
	ASSERT_TEST(!strcmp(ramones, ramonesCopy));
	ASSERT_TEST(!strcmp(toLastCell, toLastCellCopy));
	ASSERT_TEST(outOfRangeCopy == NULL);

	ASSERT_TEST(cacheExtractElementByKey(cache, getFirstLetter(toFirstCell)) == NULL);
	ASSERT_TEST(cacheExtractElementByKey(cache, getFirstLetter(ramones)) == NULL);
	ASSERT_TEST(cacheExtractElementByKey(cache, getFirstLetter(toLastCell)) == NULL);

	freeString(toFirstCellCopy);
	freeString(ramonesCopy);
	freeString(toLastCellCopy);
	cacheDestroy(cache);

	return true;
}
Example #5
0
static bool testCacheCreate(void) {
	ASSERT_TEST(!cacheCreate(0, freeString, copyString, compareStrings, getFirstLetter));
	ASSERT_TEST(!cacheCreate(256, NULL, copyString, compareStrings, getFirstLetter));
	ASSERT_TEST(!cacheCreate(256, freeString, NULL, compareStrings, getFirstLetter));
	ASSERT_TEST(!cacheCreate(256, freeString, copyString, NULL, getFirstLetter));
	ASSERT_TEST(!cacheCreate(256, freeString, copyString, compareStrings, NULL));

	Cache cache = cacheCreate(1, freeString, copyString, compareStrings, getFirstLetter);
	ASSERT_TEST(cache != NULL);
	cacheDestroy(cache);
	return true;
}
int main(void){  
  cache * c = NULL;
  int data;
  int * lookupData;
  int result, i;

  printf("%d\n", (int)c);
  fflush(stdout);

  result = cacheCreate(3, 3, compareBlockNumberNoPointer, NULL, NULL, &c);
  FAIL(result, "Error allocating cache");
  
  /* alloc entries for cache */
  for (i = 0 ; i < 3; i++){    
    data = i;

    result = cacheInsert(c, i * 100, &data);
    FAIL(result,"Error inserting");
  }

  result = cacheLookup(c, 200, &lookupData);
  FAIL(result,"Error lookup");
  printf("data %d\n", *lookupData);
  result = cacheLookup(c, 200, &lookupData);
  FAIL(result,"Error lookup");
  printf("data %d\n", *lookupData);
  result = cacheLookup(c, 0, &lookupData);
  FAIL(result,"Error lookup");
  printf("data %d\n", *lookupData);
  result = cacheLookup(c, 0, &lookupData);
  FAIL(result,"Error lookup");
  printf("data %d\n", *lookupData);

  printInfoLSCache(stdout, c -> lsc);

  data = 111;
  result = cacheInsert(c, 300, &data);
  printInfoLSCache(stdout, c -> lsc);

  fflush(stdout);

  return 0;
}
Example #7
0
// ML: string * int * int -> cache ptr_option
const cache *
apsml_cacheCreate (char *key, int maxsize, int timeout, request_data * rd)	/*{{{ */
{
//  keyNhash kn1;
//  kn1.key = &(cacheName1->data);
//  kn1.hash = charhashfunction (kn1.key);
//  char *key = &(cacheName1->data);
  apr_thread_rwlock_wrlock (rd->cachetable->rwlock);
  const cache *c;
//  void **c1 = (void **) &c;
//  ap_log_error (APLOG_MARK, LOG_DEBUG, 0, rd->server,
//    "apsml_cacheCreate: cacheName == %s, maxsize == %i, timeout == %i",
//    &(cacheName1->data), maxsize, timeout);
  if (cachetable_find (rd->cachetable->ht, key, &c) == hash_DNE)
    {
      int size = strlen(key);
      char *kn = malloc (size+1);
//      ap_log_error (APLOG_MARK, LOG_DEBUG, 0, rd->server,
//		    "apsml_cacheCreate: malloc 0x%x, length:%d", (unsigned long) kn, size);
      
      if (kn == NULL) return NULL;
//      kn->key = (char *) (kn + 1);
//      kn->hash = kn1.hash;
      strncpy (kn, key, size);
      kn[size] = 0;
      c = cacheCreate (maxsize, timeout, charhashfunction(kn), rd);
      cachetable_insert (rd->cachetable->ht, kn, c);
    }
  else
    {
      ap_log_error (APLOG_MARK, LOG_DEBUG, 0, rd->server,
		    "apsml_cacheCreate: cacheName == %s already exists", key);
    }
//  ppGlobalCache(rd);
  apr_thread_rwlock_unlock (rd->cachetable->rwlock);
  return c;
}				/*}}} */
Example #8
0
ccache *cacheAddSlave(void *el) {
    ccache *c = cacheCreate();
    c->el = el;
    listAddNodeTail(slave_caches,c);
    return c;
}
Example #9
0
int
main(int argc, char *argv[])
{
    FileIterator **fis = NULL;
    const int fi_count = argc - 1;
    int active_fi_count = 0;
    time_t ready_time;
    Cache *them, *us;
    int i;

    if (argc < 3)
	return usage(argv[0]);

    them = cacheCreate("them");
    us = cacheCreate("us");
    them->peer = us;
    us->peer = them;

    fis = xcalloc(fi_count, sizeof(FileIterator *));
    /* init iterators with files */
    fis[0] = fileIteratorCreate(argv[1], accessLogReader);
    for (i = 2; i < argc; ++i)
	fis[i - 1] = fileIteratorCreate(argv[i], swapStateReader);
    /* check that all files were found */
    for (i = 0; i < fi_count; ++i)
	if (!fis[i])
	    return -2;
    /* read prefix to get start-up contents of the peer cache */
    ready_time = -1;
    for (i = 1; i < fi_count; ++i) {
	FileIterator *fi = fis[i];
	while (fi->inner_time > 0) {
	    if (((storeSwapLogData *) fi->entry)->op == SWAP_LOG_DEL) {
		cachePurge(them, fi->entry, 0);
		if (ready_time < 0)
		    ready_time = fi->inner_time;
	    } else {
		if (ready_time > 0 && fi->inner_time > ready_time)
		    break;
		cacheStore(them, fi->entry, 0);
	    }
	    fileIteratorAdvance(fi);
	}
    }
    /* digest peer cache content */
    cacheResetDigest(them);
    us->digest = cacheDigestClone(them->digest);	/* @netw@ */

    /* shift the time in access log to match ready_time */
    fileIteratorSetCurTime(fis[0], ready_time);

    /* iterate, use the iterator with the smallest positive inner_time */
    cur_time = -1;
    do {
	int next_i = -1;
	time_t next_time = -1;
	active_fi_count = 0;
	for (i = 0; i < fi_count; ++i) {
	    if (fis[i]->inner_time >= 0) {
		if (!active_fi_count || fis[i]->inner_time < next_time) {
		    next_i = i;
		    next_time = fis[i]->inner_time;
		}
		active_fi_count++;
	    }
	}
	if (next_i >= 0) {
	    cur_time = next_time;
	    /*fprintf(stderr, "%2d time: %d %s", next_i, (int)cur_time, ctime(&cur_time)); */
	    if (next_i == 0)
		cacheFetch(us, fis[next_i]->entry);
	    else
		cacheUpdateStore(them, fis[next_i]->entry, 1);
	    fileIteratorAdvance(fis[next_i]);
	}
    } while (active_fi_count);

    /* report */
    cacheReport(them);
    cacheReport(us);
    cacheQueryReport(us, &us->qstats);

    /* clean */
    for (i = 0; i < argc - 1; ++i) {
	fileIteratorDestroy(fis[i]);
    }
    xfree(fis);
    cacheDestroy(them);
    cacheDestroy(us);
    return 0;
}