ssd 0.1.7 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +50 -16
- data/lib/ssd.rb +66 -3
- data/lib/ssd/class_methods.rb +62 -60
- data/lib/ssd/instance_methods.rb +37 -35
- data/lib/ssd/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e37a7abd30a7a3d3909b5349d64c563b37bff37c
|
4
|
+
data.tar.gz: c48ad95d301d47ddd0ab30cfeda48df3e3280d9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e254e2c2d4414f59984e08b05c01a43960b62d57f8605a8fccd49d7327153fa3633c63a733d1d293c10768645a413e4b9f55c4a8bddf189f5336803a962d4ea
|
7
|
+
data.tar.gz: 65f12fdc3cee272e634f3f28afc6985e9eeda20b61b606b909715d1d4fdd64d32b31958188e0dff727dd2a3a777b7597fbfeff805e4e685716320b8a32ab7e5d
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -31,27 +31,61 @@ Usage
|
|
31
31
|
gem install ssd
|
32
32
|
```
|
33
33
|
|
34
|
+
```ruby
|
35
|
+
# Simple Usage
|
36
|
+
# writes to the store
|
37
|
+
SSD.write("company_api/microservice_name/", "special_id", "this is a big value super funny")
|
38
|
+
|
39
|
+
# returns the last value of the append-only store
|
40
|
+
SSD.read("company_api/microservice_name/", "special_id")
|
41
|
+
|
42
|
+
# Dumps all the store
|
43
|
+
SSD.dump("company_api/microservice_name/", "special_id")
|
44
|
+
|
45
|
+
end
|
46
|
+
```
|
47
|
+
Further more you can use ssd console commands to view the contents in a readable manner,
|
48
|
+
SSD can use Marshal for performance(the default is yaml) but the ssd commands views it in yaml for readability
|
49
|
+
```bash
|
50
|
+
ssd view ./ssd/company_api/microservice_name/special_id
|
51
|
+
```
|
52
|
+
|
34
53
|
```ruby
|
35
|
-
|
54
|
+
# Entity Usage
|
55
|
+
require 'sinatra/base'
|
36
56
|
require 'ssd'
|
37
57
|
|
38
58
|
module MyApp
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
59
|
+
class User
|
60
|
+
include SSD::Entity
|
61
|
+
attr_accessor :id, :name, :bio
|
62
|
+
def initialize
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class API < Sinatra::Base
|
67
|
+
get '/' do
|
68
|
+
##################### Writing #####################
|
69
|
+
@user = MyApp::User.new
|
70
|
+
@user.id = "somekind_of_id_like_username_or_email"
|
71
|
+
|
72
|
+
# Set the ssd key of the object
|
73
|
+
@user.ssd = @user.id
|
74
|
+
|
75
|
+
# Do what you wish with your object
|
76
|
+
@user.name = "Makki Omura"
|
77
|
+
@user.bio = "a charming girl!"
|
78
|
+
|
79
|
+
# If you are ready to store your object to disk; Do an `append!` and viola! DONE
|
80
|
+
@user.append!
|
81
|
+
|
82
|
+
##################### READING #####################
|
83
|
+
# If you want to get your object back from disk; Use Klass.ssd(:ssd)
|
84
|
+
result = MyApp::User.ssd("somekind_of_id_like_username_or_email")
|
85
|
+
result.name
|
86
|
+
end
|
87
|
+
run!
|
45
88
|
end
|
46
|
-
end
|
47
|
-
|
48
|
-
class API < Sinatra::Base
|
49
|
-
get '/' do
|
50
|
-
@user = User.new("generated_id") # Initialize an object form any Klass that `include SSD` and assign it a :ssd key
|
51
|
-
@user.append! # Do an `append!` operation and viola! DONE
|
52
|
-
@user = User.ssd("generated_id") # Afterwards read it via Klass.ssd(:ssd)
|
53
|
-
end
|
54
|
-
end
|
55
89
|
end
|
56
90
|
```
|
57
91
|
|
data/lib/ssd.rb
CHANGED
@@ -3,15 +3,78 @@ require 'logger'
|
|
3
3
|
require 'securerandom'
|
4
4
|
require 'pstore'
|
5
5
|
require 'digest'
|
6
|
+
require 'yaml/store'
|
6
7
|
|
7
8
|
require_relative "./ssd/version"
|
8
9
|
require_relative "./ssd/internals"
|
9
10
|
$log = Logger.new(STDOUT)
|
10
11
|
|
11
12
|
module SSD
|
13
|
+
module Entity
|
14
|
+
def self.included(base)
|
15
|
+
base.send :include, SSD::Internals::Entity::InstanceMethods
|
16
|
+
base.extend SSD::Internals::Entity::ClassMethods
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Store
|
21
|
+
|
22
|
+
def initialize path, key, value=nil
|
23
|
+
@path = path
|
24
|
+
@key = key
|
25
|
+
@value = value
|
26
|
+
FileUtils::mkdir_p ".ssd/#{@path}"
|
27
|
+
@ssd_path = ".ssd/#{@path}/#{@key}.ssd"
|
28
|
+
|
29
|
+
#@ssd_db = PStore.new @ssd_path, true
|
30
|
+
@ssd_db = YAML::Store.new @ssd_path, true
|
31
|
+
|
32
|
+
@ssd_db.ultra_safe = true
|
33
|
+
#@ssd_db.transaction(true) {}
|
34
|
+
end
|
35
|
+
|
36
|
+
def write
|
37
|
+
p "writing"
|
38
|
+
@ssd_db.transaction do
|
39
|
+
@ssd_db[Time.now.utc.to_s + "_" + @key.to_s ] = @value
|
40
|
+
#@ssd_db[@key.to_s] = @value
|
41
|
+
end
|
42
|
+
@ssd_db
|
43
|
+
end
|
44
|
+
|
45
|
+
def dump
|
46
|
+
p "dump all"
|
47
|
+
# true sets it to be read-only transaction
|
48
|
+
@ssd_db.transaction true do
|
49
|
+
@ssd_db[@key.to_s]
|
50
|
+
end
|
51
|
+
@ssd_db
|
52
|
+
end
|
53
|
+
|
54
|
+
def read
|
55
|
+
#setup ssd
|
56
|
+
@ssd_db.transaction true do
|
57
|
+
@ssd_db[@ssd_db.roots.last]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
def self.write path, key, value
|
66
|
+
ssd_db = Store.new path, key, value
|
67
|
+
ssd_db.write
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.read path, key
|
71
|
+
ssd_db = Store.new path, key
|
72
|
+
ssd_db.read
|
73
|
+
end
|
12
74
|
|
13
|
-
def self.
|
14
|
-
|
15
|
-
|
75
|
+
def self.dump path, key
|
76
|
+
ssd_db = Store.new path, key
|
77
|
+
ssd_db.dump
|
16
78
|
end
|
79
|
+
|
17
80
|
end
|
data/lib/ssd/class_methods.rb
CHANGED
@@ -1,82 +1,84 @@
|
|
1
1
|
module SSD
|
2
2
|
module Internals
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
3
|
+
module Entity
|
4
|
+
module ClassMethods
|
5
|
+
def self.extended(base)
|
6
|
+
@@ssd_name = base.new.class.to_s.downcase
|
7
|
+
#FileUtils::mkdir_p 'DS'
|
8
|
+
#@@ssd_path = "DS/#{name}.pstore"
|
9
|
+
#@@ssd_db = PStore.new @@ssd_path, true
|
10
|
+
#@@ssd_db.ultra_safe = true
|
11
|
+
#@@ssd_db.transaction(true) {}
|
12
|
+
#@@ssd_db
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
15
|
+
def setup ssd
|
16
|
+
@@ssd = ssd
|
17
|
+
FileUtils::mkdir_p ".ssd/#{@@ssd_name}"
|
18
|
+
@@ssd_path = ".ssd/#{@@ssd_name}/#{@@ssd}.ssd"
|
19
|
+
@@ssd_db = PStore.new @@ssd_path, true
|
20
|
+
@@ssd_db.ultra_safe = true
|
21
|
+
@@ssd_db.transaction(true) {}
|
22
|
+
return @@ssd_db
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
def last_key ssd
|
26
|
+
setup ssd
|
27
|
+
last_key = @@ssd_db.transaction true do
|
28
|
+
@@ssd_db.roots
|
29
|
+
end
|
30
|
+
last_key.last
|
28
31
|
end
|
29
|
-
last_key.last
|
30
|
-
end
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
33
|
+
def keys ssd
|
34
|
+
setup ssd
|
35
|
+
@@ssd_db.transaction true do
|
36
|
+
@@ssd_db.roots
|
37
|
+
end
|
36
38
|
end
|
37
|
-
end
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
40
|
+
def key? ssd
|
41
|
+
setup ssd
|
42
|
+
@@ssd_db.transaction true do
|
43
|
+
@@ssd_db.root? ssd
|
44
|
+
end
|
43
45
|
end
|
44
|
-
end
|
45
46
|
|
46
|
-
|
47
|
+
alias exists? key?
|
47
48
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
49
|
+
def [] ssd
|
50
|
+
setup ssd
|
51
|
+
@@ssd_db.transaction true do
|
52
|
+
@@ssd_db[ssd]
|
53
|
+
end
|
52
54
|
end
|
53
|
-
end
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
56
|
+
def ssd ssd, default = nil
|
57
|
+
#TODO add raise if ssd.nil?
|
58
|
+
last_key = (last_key ssd)
|
59
|
+
@@ssd_db.transaction true do
|
60
|
+
@@ssd_db.fetch last_key, default
|
61
|
+
end
|
60
62
|
end
|
61
|
-
end
|
62
63
|
|
63
|
-
|
64
|
-
|
64
|
+
#alias get ssd
|
65
|
+
#alias find ssd
|
65
66
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
67
|
+
def delete *ssds
|
68
|
+
@@ssd_db.transaction do
|
69
|
+
ssds.each do |ssd|
|
70
|
+
@@ssd_db.delete ssd.to_sym
|
71
|
+
end
|
72
|
+
@@ssd_db.commit
|
70
73
|
end
|
71
|
-
@@ssd_db.commit
|
72
74
|
end
|
73
|
-
|
74
|
-
alias remove delete
|
75
|
+
alias remove delete
|
75
76
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
77
|
+
def count ssd
|
78
|
+
setup ssd
|
79
|
+
$log.info("count")
|
80
|
+
return keys(ssd).count
|
81
|
+
end
|
80
82
|
end
|
81
83
|
end
|
82
84
|
end
|
data/lib/ssd/instance_methods.rb
CHANGED
@@ -1,48 +1,50 @@
|
|
1
1
|
module SSD
|
2
2
|
module Internals
|
3
|
-
module
|
3
|
+
module Entity
|
4
|
+
module InstanceMethods
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
def self.included(base)
|
7
|
+
@@ssd_name = base.new.class.to_s.downcase
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
+
attr_accessor :ssd
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
12
|
+
def ssd= value
|
13
|
+
@ssd = value
|
14
|
+
FileUtils::mkdir_p ".ssd/#{@@ssd_name}"
|
15
|
+
@@ssd_path = ".ssd/#{@@ssd_name}/#{@ssd}.ssd"
|
16
|
+
@@ssd_db = PStore.new @@ssd_path, true
|
17
|
+
@@ssd_db.ultra_safe = true
|
18
|
+
@@ssd_db.transaction(true) {}
|
19
|
+
return @@ssd_db
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
def transaction
|
23
|
+
@@ssd_db.transaction do
|
24
|
+
yield @@ssd_db
|
25
|
+
@@ssd_db.commit
|
26
|
+
end
|
25
27
|
end
|
26
|
-
end
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
29
|
+
def []=
|
30
|
+
@@ssd_path = ".ssd/#{@@ssd_name}/#{@ssd}.ssd"
|
31
|
+
@@ssd_db = PStore.new @@ssd_path, true
|
32
|
+
begin
|
33
|
+
if !@ssd.nil? then
|
34
|
+
@@ssd_db.transaction do
|
35
|
+
#todo should be somthing like??? timestamp instead of ssd as a key?? and use .last while reading
|
36
|
+
@@ssd_db[Time.now.utc.to_s + "_" + Random.new_seed.to_s ] = self
|
37
|
+
end
|
38
|
+
else
|
39
|
+
raise 'ssd key can not be nil. see more (documentation url)'
|
36
40
|
end
|
37
|
-
|
38
|
-
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
41
|
+
end
|
42
|
+
end
|
42
43
|
|
43
|
-
|
44
|
-
|
45
|
-
|
44
|
+
alias append! []=
|
45
|
+
alias save! []=
|
46
|
+
alias store! []=
|
47
|
+
end
|
46
48
|
end
|
47
49
|
end
|
48
50
|
end
|
data/lib/ssd/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ssd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- zotherstupidguy
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10-
|
11
|
+
date: 2016-10-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|