ProSortはC関数APIを提供します。すべての関数は戻り値を持ちます。成功時は0を返し、失敗時は負数値を返します。

以下は、APIの形態で提供する関数です。

関数 説明

prosort_run_script

スクリプトの演算をすべて実行します。

prosort_setup

ProSortの実行に必要な情報を初期化します。

prosort_setup_with_userexit

すべての入出力パラメータがprosort_setup関数と同じです。

prosort_release_record

SORT演算を行う際に、データをレコードごとに入力します。

prosort_release_end

SORT演算を行う際に、データの入力が完了したことを示します。

prosort_return_record

演算の結果をレコード1件ごとに読み込みます。

prosort_end

すべての演算に使用したリソースを返します。

prosort_get_error_number

エラーが発生した際に、エラー番号を取得します。

prosort_get_error_message

エラーが発生した際に、エラー・メッセージを取得します。

1. prosort_run_script

スクリプトの演算をすべて実行する関数です。

  • プロトタイプ

    int prosort_run_script(const char *const script);
  • パラメータ

    パラメータ 説明

    script

    演算に使用するProSortのスクリプトです。

  • #include <stdio.h>
    #include <stdlib.h>
    #include "prosort.h"
    
    int
    main(int argc, char **argv)
    {
        int rc;
        char *script =
            "DEFREC FIXED,SIZE=256 "
            "MEMORY 500M "
            "INFILE = (x1,100M, x2, 100M, x3, 100M, x4, 100M) "
            "SORT FIELDS=(1,13,AC,A) "
            "OUTFIL FNAMES = xresult ";
    
        rc = prosort_run_script (script);
        if (rc < 0) {
            fprintf(stderr,"Error[%d]:\n", rc);
            fprintf(stderr,"%s\n", ps_err_code_to_cause(rc));
            fprintf(stderr,"%s\n", ps_err_code_to_action(rc));
    
            exit (rc);
        }
    
        return 0;
    }

2. prosort_setup

ProSortの実行に必要な情報を初期化します。ただし、prosort_run_script関数を使用する場合は、prosort_setup関数を呼び出す必要がありません。

  • プロトタイプ

    typedef int (*callback_func_t)(char **data_buf_addr,
                                   int *data_buf_length);
    
    int prosort_setup (const char *prosort_script,
                       callback_func_t *data_fetch_callback_funcs,
                       int data_fetch_callback_func_cnt,
                       void **ptr_prosort_context);
  • パラメータ

    パラメータ 説明

    *prosort_script

    ProSort APIで使用するスクリプトです。

    *data_fetch_callback_funcs

    MERGEおよびCOPY演算を行う際には、別途にデータの入力を受けず、コールバック関数(callback function)を使って入力を受けます。

    特に、MERGE演算を行う際には、複数のコールバック関数の入力を受けることもあるので、コールバック関数の配列のアドレスをパラメータとして受けます。

    data_fetch_callback_func_cnt

    コールバック関数の数です。

    **ptr_prosort_context

    演算の実行に必要なコンテキストの情報を取得します。作成されたコンテキストは他のAPIに適用して演算を実行できます。

  • #include <stdio.h>
    #include <stdlib.h>
    #include "prosort.h"
    
    static void
    print_err_msg(int ec)
    {
        fprintf(stderr,"Error[%d]:\n", ec);
        fprintf(stderr,"%s\n", ps_err_code_to_cause(ec));
        fprintf(stderr,"%s\n", ps_err_code_to_action(ec));
        fprintf(stderr,"%s\n", ps_err_get_last_msg());
    }
    
    /* 16バイト・レコード */
    #define REC_LEN 16
    
    #define RAND (char) (rand() % 24) + 'A'
    int
    main(int argc, char **argv)
    {
        int   i      = 0;
        int   j      = 0;
        int   rc     = 0;
    
        void *ps_ctx;
    
        char buf[REC_LEN];
        const char *script = "MEMORY 100M\n"
                             "DATASIZE 100M\n"
                             "DEFREC FIXED, SIZE=16\n"
                             "SORT FIELDS = (1,8,CH,A)\n";
    
        /* スクリプトの設定 */
        rc = prosort_setup (script, NULL, 0, &ps_ctx);
    
        if (rc < 0) {
            print_err_msg (rc);
            exit (rc);
        }
    
        /* レコードの入力 */
        for (i = 0; i < 100; i++) {
    
            for (j = 0; j < REC_LEN - 1; j++)
                buf[j] = RAND;
            buf[j] = 0;
    
            rc = prosort_release_record (ps_ctx, buf, REC_LEN);
            if (rc < 0) {
                print_err_msg (rc);
                exit (rc);
            }
        }
        /* レコード入力の完了 */
        prosort_release_end (ps_ctx);
    
        do {
            char outbuf[REC_LEN];
            char *outbufs[1];
            unsigned int len = REC_LEN;
    
            outbufs[0] = outbuf;
    
            rc = prosort_return_record (ps_ctx, outbufs, &len);
            if (rc < 0) {
                print_err_msg (rc);
                exit (rc);
            }
            if (len == 0)
                break;
            printf ("%s\n", outbuf);
    
        } while (1);
    
        prosort_end (ps_ctx);
        return 0;
    }

3. prosort_setup_with_userexit

すべての入出力パラメータがprosort_setup関数と同じです。ただし、func_e15、func_e32、func_e35、 func_e61パラメータは例外です。

  • プロトタイプ

    typedef int (*user_exit_func_t)(const void *rec_addr,
                                    const int rec_len,
                                    int file_no,
                                    void **ret_rec_addr,
                                    int *ret_rec_len);
    
    int prosort_setup_with_userexit(const char *prosort_script,
                                    callback_func_t *data_fetch_callback_funcs,
                                    int data_fetch_callback_func_cnt,
                                    user_exit_func_t func_e15,
                                    user_exit_func_t func_e32,
                                    user_exit_func_t func_e35,
                                    user_exit_func_t func_e61,
                                    void **ptr_prosort_context);
  • パラメータ

    パラメータ 説明

    *prosort_script

    ProSort APIで使用するスクリプトです。

    *data_fetch_callback_funcs

    MERGEおよびCOPY演算を行う際には、別途にデータの入力を受けず、コールバック関数を使って入力を受けます。

    特に、MERGE演算を行う際には、複数のコールバック関数の入力を受けることもあるので、コールバック関数の配列のアドレスをパラメータとして受けます。

    data_fetch_callback_func_cnt

    コールバック関数の数です。

    func_e15

    ユーザー出口関数E15です。

    func_e32

    ユーザー出口関数E32です。

    func_e35

    ユーザー出口関数E35です。

    func_e61

    ユーザー出口関数E61です。

    **ptr_prosort_context

    演算の実行に必要なコンテキストの情報を取得します。作成されたコンテキストは他のAPIに適用して演算を実行することができます。

  • int func_e15(const void *rec_addr,
                 const int rec_len,
                 int file_no,
                 void **ret_rec_addr,
                 int *ret_rec_len)
    {
        ...
    }
    
    int main(int argc, char **argv)
    {
        int rc;
        void *ps_ctx;
        const char *script =
            "DEFREC FIXED,SIZE=150 \n"
            "DATASIZE 2M \n"
            "MEMORY 512M \n"
            "WORKSPACE = (./) \n"
            "SORT FIELDS=(1,2,A),FORMAT=BI \n"
            "INCLUDE COND=(1,2,CH,EQ,C'12',OR,1,2,CH,EQ,C'22',OR,1,2,CH,EQ,C'32') \n"
            "OUTFIL FNAMES=OUT1 \n";
    
        rc = prosort_setup_with_userexit(script, NULL, 0, func_e15, NULL,
                                         NULL, NULL, &ps_ctx);
        ...
    }

4. prosort_release_record

SORT演算を行う際に、データをレコードごとに入力する関数です。

  • プロトタイプ

    int prosort_release_record(void *prosort_context,
                             const char *record,
                             const unsigned int record_length);
  • パラメータ

    パラメータ 説明

    *prosort_context

    prosort_setup関数で作成したコンテキストを渡します。

    *record

    レコードのアドレスです。

    record_length

    レコードの長さです。

  • #include <stdio.h>
    #include <stdlib.h>
    #include "prosort.h"
    
    static void
    print_err_msg(int ec)
    {
        fprintf(stderr,"Error[%d]:\n", ec);
        fprintf(stderr,"%s\n", ps_err_code_to_cause(ec));
        fprintf(stderr,"%s\n", ps_err_code_to_action(ec));
        fprintf(stderr,"%s\n", ps_err_get_last_msg());
    }
    
    /* 16バイト・レコード */
    #define REC_LEN 16
    
    #define RAND (char) (rand() % 24) + 'A'
    int
    main(int argc, char **argv)
    {
        int   i      = 0;
        int   j      = 0;
        int   rc     = 0;
    
        void *ps_ctx;
    
        char buf[REC_LEN];
        const char *script = "MEMORY 100M\n"
                             "DATASIZE 100M\n"
                             "DEFREC FIXED, SIZE=16\n"
                             "SORT FIELDS = (1,8,CH,A)\n";
    
        /* スクリプトの設定 */
        rc = prosort_setup (script, NULL, 0, &ps_ctx);
    
        if (rc < 0) {
            print_err_msg (rc);
            exit (rc);
        }
    
        /* レコードの入力 */
        for (i = 0; i < 100; i++) {
    
            for (j = 0; j < REC_LEN - 1; j++)
                buf[j] = RAND;
            buf[j] = 0;
    
            rc = prosort_release_record (ps_ctx, buf, REC_LEN);
            if (rc < 0) {
                print_err_msg (rc);
                exit (rc);
            }
        }
        /* レコード入力の完了 */
        prosort_release_end (ps_ctx);
    
        do {
            char outbuf[REC_LEN];
            char *outbufs[1];
            unsigned int len = REC_LEN;
    
            outbufs[0] = outbuf;
    
            rc = prosort_return_record (ps_ctx, outbufs, &len);
            if (rc < 0) {
                print_err_msg (rc);
                exit (rc);
            }
            if (len == 0)
                break;
            printf ("%s\n", outbuf);
    
        } while (1);
    
        prosort_end (ps_ctx);
        return 0;
    }

5. prosort_release_end

SORT演算を行う際に、データの入力が完了したことを示す関数です。

  • プロトタイプ

    int prosort_release_end(void *prosort_context);
  • パラメータ

    パラメータ 説明

    *prosort_context

    prosort_setup関数で作成したコンテキストを渡します。

  • #include <stdio.h>
    #include <stdlib.h>
    #include "prosort.h"
    
    static void
    print_err_msg(int ec)
    {
        fprintf(stderr,"Error[%d]:\n", ec);
        fprintf(stderr,"%s\n", ps_err_code_to_cause(ec));
        fprintf(stderr,"%s\n", ps_err_code_to_action(ec));
        fprintf(stderr,"%s\n", ps_err_get_last_msg());
    }
    
    /* 16バイト・レコード */
    #define REC_LEN 16
    
    #define RAND (char) (rand() % 24) + 'A'
    int
    main(int argc, char **argv)
    {
        int   i      = 0;
        int   j      = 0;
        int   rc     = 0;
    
        void *ps_ctx;
    
        char buf[REC_LEN];
        const char *script = "MEMORY 100M\n"
                             "DATASIZE 100M\n"
                             "DEFREC FIXED, SIZE=16\n"
                             "SORT FIELDS = (1,8,CH,A)\n";
    
        /* スクリプトの設定 */
        rc = prosort_setup (script, NULL, 0, &ps_ctx);
    
        if (rc < 0) {
            print_err_msg (rc);
            exit (rc);
        }
    
        /* レコードの入力 */
        for (i = 0; i < 100; i++) {
    
            for (j = 0; j < REC_LEN - 1; j++)
                buf[j] = RAND;
            buf[j] = 0;
    
            rc = prosort_release_record (ps_ctx, buf, REC_LEN);
            if (rc < 0) {
                print_err_msg (rc);
                exit (rc);
            }
        }
        /* レコードの入力完了 */
        prosort_release_end (ps_ctx);
    
        do {
            char outbuf[REC_LEN];
            char *outbufs[1];
            unsigned int len = REC_LEN;
    
            outbufs[0] = outbuf;
    
            rc = prosort_return_record (ps_ctx, outbufs, &len);
            if (rc < 0) {
                print_err_msg (rc);
                exit (rc);
            }
            if (len == 0)
                break;
            printf ("%s\n", outbuf);
    
        } while (1);
    
        prosort_end (ps_ctx);
        return 0;
    }

6. prosort_return_record

演算の結果をレコード1件ごとに取得する関数です。出力は、OUTFILにより複数出力されることがあるので、バッファーの配列として取得します。0番目はSORTOUT(OUTFILを実行しない出力)が出力され、スクリプトに定義されたOUTFILが順序どおり出力されます。

  • プロトタイプ

    int prosort_return_record(void *prosort_context,
                            char **output_buffer,
                            unsigned int *output_buffer_length);
  • パラメータ

    パラメータ 説明

    *prosort_context

    prosort_setup関数で作成したコンテキストを渡します。

    **output_buffer

    出力レコードを取得するバッファー・アドレスの配列です。

    *output_buffer_length

    出力レコードのサイズを保存する配列です。

  • #include <stdio.h>
    #include <stdlib.h>
    #include "prosort.h"
    
    static void
    print_err_msg(int ec)
    {
        fprintf(stderr,"Error[%d]:\n", ec);
        fprintf(stderr,"%s\n", ps_err_code_to_cause(ec));
        fprintf(stderr,"%s\n", ps_err_code_to_action(ec));
        fprintf(stderr,"%s\n", ps_err_get_last_msg());
    }
    
    /* 16バイト・レコード */
    #define REC_LEN 16
    
    #define RAND (char) (rand() % 24) + 'A'
    int
    main(int argc, char **argv)
    {
        int   i      = 0;
        int   j      = 0;
        int   rc     = 0;
    
        void *ps_ctx;
    
        char buf[REC_LEN];
        const char *script = "MEMORY 100M\n"
                             "DATASIZE 100M\n"
                             "DEFREC FIXED, SIZE=16\n"
                             "SORT FIELDS = (1,8,CH,A)\n";
    
        /* スクリプトの設定 */
        rc = prosort_setup (script, NULL, 0, &ps_ctx);
    
        if (rc < 0) {
            print_err_msg (rc);
            exit (rc);
        }
    
        /* レコードの入力 */
        for (i = 0; i < 100; i++) {
    
            for (j = 0; j < REC_LEN - 1; j++)
                buf[j] = RAND;
            buf[j] = 0;
    
            rc = prosort_release_record (ps_ctx, buf, REC_LEN);
            if (rc < 0) {
                print_err_msg (rc);
                exit (rc);
            }
        }
        /* レコードの入力完了 */
        prosort_release_end (ps_ctx);
    
        do {
            char outbuf[REC_LEN];
            char *outbufs[1];
            unsigned int len = REC_LEN;
    
            outbufs[0] = outbuf;
    
            rc = prosort_return_record (ps_ctx, outbufs, &len);
            if (rc < 0) {
                print_err_msg (rc);
                exit (rc);
            }
            if (len == 0)
                break;
            printf ("%s\n", outbuf);
    
        } while (1);
    
        prosort_end (ps_ctx);
        return 0;
    }

7. prosort_end

すべての演算に使用したリソースを返す関数です。

  • プロトタイプ

    int prosort_end(void *prosort_context);
  • パラメータ

    パラメータ 説明

    *prosort_context

    prosort_setup関数で作成したコンテキストを渡します。

  • #include <stdio.h>
    #include <stdlib.h>
    #include "prosort.h"
    
    static void
    print_err_msg(int ec)
    {
        fprintf(stderr,"Error[%d]:\n", ec);
        fprintf(stderr,"%s\n", ps_err_code_to_cause(ec));
        fprintf(stderr,"%s\n", ps_err_code_to_action(ec));
        fprintf(stderr,"%s\n", ps_err_get_last_msg());
    }
    
    /* 16バイト・レコード */
    #define REC_LEN 16
    
    #define RAND (char) (rand() % 24) + 'A'
    int
    main(int argc, char **argv)
    {
        int   i      = 0;
        int   j      = 0;
        int   rc     = 0;
    
        void *ps_ctx;
    
        char buf[REC_LEN];
        const char *script = "MEMORY 100M\n"
                             "DATASIZE 100M\n"
                             "DEFREC FIXED, SIZE=16\n"
                             "SORT FIELDS = (1,8,CH,A)\n";
    
        /* スクリプトの設定 */
        rc = prosort_setup (script, NULL, 0, &ps_ctx);
    
        if (rc < 0) {
            print_err_msg (rc);
            exit (rc);
        }
    
        /* レコードの入力 */
        for (i = 0; i < 100; i++) {
    
            for (j = 0; j < REC_LEN - 1; j++)
                buf[j] = RAND;
            buf[j] = 0;
    
            rc = prosort_release_record (ps_ctx, buf, REC_LEN);
            if (rc < 0) {
                print_err_msg (rc);
                exit (rc);
            }
        }
        /* レコードの入力完了 */
        prosort_release_end (ps_ctx);
    
        do {
            char outbuf[REC_LEN];
            char *outbufs[1];
            unsigned int len = REC_LEN;
    
            outbufs[0] = outbuf;
    
            rc = prosort_return_record (ps_ctx, outbufs, &len);
            if (rc < 0) {
                print_err_msg (rc);
                exit (rc);
            }
            if (len == 0)
                break;
            printf ("%s\n", outbuf);
    
        } while (1);
    
        prosort_end (ps_ctx);
        return 0;
    }

8. prosort_get_error_number

エラーが発生した際に、エラー番号を取得する関数です。

  • プロトタイプ

    int prosort_get_error_number(void *prosort_context);
  • パラメータ

    パラメータ 説明

    *prosort_context

    prosort_setup関数で作成したコンテキストを渡します。

  • int main(int argc, char **argv)
    {
        int rc;
        void *ps_ctx;
        const char *script =
            "DEFREC FIXED,SIZE=150 \n"
            "DATASIZE 2M \n"
            "MEMORY 512M \n"
            "WORKSPACE = (./) \n"
            "SORT FIELDS=(1,2,A),FORMAT=BI \n"
            "INCLUDE COND=(1,2,CH,EQ,C'12',OR,1,2,CH,EQ,C'22',OR,1,2,CH,EQ,C'32') \n"
            "OUTFIL FNAMES=OUT1 \n";
    
        rc = prosort_setup(script, NULL, 0, &ps_ctx);
        if(rc < 0) {
            fprintf(stderr, "Error number is %d\n", prosort_get_error_number(ps_ctx));
        }
        ...
    }

9. prosort_get_error_message

エラーが発生した際に、エラー・メッセージを取得する関数です。

  • プロトタイプ

    int prosort_get_error_message(void *prosort_context,
                                char *error_message_buffer,
                                unsigned int *error_message_length);
  • パラメータ

    パラメータ 説明

    *prosort_context

    prosort_setup関数で作成したコンテキストを渡します。

    *error_message_buffer

    エラー・メッセージを取得するバッファー・アドレスです。

    *error_message_length

    エラー・メッセージの長さです。

  • #define ERR_MSG_MAXLEN 1024
    int main(int argc, char **argv)
    {
        int rc;
        void *ps_ctx;
        char *err_msg;
        unsigned int err_msg_len;
        const char *script =
            "DEFREC FIXED,SIZE=150 \n"
            "DATASIZE 2M \n"
            "MEMORY 512M \n"
            "WORKSPACE = (./) \n"
            "SORT FIELDS=(1,2,A),FORMAT=BI \n"
            "INCLUDE COND=(1,2,CH,EQ,C'12',OR,1,2,CH,EQ,C'22',OR,1,2,CH,EQ,C'32') \n"
            "OUTFIL FNAMES=OUT1 \n";
        err_msg = (char *)malloc(sizeof(char) * ERR_MSG_MAXLEN);
        err_msg_len = ERR_MSG_MAXLEN;
    
        rc = prosort_setup(script, NULL, 0, &ps_ctx);
        if(rc < 0) {
            prosort_get_error_message(ps_ctx, err_msg, &err_msg_len);
            fprintf(stderr, "Error message is %s\n", err_msg);
        }
        ...
    }