创建 lld_copy_subset 函数

以下示例显示了 lld_copy_subset 用户定义的例程的代码。 此例程将复制一个大对象的其中一部分并将其附加到另一个大对象。

/* LLD SAPI interface example */

#include <mi.h>
#include <lldsapi.h>

/* append a (small) subset of a large object to another large object */

MI_ROW*
lld_copy_subset (MI_ROW* src,           /* source LLD_Locator */
                 MI_ROW* dest,          /* destination LLD_Locator */
                 mi_int8* offset,       /* offset to begin copy at */
                 mi_integer nbytes,     /* number of bytes to copy */
                 MI_FPARAM* fp)
{
    MI_ROW*        new_dest;       /* return value */
    MI_CONNECTION* conn;           /* database server connection */
    mi_string*     buffer;         /* I/O buffer */
    LLD_IO*        io;             /* open large object descriptor */
    mi_int8        new_offset;     /* offset after seek */
    mi_integer     bytes_read;     /* actual number of bytes copied */
    mi_integer     error;          /* error argument */
    mi_integer     _error;         /* extra error argument */
    mi_boolean     created_dest;   /* did we create the dest large object? */

    
    /* initialize variables */
    new_dest = NULL;
    conn = NULL;
    buffer = NULL;
    io = NULL;
    error = LLD_E_OK;
    created_dest = MI_FALSE;

    /* open a connection to the database server */
    conn = mi_open (NULL, NULL, NULL);
    if (conn == NULL)
        goto bad;

    /* allocate memory for I/O */
    buffer = mi_alloc (nbytes);
    if (buffer == NULL)
        goto bad;

    /* read from the source large object */
    io = lld_open (conn, src, LLD_RDONLY, &error);
    if (error != LLD_E_OK)
        goto bad;

    lld_seek (conn, io, offset, LLD_SEEK_SET, &new_offset, &error);
    if (error != LLD_E_OK)
        goto bad;

图: lld_copy_subset 函数

    bytes_read = lld_read (conn, io, buffer, nbytes, &error);
    if (error != LLD_E_OK)
        goto bad;

    lld_close (conn, io, &error);
    if (error != LLD_E_OK)
        goto bad;

    /* write to the destination large object */
    new_dest = lld_create (conn, dest, &error);
    if (error == LLD_E_OK)
        created_dest = MI_TRUE;
    else if (error != LLD_E_EXISTS)
        goto bad;

    io = lld_open (conn, new_dest, LLD_WRONLY | LLD_APPEND | LLD_SEQ, &error);
    if (error != LLD_E_OK)
        goto bad;

    lld_write (conn, io, buffer, bytes_read, &error);
    if (error != LLD_E_OK)
        goto bad;

    lld_close (conn, io, &error);
    if (error != LLD_E_OK)
        goto bad;

    /* free memory */
    mi_free (buffer);

    /* close the database server connection */
    mi_close (conn);

    return new_dest;

    /* error clean up */
bad:
    if (io != NULL)
        lld_close (conn, io, &_error);
    if (created_dest)
        lld_delete (conn, new_dest, &_error);
    if (buffer != NULL)
        mi_free (buffer);
    if (conn != NULL)
        mi_close (conn);
    lld_error_raise (conn, error);
    mi_fp_setreturnisnull (fp, 0, MI_TRUE);
    return NULL;
}
lld_copy_subset 函数定义四个参数:
  • 源大对象(lld_locator 类型)
  • 目标大对象(lld_locator 类型)
  • 开始复制的字节偏移量
  • 要复制的字节数

它将返回 lld_locator 以识别附加的对象。

mi_open 函数将打开到数据库的连接。 为 I/O 分配缓冲区。

针对源对象调用以下 Large Object Locator 函数:
lld_open
打开源对象
lld_seek
在对象中寻道至指定的字节偏移量
lld_read
从对象中读取指定的字节数
lld_close
关闭对象
针对目标对象调用以下 Large Object Locator 函数:
  • lld_open,用于打开目标对象
  • lld_write,用于将从源对象读取的字节写入目标对象
  • lld_close,用于关闭目标对象

mi_close 函数将关闭数据库连接。

此函数还包含错误处理代码。如果无法创建数据库连接、无法分配内存或者有 Large Object Locator 函数返回错误,那么将调用该错误代码。

错误代码处理代码 (bad) 在必要时会执行下面的一项或多项操作:
  • 关闭源文件
  • 删除目标文件
  • 释放缓冲区
  • 关闭数据库连接
  • 引发错误

应该为异常建立回调(出于简明扼要的目的,此示例代码不会这样做)。请参阅《GBase 8t DataBlade API 程序员指南》以获取更多信息。