obuf 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/History.txt +4 -0
  2. data/Rakefile +1 -1
  3. data/lib/obuf.rb +27 -21
  4. data/test/test_obuf.rb +2 -0
  5. metadata +28 -17
@@ -1,3 +1,7 @@
1
+ === 1.0.1 / 2011-08-02
2
+
3
+ * Fix the bug that was failing Obuf in Ruby 1.8.7 since Mutex is not loaded by default
4
+
1
5
  === 1.0.0 / 2011-08-02
2
6
 
3
7
  * 1 major enhancement
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ Hoe.spec 'obuf' do | p |
8
8
 
9
9
  p.readme_file = 'README.rdoc'
10
10
  p.extra_rdoc_files = FileList['*.rdoc'] + FileList['*.txt']
11
- p.extra_dev_deps = {"flexmock" => "~> 0.8", "cli_test" => "~>1.0"}
11
+ p.extra_dev_deps = {"flexmock" => "~> 0.8"}
12
12
  p.clean_globs = File.read(File.dirname(__FILE__) + "/.gitignore").split(/\s/).to_a
13
13
  end
14
14
 
@@ -1,11 +1,11 @@
1
1
  require "tempfile"
2
+ require "thread" # required for ruby 18
2
3
 
3
4
  # An object buffer for Ruby objects. Use it to sequentially store a shitload
4
5
  # of objects on disk and then retreive them one by one. Make sure to call clear when done
5
- # with it to discard the stored blob. It can be used like a disk-based object buffer.
6
- # (Tracksperanto stores parsed trackers into it)
6
+ # with it to discard the stored blob.
7
7
  #
8
- # a = Tracksperanto::Accumulator.new
8
+ # a = Obuf.new
9
9
  # parse_big_file do | one_node |
10
10
  # a.push(one_node)
11
11
  # end
@@ -17,9 +17,9 @@ require "tempfile"
17
17
  #
18
18
  # a.clear # ensure that the file is deleted
19
19
  #
20
- # Both reading and writing aim to be threadsafe (writing operations will be locked with a mutex, and )
20
+ # Both reading and writing aim to be threadsafe
21
21
  class Obuf
22
- VERSION = "1.0.0"
22
+ VERSION = "1.0.1"
23
23
 
24
24
  include Enumerable
25
25
 
@@ -31,7 +31,7 @@ class Obuf
31
31
 
32
32
  def initialize
33
33
  @sem = Mutex.new
34
- @store = create_store
34
+ @store = Tempfile.new("obuf")
35
35
  @store.set_encoding(Encoding::BINARY) if @store.respond_to?(:set_encoding)
36
36
  @store.binmode
37
37
 
@@ -69,13 +69,15 @@ class Obuf
69
69
 
70
70
  # Calls close! on the datastore and deletes the objects in it
71
71
  def clear
72
- @store.close!
73
- @size = 0
72
+ @sem.synchronize do
73
+ @store.close!
74
+ @size = 0
75
+ end
74
76
  end
75
77
 
76
- # Retreive a concrete object at index
77
- def [](idx)
78
- idx.respond_to?(:each) ? idx.map{|i| recover_at(i) } : recover_at(idx)
78
+ # Retreive a slice of the enumerable at index
79
+ def [](slice)
80
+ slice.respond_to?(:each) ? slice.map{|i| recover_at(i) } : recover_at(slice)
79
81
  end
80
82
 
81
83
  private
@@ -100,7 +102,7 @@ class Obuf
100
102
  # Ensure all data is written before we read it
101
103
  @sem.synchronize { @store.flush }
102
104
 
103
- iterable = File.open(@store.path, "r")
105
+ iterable = File.open(@store.path, "rb")
104
106
  begin
105
107
  yield(iterable)
106
108
  ensure
@@ -108,20 +110,24 @@ class Obuf
108
110
  end
109
111
  end
110
112
 
111
- def marshal_object(object_to_store)
112
- d = Marshal.dump(object_to_store)
113
- end
114
-
115
113
  def recover_object_from(io)
116
114
  # Up to the tab is the amount of bytes to read
117
115
  demarshal_bytes = io.gets("\t").to_i
118
116
  blob = io.read(demarshal_bytes)
119
-
120
- Marshal.load(blob)
117
+ demarshal_object(blob)
121
118
  end
122
119
 
123
- # In case you need to use something else (say, a StringIO)
124
- def create_store
125
- Tempfile.new("obuf")
120
+ # This method is only used internally.
121
+ # Override this if you need non-default marshalling
122
+ # (don't forget to also override demarshal_object)
123
+ def marshal_object(object_to_store)
124
+ d = Marshal.dump(object_to_store)
125
+ end
126
+
127
+ # This method is only used internally.
128
+ # Override this if you need non-default demarshalling
129
+ # (don't forget to also override marshal_object)
130
+ def demarshal_object(blob)
131
+ Marshal.load(blob)
126
132
  end
127
133
  end
@@ -2,6 +2,7 @@ require "test/unit"
2
2
  require "obuf"
3
3
  require "flexmock"
4
4
  require "flexmock/test_unit"
5
+ require "stringio"
5
6
 
6
7
  # http://redmine.ruby-lang.org/issues/4882
7
8
  # https://github.com/jimweirich/flexmock/issues/4
@@ -13,6 +14,7 @@ class FlexMock::PartialMockProxy
13
14
  end
14
15
 
15
16
  class TestObuf < Test::Unit::TestCase
17
+
16
18
  def test_accumulator_saves_objs
17
19
  a = Obuf.new
18
20
  values = [3, {:foo => "bar"}, "foo"]
metadata CHANGED
@@ -1,8 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: obuf
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 21
4
5
  prerelease:
5
- version: 1.0.0
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 1
10
+ version: 1.0.1
6
11
  platform: ruby
7
12
  authors:
8
13
  - Julik Tarkhanov
@@ -10,7 +15,8 @@ autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
17
 
13
- date: 2011-08-02 00:00:00 Z
18
+ date: 2011-11-20 00:00:00 +01:00
19
+ default_executable:
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
22
  name: flexmock
@@ -20,31 +26,29 @@ dependencies:
20
26
  requirements:
21
27
  - - ~>
22
28
  - !ruby/object:Gem::Version
29
+ hash: 27
30
+ segments:
31
+ - 0
32
+ - 8
23
33
  version: "0.8"
24
34
  type: :development
25
35
  version_requirements: *id001
26
36
  - !ruby/object:Gem::Dependency
27
- name: cli_test
37
+ name: hoe
28
38
  prerelease: false
29
39
  requirement: &id002 !ruby/object:Gem::Requirement
30
40
  none: false
31
41
  requirements:
32
- - - ~>
42
+ - - ">="
33
43
  - !ruby/object:Gem::Version
34
- version: "1.0"
44
+ hash: 35
45
+ segments:
46
+ - 2
47
+ - 9
48
+ - 4
49
+ version: 2.9.4
35
50
  type: :development
36
51
  version_requirements: *id002
37
- - !ruby/object:Gem::Dependency
38
- name: hoe
39
- prerelease: false
40
- requirement: &id003 !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ~>
44
- - !ruby/object:Gem::Version
45
- version: "2.10"
46
- type: :development
47
- version_requirements: *id003
48
52
  description: A simple Ruby object buffer. Use this if you need to temporarily store alot of serializable Ruby objects.
49
53
  email:
50
54
  - me@julik.nl
@@ -65,6 +69,7 @@ files:
65
69
  - lib/obuf.rb
66
70
  - test/test_obuf.rb
67
71
  - .gemtest
72
+ has_rdoc: true
68
73
  homepage: http://github.com/julik/obuf
69
74
  licenses: []
70
75
 
@@ -79,17 +84,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
79
84
  requirements:
80
85
  - - ">="
81
86
  - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
82
90
  version: "0"
83
91
  required_rubygems_version: !ruby/object:Gem::Requirement
84
92
  none: false
85
93
  requirements:
86
94
  - - ">="
87
95
  - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
88
99
  version: "0"
89
100
  requirements: []
90
101
 
91
102
  rubyforge_project: obuf
92
- rubygems_version: 1.8.5
103
+ rubygems_version: 1.6.2
93
104
  signing_key:
94
105
  specification_version: 3
95
106
  summary: A simple Ruby object buffer