DBVectorProcessor.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright (C) 2017 Carlos Aguilar Melchor, Joris Barrier, Marc-Olivier Killijian
  2. *
  3. * This file is written by Konstantinos Andrikopoulos
  4. *
  5. * This file is part of XPIR.
  6. *
  7. * XPIR is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * XPIR is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with XPIR. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #include "DBVectorProcessor.hpp"
  22. Element::Element(std::string e_name, uint64_t e_size, char* e_data) :
  23. name(e_name),
  24. data_size(e_size),
  25. data(NULL)
  26. {
  27. data = (char*) malloc(data_size);
  28. memcpy(data, e_data, data_size);
  29. }
  30. DBVectorProcessor::DBVectorProcessor(std::vector<element_t>& vector_db) :
  31. elements(vector_db)
  32. {
  33. maxFileByteSize = 0;
  34. for (auto e : elements)
  35. {
  36. if (e.data_size > maxFileByteSize)
  37. {
  38. maxFileByteSize = e.data_size;
  39. }
  40. }
  41. }
  42. DBVectorProcessor::~DBVectorProcessor() {}
  43. std::string DBVectorProcessor::getCatalog(const bool typeOfCatalog) {
  44. std::string buf;
  45. if(typeOfCatalog) {
  46. buf = std::to_string((unsigned int)0) + "\n";
  47. buf += std::to_string(getNbStream()) + "\n";
  48. for (auto e : elements)
  49. {
  50. //auto e = elements[i];
  51. buf += e.name + "\n" + std::to_string(e.data_size) + "\n";
  52. }
  53. return buf;
  54. }
  55. else {
  56. buf = std::to_string((unsigned int)1) + "\n";
  57. buf += std::to_string(getNbStream());
  58. buf += std::to_string(getmaxFileBytesize()) + "\n";
  59. return buf;
  60. }
  61. }
  62. uint64_t DBVectorProcessor::getNbStream() {
  63. return elements.size();
  64. }
  65. uint64_t DBVectorProcessor::getmaxFileBytesize() {
  66. return maxFileByteSize;
  67. }
  68. bool DBVectorProcessor::openStream(uint64_t streamNb, uint64_t requested_offset) {
  69. if(openStreamOffsets.count(streamNb)) {
  70. return false;
  71. }
  72. char* stream = elements[streamNb].data + requested_offset;
  73. openStreamOffsets.insert( std::pair<uint64_t, char*>(streamNb, stream));
  74. return true;
  75. }
  76. uint64_t DBVectorProcessor::readStream(uint64_t streamNb, char * buf, uint64_t size) {
  77. element_t e = elements[streamNb];
  78. char* stream = openStreamOffsets[streamNb];
  79. uint64_t sizeRead = stream - e.data;
  80. uint64_t sizeRemaining = e.data_size - sizeRead;
  81. if(sizeRemaining >= size) {
  82. memcpy(buf, stream, size);
  83. stream += size;
  84. }
  85. else {
  86. memcpy(buf, stream, sizeRemaining);
  87. bzero(buf + sizeRemaining, size - sizeRemaining);
  88. stream += sizeRemaining;
  89. }
  90. openStreamOffsets[streamNb] = stream;
  91. return size;
  92. }
  93. void DBVectorProcessor::closeStream(uint64_t streamNb) {
  94. openStreamOffsets.erase(streamNb);
  95. }