Skip to content

Commit 03711e8

Browse files
committed
add/remove source from dataarray
1 parent 81acba2 commit 03711e8

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

+nix/DataArray.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ function write_all(obj, data) % TODO add (optional) offset
9393
% Sources methods
9494
% -----------------
9595

96+
function [] = add_source(obj, add_this)
97+
obj.sourcesCache = nix.Utils.add_entity(obj, ...
98+
add_this, 'nix.Source', 'DataArray::addSource', obj.sourcesCache);
99+
end;
100+
101+
function delCheck = remove_source(obj, del)
102+
[delCheck, obj.sourcesCache] = nix.Utils.delete_entity(obj, ...
103+
del, 'nix.Source', 'DataArray::removeSource', obj.sourcesCache);
104+
end;
105+
96106
function sources = get.sources(obj)
97107
[obj.sourcesCache, sources] = nix.Utils.fetchObjList(obj.updatedAt, ...
98108
'DataArray::sources', obj.nix_handle, obj.sourcesCache, @nix.Source);

nix_mx.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ void mexFunction(int nlhs,
128128
.reg("openMetadataSection", GETMETADATA(IDataArray));
129129
methods->add("DataArray::readAll", nixdataarray::read_all);
130130
methods->add("DataArray::writeAll", nixdataarray::write_all);
131+
methods->add("DataArray::addSource", nixdataarray::add_source);
132+
// REMOVER for DataArray.removeSource leads to an error, therefore use method->add for now
133+
methods->add("DataArray::removeSource", nixdataarray::remove_source);
131134

132135
classdef<nix::Source>("Source", methods)
133136
.desc(&nixsource::describe)

src/nixdataarray.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ namespace nixdataarray {
5454
return sb.array();
5555
}
5656

57+
void add_source(const extractor &input, infusor &output)
58+
{
59+
nix::DataArray currObj = input.entity<nix::DataArray>(1);
60+
currObj.addSource(input.str(2));
61+
}
62+
63+
void remove_source(const extractor &input, infusor &output)
64+
{
65+
nix::DataArray currObj = input.entity<nix::DataArray>(1);
66+
output.set(0, currObj.removeSource(input.str(2)));
67+
}
68+
5769
void read_all(const extractor &input, infusor &output)
5870
{
5971
nix::DataArray da = input.entity<nix::DataArray>(1);

src/nixdataarray.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ namespace nixdataarray {
77

88
mxArray *describe(const nix::DataArray &da);
99

10+
void add_source(const extractor &input, infusor &output);
11+
12+
void remove_source(const extractor &input, infusor &output);
13+
1014
void read_all(const extractor &input, infusor &output);
1115

1216
void write_all(const extractor &input, infusor &output);

tests/TestDataArray.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
funcs{end+1} = @test_open_metadata;
88
funcs{end+1} = @test_list_sources;
99
funcs{end+1} = @test_set_data;
10+
funcs{end+1} = @test_add_source;
11+
funcs{end+1} = @test_remove_source;
1012
end
1113

1214
%% Test: Read all data from DataArray
@@ -56,3 +58,37 @@
5658
assert(isequal(d1.read_all(), data));
5759
end
5860

61+
%% Test: Add sources by entity and id
62+
function [] = test_add_source ( varargin )
63+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
64+
b = f.createBlock('sourceTest', 'nixBlock');
65+
getSource = b.create_source('sourceTest', 'nixSource');
66+
tmp = getSource.create_source('nestedSource1', 'nixSource');
67+
tmp = getSource.create_source('nestedSource2', 'nixSource');
68+
getDataArray = b.create_data_array('sourceTestDataArray', 'nixDataArray', 'double', [1 2]);
69+
70+
assert(isempty(getDataArray.sources));
71+
getDataArray.add_source(getSource.sources{1}.id);
72+
getDataArray.add_source(getSource.sources{2});
73+
assert(size(getDataArray.sources, 1) == 2);
74+
end
75+
76+
%% Test: Remove sources by entity and id
77+
function [] = test_remove_source ( varargin )
78+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
79+
b = f.createBlock('sourceTest', 'nixBlock');
80+
getSource = b.create_source('sourceTest', 'nixSource');
81+
tmp = getSource.create_source('nestedSource1', 'nixSource');
82+
tmp = getSource.create_source('nestedSource2', 'nixSource');
83+
getDataArray = b.create_data_array('sourceTestDataArray', 'nixDataArray', 'double', [1 2]);
84+
85+
getDataArray.add_source(getSource.sources{1}.id);
86+
getDataArray.add_source(getSource.sources{2});
87+
88+
assert(getDataArray.remove_source(getSource.sources{2}));
89+
assert(getDataArray.remove_source(getSource.sources{1}.id));
90+
assert(isempty(getDataArray.sources));
91+
92+
assert(getDataArray.remove_source('I do not exist'));
93+
assert(size(getSource.sources, 1) == 2);
94+
end

0 commit comments

Comments
 (0)