diver_down 0.0.1.alpha14 → 0.0.1.alpha16

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: 12f54ddda5108f877d6e1510710b5a397dfc92db94c21f6521739b5032e4351f
4
- data.tar.gz: 4c3c4b1063fd4da506077a8f9908a9f621336ffed58e0f5fc8b4c9c5b54cbd15
3
+ metadata.gz: 88a43fc4236cb489ca44a9105db9ad3750f53a2929a9153db564e20d3f4e2fee
4
+ data.tar.gz: 589ab5cce7bfdee343a4c3d967a7aece57ff1d389d3700af222579c450a13a42
5
5
  SHA512:
6
- metadata.gz: 476bee3370a190cb94adb215abe7c58deb8c0dac475fca258c9e673d09471c477f5ba7826ba2575524c0f80c1f5454236bb71fd244c8306d02d75ad7766e85b9
7
- data.tar.gz: 2e155be624f952d2b0e69d8d9a5ec549053e7f2ddfa469c2ce998777421cab30a73a423a2ecba5264f1fe7e603c4186413a801dfcbe49e82ca39d8bad7ec4531
6
+ metadata.gz: eda67006560b775d12e582afa991b789d727f2d947ad82cab470408690ab682133c34d433309467ae67637472de1cc82f246e6618c4cbc36f2a5e7f8d6bb555f
7
+ data.tar.gz: e116e4edbcd125c4a56e8641cbda5b6427ce4c6beaa86935a1339fabb9ff3db14d6bd9319538cf2643d270d1b8ee6019b74bc70089e14820511f05c7578f2079
data/exe/diver_down_web CHANGED
@@ -3,7 +3,6 @@
3
3
 
4
4
  require 'bundler/setup'
5
5
  require 'rack/contrib'
6
- require 'webrick'
7
6
  require 'diver_down'
8
7
  require 'diver_down-web'
9
8
  require 'optparse'
@@ -47,9 +46,9 @@ begin
47
46
  # Rack 2.0
48
47
  require 'rack'
49
48
  require 'rack/server'
50
- Rack::Server.new(app:, server: :webrick).start
49
+ Rack::Server.new(app:).start
51
50
  rescue LoadError
52
51
  # Rack 3.0
53
52
  require 'rackup'
54
- Rackup::Server.new(app:, server: :webrick).start
53
+ Rackup::Server.new(app:).start
55
54
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DiverDown
4
- VERSION = '0.0.1.alpha14'
4
+ VERSION = '0.0.1.alpha16'
5
5
  end
@@ -12,14 +12,18 @@ module DiverDown
12
12
  :per
13
13
  )
14
14
 
15
+ M = Mutex.new
16
+
17
+ attr_reader :store
18
+
15
19
  # @param store [DiverDown::Definition::Store]
16
20
  # @param metadata [DiverDown::Web::Metadata]
17
- # @param request [Rack::Request]
18
- def initialize(store:, metadata:, request:)
21
+ def initialize(store:, metadata:)
19
22
  @store = store
20
23
  @metadata = metadata
21
24
  @source_alias_resolver = DiverDown::Web::SourceAliasResolver.new(@metadata.source_alias)
22
- @request = request
25
+ @module_dependency_map = nil
26
+ @module_dependency_map_cache_id = nil
23
27
  end
24
28
 
25
29
  # GET /api/source_aliases.json
@@ -61,15 +65,11 @@ module DiverDown
61
65
  def sources
62
66
  source_names = Set.new
63
67
 
64
- # rubocop:disable Style/HashEachMethods
65
- @store.each do |_, definition|
66
- definition.sources.each do |source|
67
- source_names.add(source.source_name)
68
- end
68
+ @store.combined_definition.sources.each do |source|
69
+ source_names.add(source.source_name)
69
70
  end
70
- # rubocop:enable Style/HashEachMethods
71
71
 
72
- classified_sources_count = source_names.count { @metadata.source(_1).modules? }
72
+ classified_sources_count = source_names.count { @metadata.source(_1).module? }
73
73
 
74
74
  json(
75
75
  sources: source_names.sort.map do |source_name|
@@ -79,9 +79,7 @@ module DiverDown
79
79
  source_name:,
80
80
  resolved_alias: @metadata.source_alias.resolve_alias(source_name),
81
81
  memo: source_metadata.memo,
82
- modules: source_metadata.modules.map do |module_name|
83
- { module_name: }
84
- end,
82
+ module: source_metadata.module,
85
83
  }
86
84
  end,
87
85
  classified_sources_count:
@@ -91,70 +89,71 @@ module DiverDown
91
89
  # GET /api/modules.json
92
90
  def modules
93
91
  # Hash{ DiverDown::Definition::Modulee => Set<Integer> }
94
- module_set = Set.new
92
+ modules = Set.new
95
93
 
96
- # rubocop:disable Style/HashEachMethods
97
- @store.each do |_, definition|
98
- definition.sources.each do |source|
99
- modules = @metadata.source(source.source_name).modules
100
- module_set.add(modules) unless modules.empty?
101
- end
94
+ @store.combined_definition.sources.each do |source|
95
+ modulee = @metadata.source(source.source_name).module
96
+ modules.add(modulee) unless modulee.nil?
102
97
  end
103
- # rubocop:enable Style/HashEachMethods
104
98
 
105
99
  json(
106
- modules: module_set.sort.map do
107
- _1.map do |module_name|
108
- {
109
- module_name:,
110
- }
111
- end
112
- end
100
+ modules: modules.sort
113
101
  )
114
102
  end
115
103
 
116
- # GET /api/modules/:module_name.json
117
- # @param module_names [Array<String>]
118
- def module(module_names)
119
- # Hash{ DiverDown::Definition::Modulee => Set<Integer> }
120
- related_definition_store_ids = Set.new
121
- source_names = Set.new
122
-
123
- # rubocop:disable Style/HashEachMethods
124
- @store.each do |_, definition|
125
- definition.sources.each do |source|
126
- source_module_names = @metadata.source(source.source_name).modules
127
-
128
- next unless source_module_names[0..module_names.size - 1] == module_names
104
+ # GET /api/modules/:modulee.json
105
+ # @param modulee [String]
106
+ def module(modulee)
107
+ module_dependency_map = fetch_module_dependency_map
129
108
 
130
- source_names.add(source.source_name)
131
- related_definition_store_ids.add(definition.store_id)
132
- end
133
- end
134
- # rubocop:enable Style/HashEachMethods
135
-
136
- if related_definition_store_ids.empty?
109
+ unless module_dependency_map.key?(modulee)
137
110
  return not_found
138
111
  end
139
112
 
140
- related_definitions = related_definition_store_ids.map { @store.get(_1) }
113
+ module_dependency = module_dependency_map.fetch(modulee)
141
114
 
142
115
  json(
143
- modules: module_names.map do
116
+ module: modulee,
117
+ module_dependencies: module_dependency.module_dependencies.compact.sort,
118
+ module_reverse_dependencies: module_dependency.module_reverse_dependencies.compact.sort,
119
+ sources: module_dependency.sources.map do |source|
144
120
  {
145
- module_name: _1,
146
- }
147
- end,
148
- sources: source_names.sort.map do |source_name|
149
- {
150
- source_name:,
151
- memo: @metadata.source(source_name).memo,
121
+ source_name: source.source_name,
122
+ module: @metadata.source(source.source_name).module,
123
+ memo: @metadata.source(source.source_name).memo,
124
+ dependencies: source.dependencies.map do |dependency|
125
+ {
126
+ source_name: dependency.source_name,
127
+ module: @metadata.source(dependency.source_name).module,
128
+ method_ids: dependency.method_ids.sort.map do |method_id|
129
+ {
130
+ context: method_id.context,
131
+ name: method_id.name,
132
+ paths: method_id.paths.sort,
133
+ }
134
+ end,
135
+ }
136
+ end,
152
137
  }
153
138
  end,
154
- related_definitions: related_definitions.map do |definition|
139
+ source_reverse_dependencies: module_dependency.source_reverse_dependencies.map do |source|
155
140
  {
156
- id: definition.store_id,
157
- title: definition.title,
141
+ source_name: source.source_name,
142
+ module: @metadata.source(source.source_name).module,
143
+ memo: @metadata.source(source.source_name).memo,
144
+ dependencies: source.dependencies.map do |dependency|
145
+ {
146
+ source_name: dependency.source_name,
147
+ module: @metadata.source(dependency.source_name).module,
148
+ method_ids: dependency.method_ids.sort.map do |method_id|
149
+ {
150
+ context: method_id.context,
151
+ name: method_id.name,
152
+ paths: method_id.paths.sort,
153
+ }
154
+ end,
155
+ }
156
+ end,
158
157
  }
159
158
  end
160
159
  )
@@ -178,7 +177,7 @@ module DiverDown
178
177
  definition_group: definition.definition_group,
179
178
  title: definition.title,
180
179
  sources_count: definition.sources.size,
181
- unclassified_sources_count: definition.sources.reject { @metadata.source(_1.source_name).modules? }.size,
180
+ unclassified_sources_count: definition.sources.reject { @metadata.source(_1.source_name).module? }.size,
182
181
  }
183
182
  end,
184
183
  pagination: pagination.to_h
@@ -200,39 +199,13 @@ module DiverDown
200
199
  )
201
200
  end
202
201
 
203
- # GET /api/module_definition/:module_names.json
204
- #
205
- # @param bit_id [Integer]
206
- # @param compound [Boolean]
207
- # @param concentrate [Boolean]
208
- # @param only_module [Boolean]
209
- # @param modules [Array<String>]
210
- def module_definition(compound, concentrate, only_module, modules)
211
- definition = @store.combined_definition
212
-
213
- # Filter all sources and dependencies by modules if match_modules is given
214
- resolved_definition = DiverDown::Web::ModuleSourcesFilter.new(@metadata).filter(definition, modules:)
215
-
216
- # Resolve source aliases
217
- resolved_definition = @source_alias_resolver.resolve(resolved_definition)
218
-
219
- render_combined_definition(
220
- (1..@store.size).to_a,
221
- resolved_definition,
222
- modules,
223
- compound:,
224
- concentrate:,
225
- only_module:
226
- )
227
- end
228
-
229
202
  # GET /api/definitions/:bit_id.json
230
203
  #
231
204
  # @param bit_id [Integer]
232
205
  # @param compound [Boolean]
233
206
  # @param concentrate [Boolean]
234
207
  # @param only_module [Boolean]
235
- def combine_definitions(bit_id, compound, concentrate, only_module)
208
+ def combine_definitions(bit_id, compound, concentrate, only_module, remove_internal_sources, focus_modules, modules)
236
209
  ids = DiverDown::Web::BitId.bit_id_to_ids(bit_id)
237
210
 
238
211
  valid_ids = ids.select do
@@ -254,6 +227,8 @@ module DiverDown
254
227
  if definition
255
228
  # Resolve source aliases
256
229
  resolved_definition = @source_alias_resolver.resolve(definition)
230
+ # Filter sources and dependencies by condition
231
+ resolved_definition = DiverDown::Web::DefinitionFilter.new(@metadata, focus_modules:, modules:, remove_internal_sources:).filter(resolved_definition)
257
232
 
258
233
  render_combined_definition(
259
234
  valid_ids,
@@ -304,20 +279,18 @@ module DiverDown
304
279
 
305
280
  return not_found if related_definitions.empty?
306
281
 
307
- module_names = if found_sources.empty?
308
- []
309
- else
310
- source = DiverDown::Definition::Source.combine(*found_sources)
311
- @metadata.source(source.source_name).modules
312
- end
282
+ modulee = if found_sources.empty?
283
+ nil
284
+ else
285
+ source = DiverDown::Definition::Source.combine(*found_sources)
286
+ @metadata.source(source.source_name).module
287
+ end
313
288
 
314
289
  json(
315
290
  source_name:,
316
291
  resolved_alias: @metadata.source_alias.resolve_alias(source_name),
317
292
  memo: @metadata.source(source_name).memo,
318
- modules: module_names.map do
319
- { module_name: _1 }
320
- end,
293
+ module: modulee,
321
294
  related_definitions: related_definitions.map do |id, definition|
322
295
  {
323
296
  id:,
@@ -327,6 +300,7 @@ module DiverDown
327
300
  reverse_dependencies: reverse_dependencies.values.map { |reverse_dependency|
328
301
  {
329
302
  source_name: reverse_dependency.source_name,
303
+ module: @metadata.source(reverse_dependency.source_name).module,
330
304
  method_ids: reverse_dependency.method_ids.sort.map do |method_id|
331
305
  {
332
306
  context: method_id.context,
@@ -339,11 +313,11 @@ module DiverDown
339
313
  )
340
314
  end
341
315
 
342
- # POST /api/sources/:source_name/modules.json
316
+ # POST /api/sources/:source_name/module.json
343
317
  #
344
318
  # @param source_name [String]
345
- # @param modules [Array<String>]
346
- def set_modules(source_name, modules)
319
+ # @param module [Array<String>]
320
+ def set_module(source_name, modulee)
347
321
  found_source = @store.any? do |_, definition|
348
322
  definition.sources.any? do |source|
349
323
  source.source_name == source_name
@@ -351,7 +325,7 @@ module DiverDown
351
325
  end
352
326
 
353
327
  if found_source
354
- @metadata.source(source_name).modules = modules
328
+ @metadata.source(source_name).module = modulee
355
329
  @metadata.flush
356
330
 
357
331
  json({})
@@ -388,8 +362,6 @@ module DiverDown
388
362
 
389
363
  private
390
364
 
391
- attr_reader :request, :store
392
-
393
365
  def build_method_id_key(dependency, method_id)
394
366
  "#{dependency.source_name}-#{method_id.context}-#{method_id.name}"
395
367
  end
@@ -451,13 +423,28 @@ module DiverDown
451
423
  source_name: _1.source_name,
452
424
  resolved_alias: @metadata.source_alias.resolve_alias(_1.source_name),
453
425
  memo: @metadata.source(_1.source_name).memo,
454
- modules: @metadata.source(_1.source_name).modules.map do |module_name|
455
- { module_name: }
426
+ module: @metadata.source(_1.source_name).module,
427
+ dependencies: _1.dependencies.map do |dependency|
428
+ {
429
+ source_name: dependency.source_name,
430
+ }
456
431
  end,
457
432
  }
458
433
  end
459
434
  )
460
435
  end
436
+
437
+ def fetch_module_dependency_map
438
+ M.synchronize do
439
+ if @module_dependency_map_cache_id != @store.combined_definition.object_id
440
+ definition = @store.combined_definition
441
+ @module_dependency_map = DiverDown::Web::DefinitionModuleDependencies.new(@metadata, definition).build_module_dependency_map
442
+ @module_dependency_map_cache_id = definition.object_id
443
+ end
444
+ end
445
+
446
+ @module_dependency_map
447
+ end
461
448
  end
462
449
  end
463
450
  end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DiverDown
4
+ class Web
5
+ class DefinitionFilter
6
+ # @param metadata_alias [DiverDown::Web::Metadata]
7
+ # @param focus_modules [Array<String>]
8
+ # @param modules [Array<String>]
9
+ # @param remove_internal_sources [Boolean]
10
+ def initialize(metadata, focus_modules: [], modules: [], remove_internal_sources: false)
11
+ @metadata = metadata
12
+ @focus_modules = focus_modules
13
+ @modules = modules | focus_modules
14
+ @remove_internal_sources = remove_internal_sources
15
+ end
16
+
17
+ # @param definition [DiverDown::Definition]
18
+ # @return [DiverDown::Definition]
19
+ def filter(definition)
20
+ return definition if !@remove_internal_sources && @modules.empty?
21
+
22
+ new_definition = DiverDown::Definition.new(
23
+ definition_group: definition.definition_group,
24
+ title: definition.title
25
+ )
26
+
27
+ source_names = extract_match_source_names(definition.sources)
28
+
29
+ definition.sources.each do |source|
30
+ next unless source_names.include?(source.source_name)
31
+
32
+ new_source = new_definition.find_or_build_source(source.source_name)
33
+
34
+ source.dependencies.each do |dependency|
35
+ next unless source_names.include?(dependency.source_name)
36
+ next unless focus_module?(source, dependency)
37
+ next if @remove_internal_sources && @metadata.source(source.source_name).module == @metadata.source(dependency.source_name).module
38
+
39
+ new_dependency = new_source.find_or_build_dependency(dependency.source_name)
40
+
41
+ next unless new_dependency
42
+
43
+ dependency.method_ids.each do |method_id|
44
+ new_method_id = new_dependency.find_or_build_method_id(
45
+ context: method_id.context,
46
+ name: method_id.name
47
+ )
48
+
49
+ method_id.paths.each do |path|
50
+ new_method_id.add_path(path)
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ new_definition
57
+ end
58
+
59
+ private
60
+
61
+ def focus_module?(source, dependency)
62
+ return true if @focus_modules.empty?
63
+ return true if @focus_modules.include?(@metadata.source(source.source_name).module)
64
+ return true if @focus_modules.include?(@metadata.source(dependency.source_name).module)
65
+
66
+ false
67
+ end
68
+
69
+ def extract_match_source_names(sources)
70
+ # Filter sources by modules.
71
+ source_names = sources.filter_map do |source|
72
+ source.source_name if match_module?(source.source_name)
73
+ end.to_set
74
+
75
+ if @remove_internal_sources
76
+ # Remove internal sources.
77
+ external_called_sources = Set.new
78
+
79
+ sources.each do |source|
80
+ next unless source_names.include?(source.source_name)
81
+
82
+ source_module = @metadata.source(source.source_name).module
83
+
84
+ source.dependencies.each do |dependency|
85
+ next unless source_names.include?(dependency.source_name)
86
+
87
+ dependency_module = @metadata.source(dependency.source_name).module
88
+
89
+ next if source_module == dependency_module
90
+
91
+ external_called_sources << source.source_name
92
+ external_called_sources << dependency.source_name
93
+ break
94
+ end
95
+ end
96
+
97
+ external_called_sources
98
+ else
99
+ source_names
100
+ end
101
+ end
102
+
103
+ def top_module(source_name)
104
+ @metadata.source(source_name).modules.first
105
+ end
106
+
107
+ def match_module?(source_name)
108
+ @modules.empty? || @modules.include?(@metadata.source(source_name).module)
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DiverDown
4
+ class Web
5
+ class DefinitionModuleDependencies
6
+ class ModuleDependency
7
+ attr_accessor :module
8
+ attr_reader :module_dependencies, :module_reverse_dependencies, :source_map, :source_reverse_dependency_map
9
+
10
+ def initialize(modulee)
11
+ @module = modulee
12
+ @module_dependencies = Set.new
13
+ @module_reverse_dependencies = Set.new
14
+ @source_map = {}
15
+ @source_reverse_dependency_map = {}
16
+ end
17
+
18
+ # @return [Array<Source>]
19
+ def sources
20
+ @source_map.keys.sort.map { @source_map.fetch(_1) }
21
+ end
22
+
23
+ # @return [Array<Source>]
24
+ def source_reverse_dependencies
25
+ @source_reverse_dependency_map.keys.sort.map { @source_reverse_dependency_map.fetch(_1) }
26
+ end
27
+ end
28
+
29
+ def initialize(metadata, definition)
30
+ @metadata = metadata
31
+ @definition = definition
32
+ end
33
+
34
+ # @return [Hash{ String => Hash{ String => Array<DiverDown::Definition::Source> } }]
35
+ def build_module_dependency_map
36
+ module_dependency_map = Hash.new { |h, k| h[k] = ModuleDependency.new(k) }
37
+
38
+ @definition.sources.each do |source|
39
+ source_module = @metadata.source(source.source_name).module
40
+ next if source_module.nil?
41
+
42
+ source_module_dependency = module_dependency_map[source_module]
43
+ source_module_dependency.source_map[source.source_name] ||= DiverDown::Definition::Source.new(source_name: source.source_name)
44
+
45
+ source.dependencies.each do |dependency|
46
+ dependency_module = @metadata.source(dependency.source_name).module
47
+
48
+ next if source_module == dependency_module
49
+ next if dependency_module.nil?
50
+
51
+ dependency_module_dependency = module_dependency_map[dependency_module]
52
+
53
+ # Add module dependencies
54
+ source_module_dependency.module_dependencies.add(dependency_module)
55
+
56
+ # Add module reverse dependencies
57
+ dependency_module_dependency.module_reverse_dependencies.add(source_module)
58
+
59
+ # Add source
60
+ definition_source = source_module_dependency.source_map[source.source_name]
61
+ definition_dependency = definition_source.find_or_build_dependency(dependency.source_name)
62
+ dependency.method_ids.each do |method_id|
63
+ definition_dependency.find_or_build_method_id(name: method_id.name, context: method_id.context).add_path(*method_id.paths)
64
+ end
65
+
66
+ # Add source reverse dependencies
67
+ definition_source = dependency_module_dependency.source_reverse_dependency_map[dependency.source_name] ||= DiverDown::Definition::Source.new(source_name: dependency.source_name)
68
+ definition_dependency = definition_source.find_or_build_dependency(source.source_name)
69
+ dependency.method_ids.each do |method_id|
70
+ definition_dependency.find_or_build_method_id(name: method_id.name, context: method_id.context).add_path(*method_id.paths)
71
+ end
72
+ end
73
+ end
74
+
75
+ module_dependency_map
76
+ end
77
+ end
78
+ end
79
+ end