INFORMIX
Informix-ESQL/C Programmer's Manual
Chapter 7: Working with INFORMIX-Universal Server Smart Large
Home Contents Index Master Index New Book

Accessing a Smart Large Object

To access a smart large object, take the following steps:

1. Select the smart large object from the database into an ifx_lo_t host variable with the SELECT statement.

    2. Open the smart large object with the ifx_lo_open() function.

    3. Perform the appropriate read or write operations to update the data of the smart large object.

    4. Close the smart large object with the ifx_lo_close() function.

Selecting a Smart Large Object

A SELECT statement does not perform the actual output for the smart-large-object data. It does, however, establish a means for the application program to identify a smart large object so that it can then issue ESQL/C library functions to open, read, write, or perform other operations on the smart large object.

A CLOB or BLOB column in a database table contains the LO-pointer structure for a smart large object. Therefore, when you select a CLOB or BLOB column into an ifx_lo_t host variable, the SELECT statement returns an LO-pointer structure. For this reason, you declare host variables for CLOB and BLOB values as LO-pointer structures. (For more information, see "Declaring a Host Variable".)

Figure 7-5 shows how the database server transfers the data of a smart large object to the ESQL/C client application.

Figure 7-5
Transferring Smart- Large-Object Data from Database Server to Client Application

For a sample of code that selects a smart large object from a database column, see "The create_clob Program". For information on how to store a smart large object in the database, see page 7-17.

Opening a Smart Large Object

When you open a smart large object, you obtain an LO file descriptor for the smart large object. Through the LO file descriptor you can access the data of a smart large object as if it were in an operating-system file.

Access Modes

When you open a smart large object, you specify the access mode for the data. The access mode determines which read and write operations are valid on the open smart large object. You specify an access mode with one of the access-mode constants that the locator.h file defines.

Figure 7-6 shows the access modes and their corresponding defined constants that the ifx_lo_open() and ifx_lo_create() functions support.

Figure 7-6
Access-Mode Flags for Smart Large Objects

Access Mode Purpose Access-Mode Constant

Read-only mode

Only read operations are valid on the data.

LO_RDONLY

Write-only mode

Only write operations are valid on the data.

LO_WRONLY

Write/append mode

Appends any data you write to the end of the smart large object. By itself, it is equivalent to write-only mode followed by a seek to the end of the smart large object. Read operations fail.

LO_APPEND

Read/write mode

Both read and write operations are valid on the data.

LO_RDWR

Buffered access

Use standard database server buffer pool.

LO_BUFFER

Lightweight I/O

Use private buffers from the session pool of the database server.

LO_NOBUFFER

Tip: These access-mode flags for a smart large object are patterned after the UNIX System V access modes.
When you open a smart large object with LO_APPEND only, the smart large object is opened as read-only. Seek operations move the file pointer but write operations to the smart large object fail and the file pointer is not moved from its position just before the write. Read operations occur from where the file pointer is positioned and then the file pointer is moved.

You can mask the LO_APPEND flag with another access mode. In any of these OR combinations, the seek operation remains unaffected. The following table shows the effect on the read and write operations that each of the OR combinations has.
OR Operation Read Operations Write Operations

LO_RDONLY | LO_APPEND

Occur at the file position and then move the file position to the end of the data that has been read

Fail and do not move the file position.

LO_WRONLY | LO_APPEND

Fail and do not move the file position

Move the file position to the end of the smart large object and then write the data; file position is at the end of the data after the write.

LO_RDWR | LO_APPEND

Occur at the file position and then move the file position to the end of the data that has been read

Move the file position to the end of the smart large object and then write the data; file position is at the end of the data after the write.

Lightweight I/O
When the database server accesses smart large objects, it uses buffers from the buffer pool for buffered access. Unbuffered access is called lightweight I/O. Lightweight I/O uses private buffers instead of the buffer pool to hold smart large objects. These private buffers are allocated out of the database server session pool.

Lightweight I/O allows you to bypass the overhead of the least-recently-used (LRU) queues that the database server uses to manage the buffer pool. For more information about LRUs, see the INFORMIX-Universal Server Performance Guide.

You can specify lightweight I/O by setting the flags parameter to LO_NOBUFFER when you create a smart large object with the ifx_lo_create() function or when you open a particular smart large object with the ifx_lo_open() function. To specify buffered access, which is the default, use the LO_BUFFER flag.

Important: Keep the following issues in mind when you use lightweight I/O:

The database server imposes the following restrictions on switching from lightweight I/O to buffered I/O:

You can use the database server utility onspaces to specify lightweight I/O for all smart large objects in an sbspace. For more information on the onspaces utility, refer to the Administrator's Guide for your database server.

Smart-Large-Object Locks

To prevent simultaneous access to smart-large-object data, the database server locks a smart large object when you open it. Locks on smart large objects are different than row locks. If you retrieve a smart large object from a row, the database server might hold a row lock as well as a smart-large-object lock. The database server locks smart large objects because many columns can contain the same smart-large-object data.

You use the access-mode flags, LO_RDONLY, LO_APPEND, LO_WRONLY, LO_RDWR, and LO_TRUNC to specify the lock mode of a smart large object. You pass these flags to the ifx_lo_open() and ifx_lo_create() functions. When you specify LO_RDONLY, the database server places a share lock on the smart-large-object data. If you specify any other access-mode flag, the database server obtains an update lock, which it promotes to an exclusive lock on first write or other update operation.

Share and update locks (read-only mode, or write mode before an update operation occurs) are held until your program takes one of the following actions:

Exclusive locks are held until the end of a transaction even if you close the smart large object.

Important: You lose the lock at the end of a transaction, even if the smart large object remains open. When the database server detects that a smart large object has no active lock, it automatically obtains a new lock when the first access occurs to the smart large object. The lock it obtains is based on the original open mode of the smart large object.

Duration of an Open on a Smart Large Object

Once you open a smart large object with the ifx_lo_create() function or the ifx_lo_open() function, it remains open until one of the following events occurs:

Warning: The end of the current transaction does not close a smart large object. It does, however, release any lock on a smart large object.
Have your applications close smart large objects as soon as they finish with them. Leaving smart large objects open unnecessarily consumes system memory. Leaving a sufficient number of smart large objects open can eventually produce an out-of-memory condition.

Deletion of a Smart Large Object

A smart large object is not deleted until both of the following conditions are met:

Modifying a Smart Large Object

You modify the data of the smart large object with the following steps:

1. Read and write the data in the open smart large object until the data is ready to save.

    2. Store the LO-pointer for the smart large object in the database with the UPDATE or INSERT statement.

For information on how to save the smart large object, see "Storing a Smart Large Object".

Reading Data From a Smart Large Object

The ifx_lo_read() and ifx_lo_readwithseek() ESQL/C library functions read data from an open smart large object. They both read a specified number of bytes from the open smart large object into the user-defined character buffer. The ifx_lo_read() function begins the read operation at the current file position. You can specify the starting file position of the read with the ifx_lo_seek() function, and you can obtain the current file position with the ifx_lo_tell() function. The ifx_lo_readwithseek() function performs the seek and read operations with a single function call.

The ifx_lo_read() and ifx_lo_readwithseek() functions require a valid LO file descriptor to identify the smart large object to be read. You obtain an LO file descriptor with the ifx_lo_open() or ifx_lo_create() function. For more information, see the descriptions of the ifx_lo_read() function on page 7-55 and the ifx_lo_readwithseek() function on page 7-57.

Writing Data to a Smart Large Object

The ifx_lo_write() and ifx_lo_writewithseek() ESQL/C library functions write data to an open smart large object. They both write a specified number of bytes from a user-defined character buffer to the open smart large object. The ifx_lo_write() function begins the write operation at the current file position. You can specify the starting file position of the write with the ifx_lo_seek() function, and you can obtain the current file position with the ifx_lo_tell() function. The ifx_lo_writewithseek() function performs the seek and write operations with a single function call.

The ifx_lo_write() and ifx_lo_writewithseek() functions require a valid LO file descriptor to identify the smart large object to write. You obtain an LO file descriptor with the ifx_lo_open() or ifx_lo_create() function. For more information, see the descriptions of the ifx_lo_write() function on page 7-96 and the ifx_lo_writewithseek() function on page 7-98.

Closing a Smart Large Object

Once you have finished the read and write operations on the smart large object, deallocate the resources assigned to it with the ifx_lo_close() function. When the resources are freed, they can be reallocated to other structures that your program needs. In addition, the LO file descriptor can be reallocated to other smart large objects. For more information on the ifx_lo_close() function, see page 7-38.




Informix-ESQL/C Programmer's Manual, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.