|
1 /* |
|
2 * Copyright 2004-2006 Intel Corporation |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifdef HAVE_CONFIG_H |
|
18 # include <dtn-config.h> |
|
19 #endif |
|
20 |
|
21 #if SQL_ENABLED |
|
22 |
|
23 #include "SQLGlobalStore.h" |
|
24 #include "SQLStore.h" |
|
25 #include "StorageConfig.h" |
|
26 |
|
27 namespace dtn { |
|
28 |
|
29 static const char* TABLENAME = "globals"; |
|
30 |
|
31 SQLGlobalStore::SQLGlobalStore(oasys::SQLImplementation* impl) |
|
32 { |
|
33 store_ = new SQLStore(TABLENAME, impl); |
|
34 store_->create_table(this); |
|
35 } |
|
36 |
|
37 SQLGlobalStore::~SQLGlobalStore() |
|
38 { |
|
39 NOTREACHED; |
|
40 } |
|
41 |
|
42 bool |
|
43 SQLGlobalStore::load() |
|
44 { |
|
45 log_debug("loading global store"); |
|
46 |
|
47 oasys::SerializableObjectVector elements; |
|
48 elements.push_back(this); |
|
49 |
|
50 int cnt = store_->elements(&elements); |
|
51 |
|
52 if (cnt == 1) { |
|
53 log_debug("loaded next bundle id %d next reg id %d", |
|
54 next_bundleid_, next_regid_); |
|
55 |
|
56 } else if (cnt == 0 && StorageConfig::instance()->tidy_) { |
|
57 log_info("globals table does not exist... initializing it"); |
|
58 |
|
59 next_bundleid_ = 0; |
|
60 next_regid_ = 0; |
|
61 |
|
62 if (store_->insert(this) != 0) { |
|
63 log_err("error initializing globals table"); |
|
64 exit(1); |
|
65 } |
|
66 |
|
67 } else { |
|
68 log_err("error loading globals table"); |
|
69 exit(1); |
|
70 } |
|
71 |
|
72 return true; |
|
73 } |
|
74 |
|
75 bool |
|
76 SQLGlobalStore::update() |
|
77 { |
|
78 log_debug("updating global store"); |
|
79 |
|
80 if (store_->update(this, -1) == -1) { |
|
81 log_err("error updating global store"); |
|
82 return false; |
|
83 } |
|
84 |
|
85 return true; |
|
86 } |
|
87 |
|
88 } // namespace dtn |
|
89 |
|
90 #endif /* SQL_ENABLED */ |