1 /* -*- Mode: c; c-basic-offset: 2 -*-
2  *
3  * db_upgrade.c - Upgrade a redland on-disk BDB store
4  *
5  * Copyright (C) 2003-2006, David Beckett http://purl.org/net/dajobe/
6  * Copyright (C) 2003-2004, University of Bristol, UK http://www.bristol.ac.uk/
7  *
8  * This package is Free Software and part of Redland http://librdf.org/
9  *
10  * It is licensed under the following three licenses as alternatives:
11  *   1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
12  *   2. GNU General Public License (GPL) V2 or any newer version
13  *   3. Apache License, V2.0 or any newer version
14  *
15  * You may not use this file except in compliance with at least one of
16  * the above three licenses.
17  *
18  * See LICENSE.html or LICENSE.txt at the top of this package for the
19  * complete terms and further detail along with the license texts for
20  * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
21  *
22  *
23  */
24 
25 
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdlib.h>
30 
31 #include <redland.h>
32 
33 
34 /* one prototype needed */
35 int main(int argc, char *argv[]);
36 
37 
38 
39 int
main(int argc,char * argv[])40 main(int argc, char *argv[])
41 {
42   librdf_world* world;
43   librdf_storage *storage, *new_storage;
44   librdf_model *model, *new_model;
45   librdf_stream *stream;
46   char *program=argv[0];
47   char *name;
48   char *new_name;
49   int count;
50 
51   if(argc < 2 || argc >3) {
52     fprintf(stderr, "USAGE: %s: <Redland BDB name> [new DB name]\n", program);
53     return(1);
54   }
55 
56   name=argv[1];
57 
58   if(argc < 3) {
59     new_name=librdf_heuristic_gen_name(name);
60     if(!new_name) {
61       fprintf(stderr, "%s: Failed to create new name from '%s'\n", program,
62               name);
63       return(1);
64     }
65   } else {
66     new_name=argv[2];
67   }
68 
69   fprintf(stderr, "%s: Upgrading DB '%s' to '%s'\n", program, name, new_name);
70 
71   world=librdf_new_world();
72   librdf_world_open(world);
73 
74   storage=librdf_new_storage(world, "hashes", name,
75                              "hash-type='bdb',dir='.',write='no',new='no'");
76   if(!storage) {
77     fprintf(stderr, "%s: Failed to open old storage '%s'\n", program, name);
78     return(1);
79   }
80 
81   new_storage=librdf_new_storage(world, "hashes", new_name,
82                                  "hash-type='bdb',dir='.',write='yes',new='yes'");
83   if(!storage) {
84     fprintf(stderr, "%s: Failed to create new storage '%s'\n", program, new_name);
85     return(1);
86   }
87 
88   model=librdf_new_model(world, storage, NULL);
89   if(!model) {
90     fprintf(stderr, "%s: Failed to create model for '%s'\n", program, name);
91     return(1);
92   }
93 
94   new_model=librdf_new_model(world, new_storage, NULL);
95   if(!new_model) {
96     fprintf(stderr, "%s: Failed to create new model for '%s'\n", program, new_name);
97     return(1);
98   }
99 
100   stream=librdf_model_as_stream(model);
101   if(!stream) {
102     fprintf(stderr, "%s: librdf_model_as_stream returned NULL stream\n",
103             program);
104     return(1);
105   } else {
106     count=0;
107     while(!librdf_stream_end(stream)) {
108       librdf_statement *statement=librdf_stream_get_object(stream);
109       if(!statement) {
110         fprintf(stderr, "%s: librdf_stream_next returned NULL\n", program);
111         break;
112       }
113 
114       librdf_model_add_statement(new_model, statement);
115 
116       librdf_stream_next(stream);
117       count++;
118     }
119     librdf_free_stream(stream);
120   }
121 
122 
123   librdf_free_model(model);
124   librdf_free_model(new_model);
125 
126   librdf_free_storage(storage);
127   librdf_free_storage(new_storage);
128 
129   librdf_free_world(world);
130 
131   if(argc < 3)
132     free(new_name);
133 
134 
135 #ifdef LIBRDF_MEMORY_DEBUG
136   librdf_memory_report(stderr);
137 #endif
138 
139   /* keep gcc -Wall happy */
140   return(0);
141 }
142