Skip to content

Commit 773f93a

Browse files
authored
Merge pull request #59 from ruby-rdf/feat/optimizations
Memory optimisation & rubocop on tests
2 parents a73b483 + c9e4542 commit 773f93a

36 files changed

+3129
-2822
lines changed

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
require:
33
- rubocop-performance
4+
- rubocop-rspec
45

56
AllCops:
67
DisplayCopNames: true

Gemfile

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,53 @@
1+
# frozen_string_literal: true
2+
13
source "https://rubygems.org"
2-
gem "nokogiri", '~> 1.13', '>= 1.13.4'
4+
gem "nokogiri", '~> 1.13', '>= 1.13.4'
35

46
gemspec
5-
gem 'rdf', git: "https://github.com/ruby-rdf/rdf", branch: "develop"
6-
gem 'json-canonicalization',git: "https://github.com/dryruby/json-canonicalization",branch: "develop"
7+
gem 'json-canonicalization', git: "https://github.com/dryruby/json-canonicalization", branch: "develop"
8+
gem 'rdf', git: "https://github.com/ruby-rdf/rdf", branch: "develop"
79

810
group :development do
9-
gem 'ebnf', git: "https://github.com/dryruby/ebnf", branch: "develop"
10-
gem 'json-ld-preloaded', github: "ruby-rdf/json-ld-preloaded", branch: "develop"
11-
gem 'ld-patch', github: "ruby-rdf/ld-patch", branch: "develop"
12-
gem 'linkeddata', git: "https://github.com/ruby-rdf/linkeddata", branch: "develop"
13-
gem 'rack-linkeddata', git: "https://github.com/ruby-rdf/rack-linkeddata", branch: "develop"
14-
gem 'rdf-aggregate-repo', git: "https://github.com/ruby-rdf/rdf-aggregate-repo", branch: "develop"
15-
gem 'rdf-isomorphic', git: "https://github.com/ruby-rdf/rdf-isomorphic", branch: "develop"
16-
gem 'rdf-json', github: "ruby-rdf/rdf-json", branch: "develop"
17-
gem 'rdf-microdata', git: "https://github.com/ruby-rdf/rdf-microdata", branch: "develop"
18-
gem 'rdf-n3', github: "ruby-rdf/rdf-n3", branch: "develop"
19-
gem 'rdf-normalize', github: "ruby-rdf/rdf-normalize", branch: "develop"
20-
gem 'rdf-rdfa', git: "https://github.com/ruby-rdf/rdf-rdfa", branch: "develop"
21-
gem 'rdf-rdfxml', git: "https://github.com/ruby-rdf/rdf-rdfxml", branch: "develop"
22-
gem 'rdf-reasoner', github: "ruby-rdf/rdf-reasoner", branch: "develop"
23-
gem 'rdf-spec', git: "https://github.com/ruby-rdf/rdf-spec", branch: "develop"
24-
gem 'rdf-tabular', github: "ruby-rdf/rdf-tabular", branch: "develop"
25-
gem 'rdf-trig', git: "https://github.com/ruby-rdf/rdf-trig", branch: "develop"
26-
gem 'rdf-trix', github: "ruby-rdf/rdf-trix", branch: "develop"
27-
gem 'rdf-turtle', git: "https://github.com/ruby-rdf/rdf-turtle", branch: "develop"
28-
gem 'rdf-vocab', git: "https://github.com/ruby-rdf/rdf-vocab", branch: "develop"
29-
gem 'rdf-xsd', git: "https://github.com/ruby-rdf/rdf-xsd", branch: "develop"
30-
gem 'sinatra-linkeddata', git: "https://github.com/ruby-rdf/sinatra-linkeddata", branch: "develop"
31-
gem 'shex', github: "ruby-rdf/shex", branch: "develop"
32-
gem 'sparql', git: "https://github.com/ruby-rdf/sparql", branch: "develop"
33-
gem 'sparql-client', git: "https://github.com/ruby-rdf/sparql-client", branch: "develop"
34-
gem 'sxp', git: "https://github.com/dryruby/sxp.rb", branch: "develop"
35-
gem 'fasterer'
3611
gem 'earl-report'
37-
gem 'ruby-prof', platforms: :mri
12+
gem 'ebnf', git: "https://github.com/dryruby/ebnf", branch: "develop"
13+
gem 'fasterer'
14+
gem 'json-ld-preloaded', github: "ruby-rdf/json-ld-preloaded", branch: "develop"
15+
gem 'ld-patch', github: "ruby-rdf/ld-patch", branch: "develop"
16+
gem 'linkeddata', git: "https://github.com/ruby-rdf/linkeddata", branch: "develop"
17+
gem 'rack-linkeddata', git: "https://github.com/ruby-rdf/rack-linkeddata", branch: "develop"
18+
gem 'rdf-aggregate-repo', git: "https://github.com/ruby-rdf/rdf-aggregate-repo", branch: "develop"
19+
gem 'rdf-isomorphic', git: "https://github.com/ruby-rdf/rdf-isomorphic", branch: "develop"
20+
gem 'rdf-json', github: "ruby-rdf/rdf-json", branch: "develop"
21+
gem 'rdf-microdata', git: "https://github.com/ruby-rdf/rdf-microdata", branch: "develop"
22+
gem 'rdf-n3', github: "ruby-rdf/rdf-n3", branch: "develop"
23+
gem 'rdf-normalize', github: "ruby-rdf/rdf-normalize", branch: "develop"
24+
gem 'rdf-rdfa', git: "https://github.com/ruby-rdf/rdf-rdfa", branch: "develop"
25+
gem 'rdf-rdfxml', git: "https://github.com/ruby-rdf/rdf-rdfxml", branch: "develop"
26+
gem 'rdf-reasoner', github: "ruby-rdf/rdf-reasoner", branch: "develop"
27+
gem 'rdf-spec', git: "https://github.com/ruby-rdf/rdf-spec", branch: "develop"
28+
gem 'rdf-tabular', github: "ruby-rdf/rdf-tabular", branch: "develop"
29+
gem 'rdf-trig', git: "https://github.com/ruby-rdf/rdf-trig", branch: "develop"
30+
gem 'rdf-trix', github: "ruby-rdf/rdf-trix", branch: "develop"
31+
gem 'rdf-turtle', git: "https://github.com/ruby-rdf/rdf-turtle", branch: "develop"
32+
gem 'rdf-vocab', git: "https://github.com/ruby-rdf/rdf-vocab", branch: "develop"
33+
gem 'rdf-xsd', git: "https://github.com/ruby-rdf/rdf-xsd", branch: "develop"
34+
gem 'ruby-prof', platforms: :mri
35+
gem 'shex', github: "ruby-rdf/shex", branch: "develop"
36+
gem 'sinatra-linkeddata', git: "https://github.com/ruby-rdf/sinatra-linkeddata", branch: "develop"
37+
gem 'sparql', git: "https://github.com/ruby-rdf/sparql", branch: "develop"
38+
gem 'sparql-client', git: "https://github.com/ruby-rdf/sparql-client", branch: "develop"
39+
gem 'sxp', git: "https://github.com/dryruby/sxp.rb", branch: "develop"
3840
end
3941

4042
group :development, :test do
41-
gem 'simplecov', '~> 0.21', platforms: :mri
42-
gem 'simplecov-lcov', '~> 0.8', platforms: :mri
43-
gem 'psych', platforms: [:mri, :rbx]
4443
gem 'benchmark-ips'
44+
gem 'psych', platforms: %i[mri rbx]
4545
gem 'rake'
4646
gem 'rubocop'
4747
gem 'rubocop-performance'
48+
gem 'rubocop-rspec'
49+
gem 'simplecov', '~> 0.21', platforms: :mri
50+
gem 'simplecov-lcov', '~> 0.8', platforms: :mri
4851
end
4952

5053
group :debug do

Rakefile

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
# frozen_string_literal: true
2+
13
require 'rubygems'
24

3-
task default: [ :spec ]
5+
task default: [:spec]
46

57
namespace :gem do
68
desc "Build the json-ld-#{File.read('VERSION').chomp}.gem file"
@@ -17,18 +19,18 @@ end
1719
require 'rspec/core/rake_task'
1820
desc 'Run specifications'
1921
RSpec::Core::RakeTask.new(:spec) do |spec|
20-
spec.rspec_opts = %w(--options spec/spec.opts) if File.exists?('spec/spec.opts')
22+
spec.rspec_opts = %w[--options spec/spec.opts] if File.exist?('spec/spec.opts')
2123
end
2224

2325
desc "Generate schema.org context"
2426
task :schema_context do
25-
%x(
27+
`
2628
script/gen_context https://schema.org/docs/schema_org_rdfa.html \
2729
--vocab http://schema.org/ \
2830
--prefix 'schema http://schema.org/' \
2931
--body --hier \
3032
--o etc/schema.org.jsonld
31-
)
33+
`
3234
end
3335

3436
desc "Create concatenated test manifests"
@@ -37,68 +39,73 @@ file "etc/manifests.nt" do
3739
require 'json/ld'
3840
require 'rdf/ntriples'
3941
graph = RDF::Graph.new do |g|
40-
%w( https://w3c.github.io/json-ld-api/tests/compact-manifest.jsonld
42+
%w[ https://w3c.github.io/json-ld-api/tests/compact-manifest.jsonld
4143
https://w3c.github.io/json-ld-api/tests/expand-manifest.jsonld
4244
https://w3c.github.io/json-ld-api/tests/flatten-manifest.jsonld
4345
https://w3c.github.io/json-ld-api/tests/fromRdf-manifest.jsonld
4446
https://w3c.github.io/json-ld-api/tests/html-manifest.jsonld
4547
https://w3c.github.io/json-ld-api/tests/remote-doc-manifest.jsonld
4648
https://w3c.github.io/json-ld-api/tests/toRdf-manifest.jsonld
47-
https://w3c.github.io/json-ld-framing/tests/frame-manifest.jsonld
48-
).each do |man|
49+
https://w3c.github.io/json-ld-framing/tests/frame-manifest.jsonld].each do |man|
4950
puts "load #{man}"
5051
g.load(man, unique_bnodes: true)
5152
end
5253
end
5354
puts "write"
54-
RDF::NTriples::Writer.open("etc/manifests.nt", unique_bnodes: true, validate: false) {|w| w << graph}
55+
RDF::NTriples::Writer.open("etc/manifests.nt", unique_bnodes: true, validate: false) { |w| w << graph }
5556
end
5657

5758
# Presentation building
5859
namespace :presentation do
5960
desc "Clean presentation files"
6061
task :clean do
61-
FileUtils.rm %w(compacted expanded framed).map {|f| "presentation/dbpedia/#{f}.jsonld"}
62+
FileUtils.rm %w[compacted expanded framed].map { |f| "presentation/dbpedia/#{f}.jsonld" }
6263
end
6364

6465
desc "Build presentation files"
65-
task build: %w(
66+
task build: %w[
6667
presentation/dbpedia/expanded.jsonld
6768
presentation/dbpedia/compacted.jsonld
6869
presentation/dbpedia/framed.jsonld
69-
)
70+
]
7071

7172
desc "Build expanded example"
72-
file "presentation/dbpedia/expanded.jsonld" => %w(
73+
file "presentation/dbpedia/expanded.jsonld" => %w[
7374
presentation/dbpedia/orig.jsonld
74-
presentation/dbpedia/expanded-context.jsonld) do
75-
system(%w(
76-
script/parse
77-
--expand presentation/dbpedia/orig.jsonld
78-
--context presentation/dbpedia/expanded-context.jsonld
79-
-o presentation/dbpedia/expanded.jsonld).join(" "))
75+
presentation/dbpedia/expanded-context.jsonld
76+
] do
77+
system(%w[
78+
script/parse
79+
--expand presentation/dbpedia/orig.jsonld
80+
--context presentation/dbpedia/expanded-context.jsonld
81+
-o presentation/dbpedia/expanded.jsonld
82+
].join(" "))
8083
end
8184

8285
desc "Build compacted example"
83-
file "presentation/dbpedia/compacted.jsonld" => %w(
86+
file "presentation/dbpedia/compacted.jsonld" => %w[
8487
presentation/dbpedia/expanded.jsonld
85-
presentation/dbpedia/compact-context.jsonld) do
86-
system(%w(
87-
script/parse
88-
--compact presentation/dbpedia/expanded.jsonld
89-
--context presentation/dbpedia/compact-context.jsonld
90-
-o presentation/dbpedia/compacted.jsonld).join(" "))
88+
presentation/dbpedia/compact-context.jsonld
89+
] do
90+
system(%w[
91+
script/parse
92+
--compact presentation/dbpedia/expanded.jsonld
93+
--context presentation/dbpedia/compact-context.jsonld
94+
-o presentation/dbpedia/compacted.jsonld
95+
].join(" "))
9196
end
9297

9398
desc "Build framed example"
94-
file "presentation/dbpedia/framed.jsonld" => %w(
99+
file "presentation/dbpedia/framed.jsonld" => %w[
95100
presentation/dbpedia/expanded.jsonld
96-
presentation/dbpedia/frame.jsonld) do
97-
system(%w(
98-
script/parse
99-
--frame presentation/dbpedia/frame.jsonld
100-
presentation/dbpedia/expanded.jsonld
101-
-o presentation/dbpedia/framed.jsonld).join(" "))
101+
presentation/dbpedia/frame.jsonld
102+
] do
103+
system(%w[
104+
script/parse
105+
--frame presentation/dbpedia/frame.jsonld
106+
presentation/dbpedia/expanded.jsonld
107+
-o presentation/dbpedia/framed.jsonld
108+
].join(" "))
102109
end
103110
end
104111

json-ld.gemspec

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,44 @@
11
#!/usr/bin/env ruby -rubygems
2-
# -*- encoding: utf-8 -*-
2+
# frozen_string_literal: true
33

44
is_java = RUBY_PLATFORM == 'java'
55

66
Gem::Specification.new do |gem|
77
gem.version = File.read('VERSION').chomp
8-
gem.date = File.mtime('VERSION').strftime('%Y-%m-%d')
98

109
gem.name = "json-ld"
1110
gem.homepage = "https://github.com/ruby-rdf/json-ld"
1211
gem.license = 'Unlicense'
1312
gem.summary = "JSON-LD reader/writer for Ruby."
1413
gem.description = "JSON::LD parses and serializes JSON-LD into RDF and implements expansion, compaction and framing API interfaces for the Ruby RDF.rb library suite."
15-
gem.metadata = {
14+
gem.metadata = {
1615
"documentation_uri" => "https://ruby-rdf.github.io/json-ld",
17-
"bug_tracker_uri" => "https://github.com/ruby-rdf/json-ld/issues",
18-
"homepage_uri" => "https://github.com/ruby-rdf/json-ld",
19-
"mailing_list_uri" => "https://lists.w3.org/Archives/Public/public-rdf-ruby/",
20-
"source_code_uri" => "https://github.com/ruby-rdf/json-ld",
16+
"bug_tracker_uri" => "https://github.com/ruby-rdf/json-ld/issues",
17+
"homepage_uri" => "https://github.com/ruby-rdf/json-ld",
18+
"mailing_list_uri" => "https://lists.w3.org/Archives/Public/public-rdf-ruby/",
19+
"source_code_uri" => "https://github.com/ruby-rdf/json-ld",
20+
'rubygems_mfa_required' => 'true'
2121
}
2222

2323
gem.authors = ['Gregg Kellogg']
2424
gem.email = '[email protected]'
2525

2626
gem.platform = Gem::Platform::RUBY
27-
gem.files = %w(AUTHORS README.md UNLICENSE VERSION) + Dir.glob('lib/**/*.rb')
28-
gem.bindir = %q(bin)
29-
gem.executables = %w(jsonld)
30-
gem.require_paths = %w(lib)
31-
gem.test_files = Dir.glob('spec/**/*.rb') + Dir.glob('spec/test-files/*')
27+
gem.files = %w[AUTHORS README.md UNLICENSE VERSION] + Dir.glob('lib/**/*.rb')
28+
gem.bindir = 'bin'
29+
gem.executables = %w[jsonld]
30+
gem.require_paths = %w[lib]
3231

3332
gem.required_ruby_version = '>= 2.6'
3433
gem.requirements = []
35-
gem.add_runtime_dependency 'rdf', '~> 3.2', '>= 3.2.10'
36-
gem.add_runtime_dependency 'multi_json', '~> 1.15'
37-
gem.add_runtime_dependency 'link_header', '~> 0.0', '>= 0.0.8'
34+
gem.add_runtime_dependency 'htmlentities', '~> 4.3'
3835
gem.add_runtime_dependency 'json-canonicalization', '~> 0.3'
39-
gem.add_runtime_dependency 'htmlentities', '~> 4.3'
36+
gem.add_runtime_dependency 'link_header', '~> 0.0', '>= 0.0.8'
37+
gem.add_runtime_dependency 'multi_json', '~> 1.15'
4038
gem.add_runtime_dependency "rack", '>= 2.2', '< 4'
41-
gem.add_development_dependency 'sinatra-linkeddata','~> 3.2'
39+
gem.add_runtime_dependency 'rdf', '~> 3.2', '>= 3.2.10'
4240
gem.add_development_dependency 'jsonlint', '~> 0.4' unless is_java
43-
gem.add_development_dependency 'oj', '~> 3.14' unless is_java
44-
gem.add_development_dependency 'yajl-ruby', '~> 1.4' unless is_java
41+
gem.add_development_dependency 'oj', '~> 3.14' unless is_java
4542
gem.add_development_dependency 'rack-test', '>= 1.1', '< 3'
4643
gem.add_development_dependency 'rdf-isomorphic', '~> 3.2'
4744
gem.add_development_dependency 'rdf-spec', '~> 3.2'
@@ -51,7 +48,9 @@ Gem::Specification.new do |gem|
5148
gem.add_development_dependency 'rdf-xsd', '~> 3.2'
5249
gem.add_development_dependency 'rspec', '~> 3.12'
5350
gem.add_development_dependency 'rspec-its', '~> 1.3'
54-
gem.add_development_dependency 'yard' , '~> 0.9'
51+
gem.add_development_dependency 'sinatra-linkeddata', '~> 3.2'
52+
gem.add_development_dependency 'yajl-ruby', '~> 1.4' unless is_java
53+
gem.add_development_dependency 'yard', '~> 0.9'
5554

56-
gem.post_install_message = nil
55+
gem.post_install_message = nil
5756
end

lib/json/ld/context.rb

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,10 +1544,10 @@ def compact_iri(iri, base: nil, reverse: false, value: nil, vocab: nil)
15441544
candidates = []
15451545

15461546
term_definitions.each do |term, td|
1547-
next if td.nil? || td.id.nil? || td.id == iri || !iri.start_with?(td.id)
1548-
15491547
# Skip term if `@prefix` is not true in term definition
1550-
next unless td.prefix?
1548+
next unless td&.prefix?
1549+
1550+
next if td&.id.nil? || td.id == iri || !td.match_iri?(iri)
15511551

15521552
suffix = iri[td.id.length..]
15531553
ciri = "#{term}:#{suffix}"
@@ -1572,7 +1572,7 @@ def compact_iri(iri, base: nil, reverse: false, value: nil, vocab: nil)
15721572

15731573
# If iri could be confused with a compact IRI using a term in this context, signal an error
15741574
term_definitions.each do |term, td|
1575-
next unless iri.to_s.start_with?("#{term}:") && td.prefix?
1575+
next unless td.prefix? && td.match_compact_iri?(iri)
15761576

15771577
raise JSON::LD::JsonLdError::IRIConfusedWithPrefix, "Absolute IRI '#{iri}' confused with prefix '#{term}'"
15781578
end
@@ -2204,6 +2204,22 @@ def protected?
22042204
!!@protected
22052205
end
22062206

2207+
# Returns true if the term matches a IRI
2208+
#
2209+
# @param iri [String] the IRI
2210+
# @return [Boolean]
2211+
def match_iri?(iri)
2212+
iri.start_with?(id)
2213+
end
2214+
2215+
# Returns true if the term matches a compact IRI
2216+
#
2217+
# @param iri [String] the compact IRI
2218+
# @return [Boolean]
2219+
def match_compact_iri?(iri)
2220+
iri.start_with?(prefix_colon)
2221+
end
2222+
22072223
# Set container mapping, from an array which may include @set
22082224
def container_mapping=(mapping)
22092225
mapping = case mapping
@@ -2328,6 +2344,12 @@ def inspect
23282344
v << "has-context" unless context.nil?
23292345
v.join(" ") + "]"
23302346
end
2347+
2348+
private
2349+
2350+
def prefix_colon
2351+
@prefix_colon ||= "#{term}:".freeze
2352+
end
23312353
end
23322354
end
23332355
end

lib/json/ld/flatten.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,9 @@ def rename_bnodes(node)
255255
when Array
256256
node.map { |n| rename_bnodes(n) }
257257
when Hash
258-
node.inject({}) do |memo, (k, v)|
258+
node.each_with_object({}) do |(k, v), memo|
259259
v = namer.get_name(v) if k == '@id' && v.is_a?(String) && blank_node?(v)
260-
memo.merge(k => rename_bnodes(v))
260+
memo[k] = rename_bnodes(v)
261261
end
262262
else
263263
node

0 commit comments

Comments
 (0)