volatiledb 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,13 +1,48 @@
1
1
  #### Summary
2
2
 
3
- The VolatileDB gem allows you to specify a key and an action yielding a particular piece of data.
3
+ The VolatileDB gem allows you to specify a key and an action yielding a particular piece of
4
+ data.
4
5
 
5
- This data will be stored in the /tmp folder of the file system you are currently running on. Data is accessible
6
- by key. Data will be read and written to storage using File.read() and File.open() -- that's it. It's up to the
7
- consuming application to serialize and deserialize data correctly. All VolatileDB does is push and pull data to
8
- the FS.
6
+
7
+ This data will be stored in the /tmp folder of the file system you are currently running on. Data
8
+ is accessible by key. Data will be read and written to storage using File.read() and File.open()
9
+ -- that's it. It's up to the consuming application to serialize and deserialize data correctly. All
10
+ VolatileDB does is push and pull data to the FS.
9
11
 
10
12
  If the underlying file supporting the data is found to be missing, it will be re-initialized.
11
13
 
12
- This gets to the main idea behind VolatileDB: use it to persist data that is transient and can be re-seeded
13
- periodically as conditions change.
14
+ This gets to the main idea behind VolatileDB: use it to persist data that is transient and can be
15
+ re-seeded periodically as conditions change.
16
+
17
+ #### Usage
18
+
19
+ require 'volatiledb'
20
+ db = Volatile::DB.new
21
+ db.put(:foo) { "acts as" }
22
+ #=> :foo
23
+ db.get(:foo)
24
+ #=> "acts as"
25
+ db.get(:bar)
26
+ #=> nil
27
+ db.put(:bar) { "chunky bacon" }
28
+ db.get(:bar)
29
+ #=> "chunky bacon"
30
+
31
+ #### License
32
+
33
+ Copyright (c) 2012 Sebastian Wittenkamp
34
+
35
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
36
+ associated documentation files (the "Software", to deal in the Software without restriction, including
37
+ without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
38
+ copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to
39
+ the following conditions:
40
+
41
+ The above copyright notice and this permission notice shall be included in all copies or substantial
42
+ portions of the Software.
43
+
44
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
45
+ LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
46
+ NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
47
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
48
+ THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/TODO.md ADDED
@@ -0,0 +1,4 @@
1
+ *TODOS*
2
+ - the API assumes that put is called before all
3
+ other methods. ensure that get/fetch/pull return
4
+ nil if no action has been defined for the given key.
data/lib/volatiledb.rb CHANGED
@@ -9,19 +9,93 @@ module Volatile
9
9
  @db ||= {}
10
10
  end
11
11
 
12
- def put(key, &action)
13
- @db[key] = action
14
- @raw.put(key, action.call)
12
+ #
13
+ # Define an action for a given key. The action should return a String.
14
+ # A timeout in seconds may optionally be defined. Default is 3 seconds.
15
+ #
16
+ # This method should be called before using any of the read operations.
17
+ #
18
+ def put(key, timeout=3, &action)
19
+ populate_db key, timeout, action
20
+ raw_put key
15
21
  key
16
22
  end
17
23
 
24
+ #
25
+ # Read the data for the given key from storage. If the underlying storage
26
+ # has disappeared, it will be re-initialized by calling the action defined
27
+ # for that key again. After the storage has been re-initialized, its value
28
+ # will be read and returned.
29
+ #
18
30
  def get(key)
19
31
  data = @raw.get key
20
- unless data.nil?
32
+ if data.nil?
33
+ sync key
34
+ else
21
35
  data
36
+ end
37
+ end
38
+
39
+ #
40
+ # Works the same as get, but will fire the action defined for the given key
41
+ # after the timeout defined for that key has elapsed.
42
+ #
43
+ def fetch(key)
44
+ checking key do |k|
45
+ item = @db[k]
46
+ delta = now - item[:last_access]
47
+ delta > item[:timeout] ? sync(k) : get(k)
48
+ end
49
+ end
50
+
51
+ #
52
+ # Works the same as fetch, but ignores timeout and calls the defined action
53
+ # every time.
54
+ #
55
+ def pull(key)
56
+ sync key
57
+ end
58
+
59
+ private
60
+ def populate_db(key, timeout, action)
61
+ @db[key] = {
62
+ :action => action,
63
+ :timeout => timeout,
64
+ :last_access => now
65
+ }
66
+ end
67
+
68
+ def sync(key)
69
+ touch key
70
+ save key
71
+ end
72
+
73
+ def touch(key)
74
+ checking(key) {|k| @db[k][:last_access] = now }
75
+ end
76
+
77
+ def now
78
+ Time.now.to_i
79
+ end
80
+
81
+ def save(key)
82
+ raw_put key
83
+ raw_get key
84
+ end
85
+
86
+ def raw_put(key)
87
+ checking(key) {|k| @raw.put(k, @db[k][:action].call) }
88
+ end
89
+
90
+ def raw_get(key)
91
+ checking(key) {|k| @raw.get k }
92
+ end
93
+
94
+ def checking(key)
95
+ if @db.key?(key)
96
+ yield(key)
22
97
  else
23
- @raw.put(key, @db[key].call)
24
- @raw.get(key)
98
+ nil
25
99
  end
26
100
  end
27
101
  end
@@ -41,7 +115,7 @@ module Volatile
41
115
  def get(key)
42
116
  handle = @db[key]
43
117
  f = "/tmp/#{handle}"
44
- if File.exists?(f)
118
+ if File.file?(f)
45
119
  File.read f
46
120
  else
47
121
  nil
@@ -1,3 +1,3 @@
1
1
  module Volatile
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: volatiledb
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Sebastian Wittenkamp (bitops)
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-01-04 00:00:00 Z
13
+ date: 2012-01-10 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: " The VolatileDB gem allows you to specify a key and an action yielding a particular piece of data.\n \n This data will be stored in the /tmp folder of the file system you are currently running on. Data is accessible\n by key. Data will be read and written to storage using File.read() and File.open() -- that's it. It's up to the\n consuming application to serialize and deserialize data correctly. All VolatileDB does is push and pull data to\n the FS. \n \n If the underlying file supporting the data is found to be missing, it will be re-initialized.\n \n This gets to the main idea behind VolatileDB: use it to persist data that is transient and can be re-seeded \n periodically as conditions change. \n"
@@ -27,6 +27,7 @@ files:
27
27
  - Gemfile
28
28
  - README.md
29
29
  - Rakefile
30
+ - TODO.md
30
31
  - lib/volatiledb.rb
31
32
  - lib/volatiledb/version.rb
32
33
  - volatiledb.gemspec