-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.Archivesspace.Resource.rb
231 lines (214 loc) · 9.7 KB
/
class.Archivesspace.Resource.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
=begin
Abbreviations, AO = archival object(Everything's an AO, but there's also uri "archive_objects". It's confusing...)
AS = ArchivesSpace
IT = instance type
TC = top container
SC = Sub-container
_H = Hash
_A = Array
_I = Index(of Array)
_O = Object
_0R = Zero Relative
=end
class Resource
=begin
Resource just holds the resource-number and uri.
An object of this is needed to create a Resource_Record_Buf, but
there's a 'new_buffer' method that will do it from inside here too, eg:
resource_buffer_Obj = Resource.new(repository_Obj, resource-num|uri).new_buffer[.read|create]
=end
def initialize( p1_rep_O, p2_res_identifier )
if ( not p1_rep_O.is_a?( Repository ) ) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "Param 1 is not a Repository class object, it's a '#{p1_rep_O.class}'"
raise
end
@rep_O = p1_rep_O
if ( p2_res_identifier == nil ) then
@num = nil
@uri = nil
else
if ( p2_res_identifier.integer? ) then
@num = p2_res_identifier
@uri = "#{@rep_O.uri}/resources/#{@num}"
else
stringer = "#{@rep_O.uri}/archival_objects"
if ( stringer == p2_res_identifier[ 0 .. stringer.maxindex ]) then
@uri = p2_res_identifier
@num = p2_res_identifier.sub( /^.*\//, '' )
if (not @num.integer? ) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "Invalid param2: #{p2_res_identifier}"
raise
end
else
SE.puts "#{SE.lineno}: =============================================="
SE.puts "Invalid param2: #{p2_res_identifier}"
raise
end
end
end
end
attr_reader :rep_O, :num, :uri
def new_buffer
res_buf_O = Resource_Record_Buf.new( self )
return res_buf_O
end
end
class Resource_Record_Buf < Record_Buf
=begin
A "CRUD-like" class for the /resources, but (as of 4/19/2020) I don't want anything accidently
updating a Resource, so the U.D. part are missing.
Note that: The 'create' just initializes the buffer, and the Update is called 'store' (so it's IRSD)
=end
def initialize( res_O )
if ( not res_O.is_a?( Resource )) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "Param 1 is not a Resource class object, it's a '#{res_O.class}'"
raise
end
@rec_jsonmodel_type = K.resource
@res_O = res_O
@uri = @res_O.uri
@num = @res_O.num
super( @res_O.rep_O.aspace_O )
end
attr_reader :num, :uri, :res_O
def create
@record_H.merge!( Record_Format.new( @rec_jsonmodel_type ).record_H )
return self
end
def read( filter_record_B = true )
@record_H = super( filter_record_B )
# SE.pp "@record_H", @record_H
if ( @record_H.key?( @rec_jsonmodel_type ) and @record_H[ @rec_jsonmodel_type ].key?( K.ref )) then
if ( ! ( @record_H[ @rec_jsonmodel_type ][ K.ref ] == "#{@uri}" ) ) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "uri is not part of resource '#{@num}'"
SE.puts "resource => uri = '#{@uri}'"
SE.pp "@record_H:", @record_H
raise
end
end
return self
end
def store( )
SE.puts "#{SE.lineno}: =============================================="
SE.puts "Method not coded"
raise
end
end
class Resource_Query
def initialize( res_O, param_get_full_ao_buf = false )
if ( not res_O.is_a?( Resource )) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "Param 1 is not a Resource class object, it's a '#{res_O.class}'"
raise
end
=begin
Param2, if true, causes the query to read each AO record. This is about 30 times slower
than using the data from the AO indexes, which is a subset of the AO.
=end
if ( not (param_get_full_ao_buf == true or param_get_full_ao_buf == false )) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "Param 1 should be true or false, not '#{param_get_full_ao_buf}'"
raise
end
@res_O = res_O
@get_full_ao_buf = param_get_full_ao_buf
if ( @get_full_ao_buf ) then
SE.puts "'Resource_Query' returning full buffer data."
else
SE.puts "'Resource_Query' returning index buffer data ONLY!"
end
=begin
Get all the AO's for the resource, building an array of record_H,
loaded with the subset of AO data contained in the 'tree' records.
The parameter allows one to start from anyplace on the resource's tree,
but I've never need it.
=end
@record_H_A = []
@uri_num_H = {} # Hash of uri_num's with associated record_H_A index number
process_each_node( '' )
end
attr_reader :record_H_A
def get_record_H_of_uri_num( uri_num )
if ( ! uri_num.integer? ) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "Was expecting param 1 to be an integer"
raise
end
if ( @uri_num_H == {} ) then
record_H_A.each_with_index do | record_H, idx |
@uri_num_H[ record_H[ K.uri ].sub( /.*\//, '' ) ] = idx
end
end
return @record_H_A[ @uri_num_H[ uri_num ] ]
end
def process_each_node( node_uri )
if ( node_uri == '' ) then
waypoint_node_H = @res_O.rep_O.aspace_O.http_calls_O.get( "#{@res_O.uri}/tree/root", { } )
else
waypoint_node_H = @res_O.rep_O.aspace_O.http_calls_O.get( "#{@res_O.uri}/tree/node", { K.node_uri => node_uri } )
end
# SE.pp waypoint_node_H
if ( not ( waypoint_node_H.has_key?( K.precomputed_waypoints ) and
waypoint_node_H[ K.precomputed_waypoints ].has_key?( node_uri ) and
waypoint_node_H[ K.precomputed_waypoints ][ node_uri ].has_key?( '0' ) and
waypoint_node_H.has_key?( K.child_count ) and
waypoint_node_H.has_key?( K.waypoints ) and
waypoint_node_H.has_key?( K.waypoint_size ) and
waypoint_node_H.has_key?( K.uri ) ) ) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "Missing expected key"
SE.pp waypoint_node_H
raise
end
if ( waypoint_node_H[ K.precomputed_waypoints ].keys.length != 1 ) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "WARNING: waypoint_node_H[ K.precomputed_waypoints ].keys.length != 1, equals: " +
"#{waypoint_node_H[ K.precomputed_waypoints ].keys.length}"
SE.puts "waypoint_node_H[ K.precomputed_waypoints ].keys:" + waypoint_node_H[ K.precomputed_waypoints ].keys
end
if ( waypoint_node_H[ K.precomputed_waypoints ][ node_uri ].keys.length != 1 ) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "WARNING: waypoint_node_H[ K.precomputed_waypoints ][ node_uri ].keys.length != 1, equals: " +
"#{waypoint_node_H[ K.precomputed_waypoints ][ node_uri ].keys.length}"
SE.puts "waypoint_node_H[ K.precomputed_waypoints ][ node_uri ].keys:" + waypoint_node_H[ K.precomputed_waypoints ][ '0' ].keys
end
# SE.puts "node_uri = #{node_uri}, waypoint_node_H[ K.waypoints ] = #{waypoint_node_H[ K.waypoints ]}"
waypoint_A = waypoint_node_H[ K.precomputed_waypoints ][ node_uri ] [ '0' ]
waypoint_num = 0; loop do
# SE.pp waypoint_A
waypoint_A.each do | child_H |
if ( not ( child_H.has_key?( K.child_count ) and
child_H.has_key?( K.waypoints ) and
child_H.has_key?( K.waypoint_size ) and
child_H.has_key?( K.uri ) ) ) then
SE.puts "#{SE.lineno}: =============================================="
SE.puts "Missing expected key: K.uri"
SE.pp child_H
raise
end
child_H[ K.resource ] = { K.ref => @res_O.uri }
if ( @get_full_ao_buf ) then
@record_H_A << Archival_Object.new( @res_O, child_H[ K.uri ] ).new_buffer.read.record_H
else
@record_H_A << child_H
end
if ( child_H[ K.child_count ] > 0 ) then
process_each_node( child_H[ K.uri ] )
end
end
waypoint_num += 1
break if ( waypoint_num >= waypoint_node_H[ K.waypoints ] )
uri = "#{@res_O.uri}/tree/waypoint"
if ( node_uri == '' ) then
waypoint_A = @res_O.rep_O.aspace_O.http_calls_O.get( uri, { K.offset => waypoint_num } )
else
waypoint_A = @res_O.rep_O.aspace_O.http_calls_O.get( uri, { K.offset => waypoint_num , K.parent_node => node_uri } )
end
end
end
private :process_each_node
end