Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / test / xa / src2 / bdb1.c
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2011, 2012 Oracle and/or its affiliates.  All rights reserved.
5  */
6
7 /*
8  * This server is called by the client.  It inserts a value into db1 then
9  * calls bdb2 to insert a value into db2.
10  */
11
12 #include <db.h>
13 #include <xa.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <ctype.h>
17 #include <atmi.h>       /* TUXEDO Header File */
18 #include <userlog.h>    /* TUXEDO Header File */
19 #include <string.h>
20 #include <time.h>
21 #include <tpadm.h>
22 #include <unistd.h>
23 #include "../utilities/bdb_xa_util.h"
24
25 #define NUMDB 1
26
27 static int times = 1;
28 static long seq = 1;
29
30 static char *progname;
31
32 /* Open the database when the server is started. */
33 int
34 tpsvrinit(argc, argv)
35 int argc;
36 char **argv;
37 {
38         char ch;
39
40         progname = argv[0];
41
42         /* Some compilers warn if argc and argv aren't used. */
43         while ((ch = getopt(argc, argv, "t:")) != EOF)
44                 switch (ch) {
45                 case 't':
46                         times = atoi(optarg);
47                         break;
48                 }
49         
50
51         return (init_xa_server(NUMDB, progname, 0));
52 }
53
54 /* Close the database when the server is shutdown. */
55 void
56 tpsvrdone(void)
57 {
58         close_xa_server(NUMDB, progname);
59 }
60
61 /* Insert a value into db1, then call bdb2 to insert that value into db2. */
62 int
63 WRITE(rqst)
64 TPSVCINFO *rqst;
65 {
66         long rcvlen;
67         int ret, i;
68         DBT key, data;
69         size_t len;
70         int ch;
71         char *p, *t, buf[1024];
72         DB *dbp = dbs[0];
73
74         tpbegin(10,0);
75
76         for(i=0; i<times; i++){
77                 /* Insert random records into the db1*/
78                 memset(&key, 0, sizeof(DBT));
79                 memset(&data, 0, sizeof(DBT));
80                 sprintf(buf, "%ld %ld",random(), seq++);
81
82                 len = strlen(buf);
83                 key.data = buf;
84                 data.data = buf;
85                 data.size = key.size = (u_int32_t)len ;
86         
87                 switch (ret = dbp->put(dbp, NULL, &key, &data, 
88                     DB_NOOVERWRITE)) {
89                 case 0:
90                         break;
91                 case DB_LOCK_DEADLOCK:
92                         if(tpabort(0) == -1){
93                                 userlog("tpabort() fail:%s", 
94                                     tpstrerror(tperrno));
95                         }
96                         tpreturn(TPSUCCESS, 1, rqst->data, 0L, 0);
97                 default:
98                         userlog("put: %s", db_strerror(ret));
99                         if(tpabort(0) == -1){
100                                 userlog("tpabort() fail:%s", 
101                                     tpstrerror(tperrno));
102                         }
103                         tpreturn(TPFAIL, 0, rqst->data, 0L, 0);
104                 }
105         }
106
107         /* 
108          * Insert the key into a queue and call bdb2 where it will read
109          * the key out of the queue.
110          */
111         FBFR32 *reqbuf;
112         char * rcvbuf;
113
114         if((reqbuf = (FBFR32*) tpalloc("FML32", NULL, 100)) == NULL) {
115            tpabort(0);
116            userlog("alloc fail");
117            tpreturn(TPFAIL, 0, rqst->data, 0L, 0);
118         }
119
120         if((rcvbuf = (char *) tpalloc("STRING", NULL, 100)) == NULL) {
121            tpabort(0);
122            userlog("alloc fail");
123            tpreturn(TPFAIL, 0, rqst->data, 0L, 0);
124         }
125
126         Fadd32(reqbuf, TA_REPOSPARAM, buf, 0);
127
128         /* Call bdb2 */
129         ret = tpcall("WRITE2", (char *)reqbuf, 0, &rcvbuf, &rcvlen,TPSIGRSTRT);
130         tpfree((char*)reqbuf);
131         tpfree((char*)rcvbuf);
132         if(ret == -1){
133            userlog("call WRITE2 fail");
134            if(-1 == tpabort(0))
135                 userlog("tpabort() fail:%s", tpstrerror(tperrno));
136            tpreturn(TPFAIL, 0, rqst->data, 0L, 0);
137         }
138         /* Commit for a return value of 0, otherwise abort. */
139         if (tpurcode == 0) {
140                 if(tpcommit(0) == -1){
141                         userlog("tpcommit fail");
142                         tpreturn(TPFAIL, 0, rqst->data, 0L, 0);
143                 }
144         } else {
145                 if(tpabort(0) == -1){
146                         userlog("tpabort fail");
147                         tpreturn(TPFAIL, 0, rqst->data, 0L, 0);
148                 }
149         }
150
151         tpreturn(TPSUCCESS, 0, rqst->data, 0L, 0);
152 }
153
154 /* Iterates the database with a cursor. */
155 int
156 CURSOR(rqst)
157 TPSVCINFO *rqst;
158 {
159         int ret,count;
160         DBT key, value;
161         DBC *cursorp;
162         DB *dbp = dbs[0];
163         
164         tpbegin(60*10,0);
165
166         /* Get the cursor */
167         ret = dbp->cursor(dbp, NULL, &cursorp, 0);
168         if (ret != 0) {
169                 userlog("count_records: cursor open failed.");
170                 tpreturn(TPFAIL, 0, rqst->data, 0L, 0);
171         }
172
173         /* Get the key DBT used for the database read */
174         memset(&key, 0, sizeof(DBT));
175         memset(&value, 0, sizeof(DBT));
176         do {
177             ret = cursorp->c_get(cursorp, &key, &value, DB_NEXT);
178             switch (ret) {
179                     case 0:
180                             count++;
181                             break;
182                     case DB_NOTFOUND:
183                             break;
184                     case DB_LOCK_DEADLOCK:
185                             if(tpcommit(0) == -1)
186                                     userlog("tpcommit() fail:%s", 
187                                         tpstrerror(tperrno));
188                             cursorp->c_close(cursorp);
189                             tpreturn(TPSUCCESS, 1L, rqst->data, 0L, 0);
190                             break;
191                     default:
192                             if(tpabort(0) == -1)
193                                     userlog("tpabort() fail:%s", 
194                                         tpstrerror(tperrno));
195                             cursorp->c_close(cursorp);
196                             dbp->err(dbp, ret, 
197                                 "Count records unspecified error");
198                             tpreturn(TPFAIL, 0, rqst->data, 0L, 0);
199             }
200         } while (ret == 0);
201         cursorp->c_close(cursorp);
202         tpcommit(0);
203         tpreturn(TPSUCCESS, 0, rqst->data, 0L, 0);
204     }