Permalink
Browse files

changed marshal #dump #load format to be size of in-memory bitmap

  • Loading branch information...
1 parent 163d0cd commit 1bf24f11b881bc21d8e7efc382da71f4dd8506a5 @aishfenton committed Dec 5, 2012
Showing with 16 additions and 10 deletions.
  1. +1 −0 .gitignore
  2. +4 −10 ext/cbloomfilter/cbloomfilter.c
  3. +11 −0 spec/native_spec.rb
View
@@ -7,3 +7,4 @@ ext/Makefile
lib/cbloomfilter.so
tmp
Gemfile.lock
+pkg
@@ -361,13 +361,11 @@ static VALUE bf_bitmap(VALUE self) {
struct BloomFilter *bf;
Data_Get_Struct(self, struct BloomFilter, bf);
- VALUE str = rb_str_new(0, bf->m);
+ VALUE str = rb_str_new(0, bf->bytes);
unsigned char* ptr = (unsigned char *) RSTRING_PTR(str);
- int i;
- for (i = 0; i < bf->m; i++)
- *ptr++ = bucket_get(bf, i);
-
+ memcpy(ptr, bf->ptr, bf->bytes);
+
return str;
}
@@ -376,11 +374,7 @@ static VALUE bf_load(VALUE self, VALUE bitmap) {
Data_Get_Struct(self, struct BloomFilter, bf);
unsigned char* ptr = (unsigned char *) RSTRING_PTR(bitmap);
- int i;
- for (i = 0; i < bf->m; i++) {
- if (*ptr++)
- bucket_set(bf, i);
- }
+ memcpy(bf->ptr, ptr, bf->bytes);
return Qnil;
}
View
@@ -140,5 +140,16 @@
bf.send(:same_parameters?, bf2).should be_true
end
+
+ it "should serialize to a file size proporational its bucket size" do
+ fs_size = 0
+ 8.times do |i|
+ bf = Native.new(size: 10_000, bucket: i+1)
+ bf.save('bf.out')
+ prev_size, fs_size = fs_size, File.size('bf.out')
+ prev_size.should < fs_size
+ end
+ end
+
end
end

0 comments on commit 1bf24f1

Please sign in to comment.