Oracle® XML DB Developer's Guide 10g Release 1 (10.1) Part Number B10790-01 |
|
|
View PDF |
This chapter describes the SQL-based mechanisms, RESOURCE_VIEW
and PATH_VIEW
, used to access Oracle XML DB repository data. It discusses the SQL operators UNDER_PATH
and EQUALS_PATH
that query resources based on their path names and PATH
and DEPTH
operators that return resource path names and depths respectively.
This chapter contains these topics:
Figure 20-1 shows how Oracle XML DB RESOURCE_VIEW
and PATH_VIEW
provide a mechanism for using SQL to access data stored in Oracle XML DB repository. Data stored in Oracle XML DB repository through protocols such as FTP, WebDAV, or application program interfaces (APIs), can be accessed in SQL using RESOURCE_VIEW
values and PATH_VIEW
values, and vice versa.
RESOURCE_VIEW
consists of a resource, itself an XMLType
, that contains the name of the resource that can be queried, its ACLs, and its properties, static or extensible.
If the content comprising the resource is XML, stored somewhere in an XMLType
table or view, then the RESOURCE_VIEW
points to the XMLType
row that stores the content.
If the content is not XML, then the RESOURCE_VIEW
stores it as a LOB.
Parent-child relationships between folders (necessary to construct the hierarchy) are maintained and traversed efficiently using the hierarchical index. Text indexes are available to search the properties of a resource, and internal B*Tree indexes over Names and ACLs speed up access to these attributes of the Resource XMLType
.
RESOURCE_VIEW
and PATH_VIEW
together, along with PL/SQL package, DBMS_XDB
, provide all query-based access to Oracle XML DB and DML functionality that is available through the programming API.
The base table for RESOURCE_VIEW
is XDB.XDB$RESOURCE
and should only be accessed through RESOURCE_VIEW
or the DBMS_XDB
API.
Figure 20-1 Accessing Repository Resources Using RESOURCE_VIEW and PATH_VIEW
The RESOURCE_VIEW
contains one row for each resource in the repository. The following describes its structure:
Column Datatype Description ------ -------- ------------------------------------------------------- RES XMLType A resource in Oracle XML repository ANY_PATH VARCHAR2 A path that can be used to access the resource in the repository RESID RAW Resource OID which is a unique handle to the resource
The PATH_VIEW
contains one row for each unique path to access a resource in the repository. The following describes its structure:
Column Datatype Description ------ -------- ----------------------------- PATH VARCHAR2 Path name of a resource RES XMLType The resource referred by PATH LINK XMLType Link property RESID RAW Resource OID
Figure 20-2 illustrates the structure of Resource and PATH_VIEW
s.
Note: Each resource may have multiple paths called links. |
The path in the RESOURCE_VIEW
is an arbitrary one and one of the accessible paths that can be used to access that resource. Oracle XML DB provides operator UNDER_PATH
that enables applications to search for resources contained (recursively) within a particular folder, get the resource depth, and so on. Each row in the PATH_VIEW
and RESOURCE_VIEW
columns is of XMLType
. DML on Oracle XML DB repository views can be used to insert, rename, delete, and update resource properties and contents. Programmatic APIs must be used for some operations, such as creating links to existing resources.
Path names in the ANY_PATH
column of the RESOURCE_VIEW
and the PATH
column in the PATH_VIEW
are absolute paths.
Path names from the PATH
operator are relative paths under the path name specified by the UNDER_PATH
operator. Suppose there are two resources pointed to by path names '/a/b/c'
and '/a/d'
respectively, a PATH
operator that retrieves paths under the folder '/a'
will return relative paths 'b/c'
and 'd'
.
When there are multiple links to the same resource, only paths under the path name specified by the UNDER_PATH
operator are returned. Suppose '/a/b/c'
, '/a/b/d'
and '/a/e'
are links to the same resource, a query on the PATH_VIEW
that retrieves all the paths under '/a/b'
return only '/a/b/c'
and '/a/b/d'
, not '/a/e'
.
The major difference between the RESOURCE_VIEW
and PATH_VIEW
is:
PATH_VIEW
displays all the path names to a particular resource whereas RESOURCE_VIEW
displays one of the possible path names to the resource
PATH_VIEW
also displays the properties of the link
Figure 20-3 illustrates the difference between RESOURCE_VIEW
and PATH_VIEW
.
Because many Internet applications only need one URL to access a resource, RESOURCE_VIEW
is widely applicable.
PATH_VIEW
contains the link properties as well as resource properties, whereas the RESOURCE_VIEW
only contains resource properties.
The RESOURCE_VIEW
benefit is generally optimization. If the database knows that only one path is needed, then the index does not have to do as much work to determine all the possible paths.
Note: When using theRESOURCE_VIEW , if you are specifying a path with the UNDER_PATH or EQUALS_PATH operators, then they will find the resource regardless of whether or not that path is the arbitrary one chosen to normally appear with that resource using RESOURCE_VIEW . |
You can perform the following operations using UNDER_PATH
and EQUALS_PATH
:
Given a path name:
Get a resource or its OID
List the directory given by the path name
Create a resource
Delete a resource
Update a resource
Given a condition, containing UNDER_PATH
operator or other SQL operators:
Update resources
Delete resources
Get resources or their OID
See the "Using the Resource View and Path View API" and EQUALS_PATH
.
The UNDER_PATH
operator uses the Oracle XML DB repository hierarchical index to return the paths under a particular path. The hierarchical index is designed to speed access walking down a path name (the normal usage).
If the other parts of the query predicate are very selective, however, then a functional implementation of UNDER_PATH
can be chosen that walks back up the repository. This can be more efficient, because a much smaller number of links are required to be traversed. Figure 20-4 shows the UNDER_PATH
syntax.
Table 20-1 describes the UNDER_PATH
syntax.
Table 20-1 RESOURCE_VIEW and PATH_VIEW API Syntax: UNDER_PATH
Syntax | Description |
---|---|
INTEGER UNDER_PATH(resource_column, pathname); |
Determines if a resource is under a specified path.
Parameters:
|
INTEGER UNDER_PATH(resource_column, depth, pathname); |
Determines if a resource is under a specified path, with a depth argument to restrict the number of levels to search.
Parameters:
|
INTEGER UNDER_PATH(resource_column, pathname, correlation); |
Determines if a resource is under a specified path, with a correlation argument for ancillary operators.
Parameters:
|
INTEGER UNDER_PATH(resource_column, depth, pathname, correlation); |
Determines if a resource is under a specified path with a depth argument to restrict the number of levels to search, and with a correlation argument for ancillary operators.
Parameters:
Note that only one of the accessible paths to the resource must be under the path argument for a resource to be returned. |
The EQUALS_PATH
operator is used to find the resource with the specified path name. It is functionally equivalent to UNDER_PATH
with a depth restriction of zero.
EQUALS_PATH INTEGER EQUALS_PATH( resource_column,pathname);
where:
resource_column is the column name or column alias of the 'resource' column in the path_view
or resource_view.
pathname
is the path name to resolve.
Figure 20-5 illustrates the EQUALS_PATH
syntax.
PATH
is an ancillary operator that returns the relative path name of the resource under the specified pathname
argument. Note that the path column in the RESOURCE_VIEW
always contains the absolute path of the resource. The PATH
syntax is:
PATH VARCHAR2 PATH( correlation);
where:
correlation
is an integer that can be used to correlate the UNDER_PATH
operator (a primary operator) with ancillary operators (PATH
and DEPTH
).
Note: If a path is not under the specifiedpathname argument, a NULL value is returned as the output of the current path. |
Figure 20-6 illustrates the PATH
syntax.
Here are some examples of a RESOURCE_VIEW
that include resources specified by paths:
'/a/b/c' '/a/b/c/d' '/a/e/c' '/a/e/c/d'
Example 20-1 Determining Paths Under the Specified Pathname Argument
SELECT path(1) FROM resource_view WHERE UNDER_PATH(res, '/a/b', 1) = 1;
Returns the following:
PATH(1) ------- c c/d 2 rows returned
Example 20-2 Determining Paths Not Under the Specified Pathname Argument
SELECT path(1) FROM resource_view WHERE UNDER_PATH(res, '/a/b', 1)!=1
Returns the following:
PATH(1) ------- 2 rows returned
Note: For absolute paths useANY_PATH as follows:
SELECT ANY_PATH FROM resource_view WHERE UNDER_PATH(res, '/a/b')=1; This returns the following: ANY_PATH -------- /a/e/c /a/e/c/d 2 rows returned |
Example 20-3 Determining Paths Using Multiple Correlations
SELECT ANY_PATH, path(1), path(2) FROM resource_view WHERE UNDER_PATH(res, '/a/b', 1) = 1 or UNDER_PATH(res, '/a/e', 2) = 1;
This returns the following:
ANY_PATH PATH(1) PATH(2) ---------------------------------------------------- /a/b/c c /a/b/c/d c/d /a/e/c c /a/e/c/d c/d 4 rows returned
DEPTH
is an ancillary operator that returns the folder depth of the resource under the specified starting path.
DEPTH INTEGER DEPTH( correlation);
where:
correlation
is an integer that can be used to correlate the UNDER_PATH
operator (a primary operator) with ancillary operators (PATH
and DEPTH
).
The following RESOURCE_VIEW
and PATH_VIEW
examples use operators UNDER_PATH
, EQUALS_PATH
, PATH
, and DEPTH
.
The following examples illustrate how you can access paths, resources, and link properties in Oracle XML DB repository:
Example 20-4 Using UNDER_PATH: Given a Path Name, List the Directory Given by the Path Name from the RESOURCE_VIEW
SELECT any_path FROM resource_view WHERE any_path like '/sys%';
Example 20-5 Using UNDER_PATH: Given a Path Name, Get a Resource From the RESOURCE_VIEW
SELECT any_path, extract(res, '/Resource') FROM resource_view WHERE under_path(res, '/sys') = 1;
Example 20-6 Using RESOURCE_VIEW: Given a Path, Get all Relative Path Names for Resources up to Three Levels
SELECT path(1) FROM resource_view WHERE under_path (res, 3, '/sys',1)=1;
Example 20-7 Using UNDER_PATH: Given a Path Name, Get Path and Depth Under a Specified Path from the PATH_VIEW
SELECT path(1) PATH,depth(1) depth FROM path_view WHERE under_path(RES, 3,'/sys',1)=1;
Example 20-8 Given a Path Name, Get Paths and Link Properties from PATH_VIEW
SELECT path, extract(link, '/LINK/Name/text()').getstringval(), extract(link, '/LINK/ParentName/text()').getstringval(), extract(link, '/LINK/ChildName/text()').getstringval(), extract(res, '/Resource/DisplayName/text()').getstringval() FROM path_view WHERE path LIKE '/sys%';
Example 20-9 Using UNDER_PATH: Given a Path Name, Find all the Paths up to a Certain Number of Levels, Including Links Under a Specified Path from the PATH_VIEW
SELECT path(1) FROM path_view WHERE under_path(res, 3,'/sys', 1) > 0 ;
Example 20-10 Using EQUALS_PATH to Locate a Path
SELECT any_path FROM resource_view WHERE equals_path(res, '/sys') > 0;
Example 20-11 Retrieve RESID of a Given Resource
select resid from resource_view where extract(res, '/Resource/Dispname') = 'example';
Example 20-12 Get the Path Name of a Resource Given Its OID
select any_path from resource_view where resid = :1;
Example 20-13 Select all Folders Under a Given Path
select any_path from resource_view where under_path(res, 1, '/parent_folder') = 1 and existsNode(res, '/Resource[@Container="true"]') = 1;
Example 20-14 Join RESOURCE_VIEW with XMLType table MYPOs
SELECT Extract(value(p), '/PurchaseOrder/Item').getClobval() FROM MYPOs p, RESOURCE_VIEW WHERE ExistsNode(value(p), 'PurchaseOrder[PONum=1001 and Company = "Oracle Corp"]') = 1 and any_path like '/public/po1%';
Example 20-15 Creating Resources: Inserting Data Into a Resource
Note the insert syntax change here: a NULL
value is added for the resid
column.
INSERT INTO resource_view VALUES(sys.xmltype.createxml(' <Resource xmlns="http://xmlns.oracle.com/xdb/XDBResource.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/xdb/XDBResource.xsd http://xmlns.oracle.com/xdb/XDBResource.xsd"> <Author>John Doe</Author> <DisplayName>example</DisplayName> <Comment>This resource was contrived for resource view demo</Comment> <Language>en</Language> <CharacterSet>ASCII</CharacterSet> <ContentType>text/plain</ContentType> </Resource>'), '/home/SCOTT/example', NULL);
The following example illustrates how you can insert data into a resource:
Example 20-16 Creating Resources: Inserting Data Into a Resource
INSERT INTO resource_view VALUES(sys.xmltype.createxml(' <Resource xmlns="http://xmlns.oracle.com/xdb/XDBResource.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/xdb/XDBResource.xsd http://xmlns.oracle.com/xdb/XDBResource.xsd"> <Author>John Doe</Author> <DisplayName>example</DisplayName> <Comment>This resource was contrived for resource view demo</Comment> <Language>en</Language> <CharacterSet>ASCII</CharacterSet> <ContentType>text/plain</ContentType> </Resource>'), '/home/SCOTT/example', NULL);
The following examples illustrate how you can delete resources or paths:
Example 20-17 Deleting Resources
DELETE FROM resource_view WHERE any_path = '/home/SCOTT/example';
If only leaf resources are deleted, then you can perform a delete using delete from resource_view where...
.
Example 20-18 Deleting Resources With Multiple Links
For multiple links to the same resource, deleting from RESOURCE_VIEW
will delete all the links to that resource, while deleting from PATH_VIEW
will delete only the specified path.
Suppose '/home/file1'
is a link to '/public/file'
.
DELETE FROM resource_view WHERE equals_path(res, '/home/file1')=1;
deletes both paths from the repository.
DELETE FROM path_view WHERE equals_path(res, '/home/file1')=1;
deletes '/home/file1'
only.
If only leaf resources are deleted, you can delete them using "delete from resource_view where...
". For example, one way to delete leaf node /public/test/doc.xml
is as follows:
DELETE FROM resource_view WHERE under_path(res, '/public/test/doc.xml') = 1;
However, if you attempt to delete a non-empty container recursively, then the following rules apply:
Delete on a non-empty container is not allowed
The order of the paths returned from the where clause predicates is not guaranteed
Therefore you should guarantee that a container is deleted only after its children have been deleted.
The following examples illustrate how to update resources and paths:
Example 20-20 Updating Resources
UPDATE resource_view set res = sys.xmltype.createxml(' <Resource xmlns="http://xmlns.oracle.com/xdb/XDBResource.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/xdb/XDBResource.xsd http://xmlns.oracle.com/xdb/XDBResource.xsd"> <Author>John Doe</Author> <DisplayName>example</DisplayName> <Comment>Has this got updated or not ? </Comment> <Language>en</Language> <CharacterSet>ASCII</CharacterSet> <ContentType>text/plain</ContentType> </Resource>') WHERE any_path = '/home/SCOTT/example';
Example 20-21 Updating a Path in the PATH_VIEW
UPDATE path_view set path = '/home/XDB' WHERE path = '/home/SCOTT/example';
Example 20-22 Updating DisplayName in RESOURCE_VIEW
If you use the following code to update DisplayName
in RESOURCE_VIEW.res
after uploading a document, it is updated in the RESOURCE_VIEW
, However, if you access using FTP, the old name and bytes are reset to 0
.
UPDATE resource_view set res = sys.xmltype.createxml(' <Resource xmlns="http://xmlns.oracle.com/xdb/XDBResource.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/xdb/XDBResource.xsd http://xmlns.oracle.com/xdb/XDBResource.xsd"> <DisplayName>bearprob_new.txt</DisplayName> <Language>en</Language> <CharacterSet>utf-8</CharacterSet> <ContentType>text/plain</ContentType> <RefCount>1</RefCount> </Resource>') WHERE any_path = '/bearprob.txt';
Here the RESOURCE_VIEW UPDATE
statement replaces the entire resource, including its contents. Because the new Resource document specified does not have a Contents
element, the contents are cleared. The following statement will update just the DisplayName
:
UPDATE RESOURCE_VIEW r SET r.res = updatexml(r.res, '/Resource/DisplayName/text()', 'bear_prob_new.txt') WHERE any_path='/bear_prob.txt';
The FTP protocol does not distinguish between DisplayName
and the name of the resource in the path. If you have a resource at the path /a/b/c
, the FTP client always displays it as c
. However, Web Folders show the display name because the WebDAV protocol can distinguish between the path name and display name. Also note that updating the DisplayName
, using the RESOURCE_VIEW UPDATE
statement shown in the previous paragraph does not change the path of the resource. The resource will still be located at /bearprob.txt
.
Note: If you must get all the resources under a directory, then you can use theLIKE operator, as shown in Example 20-4.
If you must get the resources up to a certain number of levels, or get the relative path, then use the The query plan for Example 20-4 will be more optimal than that of Example 20-5. |
See Also: Chapter 18, " Accessing Oracle XML DB Repository Data",Table 18-3, "Accessing Oracle XML DB Repository: API Options " for additional examples that use theRESOURCE_VIEW and PATH_VIEW operators |
Operations listed in Table 18-3 typically apply to only one resource at a time. To perform the same operation on multiple Oracle XML DB resources, or to find one or more Oracle XML DB resources that meet a certain set of criteria, use RESOURCE_VIEW
and PATH_VIEW
in SQL.
For example, you can perform the following operations with these resource_view and PATH_VIEW
SQL clauses:
Updating based on attributes
UPDATE RESOURCE_VIEW SET resource = updateXML(res, '/Resource/Contents', lob) WHERE extractValue(resource, '/Resource/DisplayName') = 'My stuff';
Finding recursively in a folder
SELECT FROM RESOURCE_VIEW WHERE UNDER_PATH(resource, '/public') ...
Copying a set of Oracle XML DB resources
INSERT INTO PATH_VIEW SELECT '/newlocation' || path, res, link, NULL FROM PATH_VIEW WHERE UNDER_PATH(resource, '/public', 1) = 1 ORDER BY path;
XML DB uses the xdbconfig.xml
file for configuring the system and protocol environment. It includes an element resource-view-cache-size
parameter that defines the in-memory size of the RESOURCE_VIEW
cache. The default value is 1048576.
Some queries on RESOURCE_VIEW
and PATH_VIEW
can be sped up by tuning resource-view-cache-size
. In general, the bigger the cache size, the faster the query. The default resource-view-cache-size
is appropriate for most cases. However, you may want to enlarge your resource-view-cache-size
element when querying a sizable RESOURCE_VIEW
.
The extensible optimizer decides whether the UNDER_PATH
or the EQUALS_PATH
operator is evaluated by domain index scan or functional implementation. The optimizer needs statistics for XML DB to achieve the optimal query plan. Statistics can be collected by analyzing the XML DB tables and hierarchical index under XDB using the ANALYZE
command or the DBMS_STATS
package. The following is an example of using the ANALYZE
command:
analyze table xdb$h_link compute statistics; analyze table xdb$resource compute statistics; analyze index xdbhi_idx delete statistics;
Two new performance elements are included in 10g release 1 (10.1). The default limits for these elements are soft limits. The system automatically adapts when these limits are exceeded. These elements are:
xdbcore-loadableunit-size
- This element indicates the maximum size to which a loadable unit (partition) can grow in Kilobytes. When a partition is read into memory or a partition is built while consuming a new document, the partition is built until it reaches the maximum size. The default value is 16 Kb.
xdbcore-xobmem-bound
- This element indicates the maximum memory in kilobytes that a document is allowed to occupy. The default value is 1024 Kb. Once the document exceeds this number, some loadable units (partitions) are swapped out.
The XDB$RESOURCE
table in Oracle XML DB user schema stores in Oracle XML DB the metadata and data corresponding to resources, such as files and folders. You can search for resources containing a specific keyword by using the CONTAINS
operator in RESOURCE_VIEW
or PATH_VIEW
.
Example 20-23 Find All Resources Containing Keywords "Oracle" and "Unix"
SELECT path FROM path_view WHERE contains(res, 'Oracle AND Unix') > 0;
Example 20-24 Find All Resources Containing Keyword "Oracle" that are Also Under a Specified Path.
SELECT any_path FROM resource_view WHERE contains(res, 'Oracle') > 0 AND under_path(res, '/myDocuments') > 0;
To evaluate such queries, you must create a Context Index on the XDB$RESOURCE
table. Depending on the type of documents stored in Oracle XML DB, choose one of the following options for creating your Context Index:
If Oracle XML DB contains only XML documents, that is, no binary data, a regular Context Index can be created on the XDB$RESOURCE
table.
CREATE INDEX xdb$resource_ctx_i ON xdb.xdb$resource x (value(x)) INDEXTYPE IS ctxsys.context;
If Oracle XML DB contains binary data, for example Microsoft Word documents, a user filter is required to filter such documents prior to indexing. It is recommended that you use the DBMS_XDBT
package (dbmsxdbt.sql
) to create and configure the Context Index.
See Also:
|
REM Install the package - connected as SYS @dbmsxdbt REM Create the preferences exec dbms_xdbt.createPreferences; REM Create the index exec dbms_xdbt.createIndex;
DBMS_XDBT
package also includes procedures to sync and optimize the index. You can use the configureAutoSync()
procedure to configure automatic sync of the index by using job queues.