Additional Features

The SqliteExtDatabase accepts an initialization option to registersupport for a simple bloom filter.The bloom filter, once initialized, can then be used for efficient membershipqueries on large set of data.

Here’s an example:

  1. db = CSqliteExtDatabase(':memory:', bloomfilter=True)
  2.  
  3. # Create and define a table to store some data.
  4. db.execute_sql('CREATE TABLE "register" ("data" TEXT)')
  5. Register = Table('register', ('data',)).bind(db)
  6.  
  7. # Populate the database with a bunch of text.
  8. with db.atomic():
  9. for i in 'abcdefghijklmnopqrstuvwxyz':
  10. keys = [i * j for j in range(1, 10)] # a, aa, aaa, ... aaaaaaaaa
  11. Register.insert([{'data': key} for key in keys]).execute()
  12.  
  13. # Collect data into a 16KB bloomfilter.
  14. query = Register.select(fn.bloomfilter(Register.data, 16 * 1024).alias('buf'))
  15. row = query.get()
  16. buf = row['buf']
  17.  
  18. # Use bloomfilter buf to test whether other keys are members.
  19. test_keys = (
  20. ('aaaa', True),
  21. ('abc', False),
  22. ('zzzzzzz', True),
  23. ('zyxwvut', False))
  24. for key, is_present in test_keys:
  25. query = Register.select(fn.bloomfilter_contains(key, buf).alias('is_member'))
  26. answer = query.get()['is_member']
  27. assert answer == is_present

The SqliteExtDatabase can also register other useful functions:

  • rankfunctions (enabled by default): registers functions for rankingsearch results, such as _bm25 and lucene.
  • hash_functions: registers md5, sha1, sha256, adler32, crc32 andmurmurhash functions.
  • regexp_function: registers a regexp function.

Examples:

  1. def create_new_user(username, password):
  2. # DO NOT DO THIS IN REAL LIFE. PLEASE.
  3. query = User.insert({'username': username, 'password': fn.sha1(password)})
  4. new_user_id = query.execute()

You can use the murmurhash function to hash bytes to an integer for compactstorage:

  1. >>> db = SqliteExtDatabase(':memory:', hash_functions=True)
  2. >>> db.execute_sql('SELECT murmurhash(?)', ('abcdefg',)).fetchone()
  3. (4188131059,)