fast_trie 0.3.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 3
3
+ :minor: 4
4
4
  :build:
5
- :patch: 7
5
+ :patch: 0
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -19,6 +19,54 @@ static VALUE rb_trie_alloc(VALUE klass) {
19
19
  return obj;
20
20
  }
21
21
 
22
+ void raise_ioerror(const char * message) {
23
+ VALUE rb_eIOError = rb_const_get(rb_cObject, rb_intern("IOError"));
24
+ rb_raise(rb_eIOError, message);
25
+ }
26
+
27
+ /*
28
+ * call-seq:
29
+ * read(filename_base) -> Trie
30
+ *
31
+ * Returns a new trie with data as read from disk.
32
+ */
33
+ static VALUE rb_trie_read(VALUE self, VALUE filename_base) {
34
+ VALUE da_filename = rb_str_dup(filename_base);
35
+ rb_str_concat(da_filename, rb_str_new2(".da"));
36
+ StringValue(da_filename);
37
+
38
+ VALUE tail_filename = rb_str_dup(filename_base);
39
+ rb_str_concat(tail_filename, rb_str_new2(".tail"));
40
+ StringValue(tail_filename);
41
+
42
+ Trie *trie = trie_new();
43
+
44
+ VALUE obj;
45
+ obj = Data_Wrap_Struct(self, 0, trie_free, trie);
46
+
47
+ DArray *old_da = trie->da;
48
+ Tail *old_tail = trie->tail;
49
+
50
+ FILE *da_file = fopen(RSTRING_PTR(da_filename), "r");
51
+ if (da_file == NULL)
52
+ raise_ioerror("Error reading .da file.");
53
+
54
+ trie->da = da_read(da_file);
55
+ fclose(da_file);
56
+
57
+ FILE *tail_file = fopen(RSTRING_PTR(tail_filename), "r");
58
+ if (tail_file == NULL)
59
+ raise_ioerror("Error reading .tail file.");
60
+
61
+ trie->tail = tail_read(tail_file);
62
+ fclose(tail_file);
63
+
64
+ da_free(old_da);
65
+ tail_free(old_tail);
66
+
67
+ return obj;
68
+ }
69
+
22
70
  /*
23
71
  * call-seq:
24
72
  * has_key?(key) -> true/false
@@ -446,10 +494,47 @@ static VALUE rb_trie_node_leaf(VALUE self) {
446
494
  return trie_state_is_leaf(state) ? Qtrue : Qnil;
447
495
  }
448
496
 
497
+ /*
498
+ * call-seq:
499
+ * save(filename_base) -> true
500
+ *
501
+ * Saves the trie data to two files, filename_base.da and filename_base.tail.
502
+ * Returns true if saving was successful.
503
+ */
504
+ static VALUE rb_trie_save(VALUE self, VALUE filename_base) {
505
+ VALUE da_filename = rb_str_dup(filename_base);
506
+ rb_str_concat(da_filename, rb_str_new2(".da"));
507
+ StringValue(da_filename);
508
+
509
+ VALUE tail_filename = rb_str_dup(filename_base);
510
+ rb_str_concat(tail_filename, rb_str_new2(".tail"));
511
+ StringValue(tail_filename);
512
+
513
+ Trie *trie;
514
+ Data_Get_Struct(self, Trie, trie);
515
+
516
+ FILE *da_file = fopen(RSTRING_PTR(da_filename), "w");
517
+ if (da_file == NULL)
518
+ raise_ioerror("Error opening .da file for writing.");
519
+ if (da_write(trie->da, da_file) != 0)
520
+ raise_ioerror("Error writing DArray data.");
521
+ fclose(da_file);
522
+
523
+ FILE *tail_file = fopen(RSTRING_PTR(tail_filename), "w");
524
+ if (tail_file == NULL)
525
+ raise_ioerror("Error opening .tail file for writing.");
526
+ if (tail_write(trie->tail, tail_file) != 0)
527
+ raise_ioerror("Error writing Tail data.");
528
+ fclose(tail_file);
529
+
530
+ return Qtrue;
531
+ }
532
+
449
533
 
450
534
  void Init_trie() {
451
535
  cTrie = rb_define_class("Trie", rb_cObject);
452
536
  rb_define_alloc_func(cTrie, rb_trie_alloc);
537
+ rb_define_module_function(cTrie, "read", rb_trie_read, 1);
453
538
  rb_define_method(cTrie, "has_key?", rb_trie_has_key, 1);
454
539
  rb_define_method(cTrie, "get", rb_trie_get, 1);
455
540
  rb_define_method(cTrie, "add", rb_trie_add, -2);
@@ -457,10 +542,11 @@ void Init_trie() {
457
542
  rb_define_method(cTrie, "children", rb_trie_children, 1);
458
543
  rb_define_method(cTrie, "children_with_values", rb_trie_children_with_values, 1);
459
544
  rb_define_method(cTrie, "root", rb_trie_root, 0);
545
+ rb_define_method(cTrie, "save", rb_trie_save, 1);
460
546
 
461
547
  cTrieNode = rb_define_class("TrieNode", rb_cObject);
462
548
  rb_define_alloc_func(cTrieNode, rb_trie_node_alloc);
463
- rb_define_method(cTrieNode, "initialize_copy", rb_trie_node_initialize_copy, 1);
549
+ rb_define_method(cTrieNode, "initialize_copy", rb_trie_node_initialize_copy, 1);
464
550
  rb_define_method(cTrieNode, "state", rb_trie_node_get_state, 0);
465
551
  rb_define_method(cTrieNode, "full_state", rb_trie_node_get_full_state, 0);
466
552
  rb_define_method(cTrieNode, "walk!", rb_trie_node_walk_bang, 1);
@@ -8,12 +8,6 @@ describe Trie do
8
8
  @trie.add('frederico')
9
9
  end
10
10
 
11
- #describe :path do
12
- # it 'returns the correct path' do
13
- # @trie.path.should == TRIE_PATH
14
- # end
15
- #end
16
-
17
11
  describe :has_key? do
18
12
  it 'returns true for words in the trie' do
19
13
  @trie.has_key?('rocket').should be_true
@@ -144,15 +138,37 @@ describe Trie do
144
138
  end
145
139
  end
146
140
 
147
- #describe :save do
148
- # it 'saves the trie to disk such that another trie can be spawned which will read succesfully' do
149
- # @trie.add('omgwtf',123)
150
- # @trie.save
151
- #
152
- # trie2 = Trie.new(TRIE_PATH)
153
- # trie2.get('omgwtf').should == 123
154
- # end
155
- #end
141
+ describe 'save/read' do
142
+ let(:filename_base) do
143
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'tmp'))
144
+ FileUtils.mkdir_p(dir)
145
+ File.join(dir, 'trie')
146
+ end
147
+
148
+ context 'when I save the populated trie to disk' do
149
+ before(:each) do
150
+ @trie.add('omgwtflolbbq', 123)
151
+ @trie.save(filename_base)
152
+ end
153
+
154
+ it 'should contain the same data when reading from disk' do
155
+ trie2 = Trie.read(filename_base)
156
+ trie2.get('omgwtflolbbq').should == 123
157
+ end
158
+ end
159
+ end
160
+
161
+ describe :read do
162
+ context 'when the files to read from do not exist' do
163
+ let(:filename_base) do
164
+ "phantasy/file/path/that/does/not/exist"
165
+ end
166
+
167
+ it 'should raise an error when attempting a read' do
168
+ lambda { Trie.read(filename_base) }.should raise_error(IOError)
169
+ end
170
+ end
171
+ end
156
172
  end
157
173
 
158
174
  describe TrieNode do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_trie
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 7
10
- version: 0.3.7
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tyler McMullen
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-19 00:00:00 -07:00
18
+ date: 2010-09-21 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -31,6 +31,12 @@ extra_rdoc_files:
31
31
  files:
32
32
  - README.textile
33
33
  - VERSION.yml
34
+ - ext/darray.o
35
+ - ext/fileutils.o
36
+ - ext/tail.o
37
+ - ext/trie-private.o
38
+ - ext/trie.bundle
39
+ - ext/trie.o
34
40
  - ext/trie/darray.c
35
41
  - ext/trie/darray.h
36
42
  - ext/trie/extconf.rb