mingle_keyvalue_store 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +0 -0
- data/lib/mingle_keyvalue_store.rb +115 -0
- metadata +98 -0
data/README.md
ADDED
File without changes
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "aws"
|
3
|
+
require "pstore"
|
4
|
+
require "fileutils"
|
5
|
+
require "tempfile"
|
6
|
+
|
7
|
+
module Mingle
|
8
|
+
module KeyvalueStore
|
9
|
+
class PStoreBased
|
10
|
+
def initialize(path, namespace, key_column, value_column)
|
11
|
+
@namespace = namespace
|
12
|
+
@store_file = store_file(path)
|
13
|
+
@key_column = key_column
|
14
|
+
@value_column = value_column
|
15
|
+
@pstore = PStore.new(@store_file)
|
16
|
+
end
|
17
|
+
|
18
|
+
def [](store_key)
|
19
|
+
@pstore.transaction { @pstore[store_key] }
|
20
|
+
end
|
21
|
+
|
22
|
+
def []=(store_key, value)
|
23
|
+
@pstore.transaction do
|
24
|
+
@pstore["all_names"] ||= []
|
25
|
+
@pstore["all_names"] = (@pstore["all_names"] + [store_key]).uniq
|
26
|
+
@pstore[store_key] = JSON.parse(value.to_json)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def delete(store_key)
|
31
|
+
@pstore.transaction do
|
32
|
+
@pstore["all_names"].delete_if {|name| name == store_key}
|
33
|
+
@pstore.delete(store_key)
|
34
|
+
end
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
|
38
|
+
def clear
|
39
|
+
FileUtils.rm_f(@store_file)
|
40
|
+
@pstore = PStore.new(@store_file)
|
41
|
+
nil
|
42
|
+
end
|
43
|
+
|
44
|
+
def names
|
45
|
+
@pstore.transaction { @pstore["all_names"] || [] }
|
46
|
+
end
|
47
|
+
|
48
|
+
def all_items
|
49
|
+
@pstore.transaction do
|
50
|
+
return [] unless @pstore["all_names"]
|
51
|
+
@pstore["all_names"].map do |name|
|
52
|
+
{
|
53
|
+
@key_column.to_s => name,
|
54
|
+
@value_column.to_s => @pstore[name].to_json
|
55
|
+
}
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def store_file(path)
|
64
|
+
FileUtils.mkdir_p(path)
|
65
|
+
|
66
|
+
file = Tempfile.new("#{@namespace}_multitenancy_configs.pstore", path).path
|
67
|
+
FileUtils.mkdir_p(File.dirname(file))
|
68
|
+
file
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class DynamodbBased
|
73
|
+
def initialize(table_name, key_column, value_column)
|
74
|
+
@key_column = key_column
|
75
|
+
@value_column = value_column
|
76
|
+
@table_name = table_name
|
77
|
+
end
|
78
|
+
|
79
|
+
def [](store_key)
|
80
|
+
if attribute = table_items[store_key].attributes[@value_column]
|
81
|
+
JSON.parse(attribute)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def []=(store_key, value)
|
86
|
+
table_items.create(@key_column => store_key, @value_column => value.to_json)
|
87
|
+
end
|
88
|
+
|
89
|
+
def clear
|
90
|
+
table_items.each(&:delete)
|
91
|
+
end
|
92
|
+
|
93
|
+
def names
|
94
|
+
table_items.map(&:hash_value)
|
95
|
+
end
|
96
|
+
|
97
|
+
def all_items
|
98
|
+
table_items.map { |item| item.attributes.to_hash }
|
99
|
+
end
|
100
|
+
|
101
|
+
def delete(key)
|
102
|
+
table_items.where(@key_column => key).first.delete
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
def table_items
|
108
|
+
table = AWS::DynamoDB.new.tables[@table_name]
|
109
|
+
table.hash_key = [@key_column, :string]
|
110
|
+
table.items
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mingle_keyvalue_store
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ian
|
14
|
+
- sdqali
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2013-10-16 00:00:00 Z
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: aws-sdk
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - "="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 61
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 11
|
33
|
+
- 3
|
34
|
+
version: 1.11.3
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rake
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
description: A key value store implentation that uses DynamoDB or Pstore underneath.
|
52
|
+
email:
|
53
|
+
- reginaldthedog@gmail.com
|
54
|
+
- sadiqalikm@gmail.com
|
55
|
+
executables: []
|
56
|
+
|
57
|
+
extensions: []
|
58
|
+
|
59
|
+
extra_rdoc_files: []
|
60
|
+
|
61
|
+
files:
|
62
|
+
- README.md
|
63
|
+
- lib/mingle_keyvalue_store.rb
|
64
|
+
homepage: https://github.com/ThoughtWorksStudios/mingle_keyvalue_store
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.8.25
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: A key value store implentation that uses DynamoDB or Pstore underneath.
|
97
|
+
test_files: []
|
98
|
+
|