1 // Author:  Bruce Allen
2 // Created: 2/25/2013
3 //
4 // The software provided here is released by the Naval Postgraduate
5 // School, an agency of the U.S. Department of Navy.  The software
6 // bears no warranty, either expressed or implied. NPS does not assume
7 // legal liability nor responsibility for a User's use of the software
8 // or the results of such use.
9 //
10 // Please note that within the United States, copyright protection,
11 // under Section 105 of the United States Code, Title 17, is not
12 // available for any work of the United States Government and/or for
13 // any works created by United States Government employees. User
14 // acknowledges that this software contains work which was created by
15 // NPS government employees and is therefore in the public domain and
16 // not subject to copyright.
17 //
18 // Released into the public domain on February 25, 2013 by Bruce Allen.
19 
20 /**
21  * \file
22  * Holds state about changes applied to the hash database.
23  * The hashdb_manager updates this information while performing actions,
24  * then reports it using the logger.
25  */
26 
27 #ifndef LMDB_CHANGES_HPP
28 #define LMDB_CHANGES_HPP
29 
30 #include <sstream>
31 #include <iostream>
32 
33 namespace hashdb {
34 
35 /**
36  * Holds state about changes applied to the hash database.
37  */
38 class lmdb_changes_t {
39 
40   public:
41   // hash_data
42   size_t hash_data_inserted;
43   size_t hash_data_merged;
44   size_t hash_data_merged_same;
45   size_t hash_data_mismatched_data_detected;
46   size_t hash_data_mismatched_sub_count_detected;
47 
48   // hash
49   size_t hash_inserted;
50   size_t hash_count_changed;
51   size_t hash_count_not_changed;
52 
53   // source_data
54   size_t source_data_inserted;
55   size_t source_data_changed;
56   size_t source_data_same;
57 
58   // source_id
59   size_t source_id_inserted;
60   size_t source_id_already_present;
61 
62   // source_name
63   size_t source_name_inserted;
64   size_t source_name_already_present;
65 
lmdb_changes_t()66   lmdb_changes_t() :
67             hash_data_inserted(0),
68             hash_data_merged(0),
69             hash_data_merged_same(0),
70             hash_data_mismatched_data_detected(0),
71             hash_data_mismatched_sub_count_detected(0),
72             hash_inserted(0),
73             hash_count_changed(0),
74             hash_count_not_changed(0),
75             source_data_inserted(0),
76             source_data_changed(0),
77             source_data_same(0),
78             source_id_inserted(0),
79             source_id_already_present(0),
80             source_name_inserted(0),
81             source_name_already_present(0) {
82   }
83 
report_changes(std::ostream & os) const84   void report_changes(std::ostream& os) const {
85 
86     os << "# hashdb changes:\n";
87     // log changes
88     if (hash_data_inserted) {
89       os << "#     hash_data_inserted: "
90          << hash_data_inserted<< "\n";
91     }
92     if (hash_data_merged) {
93       os << "#     hash_data_merged: "
94          << hash_data_merged<< "\n";
95     }
96     if (hash_data_merged_same) {
97       os << "#     hash_data_merged_same: "
98          << hash_data_merged_same<< "\n";
99     }
100     if (hash_data_mismatched_data_detected) {
101       os << "#     hash_data_mismatched_data_detected: "
102          << hash_data_mismatched_data_detected << "\n";
103     }
104     if (hash_data_mismatched_sub_count_detected) {
105       os << "#     hash_data_mismatched_sub_count_detected: "
106          << hash_data_mismatched_sub_count_detected << "\n";
107     }
108     if (hash_inserted) {
109       os << "#     hash_inserted: " << hash_inserted<< "\n";
110     }
111     if (hash_count_changed) {
112       os << "#     hash_count_changed: " << hash_count_changed<< "\n";
113     }
114     if (hash_count_not_changed) {
115       os << "#     hash_count_not_changed: " << hash_count_not_changed<< "\n";
116     }
117     if (source_data_inserted) {
118       os << "#     source_data_inserted: " << source_data_inserted << "\n";
119     }
120     if (source_data_changed) {
121       os << "#     source_data_changed: " << source_data_changed<< "\n";
122     }
123     if (source_data_same) {
124       os << "#     source_data_same: " << source_data_same << "\n";
125     }
126     if (source_id_inserted) {
127       os << "#     source_id_inserted: " << source_id_inserted << "\n";
128     }
129     if (source_id_already_present) {
130       os << "#     source_id_already_present: " << source_id_already_present << "\n";
131     }
132     if (source_name_inserted) {
133       os << "#     source_name_inserted: " << source_name_inserted << "\n";
134     }
135     if (source_name_already_present) {
136       os << "#     source_name_already_present: " << source_name_already_present << "\n";
137     }
138     if (hash_data_inserted == 0 &&
139         hash_data_merged == 0 &&
140         hash_data_merged_same == 0 &&
141         hash_data_mismatched_data_detected == 0 &&
142         hash_data_mismatched_sub_count_detected == 0 &&
143         hash_inserted == 0 &&
144         hash_count_changed == 0 &&
145         hash_count_not_changed == 0 &&
146         source_data_inserted == 0 &&
147         source_data_changed == 0 &&
148         source_data_same == 0 &&
149         source_id_inserted == 0 &&
150         source_id_already_present == 0 &&
151         source_name_inserted == 0 &&
152         source_name_already_present == 0) {
153        os << "No changes.\n";
154     }
155   }
156 };
157 
158 } // end namespace hashdb
159 
operator <<(std::ostream & os,const class hashdb::lmdb_changes_t & changes)160 inline std::ostream& operator<<(std::ostream& os,
161                          const class hashdb::lmdb_changes_t& changes) {
162   changes.report_changes(os);
163   return os;
164 }
165 
166 #endif
167 
168