Skip to content

Commit 8c88a9d

Browse files
committed
Create RDF.config for configuring cache sizes. Allows a general size, URI and Node specific cache sizes to be configured at startup time.
Fixes #256.
1 parent a0d747b commit 8c88a9d

6 files changed

Lines changed: 82 additions & 17 deletions

File tree

lib/rdf.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'bigdecimal'
33
require 'date'
44
require 'time'
5+
require "ostruct"
56

67
require 'rdf/version'
78
require 'rdf/extensions'
@@ -79,6 +80,29 @@ module RDF
7980
# CLI
8081
autoload :CLI, 'rdf/cli'
8182

83+
##
84+
# Configuration, used open for configuring constants used within the codebase.
85+
#
86+
# @example set default cache size to be at most 10,000 entries
87+
#
88+
# RDF.config.cache_size = 10_000
89+
#
90+
# @example set cache size for interned URIs to 5,000 entries
91+
#
92+
# RDF.config.uri_cache_size = 5_000
93+
#
94+
# Defaults:
95+
# * `cache_size`: -1
96+
# * `uri_cache_size`: `cache_size`
97+
# * `node_cache_size`: `cache_size`
98+
#
99+
# @note cache configurations must be set before initial use, when the caches are allocated.
100+
# @see RDF::Util::Cache.new
101+
# @return [Object]
102+
def self.config
103+
@config ||= OpenStruct.new(cache_size: -1, uri_cache_size: nil, node_cache_size: nil)
104+
end
105+
82106
##
83107
# Alias for `RDF::Resource.new`.
84108
#

lib/rdf/model/node.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,14 @@ class Node
1313
include RDF::Resource
1414

1515
##
16-
# Defines the maximum number of interned Node references that can be held
17-
# cached in memory at any one time.
16+
# Cache size may be set through {RDF.config} using `node_cache_size`.
1817
#
1918
# @note caching interned nodes means that two different invocations using the same symbol will result in the same node, which may not be appropriate depending on the graph from which it is used. RDF requires that bnodes with the same label are, in fact, different bnodes, unless they are used within the same document.
20-
CACHE_SIZE = -1 # unlimited by default
21-
22-
##
2319
# @return [RDF::Util::Cache]
2420
# @private
2521
def self.cache
2622
require 'rdf/util/cache' unless defined?(::RDF::Util::Cache)
27-
@cache ||= RDF::Util::Cache.new(CACHE_SIZE)
23+
@cache ||= RDF::Util::Cache.new(RDF.config.node_cache_size)
2824
end
2925

3026
##

lib/rdf/model/uri.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ module RDF
2727
class URI
2828
include RDF::Resource
2929

30-
##
31-
# Defines the maximum number of interned URI references that can be held
32-
# cached in memory at any one time.
33-
CACHE_SIZE = -1 # unlimited by default
34-
3530
# IRI components
3631
UCSCHAR = Regexp.compile(<<-EOS.gsub(/\s+/, ''))
3732
[\\u00A0-\\uD7FF]|[\\uF900-\\uFDCF]|[\\uFDF0-\\uFFEF]|
@@ -117,11 +112,13 @@ class URI
117112
).freeze
118113

119114
##
115+
# Cache size may be set through {RDF.config} using `uri_cache_size`.
116+
#
120117
# @return [RDF::Util::Cache]
121118
# @private
122119
def self.cache
123120
require 'rdf/util/cache' unless defined?(::RDF::Util::Cache)
124-
@cache ||= RDF::Util::Cache.new(CACHE_SIZE)
121+
@cache ||= RDF::Util::Cache.new(RDF.config.uri_cache_size)
125122
end
126123

127124
##

lib/rdf/util/cache.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ module RDF; module Util
1717
# @see http://en.wikipedia.org/wiki/Weak_reference
1818
# @since 0.2.0
1919
class Cache
20+
# The configured cache capacity.
21+
attr_reader :capacity
22+
2023
##
2124
# @private
2225
def self.new(*args)
@@ -36,8 +39,8 @@ def self.new(*args)
3639

3740
##
3841
# @param [Integer] capacity
39-
def initialize(capacity = -1)
40-
@capacity = capacity
42+
def initialize(capacity = nil)
43+
@capacity = capacity || RDF.config.cache_size
4144
@cache ||= {}
4245
@index ||= {}
4346
end
@@ -106,7 +109,7 @@ def delete(key)
106109
class WeakRefCache < Cache
107110
##
108111
# @param [Integer] capacity
109-
def initialize(capacity = -1)
112+
def initialize(capacity = nil)
110113
require 'weakref' unless defined?(::WeakRef)
111114
super
112115
end

spec/model_node_spec.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
end
99

1010
describe ".intern" do
11-
before(:each) {RDF::URI.instance_variable_set(:@cache, nil)}
11+
before(:each) do
12+
RDF::Node.instance_variable_set(:@cache, nil)
13+
RDF.config.cache_size = -1
14+
RDF.config.node_cache_size = nil
15+
end
16+
1217
it "caches Node instance" do
1318
RDF::Node.intern("a")
1419
expect(RDF::Node.instance_variable_get(:@cache)[:a]).to eq RDF::Node.intern("a")
@@ -17,6 +22,24 @@
1722
it "freezes instance" do
1823
expect(RDF::Node.intern("a")).to be_frozen
1924
end
25+
26+
it "defaults cache size to -1" do
27+
RDF::Node.intern("a")
28+
expect(RDF::Node.instance_variable_get(:@cache).capacity).to eq(-1)
29+
end
30+
31+
it "defaults cache size to configured cache_size" do
32+
RDF.config.cache_size = 10_000
33+
RDF::Node.intern("a")
34+
expect(RDF::Node.instance_variable_get(:@cache).capacity).to eq(10_000)
35+
end
36+
37+
it "defaults cache size to configured uri_cache_size" do
38+
RDF.config.cache_size = 10_000
39+
RDF.config.node_cache_size = 5_000
40+
RDF::Node.intern("a")
41+
expect(RDF::Node.instance_variable_get(:@cache).capacity).to eq(5_000)
42+
end
2043
end
2144

2245
describe ".uuid" do

spec/model_uri_spec.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
end
88

99
describe ".intern" do
10-
before(:each) {RDF::URI.instance_variable_set(:@cache, nil)}
10+
before(:each) do
11+
RDF::URI.instance_variable_set(:@cache, nil)
12+
RDF.config.cache_size = -1
13+
RDF.config.uri_cache_size = nil
14+
end
1115
it "caches URI instance" do
1216
RDF::URI.intern("a")
1317
expect(RDF::URI.instance_variable_get(:@cache)[:a]).to eq RDF::URI("a")
@@ -24,6 +28,24 @@
2428
it "does not use #to_hash given a URI" do
2529
expect {RDF::URI.intern(RDF::URI("a"))}.not_to write.to(:error)
2630
end
31+
32+
it "defaults cache size to -1" do
33+
RDF::URI.intern(RDF::URI("a"))
34+
expect(RDF::URI.instance_variable_get(:@cache).capacity).to eq(-1)
35+
end
36+
37+
it "defaults cache size to configured cache_size" do
38+
RDF.config.cache_size = 10_000
39+
RDF::URI.intern(RDF::URI("a"))
40+
expect(RDF::URI.instance_variable_get(:@cache).capacity).to eq(10_000)
41+
end
42+
43+
it "defaults cache size to configured uri_cache_size" do
44+
RDF.config.cache_size = 10_000
45+
RDF.config.uri_cache_size = 5_000
46+
RDF::URI.intern(RDF::URI("a"))
47+
expect(RDF::URI.instance_variable_get(:@cache).capacity).to eq(5_000)
48+
end
2749
end
2850

2951
describe ".parse" do

0 commit comments

Comments
 (0)