tdb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +8 -0
- data/.gitignore +19 -0
- data/COPYING +165 -0
- data/GIT-VERSION-GEN +40 -0
- data/GNUmakefile +190 -0
- data/Hash_Functions +67 -0
- data/LICENSE +17 -0
- data/README +57 -0
- data/Rakefile +140 -0
- data/TODO +7 -0
- data/ext/tdb/djb.c +26 -0
- data/ext/tdb/extconf.rb +12 -0
- data/ext/tdb/fnv.c +28 -0
- data/ext/tdb/lookup3.c +429 -0
- data/ext/tdb/murmur1.c +151 -0
- data/ext/tdb/murmur2.c +290 -0
- data/ext/tdb/rbtdb.h +22 -0
- data/ext/tdb/tdb.c +690 -0
- data/lib/tdb.rb +2 -0
- data/setup.rb +1586 -0
- data/tdb.gemspec +36 -0
- data/test/test_tdb.rb +260 -0
- metadata +105 -0
data/LICENSE
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
Ruby tdb is copyrighted Free Software by all contributors, see logs in
|
2
|
+
revision control for names and email addresses of all of them.
|
3
|
+
|
4
|
+
This library is free software; you can redistribute it and/or modify it
|
5
|
+
under the terms of the GNU Lesser General Public License as published by
|
6
|
+
the Free Software Foundation; either version
|
7
|
+
{3}[http://www.gnu.org/licenses/lgpl-3.0.txt] of the License, or (at
|
8
|
+
your option) any later version.
|
9
|
+
|
10
|
+
Ruby tdb is distributed in the hope that it will be useful, but WITHOUT
|
11
|
+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
12
|
+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
13
|
+
License for more details.
|
14
|
+
|
15
|
+
You should have received a copy of the GNU Lesser General Public License
|
16
|
+
along with Ruby tdb; if not, write to the Free Software
|
17
|
+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
data/README
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
= tdb - Trivial Database bindings for Ruby
|
2
|
+
|
3
|
+
TDB is much like other DBM implementations, except it allows concurrent
|
4
|
+
writer processes. TDB was initially developed for Samba, but is used by
|
5
|
+
other projects as well. These Ruby bindings allow Ruby apps to read and
|
6
|
+
write to the same databases used by Samba!
|
7
|
+
|
8
|
+
== Features
|
9
|
+
|
10
|
+
* Concurrent reader and writer processes may safely operate on the
|
11
|
+
same file.
|
12
|
+
|
13
|
+
* Releases the GVL for slow disk operations under Ruby 1.9
|
14
|
+
|
15
|
+
* Includes several {hash functions}[link:Hash_Functions.html]
|
16
|
+
not included by upstream TDB.
|
17
|
+
|
18
|
+
== Install
|
19
|
+
|
20
|
+
The original tdb library from the {main site}[http://tdb.samba.org/] is
|
21
|
+
required. Debian users can just <code>apt-get install tdb-dev</code>.
|
22
|
+
|
23
|
+
The library consists of a C extension so you'll need a C compiler
|
24
|
+
and Ruby development libraries/headers.
|
25
|
+
|
26
|
+
You may download the tarball from our
|
27
|
+
{download site}[http://bogomips.org/ruby-tdb/files/] and run setup.rb after
|
28
|
+
unpacking it:
|
29
|
+
|
30
|
+
http://bogomips.org/ruby-tdb/files/
|
31
|
+
|
32
|
+
You may also install it via RubyGems on RubyGems.org:
|
33
|
+
|
34
|
+
gem install tdb
|
35
|
+
|
36
|
+
You can get the latest source via git from the following locations
|
37
|
+
(these versions may not be stable):
|
38
|
+
|
39
|
+
git://git.bogomips.org/ruby-tdb.git
|
40
|
+
git://repo.or.cz/ruby-tdb.git (mirror)
|
41
|
+
|
42
|
+
You may browse the code from the web and download the latest snapshot
|
43
|
+
tarballs here:
|
44
|
+
|
45
|
+
* http://git.bogomips.org/cgit/ruby-tdb.git (cgit)
|
46
|
+
* http://repo.or.cz/w/ruby-tdb.git (gitweb)
|
47
|
+
|
48
|
+
See the HACKING guide on how to contribute and build prerelease gems
|
49
|
+
from git.
|
50
|
+
|
51
|
+
== Contact
|
52
|
+
|
53
|
+
All feedback (bug reports, user/development dicussion, patches, pull
|
54
|
+
requests) go to the {mailing list}[mailto:ruby.tdb@librelist.org].
|
55
|
+
|
56
|
+
For the latest on tdb releases, you may check our NEWS page (and
|
57
|
+
subscribe to our Atom feed).
|
data/Rakefile
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
# -*- encoding: binary -*-
|
2
|
+
|
3
|
+
# most tasks are in the GNUmakefile which offers better parallelism
|
4
|
+
|
5
|
+
def tags
|
6
|
+
timefmt = '%Y-%m-%dT%H:%M:%SZ'
|
7
|
+
@tags ||= `git tag -l`.split(/\n/).map do |tag|
|
8
|
+
if %r{\Av[\d\.]+} =~ tag
|
9
|
+
header, subject, body = `git cat-file tag #{tag}`.split(/\n\n/, 3)
|
10
|
+
header = header.split(/\n/)
|
11
|
+
tagger = header.grep(/\Atagger /).first
|
12
|
+
body ||= "initial"
|
13
|
+
{
|
14
|
+
:time => Time.at(tagger.split(/ /)[-2].to_i).utc.strftime(timefmt),
|
15
|
+
:tagger_name => %r{^tagger ([^<]+)}.match(tagger)[1].strip,
|
16
|
+
:tagger_email => %r{<([^>]+)>}.match(tagger)[1].strip,
|
17
|
+
:id => `git rev-parse refs/tags/#{tag}`.chomp!,
|
18
|
+
:tag => tag,
|
19
|
+
:subject => subject,
|
20
|
+
:body => body,
|
21
|
+
}
|
22
|
+
end
|
23
|
+
end.compact.sort { |a,b| b[:time] <=> a[:time] }
|
24
|
+
end
|
25
|
+
|
26
|
+
cgit_url = "http://git.bogomips.org/cgit/ruby-tdb.git"
|
27
|
+
git_url = ENV['GIT_URL'] || 'git://git.bogomips.org/ruby-tdb.git'
|
28
|
+
web_url = "http://bogomips.org/ruby-tdb/"
|
29
|
+
|
30
|
+
desc 'prints news as an Atom feed'
|
31
|
+
task :news_atom do
|
32
|
+
require 'nokogiri'
|
33
|
+
new_tags = tags[0,10]
|
34
|
+
puts(Nokogiri::XML::Builder.new do
|
35
|
+
feed :xmlns => "http://www.w3.org/2005/Atom" do
|
36
|
+
id! "#{web_url}NEWS.atom.xml"
|
37
|
+
title "Ruby tdb news"
|
38
|
+
subtitle "Trivial Database bindings for Ruby"
|
39
|
+
link! :rel => "alternate", :type => "text/html",
|
40
|
+
:href => "#{web_url}NEWS.html"
|
41
|
+
updated(new_tags.empty? ? "1970-01-01T00:00:00Z" : new_tags.first[:time])
|
42
|
+
new_tags.each do |tag|
|
43
|
+
entry do
|
44
|
+
title tag[:subject]
|
45
|
+
updated tag[:time]
|
46
|
+
published tag[:time]
|
47
|
+
author {
|
48
|
+
name tag[:tagger_name]
|
49
|
+
email tag[:tagger_email]
|
50
|
+
}
|
51
|
+
url = "#{cgit_url}/tag/?id=#{tag[:tag]}"
|
52
|
+
link! :rel => "alternate", :type => "text/html", :href =>url
|
53
|
+
id! url
|
54
|
+
message_only = tag[:body].split(/\n.+\(\d+\):\n {6}/s).first.strip
|
55
|
+
content({:type =>:text}, message_only)
|
56
|
+
content(:type =>:xhtml) { pre tag[:body] }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end.to_xml)
|
61
|
+
end
|
62
|
+
|
63
|
+
desc 'prints RDoc-formatted news'
|
64
|
+
task :news_rdoc do
|
65
|
+
tags.each do |tag|
|
66
|
+
time = tag[:time].tr!('T', ' ').gsub!(/:\d\dZ/, ' UTC')
|
67
|
+
puts "=== #{tag[:tag].sub(/^v/, '')} / #{time}"
|
68
|
+
puts ""
|
69
|
+
|
70
|
+
body = tag[:body]
|
71
|
+
puts tag[:body].gsub(/^/sm, " ").gsub(/[ \t]+$/sm, "")
|
72
|
+
puts ""
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
desc "print release changelog for Rubyforge"
|
77
|
+
task :release_changes do
|
78
|
+
version = ENV['VERSION'] or abort "VERSION= needed"
|
79
|
+
version = "v#{version}"
|
80
|
+
vtags = tags.map { |tag| tag[:tag] =~ /\Av/ and tag[:tag] }.sort
|
81
|
+
prev = vtags[vtags.index(version) - 1]
|
82
|
+
if prev
|
83
|
+
system('git', 'diff', '--stat', prev, version) or abort $?
|
84
|
+
puts ""
|
85
|
+
system('git', 'log', "#{prev}..#{version}") or abort $?
|
86
|
+
else
|
87
|
+
system('git', 'log', version) or abort $?
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
desc "print release notes for Rubyforge"
|
92
|
+
task :release_notes do
|
93
|
+
spec = Gem::Specification.load('tdb.gemspec')
|
94
|
+
puts spec.description.strip
|
95
|
+
puts ""
|
96
|
+
puts "* #{spec.homepage}"
|
97
|
+
puts "* #{spec.email}"
|
98
|
+
puts "* #{git_url}"
|
99
|
+
|
100
|
+
_, _, body = `git cat-file tag v#{spec.version}`.split(/\n\n/, 3)
|
101
|
+
print "\nChanges:\n\n"
|
102
|
+
puts body
|
103
|
+
end
|
104
|
+
|
105
|
+
desc "post to RAA"
|
106
|
+
task :raa_update do
|
107
|
+
require 'net/http'
|
108
|
+
require 'net/netrc'
|
109
|
+
rc = Net::Netrc.locate('tdb-raa') or abort "~/.netrc not found"
|
110
|
+
password = rc.password
|
111
|
+
|
112
|
+
s = Gem::Specification.load('tdb.gemspec')
|
113
|
+
desc = [ s.description.strip ]
|
114
|
+
desc << ""
|
115
|
+
desc << "* #{s.email}"
|
116
|
+
desc << "* #{git_url}"
|
117
|
+
desc << "* #{cgit_url}"
|
118
|
+
desc = desc.join("\n")
|
119
|
+
uri = URI.parse('http://raa.ruby-lang.org/regist.rhtml')
|
120
|
+
form = {
|
121
|
+
:name => s.name,
|
122
|
+
:short_description => s.summary,
|
123
|
+
:version => s.version.to_s,
|
124
|
+
:status => 'experimental',
|
125
|
+
:owner => s.authors.first,
|
126
|
+
:email => s.email,
|
127
|
+
:category_major => 'Library',
|
128
|
+
:category_minor => 'Database',
|
129
|
+
:url => s.homepage,
|
130
|
+
:download => 'http://bogomips.org/ruby-tdb/files/',
|
131
|
+
:license => "LGPL",
|
132
|
+
:description_style => 'Plain',
|
133
|
+
:description => desc,
|
134
|
+
:pass => password,
|
135
|
+
:submit => 'Update',
|
136
|
+
}
|
137
|
+
res = Net::HTTP.post_form(uri, form)
|
138
|
+
p res
|
139
|
+
puts res.body
|
140
|
+
end
|
data/TODO
ADDED
data/ext/tdb/djb.c
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#include "rbtdb.h"
|
2
|
+
|
3
|
+
unsigned int rbtdb_djb2(TDB_DATA *data)
|
4
|
+
{
|
5
|
+
unsigned char *key = data->dptr;
|
6
|
+
size_t len = data->dsize;
|
7
|
+
unsigned int hash = 5381;
|
8
|
+
unsigned int i;
|
9
|
+
|
10
|
+
for (i = 0; i < len; ++i)
|
11
|
+
hash = ((hash << 5) + hash) + key[i]; /* (hash*33) + key[i] */
|
12
|
+
|
13
|
+
return hash;
|
14
|
+
}
|
15
|
+
unsigned int rbtdb_djb3(TDB_DATA *data)
|
16
|
+
{
|
17
|
+
unsigned char *key = data->dptr;
|
18
|
+
size_t len = data->dsize;
|
19
|
+
unsigned int hash = 5381;
|
20
|
+
unsigned int i;
|
21
|
+
|
22
|
+
for (i = 0; i < len; ++i)
|
23
|
+
hash = ((hash << 5) + hash) ^ key[i]; /* (hash*33) ^ key[i] */
|
24
|
+
|
25
|
+
return hash;
|
26
|
+
}
|
data/ext/tdb/extconf.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'mkmf'
|
2
|
+
|
3
|
+
have_func('rb_thread_blocking_region')
|
4
|
+
have_func('rb_thread_call_with_gvl')
|
5
|
+
|
6
|
+
dir_config('tdb')
|
7
|
+
have_header('tdb.h') or abort 'tdb.h missing'
|
8
|
+
have_library('tdb') or abort 'libtdb missing'
|
9
|
+
have_func('tdb_jenkins_hash')
|
10
|
+
have_const('TDB_ERR_NESTING', 'tdb.h')
|
11
|
+
|
12
|
+
create_makefile('tdb_ext')
|
data/ext/tdb/fnv.c
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
#include "rbtdb.h"
|
2
|
+
|
3
|
+
#define FNV1A_32A_INIT (unsigned int)0x811c9dc5
|
4
|
+
#define FNV_32_PRIME (unsigned int)0x01000193
|
5
|
+
|
6
|
+
unsigned int rbtdb_fnv1a(TDB_DATA * data)
|
7
|
+
{
|
8
|
+
unsigned char *bp = data->dptr;
|
9
|
+
unsigned char *be = bp + data->dsize;
|
10
|
+
unsigned int h = FNV1A_32A_INIT;
|
11
|
+
|
12
|
+
/* FNV-1a hash each octet in the buffer */
|
13
|
+
while (bp < be) {
|
14
|
+
|
15
|
+
/* xor the bottom with the current octet */
|
16
|
+
h ^= (unsigned)*bp++;
|
17
|
+
|
18
|
+
/* multiply by the 32 bit FNV magic prime mod 2^32 */
|
19
|
+
#if defined(NO_FNV_GCC_OPTIMIZATION)
|
20
|
+
h *= FNV_32_PRIME;
|
21
|
+
#else
|
22
|
+
h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24);
|
23
|
+
#endif
|
24
|
+
}
|
25
|
+
|
26
|
+
/* return our new hash value */
|
27
|
+
return h;
|
28
|
+
}
|
data/ext/tdb/lookup3.c
ADDED
@@ -0,0 +1,429 @@
|
|
1
|
+
#include "rbtdb.h"
|
2
|
+
|
3
|
+
/*
|
4
|
+
* lookup3 implementation copied from tdb.git
|
5
|
+
* (commit 3258cf3f11bf7c68a2e69e1808c4551cc899725a),
|
6
|
+
* as that tdb distribution isn't commonly available yet (as of 2010.11.29)
|
7
|
+
*/
|
8
|
+
#ifndef HAVE_TDB_JENKINS_HASH
|
9
|
+
|
10
|
+
#ifndef WORDS_BIGENDIAN
|
11
|
+
# define HASH_LITTLE_ENDIAN 1
|
12
|
+
# define HASH_BIG_ENDIAN 0
|
13
|
+
#else
|
14
|
+
# define HASH_LITTLE_ENDIAN 0
|
15
|
+
# define HASH_BIG_ENDIAN 1
|
16
|
+
#endif
|
17
|
+
|
18
|
+
/*
|
19
|
+
-------------------------------------------------------------------------------
|
20
|
+
lookup3.c, by Bob Jenkins, May 2006, Public Domain.
|
21
|
+
|
22
|
+
These are functions for producing 32-bit hashes for hash table lookup.
|
23
|
+
hash_word(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
|
24
|
+
are externally useful functions. Routines to test the hash are included
|
25
|
+
if SELF_TEST is defined. You can use this free for any purpose. It's in
|
26
|
+
the public domain. It has no warranty.
|
27
|
+
|
28
|
+
You probably want to use hashlittle(). hashlittle() and hashbig()
|
29
|
+
hash byte arrays. hashlittle() is is faster than hashbig() on
|
30
|
+
little-endian machines. Intel and AMD are little-endian machines.
|
31
|
+
On second thought, you probably want hashlittle2(), which is identical to
|
32
|
+
hashlittle() except it returns two 32-bit hashes for the price of one.
|
33
|
+
You could implement hashbig2() if you wanted but I haven't bothered here.
|
34
|
+
|
35
|
+
If you want to find a hash of, say, exactly 7 integers, do
|
36
|
+
a = i1; b = i2; c = i3;
|
37
|
+
mix(a,b,c);
|
38
|
+
a += i4; b += i5; c += i6;
|
39
|
+
mix(a,b,c);
|
40
|
+
a += i7;
|
41
|
+
final(a,b,c);
|
42
|
+
then use c as the hash value. If you have a variable length array of
|
43
|
+
4-byte integers to hash, use hash_word(). If you have a byte array (like
|
44
|
+
a character string), use hashlittle(). If you have several byte arrays, or
|
45
|
+
a mix of things, see the comments above hashlittle().
|
46
|
+
|
47
|
+
Why is this so big? I read 12 bytes at a time into 3 4-byte integers,
|
48
|
+
then mix those integers. This is fast (you can do a lot more thorough
|
49
|
+
mixing with 12*3 instructions on 3 integers than you can with 3 instructions
|
50
|
+
on 1 byte), but shoehorning those bytes into integers efficiently is messy.
|
51
|
+
*/
|
52
|
+
|
53
|
+
#define hashsize(n) ((uint32_t)1<<(n))
|
54
|
+
#define hashmask(n) (hashsize(n)-1)
|
55
|
+
#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
|
56
|
+
|
57
|
+
/*
|
58
|
+
-------------------------------------------------------------------------------
|
59
|
+
mix -- mix 3 32-bit values reversibly.
|
60
|
+
|
61
|
+
This is reversible, so any information in (a,b,c) before mix() is
|
62
|
+
still in (a,b,c) after mix().
|
63
|
+
|
64
|
+
If four pairs of (a,b,c) inputs are run through mix(), or through
|
65
|
+
mix() in reverse, there are at least 32 bits of the output that
|
66
|
+
are sometimes the same for one pair and different for another pair.
|
67
|
+
This was tested for:
|
68
|
+
* pairs that differed by one bit, by two bits, in any combination
|
69
|
+
of top bits of (a,b,c), or in any combination of bottom bits of
|
70
|
+
(a,b,c).
|
71
|
+
* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
|
72
|
+
the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
|
73
|
+
is commonly produced by subtraction) look like a single 1-bit
|
74
|
+
difference.
|
75
|
+
* the base values were pseudorandom, all zero but one bit set, or
|
76
|
+
all zero plus a counter that starts at zero.
|
77
|
+
|
78
|
+
Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
|
79
|
+
satisfy this are
|
80
|
+
4 6 8 16 19 4
|
81
|
+
9 15 3 18 27 15
|
82
|
+
14 9 3 7 17 3
|
83
|
+
Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
|
84
|
+
for "differ" defined as + with a one-bit base and a two-bit delta. I
|
85
|
+
used http://burtleburtle.net/bob/hash/avalanche.html to choose
|
86
|
+
the operations, constants, and arrangements of the variables.
|
87
|
+
|
88
|
+
This does not achieve avalanche. There are input bits of (a,b,c)
|
89
|
+
that fail to affect some output bits of (a,b,c), especially of a. The
|
90
|
+
most thoroughly mixed value is c, but it doesn't really even achieve
|
91
|
+
avalanche in c.
|
92
|
+
|
93
|
+
This allows some parallelism. Read-after-writes are good at doubling
|
94
|
+
the number of bits affected, so the goal of mixing pulls in the opposite
|
95
|
+
direction as the goal of parallelism. I did what I could. Rotates
|
96
|
+
seem to cost as much as shifts on every machine I could lay my hands
|
97
|
+
on, and rotates are much kinder to the top and bottom bits, so I used
|
98
|
+
rotates.
|
99
|
+
-------------------------------------------------------------------------------
|
100
|
+
*/
|
101
|
+
#define mix(a,b,c) \
|
102
|
+
{ \
|
103
|
+
a -= c; a ^= rot(c, 4); c += b; \
|
104
|
+
b -= a; b ^= rot(a, 6); a += c; \
|
105
|
+
c -= b; c ^= rot(b, 8); b += a; \
|
106
|
+
a -= c; a ^= rot(c,16); c += b; \
|
107
|
+
b -= a; b ^= rot(a,19); a += c; \
|
108
|
+
c -= b; c ^= rot(b, 4); b += a; \
|
109
|
+
}
|
110
|
+
|
111
|
+
/*
|
112
|
+
-------------------------------------------------------------------------------
|
113
|
+
final -- final mixing of 3 32-bit values (a,b,c) into c
|
114
|
+
|
115
|
+
Pairs of (a,b,c) values differing in only a few bits will usually
|
116
|
+
produce values of c that look totally different. This was tested for
|
117
|
+
* pairs that differed by one bit, by two bits, in any combination
|
118
|
+
of top bits of (a,b,c), or in any combination of bottom bits of
|
119
|
+
(a,b,c).
|
120
|
+
* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
|
121
|
+
the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
|
122
|
+
is commonly produced by subtraction) look like a single 1-bit
|
123
|
+
difference.
|
124
|
+
* the base values were pseudorandom, all zero but one bit set, or
|
125
|
+
all zero plus a counter that starts at zero.
|
126
|
+
|
127
|
+
These constants passed:
|
128
|
+
14 11 25 16 4 14 24
|
129
|
+
12 14 25 16 4 14 24
|
130
|
+
and these came close:
|
131
|
+
4 8 15 26 3 22 24
|
132
|
+
10 8 15 26 3 22 24
|
133
|
+
11 8 15 26 3 22 24
|
134
|
+
-------------------------------------------------------------------------------
|
135
|
+
*/
|
136
|
+
#define final(a,b,c) \
|
137
|
+
{ \
|
138
|
+
c ^= b; c -= rot(b,14); \
|
139
|
+
a ^= c; a -= rot(c,11); \
|
140
|
+
b ^= a; b -= rot(a,25); \
|
141
|
+
c ^= b; c -= rot(b,16); \
|
142
|
+
a ^= c; a -= rot(c,4); \
|
143
|
+
b ^= a; b -= rot(a,14); \
|
144
|
+
c ^= b; c -= rot(b,24); \
|
145
|
+
}
|
146
|
+
|
147
|
+
/*
|
148
|
+
-------------------------------------------------------------------------------
|
149
|
+
hashlittle() -- hash a variable-length key into a 32-bit value
|
150
|
+
k : the key (the unaligned variable-length array of bytes)
|
151
|
+
length : the length of the key, counting by bytes
|
152
|
+
val2 : IN: can be any 4-byte value OUT: second 32 bit hash.
|
153
|
+
Returns a 32-bit value. Every bit of the key affects every bit of
|
154
|
+
the return value. Two keys differing by one or two bits will have
|
155
|
+
totally different hash values. Note that the return value is better
|
156
|
+
mixed than val2, so use that first.
|
157
|
+
|
158
|
+
The best hash table sizes are powers of 2. There is no need to do
|
159
|
+
mod a prime (mod is sooo slow!). If you need less than 32 bits,
|
160
|
+
use a bitmask. For example, if you need only 10 bits, do
|
161
|
+
h = (h & hashmask(10));
|
162
|
+
In which case, the hash table should have hashsize(10) elements.
|
163
|
+
|
164
|
+
If you are hashing n strings (uint8_t **)k, do it like this:
|
165
|
+
for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
|
166
|
+
|
167
|
+
By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
|
168
|
+
code any way you wish, private, educational, or commercial. It's free.
|
169
|
+
|
170
|
+
Use for hash table lookup, or anything where one collision in 2^^32 is
|
171
|
+
acceptable. Do NOT use for cryptographic purposes.
|
172
|
+
-------------------------------------------------------------------------------
|
173
|
+
*/
|
174
|
+
|
175
|
+
static uint32_t hashlittle(const void *key, size_t length)
|
176
|
+
{
|
177
|
+
uint32_t a, b, c; /* internal state */
|
178
|
+
union {
|
179
|
+
const void *ptr;
|
180
|
+
size_t i;
|
181
|
+
} u; /* needed for Mac Powerbook G4 */
|
182
|
+
|
183
|
+
/* Set up the internal state */
|
184
|
+
a = b = c = 0xdeadbeef + ((uint32_t) length);
|
185
|
+
|
186
|
+
u.ptr = key;
|
187
|
+
if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
|
188
|
+
const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
|
189
|
+
#ifdef VALGRIND
|
190
|
+
const uint8_t *k8;
|
191
|
+
#endif
|
192
|
+
|
193
|
+
/*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
|
194
|
+
while (length > 12) {
|
195
|
+
a += k[0];
|
196
|
+
b += k[1];
|
197
|
+
c += k[2];
|
198
|
+
mix(a, b, c);
|
199
|
+
length -= 12;
|
200
|
+
k += 3;
|
201
|
+
}
|
202
|
+
|
203
|
+
/*----------------------------- handle the last (probably partial) block */
|
204
|
+
/*
|
205
|
+
* "k[2]&0xffffff" actually reads beyond the end of the string, but
|
206
|
+
* then masks off the part it's not allowed to read. Because the
|
207
|
+
* string is aligned, the masked-off tail is in the same word as the
|
208
|
+
* rest of the string. Every machine with memory protection I've seen
|
209
|
+
* does it on word boundaries, so is OK with this. But VALGRIND will
|
210
|
+
* still catch it and complain. The masking trick does make the hash
|
211
|
+
* noticably faster for short strings (like English words).
|
212
|
+
*/
|
213
|
+
#ifndef VALGRIND
|
214
|
+
|
215
|
+
switch (length) {
|
216
|
+
case 12:
|
217
|
+
c += k[2];
|
218
|
+
b += k[1];
|
219
|
+
a += k[0];
|
220
|
+
break;
|
221
|
+
case 11:
|
222
|
+
c += k[2] & 0xffffff;
|
223
|
+
b += k[1];
|
224
|
+
a += k[0];
|
225
|
+
break;
|
226
|
+
case 10:
|
227
|
+
c += k[2] & 0xffff;
|
228
|
+
b += k[1];
|
229
|
+
a += k[0];
|
230
|
+
break;
|
231
|
+
case 9:
|
232
|
+
c += k[2] & 0xff;
|
233
|
+
b += k[1];
|
234
|
+
a += k[0];
|
235
|
+
break;
|
236
|
+
case 8:
|
237
|
+
b += k[1];
|
238
|
+
a += k[0];
|
239
|
+
break;
|
240
|
+
case 7:
|
241
|
+
b += k[1] & 0xffffff;
|
242
|
+
a += k[0];
|
243
|
+
break;
|
244
|
+
case 6:
|
245
|
+
b += k[1] & 0xffff;
|
246
|
+
a += k[0];
|
247
|
+
break;
|
248
|
+
case 5:
|
249
|
+
b += k[1] & 0xff;
|
250
|
+
a += k[0];
|
251
|
+
break;
|
252
|
+
case 4:
|
253
|
+
a += k[0];
|
254
|
+
break;
|
255
|
+
case 3:
|
256
|
+
a += k[0] & 0xffffff;
|
257
|
+
break;
|
258
|
+
case 2:
|
259
|
+
a += k[0] & 0xffff;
|
260
|
+
break;
|
261
|
+
case 1:
|
262
|
+
a += k[0] & 0xff;
|
263
|
+
break;
|
264
|
+
case 0:
|
265
|
+
return c; /* zero length strings require no mixing */
|
266
|
+
}
|
267
|
+
|
268
|
+
#else /* make valgrind happy */
|
269
|
+
|
270
|
+
k8 = (const uint8_t *)k;
|
271
|
+
switch (length) {
|
272
|
+
case 12:
|
273
|
+
c += k[2];
|
274
|
+
b += k[1];
|
275
|
+
a += k[0];
|
276
|
+
break;
|
277
|
+
case 11:
|
278
|
+
c += ((uint32_t) k8[10]) << 16; /* fall through */
|
279
|
+
case 10:
|
280
|
+
c += ((uint32_t) k8[9]) << 8; /* fall through */
|
281
|
+
case 9:
|
282
|
+
c += k8[8]; /* fall through */
|
283
|
+
case 8:
|
284
|
+
b += k[1];
|
285
|
+
a += k[0];
|
286
|
+
break;
|
287
|
+
case 7:
|
288
|
+
b += ((uint32_t) k8[6]) << 16; /* fall through */
|
289
|
+
case 6:
|
290
|
+
b += ((uint32_t) k8[5]) << 8; /* fall through */
|
291
|
+
case 5:
|
292
|
+
b += k8[4]; /* fall through */
|
293
|
+
case 4:
|
294
|
+
a += k[0];
|
295
|
+
break;
|
296
|
+
case 3:
|
297
|
+
a += ((uint32_t) k8[2]) << 16; /* fall through */
|
298
|
+
case 2:
|
299
|
+
a += ((uint32_t) k8[1]) << 8; /* fall through */
|
300
|
+
case 1:
|
301
|
+
a += k8[0];
|
302
|
+
break;
|
303
|
+
case 0:
|
304
|
+
return c;
|
305
|
+
}
|
306
|
+
|
307
|
+
#endif /* !valgrind */
|
308
|
+
|
309
|
+
} else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
|
310
|
+
const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
|
311
|
+
const uint8_t *k8;
|
312
|
+
|
313
|
+
/*--------------- all but last block: aligned reads and different mixing */
|
314
|
+
while (length > 12) {
|
315
|
+
a += k[0] + (((uint32_t) k[1]) << 16);
|
316
|
+
b += k[2] + (((uint32_t) k[3]) << 16);
|
317
|
+
c += k[4] + (((uint32_t) k[5]) << 16);
|
318
|
+
mix(a, b, c);
|
319
|
+
length -= 12;
|
320
|
+
k += 6;
|
321
|
+
}
|
322
|
+
|
323
|
+
/*----------------------------- handle the last (probably partial) block */
|
324
|
+
k8 = (const uint8_t *)k;
|
325
|
+
switch (length) {
|
326
|
+
case 12:
|
327
|
+
c += k[4] + (((uint32_t) k[5]) << 16);
|
328
|
+
b += k[2] + (((uint32_t) k[3]) << 16);
|
329
|
+
a += k[0] + (((uint32_t) k[1]) << 16);
|
330
|
+
break;
|
331
|
+
case 11:
|
332
|
+
c += ((uint32_t) k8[10]) << 16; /* fall through */
|
333
|
+
case 10:
|
334
|
+
c += k[4];
|
335
|
+
b += k[2] + (((uint32_t) k[3]) << 16);
|
336
|
+
a += k[0] + (((uint32_t) k[1]) << 16);
|
337
|
+
break;
|
338
|
+
case 9:
|
339
|
+
c += k8[8]; /* fall through */
|
340
|
+
case 8:
|
341
|
+
b += k[2] + (((uint32_t) k[3]) << 16);
|
342
|
+
a += k[0] + (((uint32_t) k[1]) << 16);
|
343
|
+
break;
|
344
|
+
case 7:
|
345
|
+
b += ((uint32_t) k8[6]) << 16; /* fall through */
|
346
|
+
case 6:
|
347
|
+
b += k[2];
|
348
|
+
a += k[0] + (((uint32_t) k[1]) << 16);
|
349
|
+
break;
|
350
|
+
case 5:
|
351
|
+
b += k8[4]; /* fall through */
|
352
|
+
case 4:
|
353
|
+
a += k[0] + (((uint32_t) k[1]) << 16);
|
354
|
+
break;
|
355
|
+
case 3:
|
356
|
+
a += ((uint32_t) k8[2]) << 16; /* fall through */
|
357
|
+
case 2:
|
358
|
+
a += k[0];
|
359
|
+
break;
|
360
|
+
case 1:
|
361
|
+
a += k8[0];
|
362
|
+
break;
|
363
|
+
case 0:
|
364
|
+
return c; /* zero length requires no mixing */
|
365
|
+
}
|
366
|
+
|
367
|
+
} else { /* need to read the key one byte at a time */
|
368
|
+
const uint8_t *k = (const uint8_t *)key;
|
369
|
+
|
370
|
+
/*--------------- all but the last block: affect some 32 bits of (a,b,c) */
|
371
|
+
while (length > 12) {
|
372
|
+
a += k[0];
|
373
|
+
a += ((uint32_t) k[1]) << 8;
|
374
|
+
a += ((uint32_t) k[2]) << 16;
|
375
|
+
a += ((uint32_t) k[3]) << 24;
|
376
|
+
b += k[4];
|
377
|
+
b += ((uint32_t) k[5]) << 8;
|
378
|
+
b += ((uint32_t) k[6]) << 16;
|
379
|
+
b += ((uint32_t) k[7]) << 24;
|
380
|
+
c += k[8];
|
381
|
+
c += ((uint32_t) k[9]) << 8;
|
382
|
+
c += ((uint32_t) k[10]) << 16;
|
383
|
+
c += ((uint32_t) k[11]) << 24;
|
384
|
+
mix(a, b, c);
|
385
|
+
length -= 12;
|
386
|
+
k += 12;
|
387
|
+
}
|
388
|
+
|
389
|
+
/*-------------------------------- last block: affect all 32 bits of (c) */
|
390
|
+
switch (length) { /* all the case statements fall through */
|
391
|
+
case 12:
|
392
|
+
c += ((uint32_t) k[11]) << 24;
|
393
|
+
case 11:
|
394
|
+
c += ((uint32_t) k[10]) << 16;
|
395
|
+
case 10:
|
396
|
+
c += ((uint32_t) k[9]) << 8;
|
397
|
+
case 9:
|
398
|
+
c += k[8];
|
399
|
+
case 8:
|
400
|
+
b += ((uint32_t) k[7]) << 24;
|
401
|
+
case 7:
|
402
|
+
b += ((uint32_t) k[6]) << 16;
|
403
|
+
case 6:
|
404
|
+
b += ((uint32_t) k[5]) << 8;
|
405
|
+
case 5:
|
406
|
+
b += k[4];
|
407
|
+
case 4:
|
408
|
+
a += ((uint32_t) k[3]) << 24;
|
409
|
+
case 3:
|
410
|
+
a += ((uint32_t) k[2]) << 16;
|
411
|
+
case 2:
|
412
|
+
a += ((uint32_t) k[1]) << 8;
|
413
|
+
case 1:
|
414
|
+
a += k[0];
|
415
|
+
break;
|
416
|
+
case 0:
|
417
|
+
return c;
|
418
|
+
}
|
419
|
+
}
|
420
|
+
|
421
|
+
final(a, b, c);
|
422
|
+
return c;
|
423
|
+
}
|
424
|
+
|
425
|
+
unsigned int rbtdb_jenkins_lookup3(TDB_DATA * key)
|
426
|
+
{
|
427
|
+
return hashlittle(key->dptr, key->dsize);
|
428
|
+
}
|
429
|
+
#endif /* !HAVE_TDB_JENKINS_HASH */
|