Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Cache Invalidate - delete cache records
0003  *
0004  * @file cmd_ci.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 #include <nstdutil.h>
0043 
0044 #include <ndrxdcmn.h>
0045 #include <atmi_int.h>
0046 #include <gencall.h>
0047 #include <utlist.h>
0048 #include <Exfields.h>
0049 
0050 #include "xa_cmn.h"
0051 #include "nclopt.h"
0052 #include <ndrx.h>
0053 #include <qcommon.h>
0054 #include <atmi_cache.h>
0055 #include <typed_buf.h>
0056 /*---------------------------Externs------------------------------------*/
0057 /*---------------------------Macros-------------------------------------*/
0058 /*---------------------------Enums--------------------------------------*/
0059 /*---------------------------Typedefs-----------------------------------*/
0060 /*---------------------------Globals------------------------------------*/
0061 /*---------------------------Statics------------------------------------*/
0062 /*---------------------------Prototypes---------------------------------*/
0063 
0064 /**
0065  * Call cache server
0066  * @return
0067  */
0068 exprivate int call_cache(char *dbname, char *key, int regexp)
0069 {
0070     UBFH *p_ub = (UBFH *)tpalloc("UBF", "", 1024);
0071     int ret=EXSUCCEED;
0072     long rcvlen;
0073     char *svcnm;
0074     char cmd = NDRX_CACHE_SVCMD_CLDEL;
0075     long flags = 0;
0076     long deleted;
0077     
0078     svcnm = ndrx_cache_mgt_getsvc();
0079             
0080     /* Setup the call buffer... */
0081     if (NULL==p_ub)
0082     {
0083         NDRX_LOG(log_error, "Failed to alloc FB!");        
0084         EXFAIL_OUT(ret);
0085     }
0086     
0087     if (EXSUCCEED!=Bchg(p_ub, EX_CACHE_CMD, 0, &cmd, 0L))
0088     {
0089         NDRX_LOG(log_error, "Failed to install command code: %s", 
0090                 Bstrerror(Berror));
0091         EXFAIL_OUT(ret);
0092     }
0093     
0094     if (EXSUCCEED!=Bchg(p_ub, EX_CACHE_DBNAME, 0, dbname, 0L))
0095     {
0096         NDRX_LOG(log_error, "Failed to install db name to buffer: %s",
0097                 Bstrerror(Berror));
0098         EXFAIL_OUT(ret);
0099     }
0100     
0101     
0102     if (EXEOS!=key[0])
0103     {
0104         if (EXSUCCEED!=Bchg(p_ub, EX_CACHE_OPEXPR, 0, key, 0L))
0105         {
0106             NDRX_LOG(log_error, "Failed to install key to buffer: %s",
0107                     Bstrerror(Berror));
0108             EXFAIL_OUT(ret);
0109         }
0110     }
0111     
0112     if (regexp)
0113     {
0114         flags|=NDRX_CACHE_SVCMDF_DELREG;
0115     }
0116     
0117     if (EXSUCCEED!=Bchg(p_ub, EX_CACHE_FLAGS, 0, (char *)&flags, 0L))
0118     {
0119         NDRX_LOG(log_error, "Failed to set EX_CACHE_FLAGS: %s",
0120                     Bstrerror(Berror));
0121         EXFAIL_OUT(ret);
0122     }
0123     
0124     /* Call cache server! */
0125     if (EXSUCCEED!=tpcall(svcnm, (char *)p_ub, 0L, (char **)&p_ub, &rcvlen, 0L))
0126     {
0127         NDRX_LOG(log_error, "Failed to call [%s]: %s", svcnm, tpstrerror(tperrno));
0128         fprintf(stderr, "Failed to call cache server [%s]: %s\n", 
0129                 svcnm, tpstrerror(tperrno));
0130         
0131         if (Bpres(p_ub, EX_TPSTRERROR, 0))
0132         {
0133             fprintf(stderr, "%s\n", Bfind(p_ub, EX_TPSTRERROR, 0, 0L));
0134         }
0135         
0136         EXFAIL_OUT(ret);
0137     }
0138     
0139     if (Bpres(p_ub, EX_CACHE_NRDEL, 0))
0140     {
0141         if (EXSUCCEED!=Bget(p_ub, EX_CACHE_NRDEL, 0, (char *)&deleted, 0L))
0142         {
0143             NDRX_LOG(log_error, "Failed to get EX_CACHE_NRDEL: %s", 
0144                     Bstrerror(Berror));
0145             fprintf(stderr, "WARNING: Failed to get delete results from EX_CACHE_NRDEL: %s\n", 
0146                     Bstrerror(Berror));
0147         }
0148         else
0149         {
0150             fprintf(stderr, "%ld records deleted\n", deleted);
0151         }
0152     }
0153     
0154 out:
0155 
0156     if (NULL!=p_ub)
0157     {
0158         tpfree((char *)p_ub);
0159     }
0160 
0161     return ret;
0162 }
0163 
0164 /**
0165  * Invalidate/Delete cache records
0166  * @param p_cmd_map
0167  * @param argc
0168  * @param argv
0169  * @return SUCCEED
0170  */
0171 expublic int cmd_ci(cmd_mapping_t *p_cmd_map, int argc, char **argv, int *p_have_next)
0172 {
0173     int ret=EXSUCCEED;
0174     char dbname[NDRX_CCTAG_MAX+1]={EXEOS};
0175     char key[PATH_MAX+1]={EXEOS};
0176     int regex = EXFALSE;
0177     ncloptmap_t clopt[] =
0178     {
0179         {'d', BFLD_STRING, (void *)dbname, sizeof(dbname), 
0180                                 NCLOPT_MAND|NCLOPT_HAVE_VALUE, "Database name"},
0181     {'k', BFLD_STRING, (void *)key, sizeof(key), 
0182                                 NCLOPT_OPT|NCLOPT_HAVE_VALUE, "Key string"},
0183                 
0184     /* should we interpret the result? For example UBF
0185      * Boolean flag
0186      */
0187     {'r', BFLD_SHORT, (void *)&regex, sizeof(regex), 
0188                 NCLOPT_OPT|NCLOPT_TRUEBOOL, "Use regexp?"},
0189         {0}
0190     };
0191         
0192     /* parse command line */
0193     if (nstd_parse_clopt(clopt, EXTRUE,  argc, argv, EXFALSE))
0194     {
0195         fprintf(stderr, XADMIN_INVALID_OPTIONS_MSG);
0196         EXFAIL_OUT(ret);
0197     }
0198     
0199     /* we need to init TP subsystem... */
0200     if (EXSUCCEED!=tpinit(NULL))
0201     {
0202         fprintf(stderr, "Failed to tpinit(): %s\n", tpstrerror(tperrno));
0203         EXFAIL_OUT(ret);
0204     }
0205     
0206     if (EXSUCCEED!=call_cache(dbname, key, regex))
0207     {
0208         NDRX_LOG(log_debug, "Failed to call cache server for db [%s]", dbname);
0209         fprintf(stderr, "Failed to call cache server!\n");
0210         EXFAIL_OUT(ret);
0211     }
0212 
0213 out:
0214     return ret;
0215 }
0216 
0217 /* vim: set ts=4 sw=4 et smartindent: */