Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 59f7738

Browse files
committed
Adding cache cleanup and "intelligent" join to all_source, only when needed.
1 parent a1f6b34 commit 59f7738

10 files changed

Lines changed: 268 additions & 36 deletions

source/core/annotations/ut_annotation_cache_manager.pkb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ create or replace package body ut_annotation_cache_manager as
7474
commit;
7575
end;
7676

77+
procedure remove_from_cache(a_objects ut_annotation_objs_cache_info) is
78+
pragma autonomous_transaction;
79+
begin
80+
81+
delete from ut_annotation_cache_info i
82+
where exists (
83+
select 1 from table (a_objects) o
84+
where o.object_name = i.object_name
85+
and o.object_type = i.object_type
86+
and o.object_owner = i.object_owner
87+
);
88+
89+
commit;
90+
end;
91+
7792
function get_annotations_for_objects(a_cached_objects ut_annotation_objs_cache_info, a_parse_time timestamp) return sys_refcursor is
7893
l_results sys_refcursor;
7994
begin
@@ -92,7 +107,7 @@ create or replace package body ut_annotation_cache_manager as
92107
join ut_annotation_cache_info i
93108
on o.object_owner = i.object_owner and o.object_name = i.object_name and o.object_type = i.object_type
94109
join ut_annotation_cache c on i.cache_id = c.cache_id
95-
where ]'|| case when a_parse_time is null then ':a_parse_date is null' else 'i.parse_time >= :a_parse_time' end ||q'[
110+
where ]'|| case when a_parse_time is null then ':a_parse_date is null' else 'i.parse_time > :a_parse_time' end ||q'[
96111
group by i.object_owner, i.object_name, i.object_type, i.parse_time]'
97112
using a_cached_objects, a_parse_time;
98113
return l_results;

source/core/annotations/ut_annotation_cache_manager.pks

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,17 @@ create or replace package ut_annotation_cache_manager authid definer as
3737
/**
3838
* Removes cached information about annotations for objects on the list and updates parse_time in cache info table.
3939
*
40-
* @param a_objects a `ut_annotation_objs_cache_info` list with information about objects to remove from cache
40+
* @param a_objects a `ut_annotation_objs_cache_info` list with information about objects to remove annotations for
4141
*/
4242
procedure cleanup_cache(a_objects ut_annotation_objs_cache_info);
4343

44+
/**
45+
* Removes information about objects on the list
46+
*
47+
* @param a_objects a `ut_annotation_objs_cache_info` list with information about objects to remove from cache
48+
*/
49+
procedure remove_from_cache(a_objects ut_annotation_objs_cache_info);
50+
4451
/**
4552
* Removes cached information about annotations for objects of specified type and specified owner
4653
*

source/core/annotations/ut_annotation_manager.pkb

Lines changed: 82 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,38 @@ create or replace package body ut_annotation_manager as
1919
------------------------------
2020
--private definitions
2121

22+
-- function get_missing_objects(a_object_owner varchar2, a_object_type varchar2) return ut_annotation_objs_cache_info is
23+
-- l_rows sys_refcursor;
24+
-- l_ut_owner varchar2(250) := ut_utils.ut_owner;
25+
-- l_objects_view varchar2(200) := ut_metadata.get_dba_view('dba_objects');
26+
-- l_cursor_text varchar2(32767);
27+
-- l_result ut_annotation_objs_cache_info;
28+
-- begin
29+
-- l_cursor_text :=
30+
-- q'[select ]'||l_ut_owner||q'[.ut_annotation_obj_cache_info(
31+
-- object_owner => i.object_owner,
32+
-- object_name => i.object_name,
33+
-- object_type => i.object_type,
34+
-- needs_refresh => null
35+
-- )
36+
-- from ]'||l_ut_owner||q'[.ut_annotation_cache_info i
37+
-- where
38+
-- not exists (
39+
-- select 1 from ]'||l_objects_view||q'[ o
40+
-- where o.owner = i.object_owner
41+
-- and o.object_name = i.object_name
42+
-- and o.object_type = i.object_type
43+
-- and o.owner = :a_object_owner
44+
-- and o.object_type = :a_object_type
45+
-- )
46+
-- and i.object_owner = :a_object_owner
47+
-- and i.object_type = :a_object_type]';
48+
-- open l_rows for l_cursor_text using a_object_owner, a_object_type, a_object_owner, a_object_type;
49+
-- fetch l_rows bulk collect into l_result limit 1000000;
50+
-- close l_rows;
51+
-- return l_result;
52+
-- end;
53+
2254
function get_annotation_objs_info(a_object_owner varchar2, a_object_type varchar2, a_parse_date timestamp := null) return ut_annotation_objs_cache_info is
2355
l_rows sys_refcursor;
2456
l_ut_owner varchar2(250) := ut_utils.ut_owner;
@@ -28,25 +60,36 @@ create or replace package body ut_annotation_manager as
2860
begin
2961
l_cursor_text :=
3062
q'[select ]'||l_ut_owner||q'[.ut_annotation_obj_cache_info(
31-
object_owner => o.owner,
32-
object_name => o.object_name,
33-
object_type => o.object_type,
34-
needs_refresh => case when o.last_ddl_time < cast(i.parse_time as date) then 'N' else 'Y' end
63+
object_owner => nvl( o.owner, i.object_owner ),
64+
object_name => nvl( o.object_name, i.object_name ),
65+
object_type => nvl( o.object_type, i.object_type ),
66+
needs_refresh =>
67+
case
68+
when o.last_ddl_time < cast(i.parse_time as date) then 'N'
69+
when o.owner is null then null
70+
else 'Y'
71+
end
3572
)
36-
from ]'||l_objects_view||q'[ o
37-
left join ]'||l_ut_owner||q'[.ut_annotation_cache_info i
73+
from (
74+
select * from ]'||l_objects_view||q'[ o
75+
where o.owner = :a_object_owner
76+
and o.object_type = :a_object_type
77+
) o
78+
full outer join (
79+
select * from ]'||l_ut_owner||q'[.ut_annotation_cache_info i
80+
where i.object_owner = :a_object_owner
81+
and i.object_type = :a_object_type
82+
) i
3883
on o.owner = i.object_owner
3984
and o.object_name = i.object_name
4085
and o.object_type = i.object_type
41-
where o.owner = :a_object_owner
42-
and o.object_type = :a_object_type
43-
and ]'
86+
where ]'
4487
|| case
4588
when a_parse_date is null
4689
then ':a_parse_date is null'
47-
else 'o.last_ddl_time >= cast(:a_parse_date as date)'
90+
else 'o.last_ddl_time >= cast(:a_parse_date as date) or o.last_ddl_time is null'
4891
end;
49-
open l_rows for l_cursor_text using a_object_owner, a_object_type, a_parse_date;
92+
open l_rows for l_cursor_text using a_object_owner, a_object_type, a_object_owner, a_object_type, a_parse_date;
5093
fetch l_rows bulk collect into l_result limit 1000000;
5194
close l_rows;
5295
return l_result;
@@ -106,8 +149,9 @@ create or replace package body ut_annotation_manager as
106149
end;
107150

108151
procedure build_annot_cache_for_sources(
109-
a_object_owner varchar2, a_object_type varchar2, a_sources_cursor sys_refcursor,
110-
a_schema_objects ut_annotation_objs_cache_info
152+
a_object_owner varchar2,
153+
a_object_type varchar2,
154+
a_sources_cursor sys_refcursor
111155
) is
112156
l_annotations ut_annotations;
113157
c_lines_fetch_limit constant integer := 1000;
@@ -118,7 +162,6 @@ create or replace package body ut_annotation_manager as
118162
l_parse_time date := sysdate;
119163
pragma autonomous_transaction;
120164
begin
121-
ut_annotation_cache_manager.cleanup_cache(a_schema_objects);
122165
loop
123166
fetch a_sources_cursor bulk collect into l_names, l_lines limit c_lines_fetch_limit;
124167
for i in 1 .. l_names.count loop
@@ -148,17 +191,37 @@ create or replace package body ut_annotation_manager as
148191
end;
149192

150193

151-
procedure rebuild_annotation_cache( a_object_owner varchar2, a_object_type varchar2, a_info_rows ut_annotation_objs_cache_info) is
152-
l_objects_to_parse ut_annotation_objs_cache_info := ut_annotation_objs_cache_info();
194+
procedure rebuild_annotation_cache(
195+
a_object_owner varchar2,
196+
a_object_type varchar2,
197+
a_info_rows ut_annotation_objs_cache_info
198+
) is
199+
l_objects_to_parse ut_annotation_objs_cache_info;
200+
l_objects_to_remove ut_annotation_objs_cache_info;
153201
begin
154-
select value(x)bulk collect into l_objects_to_parse from table(a_info_rows) x where x.needs_refresh = 'Y';
202+
select value(x)bulk collect into l_objects_to_parse
203+
from table(a_info_rows) x where x.needs_refresh = 'Y';
204+
205+
ut_annotation_cache_manager.cleanup_cache(l_objects_to_parse);
206+
207+
if sys_context('userenv','current_schema') = a_object_owner
208+
or ut_metadata.is_object_visible('ut3.ut_utils')
209+
or ut_metadata.is_object_visible('dba_objects')
210+
then
211+
select value(x)bulk collect into l_objects_to_remove
212+
from table(a_info_rows) x where x.needs_refresh is null;
213+
214+
ut_annotation_cache_manager.remove_from_cache(l_objects_to_remove);
215+
-- ut_annotation_cache_manager.remove_from_cache(
216+
-- get_missing_objects(a_object_owner, a_object_type)
217+
-- );
218+
end if;
155219

156220
--if some source needs parsing and putting into cache
157221
if l_objects_to_parse.count > 0 then
158222
build_annot_cache_for_sources(
159223
a_object_owner, a_object_type,
160-
get_sources_to_annotate(a_object_owner, a_object_type, l_objects_to_parse),
161-
l_objects_to_parse
224+
get_sources_to_annotate(a_object_owner, a_object_type, l_objects_to_parse)
162225
);
163226
end if;
164227
end;

source/core/ut_metadata.pkb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,34 @@ create or replace package body ut_metadata as
148148
end;
149149

150150
function get_dba_view(a_dba_view_name varchar2) return varchar2 is
151-
l_invalid_object_name exception;
152151
l_result varchar2(128) := lower(a_dba_view_name);
152+
begin
153+
if not is_object_visible(a_dba_view_name) then
154+
l_result := replace(l_result,'dba_','all_');
155+
end if;
156+
return l_result;
157+
end;
158+
159+
function user_has_execute_any_proc return boolean is
160+
l_ut_owner varchar2(250) := ut_utils.ut_owner;
161+
l_dummy varchar2(250);
162+
begin
163+
execute immediate 'select '||l_ut_owner||'.ut_utils.ut_owner from dual'
164+
into l_dummy;
165+
return true;
166+
exception
167+
when others then
168+
return false;
169+
end;
170+
171+
function is_object_visible(a_object_name varchar2) return boolean is
172+
l_invalid_object_name exception;
153173
pragma exception_init(l_invalid_object_name,-44002);
154174
begin
155-
l_result := dbms_assert.sql_object_name(l_result);
156-
return l_result;
175+
return dbms_assert.sql_object_name(a_object_name) is not null;
157176
exception
158177
when l_invalid_object_name then
159-
return replace(l_result,'dba_','all_');
178+
return false;
160179
end;
161180

162181
function package_exists_in_cur_schema(a_object_name varchar2) return boolean is

source/core/ut_metadata.pks

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ create or replace package ut_metadata authid current_user as
6969
*/
7070
function get_dba_view(a_dba_view_name varchar2) return varchar2;
7171

72+
/**
73+
* Returns true if object is accessible to current user
74+
* @param a_object_name fully qualified object name (with schema name)
75+
*/
76+
function is_object_visible(a_object_name varchar2) return boolean;
77+
78+
/**
79+
* Returns true if current user has execute any procedure privilege
80+
* The check is performed by checking if user can execute ut_utils package
81+
*/
82+
function user_has_execute_any_proc return boolean;
83+
7284
/**
7385
* Returns true if given object is a package and it exists in current schema
7486
* @param a_object_name the name of the object to be checked

source/core/ut_suite_builder.pkb

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,31 @@ create or replace package body ut_suite_builder is
10121012
close a_suite_data_cursor;
10131013
end;
10141014

1015+
function get_missing_objects(a_object_owner varchar2) return ut_varchar2_rows is
1016+
l_rows sys_refcursor;
1017+
l_ut_owner varchar2(250) := ut_utils.ut_owner;
1018+
l_objects_view varchar2(200) := ut_metadata.get_dba_view('dba_objects');
1019+
l_cursor_text varchar2(32767);
1020+
l_result ut_varchar2_rows;
1021+
begin
1022+
l_cursor_text :=
1023+
q'[select i.object_name
1024+
from ]'||l_ut_owner||q'[.ut_suite_cache_package i
1025+
where
1026+
not exists (
1027+
select 1 from ]'||l_objects_view||q'[ o
1028+
where o.owner = i.object_owner
1029+
and o.object_name = i.object_name
1030+
and o.object_type = 'PACKAGE'
1031+
and o.owner = :a_object_owner
1032+
)
1033+
and i.object_owner = :a_object_owner]';
1034+
open l_rows for l_cursor_text using a_object_owner, a_object_owner;
1035+
fetch l_rows bulk collect into l_result limit 1000000;
1036+
close l_rows;
1037+
return l_result;
1038+
end;
1039+
10151040
function get_cached_suite_data(
10161041
a_object_owner varchar2,
10171042
a_path varchar2 := null,
@@ -1177,8 +1202,9 @@ create or replace package body ut_suite_builder is
11771202
a_object_name varchar2 := null,
11781203
a_procedure_name varchar2 := null
11791204
) return ut_suite_items is
1180-
l_annotations_cursor sys_refcursor;
1181-
l_suite_cache_time timestamp;
1205+
l_annotations_cursor sys_refcursor;
1206+
l_suite_cache_time timestamp;
1207+
l_skip_all_objects_scan boolean := false;
11821208
begin
11831209
l_suite_cache_time := ut_suite_cache_manager.get_schema_parse_time(a_owner_name);
11841210
open l_annotations_cursor for
@@ -1188,12 +1214,23 @@ create or replace package body ut_suite_builder is
11881214
)x ]'
11891215
using a_owner_name, l_suite_cache_time;
11901216

1217+
-- if current user is the onwer or current user has execute any procedure privilege
1218+
if sys_context('userenv','current_schema') = a_owner_name
1219+
or ut_metadata.is_object_visible('ut3.ut_utils')
1220+
then
1221+
l_skip_all_objects_scan := true;
1222+
end if;
1223+
if l_skip_all_objects_scan or ut_metadata.is_object_visible('dba_objects') then
1224+
ut_suite_cache_manager.remove_from_cache( a_owner_name, get_missing_objects(a_owner_name) );
1225+
end if;
1226+
11911227
return build_suites_from_annotations(
11921228
a_owner_name,
11931229
l_annotations_cursor,
11941230
a_path,
11951231
a_object_name,
1196-
a_procedure_name
1232+
a_procedure_name,
1233+
l_skip_all_objects_scan
11971234
);
11981235
end;
11991236

@@ -1202,17 +1239,27 @@ create or replace package body ut_suite_builder is
12021239
l_schema_names ut_varchar2_rows;
12031240
l_object_names ut_varchar2_rows;
12041241
l_ut_owner varchar2(250) := ut_utils.ut_owner;
1242+
l_need_all_objects_scan boolean := true;
12051243
begin
1244+
-- if current user is the onwer or current user has execute any procedure privilege
1245+
if ut_metadata.is_object_visible('ut3.ut_utils')
1246+
or (a_schema_names is not null and a_schema_names.count = 1
1247+
and sys_context('userenv','current_schema') = a_schema_names(1))
1248+
then
1249+
l_need_all_objects_scan := false;
1250+
end if;
1251+
12061252
execute immediate 'select c.object_owner, c.object_name
12071253
from '||l_ut_owner||q'[.ut_suite_cache_package c
12081254
join table ( :a_schema_names ) s
1209-
on c.object_owner = upper(s.column_value)
1255+
on c.object_owner = upper(s.column_value)]'
1256+
|| case when l_need_all_objects_scan then q'[
12101257
where exists
12111258
(select 1 from all_objects a
12121259
where a.owner = c.object_owner
12131260
and a.object_name = c.object_name
12141261
and a.object_type = 'PACKAGE')
1215-
]'
1262+
]' end
12161263
bulk collect into l_schema_names, l_object_names using a_schema_names;
12171264
l_results.extend( l_schema_names.count );
12181265
for i in 1 .. l_schema_names.count loop

source/core/ut_suite_cache_manager.pkb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,17 @@ create or replace package body ut_suite_cache_manager is
135135
commit;
136136
end;
137137

138+
procedure remove_from_cache(a_schema_name varchar2, a_objects ut_varchar2_rows) is
139+
pragma autonomous_transaction;
140+
begin
141+
delete
142+
from ut_suite_cache_package i
143+
where i.object_owner = a_schema_name
144+
and i.object_name in ( select column_value from table (a_objects) );
145+
146+
commit;
147+
end;
148+
149+
138150
end ut_suite_cache_manager;
139151
/

source/core/ut_suite_cache_manager.pks

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,7 @@ create or replace package ut_suite_cache_manager authid definer is
2929

3030
function get_schema_parse_time(a_schema_name varchar2) return timestamp result_cache;
3131

32+
procedure remove_from_cache(a_schema_name varchar2, a_objects ut_varchar2_rows);
33+
3234
end ut_suite_cache_manager;
3335
/

0 commit comments

Comments
 (0)