Skip to content

Commit 271589d

Browse files
committed
Merge pull request #60 from asobolev/master
Some Section dynamic refactoring + cache bug fix
2 parents 76adb66 + a3c23b6 commit 271589d

File tree

4 files changed

+53
-63
lines changed

4 files changed

+53
-63
lines changed

+nix/Block.m

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

99
properties(Hidden)
10-
metadataCache = nix.CacheStruct()
10+
metadataCache
1111
end
1212

1313
methods
@@ -19,6 +19,8 @@
1919
obj.add_dyn_relation('sources', @nix.Source);
2020
obj.add_dyn_relation('tags', @nix.Tag);
2121
obj.add_dyn_relation('multiTags', @nix.MultiTag);
22+
23+
obj.metadataCache = nix.CacheStruct();
2224
end;
2325

2426
% -----------------

+nix/Dynamic.m

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,28 @@ function add_dyn_relation(obj, name, constructor)
5353
cache.Hidden = true;
5454
obj.(cacheAttr) = nix.CacheStruct();
5555

56+
% adds a proxy property
5657
rel = addprop(obj, name);
5758
rel.GetMethod = @get_method;
5859

60+
% same property but returns Map
61+
rel_map = addprop(obj, strcat(name, 'Map'));
62+
rel_map.GetMethod = @get_as_map;
63+
5964
function val = get_method(obj)
6065
[obj.(cacheAttr), val] = nix.Utils.fetchObjList(obj.updatedAt, ...
6166
strcat(obj.alias, '::', name), obj.nix_handle, ...
6267
obj.(cacheAttr), constructor);
63-
end
68+
end
69+
70+
function val = get_as_map(obj)
71+
val = containers.Map();
72+
props = obj.(name);
73+
74+
for i=1:length(props)
75+
val(props{i}.name) = cell2mat(props{i}.values);
76+
end
77+
end
6478
end
6579
end
6680
end

+nix/Section.m

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,55 @@
1-
classdef Section < nix.Entity
1+
classdef Section < nix.NamedEntity
22
%SECTION Metadata Section class
33
% NIX metadata section
44

5+
properties (Access = protected)
6+
% namespace reference for nix-mx functions
7+
alias = 'Section'
8+
end
9+
510
properties(Hidden)
6-
info
7-
sectionsCache
811
propsCache
912
end;
1013

1114
properties(Dependent)
12-
name
13-
type
14-
id
15-
repository
16-
mapping
17-
18-
sections
19-
properties_cell
20-
properties_map
15+
allProperties
16+
allPropertiesMap
2117
end;
2218

2319
methods
2420
function obj = Section(h)
25-
26-
obj.info = nix_mx('Section::describe', obj.nix_handle);
27-
28-
obj.sectionsCache.lastUpdate = 0;
29-
obj.sectionsCache.data = {};
30-
obj.propsCache.lastUpdate = 0;
31-
obj.propsCache.data = {};
32-
end;
33-
34-
function name = get.name(section)
35-
name = section.info.name;
36-
end;
37-
38-
function id = get.id(section)
39-
id = section.info.id;
40-
end;
41-
42-
function type = get.type(section)
43-
type = section.info.type;
44-
end;
45-
46-
function repository = get.repository(section)
47-
repository = section.info.repository;
48-
end;
49-
50-
function mapping = get.mapping(section)
51-
mapping = section.info.mapping;
21+
22+
23+
% assign dynamic properties
24+
obj.add_dyn_attr('repository', 'rw');
25+
obj.add_dyn_attr('mapping', 'rw');
26+
27+
% assign relations
28+
obj.add_dyn_relation('sections', @nix.Section);
29+
30+
obj.propsCache = nix.CacheStruct();
5231
end;
5332

5433
function section = parent(obj)
55-
sh = nix_mx('Section::parent', obj.nix_handle);
56-
if sh ~= 0
57-
section = nix.Section(sh);
58-
else
59-
section = {};
34+
handle = nix_mx('Section::parent', obj.nix_handle);
35+
section = {};
36+
if handle ~= 0
37+
section = nix.Section(handle);
6038
end;
6139
end;
6240

6341
function section = link(obj)
64-
sh = nix_mx('Section::link', obj.nix_handle);
65-
if sh ~= 0
66-
section = nix.Section(sh);
67-
else
68-
section = {};
42+
handle = nix_mx('Section::link', obj.nix_handle);
43+
section = {};
44+
if handle ~= 0
45+
section = nix.Section(handle);
6946
end;
7047
end;
7148

7249
% ----------------
7350
% Section methods
7451
% ----------------
7552

76-
function sections = get.sections(obj)
77-
[obj.sectionsCache, sections] = nix.Utils.fetchObjList(obj.updatedAt, ...
78-
'Section::sections', obj.nix_handle, obj.sectionsCache, @nix.Section);
79-
end
80-
8153
function retObj = open_section(obj, id_or_name)
8254
handle = nix_mx('Section::openSection', obj.nix_handle, id_or_name);
8355
retObj = {};
@@ -94,14 +66,14 @@
9466
% Property methods
9567
% ----------------
9668

97-
function props = get.properties_cell(obj)
69+
function props = get.allProperties(obj)
9870
[obj.propsCache, props] = nix.Utils.fetchPropList(obj.updatedAt, ...
9971
'Section::properties', obj.nix_handle, obj.propsCache);
10072
end
10173

102-
function p_map = get.properties_map(obj)
74+
function p_map = get.allPropertiesMap(obj)
10375
p_map = containers.Map();
104-
props = obj.properties_cell;
76+
props = obj.allProperties;
10577

10678
for i=1:length(props)
10779
p_map(props{i}.name) = cell2mat(props{i}.values);

tests/TestSection.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,15 @@
8080
f = nix.File(fullfile(pwd, 'tests', 'test.h5'), nix.FileMode.ReadOnly);
8181
trial = f.sections{2}.sections{2}.sections{1};
8282

83-
assert(~isempty(trial.properties_cell));
83+
assert(~isempty(trial.allProperties));
8484

85-
p1 = trial.properties_cell{1};
85+
p1 = trial.allProperties{1};
8686
assert(strcmp(p1.name, 'ExperimentalCondition'));
8787
assert(p1.values{1} == 1);
8888

89-
assert(trial.properties_map('ExperimentalCondition') == p1.values{1});
89+
assert(trial.allPropertiesMap('ExperimentalCondition') == p1.values{1});
9090

91-
assert(isempty(f.sections{3}.properties_cell));
91+
disp(f.sections{3}.allProperties);
92+
93+
assert(isempty(f.sections{3}.allProperties));
9294
end

0 commit comments

Comments
 (0)