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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f412d9d7275b088771b8f1d3e27c5a5a491e9578
4
- data.tar.gz: 51fbff3062486cc2afebb78809b689432a815d47
3
+ metadata.gz: e37a7abd30a7a3d3909b5349d64c563b37bff37c
4
+ data.tar.gz: c48ad95d301d47ddd0ab30cfeda48df3e3280d9a
5
5
  SHA512:
6
- metadata.gz: daf9d2a958b82531af90c845b385e4b9b820e9e8351e07becce4d1dc7ca733344d418d575ad9b6979cda70caf36a0a9229c3b62e1fe7bcec491e36fdce134346
7
- data.tar.gz: b9ca75641c32bafe80bce22d1a2c0660525463d5fa6982c0ef7c83d8916fc90b1a298048d224c70ad2931d5214c7f97609febff068934655ae5bb5f1aa7ffa92
6
+ metadata.gz: 0e254e2c2d4414f59984e08b05c01a43960b62d57f8605a8fccd49d7327153fa3633c63a733d1d293c10768645a413e4b9f55c4a8bddf189f5336803a962d4ea
7
+ data.tar.gz: 65f12fdc3cee272e634f3f28afc6985e9eeda20b61b606b909715d1d4fdd64d32b31958188e0dff727dd2a3a777b7597fbfeff805e4e685716320b8a32ab7e5d
data/.gitignore CHANGED
@@ -4,3 +4,4 @@ SSD
4
4
  *.gem
5
5
  .ssd
6
6
  pkg
7
+ *.ssd
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
- require 'sinatra'
54
+ # Entity Usage
55
+ require 'sinatra/base'
36
56
  require 'ssd'
37
57
 
38
58
  module MyApp
39
- class User
40
- include SSD
41
- attr_accessor :id # ssd key
42
-
43
- def initialize id
44
- @ssd = id # Setting :ssd
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.included(base)
14
- base.send :include, SSD::Internals::InstanceMethods
15
- base.extend SSD::Internals::ClassMethods
75
+ def self.dump path, key
76
+ ssd_db = Store.new path, key
77
+ ssd_db.dump
16
78
  end
79
+
17
80
  end
@@ -1,82 +1,84 @@
1
1
  module SSD
2
2
  module Internals
3
- module ClassMethods
4
- def self.extended(base)
5
- @@ssd_name = base.new.class.to_s.downcase
6
- #FileUtils::mkdir_p 'DS'
7
- #@@ssd_path = "DS/#{name}.pstore"
8
- #@@ssd_db = PStore.new @@ssd_path, true
9
- #@@ssd_db.ultra_safe = true
10
- #@@ssd_db.transaction(true) {}
11
- #@@ssd_db
12
- end
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
- def setup ssd
15
- @@ssd = ssd
16
- FileUtils::mkdir_p ".ssd/#{@@ssd_name}"
17
- @@ssd_path = ".ssd/#{@@ssd_name}/#{@@ssd}.ssd"
18
- @@ssd_db = PStore.new @@ssd_path, true
19
- @@ssd_db.ultra_safe = true
20
- @@ssd_db.transaction(true) {}
21
- return @@ssd_db
22
- end
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
- def last_key ssd
25
- setup ssd
26
- last_key = @@ssd_db.transaction true do
27
- @@ssd_db.roots
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
- def keys ssd
33
- setup ssd
34
- @@ssd_db.transaction true do
35
- @@ssd_db.roots
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
- def key? ssd
40
- setup ssd
41
- @@ssd_db.transaction true do
42
- @@ssd_db.root? ssd
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
- alias exists? key?
47
+ alias exists? key?
47
48
 
48
- def [] ssd
49
- setup ssd
50
- @@ssd_db.transaction true do
51
- @@ssd_db[ssd]
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
- def ssd ssd, default = nil
56
- #TODO add raise if ssd.nil?
57
- last_key = (last_key ssd)
58
- @@ssd_db.transaction true do
59
- @@ssd_db.fetch last_key, default
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
- #alias get ssd
64
- #alias find ssd
64
+ #alias get ssd
65
+ #alias find ssd
65
66
 
66
- def delete *ssds
67
- @@ssd_db.transaction do
68
- ssds.each do |ssd|
69
- @@ssd_db.delete ssd.to_sym
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
- end
74
- alias remove delete
75
+ alias remove delete
75
76
 
76
- def count ssd
77
- setup ssd
78
- $log.info("count")
79
- return keys(ssd).count
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
@@ -1,48 +1,50 @@
1
1
  module SSD
2
2
  module Internals
3
- module InstanceMethods
3
+ module Entity
4
+ module InstanceMethods
4
5
 
5
- def self.included(base)
6
- @@ssd_name = base.new.class.to_s.downcase
7
- end
6
+ def self.included(base)
7
+ @@ssd_name = base.new.class.to_s.downcase
8
+ end
8
9
 
9
- attr_accessor :ssd
10
+ attr_accessor :ssd
10
11
 
11
- def ssd= value
12
- @ssd = value
13
- FileUtils::mkdir_p ".ssd/#{@@ssd_name}"
14
- @@ssd_path = ".ssd/#{@@ssd_name}/#{@ssd}.ssd"
15
- @@ssd_db = PStore.new @@ssd_path, true
16
- @@ssd_db.ultra_safe = true
17
- @@ssd_db.transaction(true) {}
18
- return @@ssd_db
19
- end
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
- def transaction
22
- @@ssd_db.transaction do
23
- yield @@ssd_db
24
- @@ssd_db.commit
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
- def []=
29
- @@ssd_path = ".ssd/#{@@ssd_name}/#{@ssd}.ssd"
30
- @@ssd_db = PStore.new @@ssd_path, true
31
- begin
32
- if !@ssd.nil? then
33
- @@ssd_db.transaction do
34
- #todo should be somthing like??? timestamp instead of ssd as a key?? and use .last while reading
35
- @@ssd_db[Time.now.utc.to_s + "_" + Random.new_seed.to_s ] = self
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
- else
38
- raise 'ssd key can not be nil. see more (documentation url)'
39
- end
40
- end
41
- end
41
+ end
42
+ end
42
43
 
43
- alias append! []=
44
- alias save! []=
45
- alias store! []=
44
+ alias append! []=
45
+ alias save! []=
46
+ alias store! []=
47
+ end
46
48
  end
47
49
  end
48
50
  end
@@ -1,3 +1,3 @@
1
1
  module SSD
2
- VERSION = "0.1.7"
2
+ VERSION = "1.0.0"
3
3
  end
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.1.7
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-09 00:00:00.000000000 Z
11
+ date: 2016-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler