armon / bloomd

C network daemon for bloom filters
http://armon.github.io/bloomd
Other
1.24k stars 112 forks source link

bitmap_from_filename & bf_from_bitmap #51

Closed Revolle closed 6 years ago

Revolle commented 6 years ago

in a C program, I would like to reuse an existing file generated by bloomd. I've used the standard bloomd/telnet commands "create"/"set", which generates the bloomd flushed file : bloomd.foobar/data.000.mmap

int retCode ;
char myFile[1024];
strcpy(myFile,"bloomd.foobar/data.000.mmap");
struct stat myFileStat;
retCode = stat(myFile,&myFileStat) ; // return 0 : ok
bloom_bitmap myBloom_bitmap ;
retCode = bitmap_from_filename(myFile,myFileStat.st_size, 0, ANONYMOUS , &myBloom_bitmap); // return 0 : ok
bloom_bloomfilter mybloom_bloomfilter ;
retCode = bf_from_bitmap(&myBloom_bitmap, 1, 0, &mybloom_bloomfilter ); // return -1 : ko

bitmap_from_filename is OK ( return 0 ) bf_from_bitmap is KO ( return -1 ) Next steps : check if a string does not exist in the bloom_bloomfilter ... Thank you for your support.

Revolle commented 6 years ago

Load a bloomd file, and checks non existence of string

// test libbloom.a
//
// Compilation :
// cc -Wall frtestbloom_bloomfilter.c -L. -lbloom -linih -lmurmur -lspooky -lm -o frtestbloom_bloomfilter

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include "src/libbloom/bloom.h"
#include "src/libbloom/bitmap.h"
#include "src/libbloom/sbf.h"

// private structure for the bloom_bloom_filter
bloom_bitmap myBloom_bitmap ;
bloom_bloomfilter mybloom_bloomfilter ;

///////////////////////////////////////////////
int constructor_bloom_bloomfilter(char *myFile)
///////////////////////////////////////////////
{
  struct stat myFileStat;

  // Read size of the file data.000.mmap
  int retCode = stat(myFile,&myFileStat) ;

  if( retCode < 0)
  {
    fprintf(stderr,"stat <%s> : error %d\n",myFile,errno);
    return(-1);
  } 
  fprintf(stderr,"stat <%s> : size %ld\n",myFile,myFileStat.st_size);

  // load the file data.000.mmap in the bloom_bitmap
  retCode = bitmap_from_filename(myFile,myFileStat.st_size, 0, PERSISTENT , &myBloom_bitmap);

  if (retCode < 0 )
  {
    fprintf(stderr,"bitmap_from_filename : error %d\n",retCode);
    return(-1);
  }
  fprintf(stderr,"bitmap_from_filename size=%ld : done\n",myBloom_bitmap.size);

  // load the bloom_bitmap in a bloom_bloomfilter
  retCode = bf_from_bitmap(&myBloom_bitmap, 1, 0, &mybloom_bloomfilter );

  if (retCode < 0 )
  {
    fprintf(stderr,"bf_from_bitmap : error %d\n",retCode);
    return(-1);
  }
  fprintf(stderr,"bf_from_bitmap : done\n");

  return(0);

}

///////////////////////////////////////////////
int ckeck_non_existence_bloom_bloomfilter(char *key)
///////////////////////////////////////////////
{
  int retCode = bf_contains(&mybloom_bloomfilter, key);
  switch (retCode) 
  {
  case 0 : fprintf(stderr,"bf_contains <%s> : no\n",key); break;
  case 1 : fprintf(stderr,"bf_contains <%s> : maybe\n",key); break ;
  default: fprintf(stderr,"bf_contains <%s> : error %d\n",key,retCode); break ;
  }
  return retCode;
}

///////////////////////////////////////////////
int destructor_bloom_bloomfilter()
///////////////////////////////////////////////
{
  // close a bloom bitmap 
  int retCode = bitmap_close(&myBloom_bitmap);
  if (retCode < 0 )
  {
    fprintf(stderr,"bitmap_close : error %d\n",retCode);
    return(-1);
  }
  fprintf(stderr,"bitmap_close : done\n");
  return 0 ;
}

///////////////////////////////////////////////
int main(int argc,char **argv)
///////////////////////////////////////////////
{
  fprintf(stderr,"hello bloom_bloomfilter\n");

  if ( constructor_bloom_bloomfilter("/home/revolle/bloomd/testfile/bloomd.foobar/data.000.mmap") != 0 )
    return(-1);
  ckeck_non_existence_bloom_bloomfilter("titi");
  ckeck_non_existence_bloom_bloomfilter("tutu");
  ckeck_non_existence_bloom_bloomfilter("tata");
  ckeck_non_existence_bloom_bloomfilter("toto");
  destructor_bloom_bloomfilter();
  return(0);
}