ninjudd-bdb 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bdb.gemspec +2 -1
  2. data/lib/bdb/simple.rb +75 -0
  3. metadata +2 -1
data/bdb.gemspec CHANGED
@@ -2,7 +2,7 @@ BDB_SPEC = Gem::Specification.new do |s|
2
2
  s.platform = Gem::Platform::RUBY
3
3
  s.required_ruby_version = '>=1.8.4'
4
4
  s.name = "bdb"
5
- s.version = "0.0.3"
5
+ s.version = "0.0.4"
6
6
  s.authors = ["Matt Bauer", "Dan Janowski"]
7
7
  s.email = "bauer@pedalbrain.com"
8
8
  s.summary = "A Ruby interface to BerkeleyDB"
@@ -10,6 +10,7 @@ BDB_SPEC = Gem::Specification.new do |s|
10
10
  'ext/bdb.c',
11
11
  'ext/bdb.h',
12
12
  'ext/extconf.rb',
13
+ 'lib/bdb/simple.rb',
13
14
  'LICENSE',
14
15
  'README.textile',
15
16
  'Rakefile']
data/lib/bdb/simple.rb ADDED
@@ -0,0 +1,75 @@
1
+ require 'bdb'
2
+
3
+ class Bdb::Simple
4
+ include Enumerable
5
+
6
+ def initialize(file, opts = {})
7
+ @dup = opts[:dup]
8
+ @file = file
9
+ end
10
+
11
+ def dup?
12
+ @dup
13
+ end
14
+
15
+ attr_reader :file
16
+
17
+ def db
18
+ if @db.nil?
19
+ @db = Bdb::Db.new
20
+ @db.flags = Bdb::DB_DUPSORT if dup?
21
+ @db.btree_compare = lambda do |db, key1, key2|
22
+ Marshal.load(key1) <=> Marshal.load(key2)
23
+ end
24
+ @db.open(nil, file, nil, Bdb::Db::BTREE, Bdb::DB_CREATE, 0)
25
+ end
26
+ @db
27
+ end
28
+
29
+ def []=(key, value)
30
+ db[Marshal.dump(key)] = Marshal.dump(value)
31
+ end
32
+
33
+ def [](key)
34
+ key = Marshal.dump(key)
35
+
36
+ if dup?
37
+ values = []
38
+ cursor = db.cursor(nil, 0)
39
+ data = cursor.get(key, nil, Bdb::DB_SET)
40
+ while data
41
+ values << Marshal.load(data[1])
42
+ data = cursor.get(nil, nil, Bdb::DB_NEXT_DUP)
43
+ end
44
+ cursor.close
45
+ values
46
+ else
47
+ Marshal.load(db.get(nil, key, nil, 0))
48
+ end
49
+ end
50
+
51
+ def each
52
+ cursor = db.cursor(nil, 0)
53
+ while data = cursor.get(nil, nil, Bdb::DB_NEXT)
54
+ key = Marshal.load(data[0])
55
+ value = Marshal.load(data[1])
56
+ yield(key, value)
57
+ end
58
+ cursor.close
59
+ end
60
+
61
+ def sync
62
+ db.sync
63
+ end
64
+
65
+ def clear
66
+ close
67
+ File.delete(file) if File.exists?(file)
68
+ nil
69
+ end
70
+
71
+ def close
72
+ db.close(0)
73
+ @db = nil
74
+ end
75
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ninjudd-bdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Bauer
@@ -27,6 +27,7 @@ files:
27
27
  - ext/bdb.c
28
28
  - ext/bdb.h
29
29
  - ext/extconf.rb
30
+ - lib/bdb/simple.rb
30
31
  - LICENSE
31
32
  - README.textile
32
33
  - Rakefile