~ubuntu-branches/ubuntu/lucid/ncbi-tools6/lucid

« back to all changes in this revision

Viewing changes to biostruc/loader.c

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2002-04-04 22:13:09 UTC
  • Revision ID: james.westby@ubuntu.com-20020404221309-vfze028rfnlrldct
Tags: upstream-6.1.20011220a
ImportĀ upstreamĀ versionĀ 6.1.20011220a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*   $Id: loader.c,v 6.14 1999/05/25 16:21:20 kimelman Exp $
 
2
 * ===========================================================================
 
3
 *
 
4
 *                            PUBLIC DOMAIN NOTICE
 
5
 *            National Center for Biotechnology Information (NCBI)
 
6
 *
 
7
 *  This software/database is a "United States Government Work" under the
 
8
 *  terms of the United States Copyright Act.  It was written as part of
 
9
 *  the author's official duties as a United States Government employee and
 
10
 *  thus cannot be copyrighted.  This software/database is freely available
 
11
 *  to the public for use. The National Library of Medicine and the U.S.
 
12
 *  Government do not place any restriction on its use or reproduction.
 
13
 *  We would, however, appreciate having the NCBI and the author cited in
 
14
 *  any work or product based on this material
 
15
 *
 
16
 *  Although all reasonable efforts have been taken to ensure the accuracy
 
17
 *  and reliability of the software and data, the NLM and the U.S.
 
18
 *  Government do not and cannot warrant the performance or results that
 
19
 *  may be obtained by using this software or data. The NLM and the U.S.
 
20
 *  Government disclaim all warranties, express or implied, including
 
21
 *  warranties of performance, merchantability or fitness for any particular
 
22
 *  purpose.
 
23
 *
 
24
 * ===========================================================================
 
25
 *
 
26
 * Author:  Michael Kimelman
 
27
 *
 
28
 * PubStruct DB Loader.
 
29
 *                   
 
30
 * Modifications:  
 
31
 * --------------------------------------------------------------------------
 
32
 * $Log: loader.c,v $
 
33
 * Revision 6.14  1999/05/25 16:21:20  kimelman
 
34
 * bugfix : --checks & --load / didn't generate error message
 
35
 *
 
36
 * Revision 6.13  1998/11/20 22:46:00  kimelman
 
37
 * fix loader return code
 
38
 *
 
39
 * Revision 6.12  1998/11/06 18:59:07  kimelman
 
40
 * PubStruct loading transaction granularity changed
 
41
 *
 
42
 * Revision 6.11  1998/10/22 17:01:20  kimelman
 
43
 * fix default dump directory
 
44
 *
 
45
 * Revision 6.10  1998/10/22  15:24:28  kimelman
 
46
 * fixed path to check_loader & default dump area
 
47
 *
 
48
 * Revision 6.9  1998/10/05  17:50:20  kimelman
 
49
 * processing fixed for "file not found" on load case
 
50
 *
 
51
 * Revision 6.8  1998/09/03 22:05:26  kimelman
 
52
 * added cycles to retry on failes and
 
53
 * wait for unhappy server to recover
 
54
 *
 
55
 * Revision 6.7  1998/08/05 21:12:35  kimelman
 
56
 * skip/load bugfix
 
57
 *
 
58
 * Revision 6.6  1998/07/22 22:08:28  kimelman
 
59
 * bugfix : check&create  (./mmdb ==> $outdir)
 
60
 *
 
61
 * Revision 6.5  1998/07/16 20:00:33  kimelman
 
62
 * pack & enforce options added
 
63
 *
 
64
 * Revision 6.4  1998/07/14 20:24:51  kimelman
 
65
 * FT schema & smart load
 
66
 *
 
67
 * Revision 6.3  1998/05/14 16:11:17  kimelman
 
68
 * Compression stuff added in debug mode.
 
69
 * few bugs fixed, related to OpenServer/SQL Server switching
 
70
 *
 
71
 * Revision 6.2  1998/05/08 03:03:47  kimelman
 
72
 * Open Server fix
 
73
 *
 
74
 * Revision 6.1  1998/04/03 20:25:21  kimelman
 
75
 * PubStruct access code added to mmdbsrv
 
76
 *
 
77
 *
 
78
 * ==========================================================================
 
79
 */
 
80
 
 
81
 
 
82
#include "PubStructAsn.h"
 
83
#include <assert.h>
 
84
 
 
85
#define LOAD_IN_STATE 1
 
86
 
 
87
static char  MMDB[PATH_MAX];
 
88
static char  outdir[PATH_MAX];
 
89
static char  bindir[PATH_MAX];
 
90
static char *server=NULL;
 
91
static int   state = -1;
 
92
static int   enforce = 0;
 
93
 
 
94
int
 
95
remove_struct(int uid)
 
96
{
 
97
  char path[1024];
 
98
  int acc;
 
99
  
 
100
  acc = PubStruct_lookup(server,uid,state);
 
101
  if(acc)
 
102
    acc = PubStruct_removeasn(server,acc);
 
103
  return acc;
 
104
}
 
105
 
 
106
int
 
107
load(int uid)
 
108
{
 
109
  char path[1024];
 
110
  char fname[1024];
 
111
  AsnIoPtr aip;
 
112
  FILE *p;
 
113
  int acc = 0;
 
114
  int pipe = 1;
 
115
  
 
116
  sprintf(fname, "%s%ld.val.gz",MMDB,uid);
 
117
  p = fopen(fname,"r");
 
118
  if(p)
 
119
    {
 
120
      fclose(p);
 
121
      sprintf(path, "gzip -dc %s%ld.val.gz",MMDB,uid);
 
122
      /* sprintf(path, "gzip -dc %ld.val.gz",uid); */
 
123
      p = popen(path,"rb");
 
124
    }
 
125
  if(!p)
 
126
    {
 
127
      sprintf(fname, "%s%ld.val",MMDB,uid);
 
128
      p = fopen(fname,"r");
 
129
      pipe = 0;
 
130
    }
 
131
  if(p)
 
132
    {
 
133
      acc = PubStruct_load(p,(enforce?-state-1:state),server);
 
134
      if(pipe)
 
135
        pclose(p);
 
136
      else
 
137
        fclose(p);
 
138
      return acc;
 
139
    }
 
140
#if 1
 
141
  fprintf(stderr, "Can't open file: %s%d.val(.gz) ",MMDB,uid);
 
142
  return -2;
 
143
#else
 
144
  sprintf(fname, "%s%ld.txt",MMDB,uid);
 
145
  p = fopen(fname,"r");
 
146
  if(!p)
 
147
    return 0;
 
148
 
 
149
  fclose(p);
 
150
  
 
151
  {
 
152
    BiostrucPtr bsp = NULL;
 
153
    Boolean     commit;
 
154
          
 
155
    aip = AsnIoOpen(fname,"r");
 
156
    bsp=BiostrucAsnRead(aip,NULL);
 
157
    AsnIoClose(aip);
 
158
          
 
159
    aip = PubStruct_newasn(server,state,&acc);
 
160
    commit = BiostrucAsnWrite(bsp,aip,NULL);
 
161
    commit = PubStruct_closeasn(aip,commit);
 
162
    
 
163
    if (!commit)  acc = 0;
 
164
  }
 
165
  return acc;
 
166
#endif
 
167
}
 
168
 
 
169
int
 
170
download(int uid, int pack)
 
171
{
 
172
  char path[1024];
 
173
  FILE *p;
 
174
  int acc;
 
175
 
 
176
  if(!pack)
 
177
    {
 
178
      sprintf(path,"%s/%ld.val",outdir,uid);
 
179
      p = fopen(path,"wb");
 
180
    }
 
181
  else
 
182
    {
 
183
      sprintf(path, "gzip -c >%s/%ld.val.gz",outdir,uid);
 
184
      p = popen(path,"wb");
 
185
    }
 
186
  
 
187
  if(!p)
 
188
    return 0;
 
189
  if (state==0)
 
190
    acc = PubStruct_download(server,0,uid,p); /* can go throuth the open server */
 
191
  else
 
192
    {
 
193
      acc = PubStruct_lookup(server,uid,state);
 
194
      if(acc)
 
195
        acc = PubStruct_download(server,acc,uid,p);
 
196
    }
 
197
  if(pack)
 
198
    pclose(p);
 
199
  else
 
200
    fclose(p);
 
201
  return acc;
 
202
}
 
203
 
 
204
/* QA */
 
205
void
 
206
check(int uid)
 
207
{
 
208
  char cmd[1024];
 
209
  
 
210
  sprintf(cmd, "sh %s/loader_check.sh %s %ld.val.gz %s", bindir, MMDB, uid, outdir);
 
211
  system(cmd);
 
212
}
 
213
 
 
214
 
 
215
#ifdef PURIFY
 
216
static void
 
217
memory_leaks(void)
 
218
{
 
219
  CTLibUtils       clu;
 
220
  
 
221
  if(!CTLibInit(&clu,"MOZART_SYS10","PubStruct","kimelman","kmlmNu",0,NULL))
 
222
    return;
 
223
  CTLibDrop(&clu);
 
224
  purify_new_inuse();
 
225
  purify_new_leaks();
 
226
}
 
227
#endif
 
228
 
 
229
 
 
230
int
 
231
main(int argc, char**argv)
 
232
{
 
233
  int mode = 0;
 
234
  int pack = 0;
 
235
  int failed = 0; 
 
236
  int uid;
 
237
  unsigned long flags;
 
238
  setbuf(stdout,NULL);
 
239
  strcpy(MMDB,"/net/vaster/mmdb/mmdb/data/");
 
240
  strcpy(outdir,"/tmp");
 
241
  {
 
242
    FILE *f = fopen(argv[0],"r");
 
243
    char *p; 
 
244
    if (f) /* great! - we have a direct path to binary program */
 
245
      strcpy(bindir,argv[0]);
 
246
    else
 
247
      { /* we should find the binary in the path */
 
248
        char buf[1000];
 
249
        sprintf(buf,"which %s", argv[0]);
 
250
        f = popen(buf,"r");
 
251
        assert(f);
 
252
        fscanf(f,"%s",bindir);
 
253
        pclose(f);
 
254
        f = fopen(bindir,"r");
 
255
      }
 
256
    if (!f)
 
257
      {
 
258
        fprintf(stderr,"Can't determine the path to binary - tried :\n%s\n%s\n",argv[0],bindir);
 
259
        exit(1);
 
260
      }
 
261
    assert(f);
 
262
    fclose(f);
 
263
    /* strip file name from path to get the dir name */
 
264
    p = bindir + strlen(bindir);
 
265
    while (p != bindir && *p != '/')
 
266
      p--;
 
267
    assert( p == bindir || *p == '/');
 
268
    if (*p == '/')
 
269
      *p = 0;
 
270
  }
 
271
#ifdef PURIFY
 
272
  purify_all_leaks();
 
273
  purify_all_inuse();
 
274
#endif
 
275
  {
 
276
    int i;
 
277
    for (i = 1 ; i < argc ; i++)
 
278
      {
 
279
        if (strcmp(argv[i],"--remove")==0 || strcmp(argv[i],"-r")==0)
 
280
          mode |=8;
 
281
        else if (strcmp(argv[i],"--load")==0 || strcmp(argv[i],"-l")==0)
 
282
          mode |=1;
 
283
        else if (strcmp(argv[i],"--download")==0|| strcmp(argv[i],"-d")==0)
 
284
          mode |=2;
 
285
        else if (strcmp(argv[i],"--checks")==0|| strcmp(argv[i],"-c")==0)
 
286
          {
 
287
            if (mode & 2 )
 
288
              mode |= 4;
 
289
            else
 
290
              goto errexit;
 
291
          }
 
292
        else if (strcmp(argv[i],"--verbose")==0 || strcmp(argv[i],"-v")==0)
 
293
          {
 
294
            ErrSetLogLevel(0);
 
295
            ErrSetOptFlags(EO_LOGTO_STDERR);
 
296
          }
 
297
        else if (strncmp(argv[i],"--path=",strlen("--path="))==0)
 
298
          strcpy(MMDB,argv[i]+strlen("--path="));
 
299
        else if (strcmp(argv[i],"--pack")==0)
 
300
          pack = 1;
 
301
        else if (strcmp(argv[i],"--enforce")==0)
 
302
          enforce = 1;
 
303
        else if (strncmp(argv[i],"--downpath=",strlen("--downpath="))==0)
 
304
          strcpy(outdir,argv[i]+strlen("--downpath="));
 
305
        else if (strncmp(argv[i],"--dbpath=",strlen("--dbpath="))==0)
 
306
          server = argv[i]+strlen("--dbpath=");
 
307
        else if (strncmp(argv[i],"--state=",strlen("--state="))==0)
 
308
          state = atol(argv[i]+strlen("--state="));
 
309
        else
 
310
          goto errexit;
 
311
      }
 
312
  }
 
313
  if ( MMDB[strlen(MMDB)-1] != '/')
 
314
    strcat(MMDB,"/");
 
315
  if (state<0 )
 
316
    state = (mode & 1 ? LOAD_IN_STATE :0);
 
317
  if (mode & 2 )
 
318
    {
 
319
      char shcmd[1024];
 
320
      sprintf(shcmd," test -d %s || mkdir %s",outdir,outdir);
 
321
      system (shcmd);
 
322
    }
 
323
    
 
324
  while(scanf("%d",&uid) != EOF)
 
325
    {
 
326
      int rc, repeat=5;
 
327
      printf("%5d: ",uid);
 
328
      while(repeat--)
 
329
        {
 
330
          if(mode & 8)
 
331
            {
 
332
              printf("removing... ");
 
333
              rc = remove_struct(uid);
 
334
            }
 
335
          else
 
336
            {
 
337
              rc = 1;
 
338
              if(mode & 1)
 
339
                { printf("loading...     "); rc = load(uid);          }
 
340
              if(mode & 2 && rc )
 
341
                { printf("downloading... "); rc = download(uid,pack); }
 
342
              if(mode & 4 && rc )
 
343
                { check(uid); rc = -1 ;                               }
 
344
            }
 
345
          if(rc)
 
346
            break;
 
347
          printf("slee-e-ep...  ");
 
348
          sleep(60);
 
349
        }
 
350
      if (rc == -1 )
 
351
        printf(";\n");
 
352
      else
 
353
        {
 
354
          printf("%s ;\n",rc ?"ok":"FAILED");
 
355
          if (rc==0)
 
356
            failed = 1;
 
357
        }
 
358
      ErrShow();
 
359
    }
 
360
  return (failed?1:0);
 
361
errexit:
 
362
  printf("usage: %s [ --load | --remove | --download [ --checks | --pack ] ] \n"
 
363
         "          [ --path=mmdb_files_dir ] [ --downpath=dump_mmdb_files_dir  ] \n"
 
364
         "          [ --state=<to_state> ] [ --verbose | -v ] [ --enforce ] \n"
 
365
         "          [ --dbpath=servername:DBname=username:password ]\n",
 
366
         argv[0]);
 
367
  return 2;
 
368
}