fmcache 0.1.3 → 0.2.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
2
  SHA256:
3
- metadata.gz: 3420398f6bb70d1972fe648bc4a880ac64bbc08d43cad850109b60a11cee4c38
4
- data.tar.gz: 170a4ef45eaa1ebdb23b12abfa7d142792a33cbad1383c0470c2cabcfc202e2c
3
+ metadata.gz: 681a0559134840d7fb2a89c83adb8ccf2ab337d52ba3874da6a98b414ab8c90a
4
+ data.tar.gz: f4d2e887054ff80d5d72e75e29d8f4c945b6620f6fe35d4453a6a446705203ec
5
5
  SHA512:
6
- metadata.gz: e633c361b62356812fcce2fcdab642b3678d444c063fe06497644146e3ad02fddfa5927664418c0bbdaf7b89c3835a8d8118bcca1b8e3cb92e9eeaeac823c739
7
- data.tar.gz: 4ae7c3394bb79a1535f5eac8625b18ccbfd4f318a54e855e94dbe25584e46cb7e8d979beed88a06736d5131da73d4cebb481315e378217b5c74049d272d78ed0
6
+ metadata.gz: ad936f1afa79d39ae0b94c5eb64db9921e04888d5b84bd3a97e7e18a52a009960ce6601d8917a868eb6549fb867e0f5fee00b502a07870a6e2a965643b59a213
7
+ data.tar.gz: 8560db62119da8501bc671b900a06dc26fa600cb0cd8f5fc8cb304e0430ba894c14003002dff592b34da0da4d92ced535dc9476cae8b3615e19673896ba9c37a
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fmcache (0.1.3)
5
- field_mask_parser (~> 0.4.2)
4
+ fmcache (0.2.1)
5
+ field_mask_parser (~> 0.4.3)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
@@ -22,7 +22,7 @@ GEM
22
22
  coderay (1.1.2)
23
23
  concurrent-ruby (1.1.3)
24
24
  diff-lcs (1.3)
25
- field_mask_parser (0.4.2)
25
+ field_mask_parser (0.4.3)
26
26
  i18n (1.2.0)
27
27
  concurrent-ruby (~> 1.0)
28
28
  method_source (0.9.2)
data/fmcache.gemspec CHANGED
@@ -30,5 +30,5 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency "mock_redis", "~> 0.19"
31
31
  spec.add_development_dependency "redis", "~> 4.0"
32
32
  spec.add_development_dependency "activerecord", "~> 5.2"
33
- spec.add_dependency "field_mask_parser", "~> 0.4.2"
33
+ spec.add_dependency "field_mask_parser", "~> 0.4.3"
34
34
  end
@@ -37,7 +37,12 @@ module FMCache
37
37
  keys = Helper.to_keys(ids)
38
38
  fields = Helper.to_fields(field_mask)
39
39
  h = client.get(keys: keys, fields: fields)
40
- decode(merge(@jsonizer.dejsonize(h), ids), field_mask)
40
+
41
+ with_id, with_no_id = split(h)
42
+ v, i_v, i_i = decode(@jsonizer.dejsonize(with_id), field_mask)
43
+ with_no_id_list = Helper.to_ids(with_no_id.keys)
44
+
45
+ return v, i_v, merge(i_i, with_no_id_list, field_mask)
41
46
  end
42
47
 
43
48
  # @param [<Integer | String>] ids
@@ -50,7 +55,7 @@ module FMCache
50
55
  normalize!(field_mask)
51
56
 
52
57
  values, incomplete_values, incomplete_info = read(ids: ids, field_mask: field_mask)
53
- return values if incomplete_values.size == 0
58
+ return values if incomplete_info.ids.size == 0
54
59
 
55
60
  # NOTE: get new data
56
61
  d = block.call(incomplete_info.ids, incomplete_info.field_mask)
@@ -61,13 +66,16 @@ module FMCache
61
66
 
62
67
  v, i_v, i_i = decode(older.deep_merge(newer), field_mask)
63
68
 
64
- # NOTE: Delete invalid data as read repair
65
- client.hdel(
66
- keys: Helper.to_keys(i_i.ids),
67
- fields: Helper.to_fields(i_i.field_mask),
68
- )
69
+ if i_i.ids.size == 0
70
+ r = values + v + i_v
71
+ else
72
+ # NOTE: Fallback to block.call with full field_mask
73
+ d2 = block.call(i_i.ids, field_mask)
74
+ write(values: d2, field_mask: field_mask)
75
+ r = values + d2
76
+ end
69
77
 
70
- Helper.sort(values + v + i_v, ids)
78
+ Helper.sort(r, ids)
71
79
  end
72
80
 
73
81
  # @param [<Integer | String>] ids
@@ -92,18 +100,6 @@ module FMCache
92
100
  encoder.encode(values, field_mask)
93
101
  end
94
102
 
95
- # @param [{ String => { String => <Hash> } }] hash
96
- # @param [<Integer>] ids
97
- # @return [{ String => { String => <Hash> } }]
98
- def merge(hash, ids)
99
- # NOTE: Set `id` to list. json format must be consistent with Encoder and Decoder
100
- ids.each do |id|
101
- h = hash.fetch(Helper.to_key(id))
102
- h.merge!({ "id" => [{ id: id, p_id: nil, value: id }] })
103
- end
104
- hash
105
- end
106
-
107
103
  # @param [Proc] fm_parser
108
104
  # @return [Proc]
109
105
  def wrap(fm_parser)
@@ -123,5 +119,35 @@ module FMCache
123
119
  normalize!(a)
124
120
  end
125
121
  end
122
+
123
+ def split(h)
124
+ with_id = {}
125
+ with_no_id = {}
126
+
127
+ h.each do |k, v|
128
+ if v.fetch("id").nil?
129
+ with_no_id[k] = v
130
+ else
131
+ with_id[k] = v
132
+ end
133
+ end
134
+
135
+ return with_id, with_no_id
136
+ end
137
+
138
+ # @param [IncompleteInfo] incomplete_info
139
+ # @param [<Integer>] with_no_id_list
140
+ # @param [FieldMaskParser::Node] field_mask
141
+ # @return [IncompleteInfo]
142
+ def merge(incomplete_info, with_no_id_list, field_mask)
143
+ if with_no_id_list.size == 0
144
+ return incomplete_info
145
+ end
146
+
147
+ ids = incomplete_info.ids + with_no_id_list
148
+ fields = Set.new(Helper.to_fields(incomplete_info.field_mask)) | Set.new(Helper.to_fields(field_mask))
149
+
150
+ IncompleteInfo.new(ids: ids, field_mask: fm_parser.call(fields))
151
+ end
126
152
  end
127
153
  end
@@ -1,5 +1,7 @@
1
1
  module FMCache
2
2
  class Helper
3
+ KEY_PREFIX = "fmcache:"
4
+
3
5
  class << self
4
6
  # @param [FieldMaskParser::Node] field_mask
5
7
  # @param [<Symbol>] prefix
@@ -17,7 +19,23 @@ module FMCache
17
19
  # @param [Integer] id
18
20
  # @return [String]
19
21
  def to_key(id)
20
- "fmcache:#{id}"
22
+ "#{KEY_PREFIX}#{id}"
23
+ end
24
+
25
+ # @param [<String>] keys
26
+ # @return [<Integer>]
27
+ def to_ids(keys)
28
+ keys.map { |key| to_id(key) }
29
+ end
30
+
31
+ # @param [String] id
32
+ # @return [Integer]
33
+ def to_id(key)
34
+ if key[0..7] == KEY_PREFIX
35
+ key[8..-1].to_i
36
+ else
37
+ raise "invalid key: #{key}"
38
+ end
21
39
  end
22
40
 
23
41
  # @param [<Hash>] values
@@ -8,7 +8,8 @@ module FMCache
8
8
  attr_reader :ids, :field_mask
9
9
 
10
10
  def ==(other)
11
- @ids == other.ids &&
11
+ self.class == other.class &&
12
+ @ids == other.ids &&
12
13
  @field_mask.to_paths == other.field_mask.to_paths
13
14
  end
14
15
 
@@ -1,3 +1,3 @@
1
1
  module FMCache
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fmcache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nao Minami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-13 00:00:00.000000000 Z
11
+ date: 2018-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -114,14 +114,14 @@ dependencies:
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: 0.4.2
117
+ version: 0.4.3
118
118
  type: :runtime
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: 0.4.2
124
+ version: 0.4.3
125
125
  description: Library for caching json masked by FieldMask
126
126
  email:
127
127
  - south37777@gmail.com