Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Print UBF Database
0003  *
0004  * @file cmd_pubfdb.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <memory.h>
0038 #include <sys/param.h>
0039 
0040 #include <ndrstandard.h>
0041 #include <ndebug.h>
0042 
0043 #include <ndrx.h>
0044 #include <ndrxdcmn.h>
0045 #include <atmi_int.h>
0046 #include <gencall.h>
0047 #include <nclopt.h>
0048 #include <ubf.h>
0049 #include <exdb.h>
0050 #include <fdatatype.h>
0051 #include <cconfig.h>
0052 /*---------------------------Externs------------------------------------*/
0053 /*---------------------------Macros-------------------------------------*/
0054 /*---------------------------Enums--------------------------------------*/
0055 /*---------------------------Typedefs-----------------------------------*/
0056 /*---------------------------Globals------------------------------------*/
0057 /*---------------------------Statics------------------------------------*/
0058 /*---------------------------Prototypes---------------------------------*/
0059 
0060 /**
0061  * Print UBF database
0062  * @param p_cmd_map
0063  * @param argc
0064  * @param argv
0065  * @return SUCCEED
0066  */
0067 expublic int cmd_pubfdb(cmd_mapping_t *p_cmd_map, int argc, char **argv, int *p_have_next)
0068 {
0069     int ret=EXSUCCEED;
0070     EDB_env * dbenv;
0071     EDB_dbi *dbi_id, *dbi_nm;
0072     EDB_txn *txn = NULL;
0073     EDB_cursor * cursor;
0074     int tran_started = EXFALSE;
0075     EDB_cursor_op op;
0076     EDB_val keydb, data;
0077     int cursor_open = EXFALSE;
0078     BFLDID bfldid;
0079     BFLDID bfldno;
0080     short fldtype;
0081     char fldname[UBFFLDMAX+1];
0082     int first = EXTRUE;
0083     
0084     /* Load UBF fields (if no already loaded...) */
0085     if (EXFAIL==(ret=Bflddbload()))
0086     {
0087         fprintf(stderr, NDRX_XADMIN_ERR_FMT_PFX "Failed to load UBF field database: %s\n", 
0088                 Bstrerror(Berror));
0089         EXFAIL_OUT(ret);
0090     }
0091     
0092     if (EXTRUE!=ret)
0093     {
0094         fprintf(stderr, "WARNING ! No configuration defined for UBF DB!\n");
0095         goto out;
0096     }
0097     
0098     /* reset back to succeed */
0099     ret = EXSUCCEED;
0100     
0101     /* get DB Env */
0102     if (NULL==(dbenv=Bfldddbgetenv(&dbi_id, &dbi_nm)))
0103     {
0104         fprintf(stderr, NDRX_XADMIN_ERR_FMT_PFX "Failed to load UBF DB env handler: %s\n", 
0105                 Bstrerror(Berror));
0106         EXFAIL_OUT(ret);
0107     }
0108     
0109     /* begin transaction */
0110     if (EXSUCCEED!=(ret=edb_txn_begin(dbenv, NULL, EDB_RDONLY, &txn)))
0111     {
0112         fprintf(stderr, NDRX_XADMIN_ERR_FMT_PFX "Failed to start LMDB transaction: %s\n", 
0113                 edb_strerror(ret));
0114         EXFAIL_OUT(ret);
0115     }
0116     
0117     tran_started = EXTRUE;
0118     
0119     /* loop over the db... */
0120     
0121     if (EXSUCCEED!=(ret=edb_cursor_open(txn, *dbi_nm, &cursor)))
0122     {
0123         fprintf(stderr, NDRX_XADMIN_ERR_FMT_PFX "Failed to open cursor: %s\n",
0124             edb_strerror(ret));
0125         EXFAIL_OUT(ret);
0126     }
0127     cursor_open = EXTRUE;
0128 
0129     op = EDB_FIRST;
0130     do
0131     {
0132         if (EXSUCCEED!=(ret=edb_cursor_get(cursor, &keydb, &data, op)))
0133         {
0134             if (ret!=EDB_NOTFOUND)
0135             {
0136                 fprintf(stderr, NDRX_XADMIN_ERR_FMT_PFX "cursor get failed: %s\n",
0137                     edb_strerror(ret));
0138             }
0139             goto out;
0140         }
0141         
0142         if (EXSUCCEED!=Bflddbget(&data, &fldtype, &bfldno, &bfldid, 
0143                 fldname, sizeof(fldname)))
0144         {
0145             fprintf(stderr, NDRX_XADMIN_ERR_FMT_PFX "failed to decode db data: %s\n", 
0146                     Bstrerror(Berror));
0147             EXFAIL_OUT(ret);
0148         }
0149         
0150         /* print the data on screen */
0151         if (first)
0152         {
0153             fprintf(stderr, "NO         ID         TYPE       Field Name\n");
0154             fprintf(stderr, "---------- ---------- ---------- --------------------\n");
0155             first = EXFALSE;
0156         }
0157         printf("%-10d %-10d %-10.10s %s\n",
0158            bfldno, bfldid, G_dtype_str_map[fldtype].fldname, fldname);
0159         
0160         if (EDB_FIRST == op)
0161         {
0162             op = EDB_NEXT;
0163         }
0164 
0165     } while (EXSUCCEED==ret);
0166     
0167 out:
0168     
0169     if (cursor_open)
0170     {
0171         edb_cursor_close(cursor);
0172     }
0173 
0174     if (tran_started)
0175     {
0176         edb_txn_abort(txn);
0177     }
0178 
0179     Bflddbunload();
0180 
0181     return ret;
0182 }
0183 /* vim: set ts=4 sw=4 et smartindent: */