Permalink
Browse files

Initial check-in.

  • Loading branch information...
0 parents commit 040130738bdd7315736c8e9ae7ad735b9e4fccf3 Andrei Zmievski committed Mar 16, 2009
Showing with 718 additions and 0 deletions.
  1. +44 −0 .cvsignore
  2. +5 −0 EXPERIMENTAL
  3. +70 −0 README
  4. +138 −0 bloom.c
  5. +41 −0 bloom.h
  6. +112 −0 bloomy.dsp
  7. +29 −0 config.m4
  8. +8 −0 config.w32
  9. +62 −0 package.xml
  10. +115 −0 php_bloomy.c
  11. +94 −0 php_bloomy.h
@@ -0,0 +1,44 @@
+*.lo
+*.la
+.deps
+.libs
+Makefile
+Makefile.fragments
+Makefile.global
+Makefile.objects
+acinclude.m4
+aclocal.m4
+autom4te.cache
+build
+config.cache
+config.guess
+config.h
+config.h.in
+config.log
+config.nice
+config.status
+config.sub
+configure
+configure.in
+conftest
+conftest.c
+include
+install-sh
+libtool
+ltmain.sh
+missing
+mkinstalldirs
+modules
+scan_makefile_in.awk
+*.dsw
+*.plg
+*.opt
+*.ncb
+Release
+Release_inline
+Debug
+Release_TS
+Release_TSDbg
+Release_TS_inline
+Debug_TS
+bloomy*.tgz
@@ -0,0 +1,5 @@
+this extension is experimental,
+its functions may change their names
+or move to extension all together
+so do not rely to much on them
+you have been warned!
70 README
@@ -0,0 +1,70 @@
+This is a standalone PHP extension created using CodeGen_PECL 1.1.2
+
+HACKING
+=======
+
+There are two ways to modify an extension created using CodeGen_PECL:
+
+1) you can modify the generated code as with any other PHP extension
+
+2) you can add custom code to the CodeGen_PECL XML source and re-run pecl-gen
+
+The 2nd approach may look a bit complicated but you have be aware that any
+manual changes to the generated code will be lost if you ever change the
+XML specs and re-run PECL-Gen. All changes done before have to be applied
+to the newly generated code again.
+Adding code snippets to the XML source itself on the other hand may be a
+bit more complicated but this way your custom code will always be in the
+generated code no matter how often you rerun CodeGen_PECL.
+
+
+BUILDING ON UNIX etc.
+=====================
+
+To compile your new extension, you will have to execute the following steps:
+
+1. $ ./phpize
+2. $ ./configure [--enable--bloomy]
+3. $ make
+4. $ make test
+5. $ [sudo] make install
+
+
+
+BUILDING ON WINDOWS
+===================
+
+The extension provides the VisualStudio V6 project file
+
+ bloomy.dsp
+To compile the extension you open this file using VisualStudio,
+select the apropriate configuration for your installation
+(either "Release_TS" or "Debug_TS") and create "php_bloomy.dll"
+
+After successfull compilation you have to copy the newly
+created "bloomy.dll" to the PHP
+extension directory (default: C:\PHP\extensions).
+
+
+TESTING
+=======
+
+You can now load the extension using a php.ini directive
+
+ extension="bloomy.[so|dll]"
+
+or load it at runtime using the dl() function
+
+ dl("bloomy.[so|dll]");
+
+The extension should now be available, you can test this
+using the extension_loaded() function:
+
+ if (extension_loaded("bloomy"))
+ echo "bloomy loaded :)";
+ else
+ echo "something is wrong :(";
+
+The extension will also add its own block to the output
+of phpinfo();
+
138 bloom.c
@@ -0,0 +1,138 @@
+/*
+ +----------------------------------------------------------------------+
+ | Copyright (c) 2009 The PHP Group |
+ +----------------------------------------------------------------------+
+ | This source file is subject to version 3.0 of the PHP license, |
+ | that is bundled with this package in the file LICENSE, and is |
+ | available through the world-wide-web at the following url: |
+ | http://www.php.net/license/3_0.txt. |
+ | If you did not receive a copy of the PHP license and are unable to |
+ | obtain it through the world-wide-web, please send a note to |
+ | license@php.net so we can mail you a copy immediately. |
+ +----------------------------------------------------------------------+
+ | Authors: Andrei Zmievski <andrei@php.net> |
+ +----------------------------------------------------------------------+
+*/
+
+
+#include "bloom.h"
+#include <stdio.h>
+#include <string.h>
+#include <math.h>
+
+#define BITS_PER_CHAR 8
+#define MAX_HASHES 50
+
+struct _filter_spec_t {
+ size_t filter_size;
+ uint8_t num_hashes;
+};
+
+struct _bloom_t {
+ uint8_t *filter;
+ struct _filter_spec_t spec;
+
+ double max_error_rate;
+ size_t num_elements;
+
+ uint32_t salt1;
+ uint32_t salt2;
+};
+
+static void bloom_calc_salts(bloom_t *bloom)
+{
+ if (!bloom) {
+ return;
+ }
+}
+
+bloom_return bloom_init(bloom_t *bloom, size_t num_elements, double max_error_rate)
+{
+ filter_spec_t spec;
+ bloom_return status;
+
+ if (!bloom) {
+ return BLOOM_BADARG;
+ }
+
+ if (num_elements == 0 ||
+ num_elements > SIZE_MAX ||
+ max_error_rate <= 0.0 ||
+ max_error_rate >= 1.0) {
+
+ return BLOOM_BADARG;
+ }
+
+ memset(bloom, 0, sizeof(*bloom));
+
+ status = bloom_calc_optimal(&spec, num_elements, max_error_rate);
+ if (status != BLOOM_SUCCESS) {
+ return status;
+ }
+
+ bloom->filter = (uint8_t *)ecalloc(spec.filter_size / BITS_PER_CHAR, sizeof(uint8_t));
+ bloom->spec = spec;
+ bloom->max_error_rate = max_error_rate;
+
+ return BLOOM_SUCCESS;
+}
+
+void bloom_clean(bloom_t *bloom)
+{
+ if (!bloom) {
+ return BLOOM_BADARG;
+ }
+
+ efree(bloom->filter);
+}
+
+bloom_return bloom_add(bloom_t *bloom, const char *data, size_t data_len)
+{
+}
+
+bloom_return bloom_contains(bloom_t *bloom, const char *data, size_t data_len)
+{
+}
+
+bloom_return bloom_calc_optimal(filter_spec_t *spec, size_t num_elements, double max_error_rate)
+{
+ double h;
+ double curr_size = 0.0;
+ double opt_size = (double)SIZE_MAX;
+ double opt_h = 0.0;
+ size_t filter_size;
+ uint8_t num_hashes;
+
+ if (!spec) {
+ return BLOOM_FAILURE;
+ }
+
+ for (h = 0.0; h < (double)MAX_HASHES; h++) {
+ if ((curr_size = ((-h * num_elements) / log(1 - pow(max_error_rate, 1 / h)))) < opt_size) {
+ opt_size = curr_size;
+ opt_h = h;
+ }
+ }
+
+ if (opt_h == 0.0) {
+ return BLOOM_BAD_FILTER_SIZE;
+ }
+
+ filter_size = (size_t)opt_size;
+ filter_size += ((filter_size % BITS_PER_CHAR) > 0 ? BITS_PER_CHAR - (filter_size % BITS_PER_CHAR) : 0);
+ num_hashes = (size_t)opt_h;
+
+ spec->filter_size = filter_size;
+ spec->num_hashes = num_hashes;
+
+ return BLOOM_SUCCESS;
+}
+
+int main()
+{
+ filter_spec_t spec;
+
+ bloom_calc_optimal(&spec, 1000000, 0.01);
+ printf("filter size = %d\nhashes = %d\n", spec.filter_size, spec.num_hashes);
+ return 0;
+}
41 bloom.h
@@ -0,0 +1,41 @@
+/*
+ +----------------------------------------------------------------------+
+ | Copyright (c) 2009 The PHP Group |
+ +----------------------------------------------------------------------+
+ | This source file is subject to version 3.0 of the PHP license, |
+ | that is bundled with this package in the file LICENSE, and is |
+ | available through the world-wide-web at the following url: |
+ | http://www.php.net/license/3_0.txt. |
+ | If you did not receive a copy of the PHP license and are unable to |
+ | obtain it through the world-wide-web, please send a note to |
+ | license@php.net so we can mail you a copy immediately. |
+ +----------------------------------------------------------------------+
+ | Authors: Andrei Zmievski <andrei@php.net> |
+ +----------------------------------------------------------------------+
+*/
+
+#ifndef _BLOOM_H_
+#define _BLOOM_H_
+
+#include <stdlib.h>
+#include <stdint.h>
+
+typedef struct _bloom_t bloom_t;
+typedef struct _filter_spec_t filter_spec_t;
+
+typedef enum {
+ BLOOM_SUCCESS,
+ BLOOM_FAILURE,
+ BLOOM_BADARG,
+ BLOOM_BAD_FILTER_SIZE
+} bloom_return;
+
+bloom_return bloom_init(bloom_t *bloom, size_t num_elements, double max_error_rate);
+void bloom_clean(bloom_t *bloom);
+
+bloom_return bloom_add(bloom_t *bloom, const char *data, size_t data_len);
+bloom_return bloom_contains(bloom_t *blook, const char *data, size_t data_len);
+
+bloom_return bloom_calc_optimal(filter_spec_t *spec, size_t num_elements, double max_error_rate);
+
+#endif /* _BLOOM_H_ */
Oops, something went wrong.

0 comments on commit 0401307

Please sign in to comment.