Skip to content

Commit b831345

Browse files
committed
Store entities in Sets for fast lookups. #27
1 parent 88e1bcf commit b831345

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

lib/ro_crate/model/crate.rb

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'set'
2+
13
module ROCrate
24
##
35
# A Ruby abstraction of an RO-Crate.
@@ -20,8 +22,8 @@ def self.format_local_id(id)
2022
##
2123
# Initialize an empty RO-Crate.
2224
def initialize(id = IDENTIFIER, properties = {})
23-
@data_entities = []
24-
@contextual_entities = []
25+
@data_entities = Set.new
26+
@contextual_entities = Set.new
2527
super(self, nil, id, properties)
2628
end
2729

@@ -144,8 +146,8 @@ def add_organization(id, properties = {})
144146
# @return [Entity] the entity itself, or a clone of the entity "owned" by this crate.
145147
def add_contextual_entity(entity)
146148
entity = claim(entity)
147-
contextual_entities.delete(entity) # Remove (then re-add) the entity if it exists
148-
contextual_entities.push(entity)
149+
contextual_entities.delete?(entity) # Remove (then re-add) the entity if it exists
150+
contextual_entities.add(entity)
149151
entity
150152
end
151153

@@ -156,8 +158,8 @@ def add_contextual_entity(entity)
156158
# @return [Entity] the entity itself, or a clone of the entity "owned" by this crate.
157159
def add_data_entity(entity)
158160
entity = claim(entity)
159-
data_entities.delete(entity) # Remove (then re-add) the entity if it exists
160-
data_entities.push(entity)
161+
data_entities.delete?(entity) # Remove (then re-add) the entity if it exists
162+
data_entities.add(entity)
161163
entity
162164
end
163165

@@ -191,7 +193,7 @@ def preview=(preview)
191193
#
192194
# @return [Array<Entity>]
193195
def entities
194-
default_entities | data_entities | contextual_entities
196+
default_entities | data_entities.to_a | contextual_entities.to_a
195197
end
196198

197199
##
@@ -249,7 +251,7 @@ def payload
249251
# file data entities. This ensures in the case of a conflict, the more "specific" data entities take priority.
250252
entries = own_payload
251253
non_self_entities = default_entities.reject { |e| e == self }
252-
sorted_entities = (non_self_entities | data_entities).sort_by { |e| e.is_a?(ROCrate::Directory) ? 0 : 1 }
254+
sorted_entities = (non_self_entities | data_entities.to_a).sort_by { |e| e.is_a?(ROCrate::Directory) ? 0 : 1 }
253255

254256
sorted_entities.each do |entity|
255257
entity.payload.each do |path, entry|
@@ -277,7 +279,7 @@ def delete(entity, remove_orphaned: true)
277279
entity = dereference(entity) if entity.is_a?(String)
278280
return unless entity
279281

280-
deleted = data_entities.delete(entity) || contextual_entities.delete(entity)
282+
deleted = data_entities.delete?(entity) || contextual_entities.delete?(entity)
281283

282284
if deleted && remove_orphaned
283285
crate_entities = crate.linked_entities(deep: true)

lib/ro_crate/model/entity.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def linked_entities(deep: false, linked: {})
251251
value = properties[key] # We're doing this to call the JSONLDHash#[] method which wraps
252252
value = [value] if value.is_a?(JSONLDHash)
253253

254-
if value.is_a?(Array)
254+
if value.respond_to?(:each)
255255
value.each do |v|
256256
if v.is_a?(JSONLDHash) && !linked.key?(v['@id'])
257257
entity = v.dereference

test/crate_test.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ class CrateTest < Test::Unit::TestCase
171171
crate.author = bob
172172
info.author = bob
173173

174-
assert_equal [bob], crate.contextual_entities
174+
assert_includes crate.contextual_entities, bob
175+
assert_equal 1, crate.contextual_entities.length
175176
assert_equal bob, info.author
176177
assert_equal bob, crate.author
177178
end

0 commit comments

Comments
 (0)