File: PolyDBSection.h

package info (click to toggle)
polymake 4.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,888 kB
  • sloc: cpp: 168,933; perl: 43,407; javascript: 31,575; ansic: 3,007; java: 2,654; python: 632; sh: 268; xml: 117; makefile: 61
file content (336 lines) | stat: -rw-r--r-- 10,787 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* Copyright (c) 1997-2024
   Ewgenij Gawrilow, Michael Joswig, and the polymake team
   Technische Universität Berlin, Germany
   https://polymake.org

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
--------------------------------------------------------------------------------

  This file is part of the polymake database interface polyDB.

   @author Andreas Paffenholz
   (c) 2015-2023
   https://polydb.org
   https://www.mathematik.tu-darmstadt.de/~paffenholz
*/

#pragma once

#include "polymake/common/PolyDB.h"

#include "polymake/common/PolyDBconfig.h"
#include "polymake/common/PolyDBFunctions.h"


namespace polymake {
namespace common {
namespace polydb {

/**
 * @brief a class defining a section in polyDB
 * 
 */
class PolyDBSection {

    public:
    
    ~PolyDBSection() {
      mongoc_collection_destroy(section_);
    }

    PolyDBSection(const std::string& name,
                  const std::shared_ptr<mongoc_client_t>& client
                 ) : client_(client), name_(name) { 
                    mongoc_database_t *database;
                    database = mongoc_client_get_database(client_.get(), "polydb");
                    section_ = mongoc_database_get_collection (database, ("_sectionInfo."+name).c_str());
                    mongoc_database_destroy(database);
                 };

    PolyDBSection(PolyDBSection &&pc ) {
      std::swap(client_,pc.client_);
      std::swap(section_,pc.section_);
      name_ = pc.name_;
    }

    /**
     * @brief drop the section
     * this removes the documentation
     */
    const void drop() const {

      bson_error_t error;
      bool res = mongoc_collection_drop(section_,&error);
      if ( !res ) {
        std::string message = "drop section failed with error ";
        message += error.message;
        throw std::runtime_error(message);        
      }
    }

    /**
     * @brief Get the documentation
     * 
     * @param id the id for the section documentation
     * @return const std::string 
     */
    const std::string get_info(const std::string& id) const {
      bson_t *query = bson_new();
      bson_error_t error;

      bson_append_utf8(query, "_id", -1, id.c_str(), -1);
      mongoc_cursor_t * cursor = mongoc_collection_find_with_opts (section_, query, nullptr, nullptr);
      bson_destroy (query);
      if (mongoc_cursor_error (cursor, &error)) {
        std::string message = "check for section id failed with error ";
        message += error.message;
        message += "and error code ";
        message += std::to_string(error.domain);
        message += std::to_string(MONGOC_ERROR_SERVER);
        mongoc_cursor_destroy (cursor);
        throw std::runtime_error(message);
      }
      
      const bson_t *doc;
      if ( !mongoc_cursor_next (cursor, &doc)) {
        mongoc_cursor_destroy (cursor);
        throw std::runtime_error("no info with given id");
      }

      std::string info = to_string_and_free(bson_as_relaxed_extended_json(doc,nullptr));
      
      mongoc_cursor_destroy (cursor);

      return info;
    }

    /**
     * @brief checks if documentation with the given id exists
     * 
     * @param id the id
     * @return true if the doc exists
     */
    const bool check_for_id(const std::string& id) const {
      
      bson_t *query = bson_new();
      bson_error_t error;

      bson_append_utf8(query, "_id", -1, id.c_str(), -1);
      mongoc_cursor_t * cursor = mongoc_collection_find_with_opts (section_, query, nullptr, nullptr);
      bson_destroy (query);
      if (mongoc_cursor_error (cursor, &error)) {
        std::string message = "check for section id failed with error ";
        message += error.message;
        message += "and error code ";
        message += std::to_string(error.domain);
        message += std::to_string(MONGOC_ERROR_SERVER);
        mongoc_cursor_destroy (cursor);
        throw std::runtime_error(message);
      }
      
      const bson_t *doc;
      bool result_found = false;
      if ( mongoc_cursor_next (cursor, &doc)) {
        result_found = true;
      }
      mongoc_cursor_destroy (cursor);

      return result_found;
    }

    
    /**
     * @brief insert into the collection for this section
     * 
     * @param docstring 
     */
    // FIXME check result
    const void insert(const std::string& docstring) const {

      bson_error_t error;
      bson_t * doc = bson_new_from_json((unsigned char *)docstring.c_str(),-1,&error); 
      bson_t reply;

      //char * str = bson_as_relaxed_extended_json (doc, nullptr);
      //std::cout << "doc: " << str << std::endl;

      bool ret = mongoc_collection_insert_one ( section_, doc, nullptr, &reply, &error ) ;

      bson_destroy(doc);
      if ( !ret ) {
        std::string message = "inserting section info failed with error ";
        message += error.message;
        message += "and error code ";
        message += std::to_string(error.domain);
        message += std::to_string(MONGOC_ERROR_SERVER);
        throw std::runtime_error(message);
      }

      bson_destroy(&reply);
    }

    /**
     * @brief update a document in the collection for this section
     * 
     * @param id the id to update
     * @param docstring the update
     */
    // FIXME check result
    const void update(const std::string& id, const std::string& docstring) const {

      bson_error_t error;
      bson_t * doc = bson_new_from_json((unsigned char *)docstring.c_str(),-1,&error); 
      bson_t * filter = bson_new();
      bson_append_utf8(filter, "_id", -1, id.c_str(), -1);
      bson_t reply;

      bool ret = mongoc_collection_update_one (section_, filter, doc, nullptr, &reply, &error);
      bson_destroy(doc);
      bson_destroy(filter);
      if ( !ret ) {
        std::string message = "updating section info failed with error ";
        message += error.message;
        message += "and error code ";
        message += std::to_string(error.domain);
        message += std::to_string(MONGOC_ERROR_SERVER);
        throw std::runtime_error(message);
      }


      // FIXME do something with the reply first
      bson_destroy(&reply);
    }


    /**
     * @brief replace a document in the collection for this section
     * 
     * @param id the id to replace
     * @param docstring the replacement
     */
    // FIXME check result
    const void replace(const std::string& id, const std::string& docstring) const {

      bson_error_t error;
      bson_t * doc = bson_new_from_json((unsigned char *)docstring.c_str(),-1,&error); 
      bson_t * filter = bson_new();
      bson_append_utf8(filter, "_id", -1, id.c_str(), -1);
      bson_t reply;

      bool ret = mongoc_collection_replace_one (section_, filter, doc, nullptr, &reply, &error);
      bson_destroy(doc);
      bson_destroy(filter);
      bson_destroy(&reply);
      if ( !ret ) {
        std::string message = "replacing section info failed with error ";
        message += error.message;
        message += "and error code ";
        message += std::to_string(error.domain);
        message += std::to_string(MONGOC_ERROR_SERVER);
        throw std::runtime_error(message);
      }

    }

    /**
     * @brief Set the documentation of this section
     * 
     * @param doc the documentation
     * @param update_existing true if an existing documenation should be updated
     * @return true on success
     */
    const bool set_doc(const std::string& doc, bool update_existing ) const {

      bson_error_t error;
      bson_t * doc_bson = bson_new_from_json((unsigned char *)doc.c_str(), -1,&error);
      if ( !doc_bson ) {
        throw std::runtime_error(prepare_error_message(error,"bson_creation"));
      }
      bson_iter_t iter;
      bson_iter_init (&iter, doc_bson);
      std::string version = polyDBVersion;

      if ( bson_iter_find(&iter,"polydb_version") ) {
        version = bson_iter_utf8(&iter, nullptr);
      }

      std::string id_string = name_ +"."+version;
      bson_iter_init (&iter, doc_bson);
      if (bson_iter_find(&iter,"_id") ) {
        id_string = bson_iter_utf8(&iter, nullptr);
      } else {
        bson_append_utf8(doc_bson, "_id", -1, id_string.c_str(), -1);
      }

      int sectionDepth = 0;
      bson_iter_init (&iter, doc_bson);
      if ( !bson_iter_find(&iter,"section") ) {
        std::stringstream name(name_);
        std::string section;
        std::vector<std::string> sections;
        while(std::getline(name, section, '.')) {
          sectionDepth++;
          sections.push_back(section);
        }
        bson_t section_list;
        bson_append_array_begin(doc_bson, "section", -1, &section_list);
        for (size_t i = 0; i < sections.size(); i++) {
          bson_append_utf8(&section_list, std::to_string(i).c_str(), -1, sections[i].c_str(), -1);
        }
        bson_append_array_end(doc_bson, &section_list);
      }

      bson_iter_init (&iter, doc_bson);
      if ( !bson_iter_find(&iter,"sectionDepth") ) {
        if ( sectionDepth == 0 ) {
          bson_iter_t iter2;
          bson_iter_init (&iter2, doc_bson);
          bson_iter_find(&iter2,"section");
          bson_iter_t child;
          bson_iter_recurse (&iter2, &child);
          while (bson_iter_next (&child)) {
            sectionDepth++;
          }
        }
        bson_append_int32(doc_bson, "sectionDepth", -1, sectionDepth);
      } 

      bool doc_exits = check_for_id(id_string);
      if ( update_existing && !doc_exits ) { 
        bson_destroy(doc_bson);
        throw std::runtime_error("updating non-existent section documentation");
      }

      if ( update_existing ) {
        bson_t * update_doc = bson_new();
        bson_append_document(update_doc,"$set",-1,doc_bson);
        std::string update_doc_string = to_string_and_free(bson_as_canonical_extended_json(update_doc,nullptr));
        bson_destroy(update_doc);
        update(id_string,update_doc_string);
      } else {
        std::string insert_replace_doc_string = to_string_and_free(bson_as_canonical_extended_json(doc_bson,nullptr));
        if ( doc_exits ) {
          replace(id_string,insert_replace_doc_string);
        } else {
          insert(insert_replace_doc_string);
        }
      }
      bson_destroy(doc_bson);
      return true;
    }

    private:
        mongoc_collection_t * section_;
        std::shared_ptr<mongoc_client_t> client_;
        std::string name_;

};

}}}