Exemplo n.º 1
0
/** ***************************************************************************
 * A few simple tests to check if it works at all.
 *
 */
static void basic()
{
  (void)printf("----- basic -----\n");

  struct bloom bloom;

  assert(bloom_init(&bloom, 0, 1.0) == 1);
  assert(bloom_init(&bloom, 10, 0) == 1);
  assert(bloom.ready == 0);
  assert(bloom_add(&bloom, "hello world", 11) == -1);
  assert(bloom_check(&bloom, "hello world", 11) == -1);
  bloom_free(&bloom);

  assert(bloom_init(&bloom, 102, 0.1) == 0);
  assert(bloom.ready == 1);
  bloom_print(&bloom);

  assert(bloom_check(&bloom, "hello world", 11) == 0);
  assert(bloom_add(&bloom, "hello world", 11) == 0);
  assert(bloom_check(&bloom, "hello world", 11) == 1);
  assert(bloom_add(&bloom, "hello world", 11) > 0);
  assert(bloom_add(&bloom, "hello", 5) == 0);
  assert(bloom_add(&bloom, "hello", 5) > 0);
  assert(bloom_check(&bloom, "hello", 5) == 1);
  bloom_free(&bloom);
}
Exemplo n.º 2
0
int
ppbloom_check(const void *buffer, int len)
{
    int ret;

    ret = bloom_check(ppbloom + PING, buffer, len);
    if (ret)
        return ret;

    ret = bloom_check(ppbloom + PONG, buffer, len);
    if (ret)
        return ret;

    return 0;
}
Exemplo n.º 3
0
Arquivo: hfile.c Projeto: pipul/lab
entry_t *hfile_loup(hfile_t *fp, sds key)
{
	int32_t fd;
	meta_t *m;
	entry_t *t;
	sds firstkey;

	if (!fp || !key)
		return(NULL);
	if (-1 == bloom_check(fp->bloom,key))
		return(NULL);
	firstkey = (index_head(fp->metas))->key;
	if (sdscmp(key,firstkey) < 0)
		return(NULL);
	if (sdscmp(key,fp->fileinfo->lastkey) > 0)
		return(NULL);
	m = index_loup(fp->metas,key);
	if (m->state != IN_CACHE) {
		if ((fd = open(fp->name,O_RDONLY)) < 0)
			/* error handle */
			return(NULL);
		fp->blocks[m->id] = block_load(fd,m->offset,m->blocksize);
		m->state = IN_CACHE;
		close(fd);
	}
	t = block_loup(fp->blocks[m->id],key);
	return(t);
}
Exemplo n.º 4
0
int get_rating(unsigned char *info_hash, const struct sockaddr_storage *target,
		const struct sockaddr *from, int fromlen) {
	struct dht_rating_entry * entry = list;
	while (entry) {
		if (memcmp(entry->key, info_hash, SHA_DIGEST_LENGTH) == 0) {
			break;
		}
		entry = entry->next;
	}
	if (entry) {
		struct dht_result_rating * pos = entry->ratings;
		struct dht_result_rating * prev = NULL;
		while (pos) {
			if (sockaddr_storage_equals(pos->ss, target)) {
				entry->updated = time(NULL);
				if (pos->rating < 10) {
					unsigned char md[SHA_DIGEST_LENGTH];
					SHA1((const unsigned char*) from, fromlen, md);
					if (!bloom_check(pos->frombloom, (const char*) md)) {
						bloom_add(pos->frombloom,(const char*) md);
						pos->rating++;
					}
				}
				return pos->rating;
			}
			prev = pos;
			pos = pos->next;
		}
		prev->next = rating_create(target, from, fromlen);
		return 0;
	} else {
		rating_create_entry(info_hash, target, from, fromlen);
		return 0;
	}
}
Exemplo n.º 5
0
static void test_bloom_based_on_dictionary_fixture(void)
{
    int in = 0;
    int not_in = 0;
    double false_positive_rate = 0;

    load_dictionary_fixture();

    for (int i = 0; i < lenA; i++)
    {
        if (bloom_check(&bloom, (const uint8_t *) A[i], strlen(A[i])))
        {
            in++;
        }
        else
        {
            not_in++;
        }
    }
    false_positive_rate = (double) in / (double) lenA;

    TEST_ASSERT_EQUAL_INT(TESTS_BLOOM_PROB_IN_FILTER, in);
    TEST_ASSERT_EQUAL_INT(TESTS_BLOOM_NOT_IN_FILTER, not_in);
    TEST_ASSERT(false_positive_rate < TESTS_BLOOM_FALSE_POS_RATE_THR);
}
Exemplo n.º 6
0
/* handle a received beacon */
static void _handle_beacon(gnrc_pktsnip_t *pkt)
{
    if (pkt->size != sizeof(beacon_t)) {
        LOG_WARNING("beaconing: received packet doesn't seem to be a beacon - wrong size\n");
        gnrc_pktbuf_release(pkt);
        return;
    }
    beacon_t b = *((beacon_t*) pkt->data);
    if (b.magic_key != BEACONING_MK) {
        LOG_WARNING("beaconing: received packet doesn't seem to be a beacon - wrong magic key\n");
        gnrc_pktbuf_release(pkt);
        return;
    }
    LOG_DEBUG("beaconing: received a beacon, id is %" PRIu32 "\n", b.id);
    if (bloom_check(&dow_neighbors, (uint8_t*) &(b.id), sizeof(b.id))) {
        LOG_DEBUG("beaconing: already know this neighbor\n");
    }
    /* if we don't know the neighbor we analyze its ID */
    else {
        bloom_add(&dow_neighbors, (uint8_t*) &(b.id), sizeof(b.id));
        dow_size++;
        if (b.id < dow_my_id) {
            dow_position++;
        }
    }
    gnrc_pktbuf_release(pkt);
}
Exemplo n.º 7
0
int main(void)
{
    bloom_t *bloom = bloom_new(1 << 7, 6, fnv_hash, sax_hash, sdbm_hash,
                                      djb2_hash, kr_hash, dek_hash, rotating_hash, one_at_a_time_hash);

    printf("Testing Bloom filter.\n\n");
    printf("m: %zd\nk: %zd\n\n", bloom->m, bloom->k);

    for (int i = 0; i < lenB; i++) {
        bloom_add(bloom, (const uint8_t *) B[i], strlen(B[i]));
        printf("Added \"%s\"\n", B[i]);
    }

    int in = 0;
    int not_in = 0;

    for (int i = 0; i < lenA; i++) {
        if (bloom_check(bloom, (const uint8_t *) A[i], strlen(A[i]))) {
            in++;
        }
        else {
            not_in++;
        }
    }

    printf("\n");
    printf("%d elements probably in the filter.\n", in);
    printf("%d elements not in the filter.\n", not_in);
    double false_positive_rate = (double) in / (double) lenA;
    printf("%f false positive rate.\n", false_positive_rate);

    bloom_del(bloom);
    printf("\nAll done!\n");
    return 0;
}
Exemplo n.º 8
0
void run_test3(int* array1, int* array2, int arraysize)
{
	int bloomsize = 1000;	
	bloom_filter_t bloomfilter;
	bloom_init(&bloomfilter, bloomsize);
	int x = 0;

	//set the bits in bloomfilter based on array1
	for (x= 0; x< arraysize; x++)
	{
		bloom_add(&bloomfilter, array1[x]);
	}

	//First, count all the bits that are set
	int totalbits = 0;
	for (x = 0; x< bloomsize; x++)
	{
		totalbits += get_bit(&bloomfilter, x);
	}
	printf("Total bits set: %i\n",totalbits);	

	int array2bits = 0;
	//Next, count all the bits in the second array that are set in bloomfiter
	for (x = 0; x< arraysize; x++)
	{
		array2bits += bloom_check(&bloomfilter, array2[x]);
	}
	printf("Array2 bits set: %i\n",array2bits);

	bloom_destroy(&bloomfilter);

}
Exemplo n.º 9
0
int main(void)
{
    xtimer_init();

    bloom_t *bloom = bloom_new(1 << 12, 8, fnv_hash, sax_hash, sdbm_hash,
                                      djb2_hash, kr_hash, dek_hash, rotating_hash, one_at_a_time_hash);

    printf("Testing Bloom filter.\n\n");
    printf("m: %" PRIu32 " k: %" PRIu32 "\n\n", (uint32_t) bloom->m,
           (uint32_t) bloom->k);

    genrand_init(myseed);

    unsigned long t1 = xtimer_now();

    for (int i = 0; i < lenB; i++) {
        buf_fill(buf, BUF_SIZE);
        buf[0] = MAGIC_B;
        bloom_add(bloom,
                  (uint8_t *) buf,
                  BUF_SIZE * sizeof(uint32_t) / sizeof(uint8_t));
    }

    unsigned long t2 = xtimer_now();
    printf("adding %d elements took %" PRIu32 "ms\n", lenB,
           (uint32_t) (t2 - t1) / 1000);

    int in = 0;
    int not_in = 0;

    unsigned long t3 = xtimer_now();

    for (int i = 0; i < lenA; i++) {
        buf_fill(buf, BUF_SIZE);
        buf[0] = MAGIC_A;

        if (bloom_check(bloom,
                        (uint8_t *) buf,
                        BUF_SIZE * sizeof(uint32_t) / sizeof(uint8_t))) {
            in++;
        }
        else {
            not_in++;
        }
    }

    unsigned long t4 = xtimer_now();
    printf("checking %d elements took %" PRIu32 "ms\n", lenA,
           (uint32_t) (t4 - t3) / 1000);

    printf("\n");
    printf("%d elements probably in the filter.\n", in);
    printf("%d elements not in the filter.\n", not_in);
    double false_positive_rate = (double) in / (double) lenA;
    printf("%f false positive rate.\n", false_positive_rate);

    bloom_del(bloom);
    printf("\nAll done!\n");
    return 0;
}
Exemplo n.º 10
0
Datum pgbloomfun_add(PG_FUNCTION_ARGS)
{
  bytea *newbloomba, *bloomba = PG_GETARG_BYTEA_P(0);
  text *key = PG_GETARG_TEXT_P(1);
  pgbloom_t *pgbloom = get_pgbloom(bloomba);
  bloom_t newbloom, *bloom = NULL;
  size_t newbloom_size;
  int space_left, i;

  space_left = (pgbloom->last_capacity > pgbloom->last_entries) ||
               (pgbloom->growth_factor == 0);
  for (i=0; i<pgbloom->filters; i++)
    {
      bloom = next_bloom(bloomba, bloom);
      if (bloom == NULL)
        {
          elog(ERROR, "pgbloomfun: missing filter in bloom object");
        }
      if (i == pgbloom->filters - 1 && space_left)
        {
          if (bloom_add(bloom, VARDATA(key), VARSIZE(key) - VARHDRSZ) == 0)
            {
              pgbloom->total_entries ++;
              pgbloom->last_entries ++;
            }
          PG_RETURN_BYTEA_P(bloomba);
        }
      else if (bloom_check(bloom, VARDATA(key), VARSIZE(key) - VARHDRSZ))
        {
          PG_RETURN_BYTEA_P(bloomba);  /* key already exists */
        }
    }

  /* create a new filter */
  pgbloom->filters += 1;
  pgbloom->total_entries += 1;
  pgbloom->last_entries = 1;
  pgbloom->last_capacity *= pgbloom->growth_factor;
  pgbloom->total_capacity += pgbloom->last_capacity;

  /* calculate and allocate space */
  bloom_init(&newbloom, pgbloom->last_capacity, pgbloom->error_rate);
  newbloom_size = sizeof(newbloom) + newbloom.bits / 8;
  newbloomba = palloc(VARSIZE(bloomba) + newbloom_size);
  memcpy(newbloomba, bloomba, VARSIZE(bloomba));
  SET_VARSIZE(newbloomba, VARSIZE(bloomba) + newbloom_size);

  /* initialize the new bloom filter and add the new key to it */
  bloom = (bloom_t *) (((unsigned char *) newbloomba) + VARSIZE(bloomba));
  memset(bloom, 0, newbloom_size);
  memcpy(bloom, &newbloom, sizeof(newbloom));
  bloom_add(bloom, VARDATA(key), VARSIZE(key) - VARHDRSZ);

  PG_RETURN_BYTEA_P(newbloomba);
}
Exemplo n.º 11
0
Datum pgbloomfun_check(PG_FUNCTION_ARGS)
{
  bytea *bloomba = PG_GETARG_BYTEA_P(0);
  text *key = PG_GETARG_TEXT_P(1);
  bloom_t *bloom = NULL;

  while ((bloom = next_bloom(bloomba, bloom)) != NULL)
    if (bloom_check(bloom, VARDATA(key), VARSIZE(key) - VARHDRSZ))
      PG_RETURN_BOOL(1);

  PG_RETURN_BOOL(0);
}
Exemplo n.º 12
0
/**
 cs_setopt_url : add urls to task queue
 @cspider : the cspider
 @url : new task's url
**/
void cs_setopt_url(cspider_t *cspider, char *url){
  PANIC(cspider);
  PANIC(url);
  if (!bloom_check(cspider->bloom, url)) {
    //url no exits
    bloom_add(cspider->bloom, url);
    unsigned int len = strlen(url);
    char *reUrl = (char*)malloc(sizeof(char) * (len+1));
    PANIC(reUrl);
    strncpy(reUrl, url, len+1);
    createTask(cspider->task_queue, reUrl);
  }
}
Exemplo n.º 13
0
int checkLastFromFiles(request_rec * r, const char * filter, const char * packet) {
	BLOOM * bloom;
	
	FILE * fp = fopen(filter, "r");
	
	size_t read;
	
	/* read the filter size */
	size_t size;
	read = fread(&size, sizeof(size_t), 1, fp);
	
	/* Re-create the filter */		
	if(!(bloom=bloom_create(size, 2, sax_hash, sdbm_hash)))
		ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(05011) "check last: bloom filter");

	/* Read the filter from file */			
	read = fread(bloom->a, sizeof(char), (bloom->asize+CHAR_BIT-1)/CHAR_BIT, fp);
	
	fclose(fp);
				
	fp = fopen(packet, "r");
	
	/* Read len of the packet */			
	int buflen;
	read = fread(&buflen, sizeof(int), 1, fp);
	
	/* Alocate memory for the buffer */		
	char * buffer;
	if((buffer = (char *) malloc(buflen * sizeof(char))) == NULL)
		ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(05011) "check last: buffer");

	/* Read the buffer */	
	read = fread(buffer, sizeof(char), buflen, fp);
			
	fclose(fp);
	
	
	/* Check and return if the request was received */
	if(bloom_check(bloom, buffer)){
		free(buffer);
		bloom_destroy(bloom);
		return 1;
	}
	else{
		free(buffer);
		bloom_destroy(bloom);
		return 0;
	}

}
Exemplo n.º 14
0
Arquivo: tool.c Projeto: blahah/facs
int
fastq_read_check (char *begin, int length, char model, bloom * bl, 
                  float tole_rate, F_set * File_head)
{
  char *p = begin;
  int distance = length;
  int signal = 0, result = 0;
  char *previous, *key = (char *) malloc (bl->k_mer * sizeof (char) + 1);

  while (distance > bl->k_mer)
    {
      if (signal == 1)
	break;

      if (distance >= bl->k_mer)
	{
	  memcpy (key, p, sizeof (char) * bl->k_mer);	//need to be tested
	  key[bl->k_mer] = '\0';
	  p += bl->k_mer;
	  previous = p;
	  distance -= bl->k_mer;
	}

      else
	{
	  memcpy (key, previous + distance, sizeof (char) * bl->k_mer);
	  p += (bl->k_mer - distance);
	  signal = 1;
	}

      if (model == 'r')
	rev_trans (key);

      if (bloom_check (bl, key))
	{
	  result =
	    fastq_full_check (bl, begin, length, model, tole_rate, File_head);
	  if (result > 0)
	    return result;
	  else if (model == 'n')
	    break;
	}

    }				//outside while
  if (model == 'r')
    return 0;
  else
    return fastq_read_check (begin, length, 'r', bl, tole_rate, File_head);
}
Exemplo n.º 15
0
int main(int argc, char *argv[])
{
	FILE *fp;
	char line[1024];
	char *p;
	BLOOM *bloom;
	
	if(argc<2) {
		fprintf(stderr, "ERROR: No word file specified\n");
		return EXIT_FAILURE;
	}

	if(!(bloom=bloom_create(2500000, 2, sax_hash, sdbm_hash))) {
		fprintf(stderr, "ERROR: Could not create bloom filter\n");
		return EXIT_FAILURE;
	}

	if(!(fp=fopen(argv[1], "r"))) {
		fprintf(stderr, "ERROR: Could not open file %s\n", argv[1]);
		return EXIT_FAILURE;
	}

	while(fgets(line, 1024, fp)) {
		if((p=strchr(line, '\r'))) *p='\0';
		if((p=strchr(line, '\n'))) *p='\0';

		bloom_add(bloom, line);
	}

	fclose(fp);

	while(fgets(line, 1024, stdin)) {
		if((p=strchr(line, '\r'))) *p='\0';
		if((p=strchr(line, '\n'))) *p='\0';

		p=strtok(line, " \t,.;:\r\n?!-/()");
		while(p) {
			if(!bloom_check(bloom, p)) {
				printf("No match for ford \"%s\"\n", p);
			}
			p=strtok(NULL, " \t,.;:\r\n?!-/()");
		}
	}

	bloom_destroy(bloom);

	return EXIT_SUCCESS;
}
Exemplo n.º 16
0
void bf_check(FILE* fp1,FILE* fp2,FILE *fp3) {
	BLOOM *bloom ;
	char line[1024];
	int pos = -1;
	bloom = bloom_create(239620000);
	printf("Create done\n");
	while(fgets(line,1024,fp1))
		bloom_add(bloom,line);
	printf("add done\n");
	fprintf(fp3,"-------------------------------------Bloom Filter Match-------------------------------------------\n");
	while(fgets(line,1024,fp2)) {
		pos++;
		if(!bloom_check(bloom,line))
			fprintf(fp3,"%d no\n",pos);
	}
}
Exemplo n.º 17
0
int searchpattern (char *pattern) {
	char *toprocess;
	int found=0;
	int size;
	char pline[MAX_LINE_SIZE];
	char unhex[MAX_LINE_SIZE];

	toprocess=pattern;
	size=strlen(toprocess);
	if (opt_ignorecase) {
		toprocess=str2upper(toprocess,pline);
	}
	if (opt_unhex) {
		size=hexstr2char(toprocess,unhex,MAX_LINE_SIZE);
		toprocess=unhex;
	}
	found=bloom_check(&bloom, toprocess, size);
	return (found);
}
Exemplo n.º 18
0
int bloom_delete(BLOOM *bloom, const char *s, uint32_t len)
{
	uint32_t i;
	uint32_t idx;

	uint32_t *hashes = bloom->hashes;
	hash_func(bloom, s, len, hashes);

	if (!bloom_check(bloom,s,len))
	{
		return 0;
	}
	for (i = 0; i < bloom->num_funcs; ++i)
	{
		idx = hashes[i];
		bloom->filter[idx]--;
		if (bloom->filter[idx] <= 0)
		{
			bloom->filter[idx] = 0;
		}
	}
	return 1;
}
Exemplo n.º 19
0
Arquivo: tool.c Projeto: blahah/facs
/*-------------------------------------*/
int
fasta_full_check (bloom * bl, char *begin, char *next, char model, float tole_rate, F_set * File_head)
{
  int match_s = 0, count = 0, mark = 1;

  int n = 0, m = 0, count_enter = 0, match_time = 0;

  short previous = 0, conse = 0;

  float result;

  char *key = (char *) malloc ((bl->k_mer + 1) * sizeof (char));

  begin = strchr (begin + 1, '\n') + 1;

  char *p = begin;

  while (p != next)
    {
      if (*p == '\n')
	count_enter++;
      p++;
    }

  p = begin;

  while (*p != '>' && *p != '\0')
    {
      while (n < bl->k_mer)
	{
	  if (p[m] == '>' || p[m] == '\0')
	    {
	      m--;
	      break;
	    }

	  if (p[m] != '\r' && p[m] != '\n')
	    key[n++] = p[m];

	  m++;
	}
      key[n] = '\0';

      if (model == 'r')
	rev_trans (key);
      //printf("key->%s\n",key);
      if (count >= bl->k_mer)
	{
	  mark = 1;
	  count = 0;
	}
      if (strlen (key) == bl->k_mer)
	{
	  if (bloom_check (bl, key))
	    {
	      match_time++;
	      if (previous == 1)
		conse++;
	      else
		{
		  conse += bl->k_mer;
		  previous = 1;
		}
	      if (mark == 1)
		{
		  match_s += (bl->k_mer - 1);
		  mark = 0;
		}
	      else
		match_s++;
	    }

	  else
	    {
	      previous = 0;
	      //printf("unhit--->\n");
	    }

	  count++;
	}			//outside if
      //printf("score->%d\n",match_s);
      p++;
      if (p[0] == '\n')
	p++;
      n = 0;
      m = 0;
    }				// end of while
  //result = (float) match_s / (float) (next - begin - count_enter);
  //result = (float) match_time*(bl->k_mer)/(float)((next-begin-count_enter-bl->k_mer+2)*(bl->k_mer)+2*dx_add(bl->k_mer));
  //result = (float) ((match_time+conse)*(bl->k_mer))/(float)((next-begin-count_enter-bl->k_mer+2+conse)*(bl->k_mer)+2*dx_add(bl->k_mer));
  //result = (float) ((match_time)*(bl->k_mer))/(float)((next-begin-count_enter-bl->k_mer+2)*(bl->k_mer)+2*dx_add(bl->k_mer));
  //result = (float)(match_time*bl->k_mer+conse)/(float)((next-begin-count_enter-bl->k_mer+2)*bl->k_mer+conse+2*dx_add(bl->k_mer));
  //printf ("result1->%f\n",result);
  //result = (float)(match_time*bl->k_mer)/(float)((next-begin-count_enter)*bl->k_mer-2*dx_add(bl->k_mer-1));
  result = (float) (match_time * bl->k_mer + conse) / (float) ((next - begin - count_enter) * bl->k_mer - 2 * bl->dx + (next - begin - count_enter) - bl->k_mer + 1);

#pragma omp atomic
  File_head->hits += match_time;
#pragma omp atomic
  File_head->all_k += (next - begin - count_enter - bl->k_mer);

  if (result >= tole_rate)	//match >tole_rate considered as contaminated
    return match_s;
  else
    return 0;
}
Exemplo n.º 20
0
Arquivo: tool.c Projeto: blahah/facs
/*-------------------------------------*/
int
fasta_read_check (char *begin, char *next, char model, bloom * bl, float tole_rate, F_set * File_head)
{

  char *p = strchr (begin + 1, '\n') + 1;

  if (!p || *p == '>')
    return 1;

  int n, m, result, count_enter;
  char *key = (char *) malloc ((bl->k_mer + 1) * sizeof (char));
  char *pre_key = (char *) malloc ((bl->k_mer + 1) * sizeof (char));

  key[bl->k_mer] = '\0';

  while (p != next)
    {
      while (n < bl->k_mer)
	{
	  if (p[m] == '>' || p[m] == '\0')
	    {
	      m--;
	      break;
	    }

	  if (p[m] != '\r' && p[m] != '\n')
	    key[n++] = p[m];
	  else
	    count_enter++;
	  m++;
	}			//inner while

      if (m == 0)
	break;

      if (strlen (key) == bl->k_mer)
	memcpy (pre_key, key, sizeof (char) * (bl->k_mer + 1));

      else
	{
	  char *temp_key = (char *) malloc (bl->k_mer * sizeof (char));

	  memcpy (temp_key, pre_key + strlen (key), bl->k_mer - strlen (key));

	  memcpy (temp_key + bl->k_mer - strlen (key), key, sizeof (char) * (strlen (key) + 1));

	  free (key);

	  key = temp_key;

	}
      p += m;

      n = 0;

      m = 0;

      if (model == 'r')
	rev_trans (key);

      if (bloom_check (bl, key))
	{
	  result = fasta_full_check (bl, begin, next, model, tole_rate, File_head);
	  if (result > 0)
	    return result;
	  //else if (model == 'n')     //use recursion to check the sequence forward and backward
	  //    return fasta_read_check (begin, next, 'r', bl);
	  else if (model == 'n')
	    break;
	}

      //memset (key, 0, bl->k_mer);
    }				//outside while
  if (model == 'r')
    return 0;
  else
    return fasta_read_check (begin, next, 'r', bl, tole_rate, File_head);
}
Exemplo n.º 21
0
Arquivo: tool.c Projeto: blahah/facs
/*-------------------------------------*/
int
fastq_full_check (bloom * bl, char *p, int distance, char model, float tole_rate, F_set * File_head)
{

  //printf ("fastq full check...\n");

  int length = distance;

  int count = 0, match_s = 0, mark = 1, match_time = 0;

  float result;

  char *key = (char *) malloc (bl->k_mer * sizeof (char) + 1);

  short prev = 0, conse = 0;

  while (distance >= bl->k_mer)
    {
      memcpy (key, p, sizeof (char) * bl->k_mer);
      key[bl->k_mer] = '\0';
      p += 1;

      if (model == 'r')
	rev_trans (key);

      if (count >= bl->k_mer)
	{
	  mark = 1;
	  count = 0;
	}
      if (strlen (key) == bl->k_mer)
	{
	  if (bloom_check (bl, key))
	    {
	      match_time++;
	      if (prev == 1)
		conse++;
	      else
		{
		  conse += bl->k_mer;
		  prev = 1;
		}
	      if (mark == 1)
		{
		  match_s += (bl->k_mer - 1);
		  mark = 0;
		}
	      else
		match_s++;
	    }

	  else
	    {
	      prev = 0;
	      //printf("unhit--->\n");
	    }
	  count++;
	}			//outside if
      distance--;
    }				// end while
  free (key);
  result = (float) (match_time * bl->k_mer + conse) / (float) (length * bl->k_mer - 2 * bl->dx + length - bl->k_mer + 1);
  //result = (float) match_s / (float) length;
#pragma omp atomic
  File_head->hits += match_time;
#pragma omp atomic
  File_head->all_k += (length - bl->k_mer);
  if (result >= tole_rate)
    return match_s;
  else
    return 0;
}
Exemplo n.º 22
0
int main(int argc, char *argv[])
{
    FILE *fp1;
    FILE *fp2;
    FILE *fp3;
    
    int  i = 0;
    char line[1024];
    char *p;
    BF *bloom;
    
    if(argc<2) {
        fprintf(stderr, "ERROR: No word file specified\n");
        return EXIT_FAILURE;
    }
    if(!(bloom=bloom_create(200000000, 11, RSHash, JSHash, PJWHash, ELFHash, BKDRHash, SDBMHash, DJBHash, DEKHash, BPHash, FNVHash, APHash))) {
        fprintf(stderr, "ERROR: Could not create bloom filter\n");
        return EXIT_FAILURE;
    }

    if(!(fp1=fopen(argv[1], "r"))) {
        fprintf(stderr, "ERROR: Could not open file %s\n", argv[1]);
        return EXIT_FAILURE;
    }
    
    while(fgets(line, 1024, fp1)) {
        if((p=strchr(line, '\r'))) *p='\0';
        if((p=strchr(line, '\n'))) *p='\0';
        bloom_add(bloom, line);
    }

    fclose(fp1);

    if(!(fp2=fopen(argv[2], "r"))) {
        fprintf(stderr, "ERROR: Could not open file %s\n", argv[2]);
        return EXIT_FAILURE;
    }
    if(!(fp3=fopen("checkedemailresult.dat","w"))){
                fprintf(stderr, "ERROR:Could not open file");
                return EXIT_FAILURE;
    }
    
    while(fgets(line, 1024, fp2)) {
        i++;
        if((p=strchr(line, '\r'))) *p='\0';
        if((p=strchr(line, '\n'))) *p='\0';
        p=strtok(line, "\r\n");
        while(p) {
            if(bloom_check(bloom, line)==1) {
            fputs("yes\n",fp3);
            }
            else{fputs("no\n",fp3);}
          p=strtok(NULL, "\r\n");
        }
    }

    fclose(fp3);     
    fclose(fp2);

    bloom_destroy(bloom);

    return EXIT_SUCCESS;
}
Exemplo n.º 23
0
int main(int argc, char *argv[])
{
	FILE *fp;
    /* No domain more than 1024 characs */
	char line[1024];
	char *p;
	BLOOM *bloom;
	
	if(argc<2) {
		fprintf(stderr, "ERROR: No word file specified\n");
		return EXIT_FAILURE;
	}

    if(!(bloom=bloom_create(2500000, 2, sax_hash, sdbm_hash))) {
        fprintf(stderr, "ERROR: Could not create bloom filter\n");
        return EXIT_FAILURE;
	}

	if(!(fp=fopen(argv[1], "r"))) {
		fprintf(stderr, "ERROR: Could not open file %s\n", argv[1]);
		return EXIT_FAILURE;
	}

    /* set block - read names from file argv[1] and add to filter */
	while(fgets(line, 1024, fp)) {
		if((p=strchr(line, '\r'))) *p='\0';
		if((p=strchr(line, '\n'))) *p='\0';

		bloom_add(bloom, line);
	}

	fclose(fp);

    /* TODO Dump twice - filter.bin and filter_version.bin */
    const char *filepath;
    filepath = "bloomfilter/filter.bin";
    if (!bloom_dump(bloom, filepath)) {
        fprintf(stderr, "ERROR: Could not dump bloom filter %s\n", filepath);
    }
    char filepath2[30];
    filepath = "";
    sprintf(filepath2, "%s%d.bin", "bloomfilter/filter_", bloom->timestamp);
    if (!bloom_dump(bloom, filepath2)) {
        fprintf(stderr, "ERROR: Could not dump bloom filter %s\n", filepath2);
    }

	/* check block - enter name to check */
    while(fgets(line, 1024, stdin)) {
		if((p=strchr(line, '\r'))) *p='\0';
		if((p=strchr(line, '\n'))) *p='\0';

        /* Domain name can have ".", "/", ":" and "-"
         * p=strtok(line, " \t,.;:\r\n?!-/()");
         * Divide line on symbols to check each word*/
		p=strtok(line, " \t,;\r\n?!()");
		while(p) {
			if(!bloom_check(bloom, p)) {
				printf("No match for word \"%s\"\n", p);
			}
			p=strtok(NULL, " \t,.;:\r\n?!-/()");
		}
	}


	bloom_destroy(bloom);

	return EXIT_SUCCESS;
}
Exemplo n.º 24
0
int main(int argc, char *argv[]) {
	clock_t start=clock();
	//程序运行时需从cmd启动,后面两个参数依次为源字符串和待查询字符串的文件名(文件地址)
	char line[1024];
	if (argc < 2) {
		fprintf(stderr, "ERROR: No word file specified\n");
		return -1;
	}

	//获取时间

	int big_count = 0;
	int count = 0;

	//bloom filter algorithm

	BF *bloomfilter = bloom_create(BIG_PRIME);
	freopen(argv[1], "r", stdin);
	while (scanf("%s", line) != EOF) {
		upper_string(line);
		if (check_string(line) == 1) {
			bloom_add(bloomfilter, line);
			big_count++;
		}
	}
	fclose(stdin);
	freopen(argv[2], "r", stdin);
	freopen("result_bf.dat", "w", stdout);
	while (scanf("%s", line) != EOF) {
		upper_string(line);
		if (check_string(line) == 1 && bloom_check(bloomfilter, line) == 1) {
			printf("yes\n");
			count++;
		} else {
			printf("no\n");
		}
	}
	fclose(stdin);
	fclose(stdout);
	freopen("/dev/stdin", "r", stdin);
	freopen("computer.time", "a", stdout);

	printf("%f\n",(double)(clock()-start)/CLOCKS_PER_SEC);
	bloom_destroy(bloomfilter);

	/*****************************************************************************/
/*
	//压缩trie树
	trie_Node* root;
	root = trie_create();
	//读文件,建立压缩trie树
	freopen(argv[1], "r", stdin);
	while (scanf("%s", line) != EOF) {
		upper_string(line);
		if (check_string(line) == 1) {
			trie_add(line, root);
		}
	}
	fclose(stdin);

	//查询是否在树中
	count = 0;
	freopen(argv[2], "r", stdin);
	freopen("result_trie.dat", "w", stdout);
	while (scanf("%s", line) != EOF) {
		upper_string(line);
		if (check_string(line) == 1 && trie_check(line, root) == 1) {
			printf("yes\n");
			count++;
		} else {
			printf("no\n");
		}
	}
	fclose(stdin);
	fclose(stdout);
	freopen("/dev/pts/3", "r", stdin);
	freopen("computer.time", "a", stdout);
	//printf("存在%d个\n", count);
*/	
//	trie_destroy(root);//运行完直接退出,自动释放内存,是否destroy不重要(取消注释可以运行destroy)
	return 0;
}