Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief UBF library
0003  *   Get functions lives here
0004  *
0005  * @file get_impl.c
0006  */
0007 /* -----------------------------------------------------------------------------
0008  * Enduro/X Middleware Platform for Distributed Transaction Processing
0009  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0010  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0011  * This software is released under one of the following licenses:
0012  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0013  * See LICENSE file for full text.
0014  * -----------------------------------------------------------------------------
0015  * AGPL license:
0016  *
0017  * This program is free software; you can redistribute it and/or modify it under
0018  * the terms of the GNU Affero General Public License, version 3 as published
0019  * by the Free Software Foundation;
0020  *
0021  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0022  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0023  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0024  * for more details.
0025  *
0026  * You should have received a copy of the GNU Affero General Public License along 
0027  * with this program; if not, write to the Free Software Foundation, Inc.,
0028  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0029  *
0030  * -----------------------------------------------------------------------------
0031  * A commercial use license is available from Mavimax, Ltd
0032  * contact@mavimax.com
0033  * -----------------------------------------------------------------------------
0034  */
0035 
0036 #include <stdio.h>
0037 #include <stdlib.h>
0038 
0039 /*---------------------------Includes-----------------------------------*/
0040 #include <string.h>
0041 #include <stdio.h>
0042 #include <stdlib.h>
0043 #include <memory.h>
0044 
0045 #include <ubf.h>
0046 #include <ubf_int.h>    /* Internal headers for UBF... */
0047 #include <fdatatype.h>
0048 #include <ferror.h>
0049 #include <fieldtable.h>
0050 #include <ndrstandard.h>
0051 #include <ndebug.h>
0052 #include <cf.h>
0053 /*---------------------------Externs------------------------------------*/
0054 /*---------------------------Macros-------------------------------------*/
0055 /*---------------------------Enums--------------------------------------*/
0056 /*---------------------------Typedefs-----------------------------------*/
0057 /*---------------------------Globals------------------------------------*/
0058 /*---------------------------Statics------------------------------------*/
0059 /*---------------------------Prototypes---------------------------------*/
0060 /**
0061  * Internal version of Bgetalloc
0062  * uses Bfind in core.
0063  * TODO: How about FLD_PTR, FLD_UBF, FLD_VIEW?
0064  * ndrx_ubf_get_cbuf() won't work for these.
0065  * @param p_ub
0066  * @param bfldid
0067  * @param occ
0068  * @param extralen - extra size to allocate/data len returned
0069  * @return
0070  */
0071 expublic char * ndrx_Bgetalloc (UBFH * p_ub, BFLDID bfldid,
0072                             BFLDOCC occ, BFLDLEN *extralen)
0073 {
0074     int data_type = (bfldid>>EFFECTIVE_BITS);
0075     BFLDLEN tmp_len = 0;
0076     char *fb_data;
0077     char *alloc_buf=NULL;
0078     int alloc_size = 0;
0079     char fn[]="_Bgetalloc";
0080     char *ret=NULL;
0081     char *p_fld=NULL;
0082     dtype_str_t *dtype_str = &G_dtype_str_map[data_type];
0083 /***************************************** DEBUG *******************************/
0084     #ifdef UBF_API_DEBUG
0085     dtype_ext1_t *__dbg_dtype_ext1 = &G_dtype_ext1_map[data_type];
0086     #endif
0087 /*******************************************************************************/
0088 
0089     fb_data=ndrx_Bfind (p_ub, bfldid, occ, &tmp_len, &p_fld);
0090 
0091     if (NULL!=fb_data)
0092     {
0093         /* Allocate the buffer */
0094         if (NULL==(ret=ndrx_ubf_get_cbuf(data_type,data_type,
0095                         NULL, fb_data, tmp_len,
0096                         &alloc_buf,
0097                         &alloc_size,
0098                         CB_MODE_ALLOC,
0099                         extralen!=NULL?(*extralen):0)))
0100         {
0101             UBF_LOG(log_error, "%s: get_cbuf failed!", fn);
0102         }
0103         else
0104         {
0105             /* put data into allocated buffer */
0106             if (EXSUCCEED!=dtype_str->p_get_data(dtype_str, p_fld, ret, &tmp_len))
0107             {
0108                 NDRX_FREE(ret); /* free up allocated memory */
0109                 ret=NULL;
0110             }
0111             else
0112             {
0113                 /* return the len if needed. */
0114                 if (NULL!=extralen)
0115                     *extralen = tmp_len;
0116             }
0117         }
0118     }
0119     else
0120     {
0121         ret=NULL;
0122     }
0123 /***************************************** DEBUG *******************************/
0124     /* Dump here happens two times?
0125      * One time data is dumpet at end of _Bfind
0126      * second time it is dumped here. But OK it this way we see that data is
0127      * successfully pushed into return buffer.
0128      */
0129     #ifdef UBF_API_DEBUG
0130     if (NULL!=ret)
0131     {
0132         __dbg_dtype_ext1->p_dump_data(__dbg_dtype_ext1, "_Bgetalloc got data", ret, &tmp_len);
0133     }
0134     #endif
0135 /*******************************************************************************/
0136 
0137     return ret;
0138 }
0139 
0140 /**
0141  * Read the data from buffer and loaded it into output buff
0142  * @param p_ub
0143  * @param bfldid
0144  * @param occ
0145  * @param buf
0146  * @param buflen
0147  * @return
0148  */
0149 expublic int ndrx_Bget (UBFH * p_ub, BFLDID bfldid, BFLDOCC occ,
0150                             char * buf, BFLDLEN * len)
0151 {
0152     int ret=EXSUCCEED;
0153     dtype_str_t *dtype;
0154     char *p;
0155     char *last_checked=NULL;
0156     int last_occ=-1;
0157     char fnname[] = "_Bget";
0158     
0159 /***************************************** DEBUG *******************************/
0160     #ifdef UBF_API_DEBUG
0161     dtype_ext1_t *__dbg_dtype_ext1;
0162     #endif
0163 /*******************************************************************************/
0164 
0165     UBF_LOG(log_debug, "%s: bfldid: %x, occ: %d", fnname, bfldid, occ);
0166 
0167     if (UBF_BINARY_SEARCH_OK(bfldid))
0168     {
0169         p=get_fld_loc_binary_search(p_ub, bfldid, occ, &dtype, 
0170                 UBF_BINSRCH_GET_LAST_NONE, NULL, NULL, NULL);
0171     }
0172     else
0173     {
0174         p=get_fld_loc(p_ub, bfldid, occ, &dtype, &last_checked, 
0175                 NULL, &last_occ, NULL);
0176     }
0177     
0178     /* So started lookup for strings & carrays  */
0179     if (NULL!=p)
0180     {
0181         if (NULL!=buf)
0182         {
0183             ret=dtype->p_get_data(dtype, (char *)p, buf, len);
0184         }
0185         else
0186         {
0187             UBF_LOG(log_debug, "%s: buf=NULL, not returning data!", fnname);
0188         }
0189     }
0190     else
0191     {
0192         ndrx_Bset_error(BNOTPRES);
0193         ret=EXFAIL;
0194     }
0195 /***************************************** DEBUG *******************************/
0196     #ifdef UBF_API_DEBUG
0197     if (EXSUCCEED==ret)
0198     {
0199         __dbg_dtype_ext1 = &G_dtype_ext1_map[bfldid>>EFFECTIVE_BITS];
0200         __dbg_dtype_ext1->p_dump_data(__dbg_dtype_ext1, "_Bget got data", buf, len);
0201     }
0202     #endif
0203 /*******************************************************************************/
0204     UBF_LOG(log_debug, "%s: ret: %d", fnname, ret);
0205 
0206     return ret;
0207 }
0208 
0209 /**
0210  * Read the data from buffer and loaded it into output buff
0211  * @param p_ub
0212  * @param bfldid
0213  * @param occ
0214  * @param buf
0215  * @param buflen
0216  * @return
0217  */
0218 expublic int ndrx_Bgetlast (UBFH *p_ub, BFLDID bfldid,
0219                         BFLDOCC *occ, char *buf, BFLDLEN *len)
0220 {
0221     int ret=EXSUCCEED;
0222     dtype_str_t *dtype;
0223     char *p;
0224     char *last_checked = NULL;
0225     char *last_match = NULL;
0226     int last_occ=-1;
0227     char fnname[] = "_Bgetlast";
0228 /***************************************** DEBUG *******************************/
0229     #ifdef UBF_API_DEBUG
0230     dtype_ext1_t *__dbg_dtype_ext1;
0231     #endif
0232 /*******************************************************************************/
0233 
0234     UBF_LOG(log_debug, "%s: bfldid: %x", fnname, bfldid);
0235     if (UBF_BINARY_SEARCH_OK(bfldid))
0236     {
0237         get_fld_loc_binary_search(p_ub, bfldid, EXFAIL, &dtype, 
0238                     UBF_BINSRCH_GET_LAST, &last_occ, NULL, &last_match);
0239     }
0240     else
0241     {
0242         get_fld_loc(p_ub, bfldid, -2, &dtype, &last_checked, &last_match, &last_occ, NULL);
0243     }
0244 
0245     if (EXFAIL!=last_occ && !ndrx_Bis_error()) /* Exclude cases when error have been raised! */
0246     {
0247         /* Have to get data type again - because in last mode it is not avaialble  */
0248         dtype = &G_dtype_str_map[bfldid>>EFFECTIVE_BITS];
0249         if (NULL!=buf)
0250         {
0251             ret=dtype->p_get_data(dtype, (char *)last_match, buf, len);
0252         }
0253         else
0254         {
0255             UBF_LOG(log_debug, "%s: buf=NULL, not returning data!", fnname);
0256         }
0257 
0258         if (NULL!=occ)
0259         {
0260             *occ = last_occ;
0261             UBF_LOG(log_debug, "%s: Got occ %d!", fnname, *occ);
0262         }
0263         else
0264         {
0265             UBF_LOG(log_debug, "%s: occ=NULL, not returning occ!", fnname);
0266         }
0267     }
0268     else
0269     {
0270         ndrx_Bset_error(BNOTPRES); /* IF error have been set this will not override! */
0271         ret=EXFAIL;
0272     }
0273 /***************************************** DEBUG *******************************/
0274     #ifdef UBF_API_DEBUG
0275     if (EXSUCCEED==ret && NULL!=buf)
0276     {
0277         __dbg_dtype_ext1 = &G_dtype_ext1_map[bfldid>>EFFECTIVE_BITS];
0278         __dbg_dtype_ext1->p_dump_data(__dbg_dtype_ext1, "_Bget got data", buf, len);
0279     }
0280     #endif
0281 /*******************************************************************************/
0282     UBF_LOG(log_debug, "%s: ret: %d", fnname, ret);
0283 
0284     return ret;
0285 }
0286 
0287 /* vim: set ts=4 sw=4 et smartindent: */