cdb-ruby 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 5ff7193e71ba8c7b0859abad844985f4cc5d674ccb9f93801a8c63fdfd24438e
4
- data.tar.gz: f5b7b2a5e85ab59666230b95ab9761b969695764cf8678eadff029b8948dc5b3
2
+ SHA1:
3
+ metadata.gz: bfee6b5d54015ab9d2678b0e17321601595cd118
4
+ data.tar.gz: 01c7b55334e64ba01906cd08229aaea48db7eec4
5
5
  SHA512:
6
- metadata.gz: b50bcd22e9574d7347c7990f8323724330ea97e1454f419aef0d2ad2a67b19fe7678ccbdc25519f079e72d77438cf9a249ceadbd10059222eb9c980f3f7595bb
7
- data.tar.gz: e6ea469d9b0cd09ea680a18bdd4a965918a212ada615ff60e4870aacafcbe40450d7c19bded4901e2d94d5b6779fddac903a0cc837fc45f05649a4ebc0d86b6f
6
+ metadata.gz: ab3157637d9bfae8afa30965fb540a11ba895246bfe9024161b2698de2063c5813bca58babb7e6f8da00e7c712c9df8aa08b64285bce0fcbf37a4c2eb40be448
7
+ data.tar.gz: 4d4be6eafe86f40ad662c41f354c7fad5147b42b524b8f8dff4e442361670c1a4d1f24f89738ecda75be078140f2111ba14a36f635bf3c32e03b3d9bf9144ff3
data/lib/cdb/reader.rb CHANGED
@@ -2,7 +2,7 @@ module Cdb
2
2
  # Provides read-only access to a cdb.
3
3
  class Reader
4
4
  def initialize(file)
5
- @file = file
5
+ @file = file.binmode
6
6
  end
7
7
 
8
8
  # Fetches the value associated with the given key.
@@ -54,66 +54,4 @@ module Cdb
54
54
  @file.read(len)
55
55
  end
56
56
  end
57
-
58
- # Provides write-only access to a cdb.
59
- class Writer
60
- # Initializes an empty cdb for writing to the given file-like object.
61
- def self.create(file)
62
- file.truncate(0)
63
- file.write(empty_header)
64
- Writer.new(file)
65
- end
66
-
67
- # Writes a key/value pair to the cdb.
68
- #
69
- # Attempting to write the same key twice will cause an error.
70
- def []=(key, value)
71
- offset = append(key, value)
72
- index(key, offset)
73
- end
74
-
75
- # Finish writing the cdb.
76
- #
77
- # This flushes the hash table structure to disk.
78
- def close
79
- lookups = @tables.map { |t| write_table(t) }
80
- @file.rewind
81
- @file.write(lookups.flatten.pack('V*'))
82
- end
83
-
84
- # Returns an empty header -- NUM_HASHTABLES pairs of 32-bit integers, all
85
- # containing zero.
86
- def self.empty_header
87
- "\0" * (Cdb::NUM_HASHTABLES * 8)
88
- end
89
-
90
- private
91
-
92
- def initialize(file)
93
- @file = file
94
- @tables = (0...Cdb::NUM_HASHTABLES).map { HashTable.new }
95
- end
96
-
97
- def append(key, value)
98
- offset = @file.pos
99
- @file.write([key.length, value.length, key, value].pack('VVA*A*'))
100
- offset
101
- end
102
-
103
- def index(key, offset)
104
- hash = Cdb.hash(key)
105
- table_for_hash(hash).put(HashTableEntry.new(hash, key, offset))
106
- end
107
-
108
- def write_table(table)
109
- return [0, 0] if table.nil?
110
- offset = @file.pos
111
- @file.write(table.bytes)
112
- [offset, table.capacity]
113
- end
114
-
115
- def table_for_hash(hash)
116
- @tables[hash % Cdb::NUM_HASHTABLES]
117
- end
118
- end
119
57
  end
data/lib/cdb/writer.rb CHANGED
@@ -1,4 +1,66 @@
1
1
  module Cdb
2
+ # Provides write-only access to a cdb.
3
+ class Writer
4
+ # Initializes an empty cdb for writing to the given file-like object.
5
+ def self.create(file)
6
+ file.truncate(0)
7
+ file.write(empty_header)
8
+ Writer.new(file)
9
+ end
10
+
11
+ # Writes a key/value pair to the cdb.
12
+ #
13
+ # Attempting to write the same key twice will cause an error.
14
+ def []=(key, value)
15
+ offset = append(key, value)
16
+ index(key, offset)
17
+ end
18
+
19
+ # Finish writing the cdb.
20
+ #
21
+ # This flushes the hash table structure to disk.
22
+ def close
23
+ lookups = @tables.map { |t| write_table(t) }
24
+ @file.rewind
25
+ @file.write(lookups.flatten.pack('V*'))
26
+ end
27
+
28
+ # Returns an empty header -- NUM_HASHTABLES pairs of 32-bit integers, all
29
+ # containing zero.
30
+ def self.empty_header
31
+ "\0" * (Cdb::NUM_HASHTABLES * 8)
32
+ end
33
+
34
+ private
35
+
36
+ def initialize(file)
37
+ @file = file.binmode
38
+ @tables = (0...Cdb::NUM_HASHTABLES).map { HashTable.new }
39
+ end
40
+
41
+ def append(key, value)
42
+ offset = @file.pos
43
+ @file.write([key.length, value.length, key, value].pack('VVA*A*'))
44
+ offset
45
+ end
46
+
47
+ def index(key, offset)
48
+ hash = Cdb.hash(key)
49
+ table_for_hash(hash).put(HashTableEntry.new(hash, key, offset))
50
+ end
51
+
52
+ def write_table(table)
53
+ return [0, 0] if table.nil?
54
+ offset = @file.pos
55
+ @file.write(table.bytes)
56
+ [offset, table.capacity]
57
+ end
58
+
59
+ def table_for_hash(hash)
60
+ @tables[hash % Cdb::NUM_HASHTABLES]
61
+ end
62
+ end
63
+
2
64
  # In-memory hash table structure. Indexes key/value pairs in a Writer.
3
65
  class HashTable
4
66
  # Creates an empty hash table.
data/lib/cdb.rb CHANGED
@@ -24,7 +24,7 @@ require 'cdb/writer'
24
24
  module Cdb
25
25
  # Write data to a cdb in a file-like object.
26
26
  def self.create(file)
27
- writer = Writer.create(file)
27
+ writer = Cdb::Writer.create(file)
28
28
  yield(writer)
29
29
  writer.close
30
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cdb-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Olly Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-23 00:00:00.000000000 Z
11
+ date: 2018-03-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: olly.smith@gmail.com
@@ -40,7 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
40
  version: '0'
41
41
  requirements: []
42
42
  rubyforge_project:
43
- rubygems_version: 2.7.3
43
+ rubygems_version: 2.6.14
44
44
  signing_key:
45
45
  specification_version: 4
46
46
  summary: A lightweight, pure-ruby reader/writer for DJ Bernstein's cdb format