85 for (
int i = 0;
i <
n;
i++)
99 return myMDSql->setObjectValue(
id, mdValueIn);
106 return myMDSql->setObjectValue(mdValueIn);
117 return myMDSql->getObjectValue(
id, mdValueOut);
123 std::vector<size_t> objectsId;
125 size_t n = objectsId.size();
126 valuesOut.resize(n,mdValueOut);
127 for (
size_t i = 0;
i <
n; ++
i)
130 valuesOut[
i] = mdValueOut;
136 bool addObjects=
false;
139 if (valuesIn.size()!=
size() && !addObjects)
144 for (
size_t objId : this->
ids())
149 size_t nmax=valuesIn.size();
160 if (!
myMDSql->bindStatement(
id))
173 if (!
myMDSql->initializeSelect( addWhereClause, this->_activeLabels))
183 std::vector<MDObject> mdValues;
190 for (
const auto &
obj : mdValues)
198 myMDSql->finalizePreparedStmt();
203 std::vector<MDObject> values;
205 values.reserve(labels.size());
206 for (
auto &l : labels) {
207 values.emplace_back(l);
214 std::vector<std::vector<MDObject>> rawRows;
215 rawRows.reserve(this->
size());
220 rawRows))
return false;
223 const auto noOfRows = rawRows.size();
224 rows.resize(noOfRows);
225 for (
size_t i = 0;
i < noOfRows; ++
i) {
226 auto &row = rows.at(
i);
227 const auto &vals = rawRows.at(
i);
229 for (
auto &v : vals) {
237 std::unique_ptr<MDRowSql> row(
new MDRowSql());
240 return std::move(row);
244 std::unique_ptr<MDRowSql> row(
new MDRowSql());
247 return std::move(row);
276 values))
return false;
278 for (
auto &v : values)
314 std::vector<MDLabel> labels;
315 labels.reserve(row.
size());
317 labels.emplace_back(
obj->label);
320 std::vector<const MDObject*> vals;
321 vals.reserve(row.size());
322 for (
const auto &l : labels) {
323 vals.emplace_back(row.getObject(l));
335 std::vector<MDLabel> labels;
336 std::vector<MDObject*> mdValues;
339 labels.resize(row.
size());
345 labels[
j] =
obj->label;
351 if (!
myMDSql->initializeInsert( &labels, mdValues))
353 std::cerr <<
"initAddRow: error executing myMDSql->initializeInsert" << std::endl;
365 std::vector<const MDObject*> mdValues;
368 mdValues.resize(row.
size());
374 mdValues[
j] = row.getObject(
obj->label);
380 if (!
myMDSql->setObjectValues( -1, mdValues))
382 std::cerr <<
"execAddRow: error executing myMDSql->setObjectValues" << std::endl;
391 myMDSql->finalizePreparedStmt();
405 for (
auto &v : values) {
424 std::vector<MDLabel> missingLabels;
425 auto definedLabels = row.
labels();
426 for (
const auto &l : definedLabels){
428 missingLabels.emplace_back(l);
432 if ( ! missingLabels.empty()) {
442 const auto noOfRows = rows.size();
452 std::vector<MDLabel> labels;
455 labels.emplace_back(
obj->label);
456 const auto noOfLabels = labels.size();
459 std::vector<std::vector<const MDObject*>> records;
460 records.reserve(noOfRows);
461 for (
const auto &r : rows) {
462 records.emplace_back(std::vector<const MDObject*>());
463 auto &vals = records.back();
464 vals.reserve(noOfLabels);
465 for (
const auto &l : labels) {
466 vals.emplace_back(r.getObject(l));
518 read(fileName, desiredLabels.empty() ? nullptr : &desiredLabels);
546 return myMDSql->columnMaxLength(thisLabel);
568 std::vector<MDLabel>::iterator location;
580 return (
size_t)
myMDSql->addRow();
586 assert(dynamic_cast<const MetaDataDb*>(&md) !=
nullptr);
590 mdd.
myMDSql->copyObjects(
this, &query);
598 int size = objectsToAdd.size();
606 assert(dynamic_cast<const MetaDataDb*>(&md) !=
nullptr);
626 md.
myMDSql->copyObjects(
this, &query);
631 return (removed > 0);
636 int size = toRemove.size();
643 int removed =
myMDSql->deleteObjects(&query);
649 int removed =
myMDSql->deleteObjects();
655 std::vector<MDLabel> labels(1);
662 myMDSql->indexModify(desiredLabels,
true);
667 std::vector<MDLabel> labels(1);
674 myMDSql->indexModify(desiredLabels,
false);
697 std::vector<size_t>
ids;
699 size_t id = ids.size() == 1 ? ids[0] :
BAD_OBJID;
712 myMDSql->selectObjects(objectsOut, &query);
719 myMDSql->selectObjects(objectsOut, &query);
724 std::vector<size_t> objects;
726 return objects.size();
736 std::vector<size_t> objects;
738 return objects.size() > 0;
742 #include <sys/stat.h> 745 #include <sys/mman.h> 755 if (blockName.empty())
764 else if(extFile==
"sqlite")
766 writeDB(outFile, blockName, mode);
778 std::sort(sortedLabels.begin(), sortedLabels.end());
779 for (
const auto& row : *
this)
781 for (
size_t i = 0;
i < sortedLabels.size();
i++)
786 row.getObject(sortedLabels[
i])->toStream(os,
true);
802 String _szBlockName(
"data_");
803 _szBlockName += blockName;
808 os << _szBlockName <<
'\n';
809 os <<
"loop_" <<
'\n';
811 std::sort(sortedLabels.begin(), sortedLabels.end());
812 for (
size_t i = 0;
i < sortedLabels.size();
i++)
814 const auto &label = sortedLabels.at(
i);
826 os << _szBlockName <<
'\n';
834 std::sort(sortedLabels.begin(), sortedLabels.end());
835 for (
size_t i = 0;
i < sortedLabels.size();
i++)
837 const auto &label = sortedLabels.at(
i);
842 myMDSql->getObjectValue(
id, mdValue);
859 size = columnValues.size();
860 for (i=0; i<
size ;i++)
862 columnValues[
i]->fromStream(is);
866 columnValues[
i]->failed =
true;
867 std::cerr <<
"WARNING: " << errorMsg << std::endl;
878 bool reallyAdd=
false;
879 if (desiredLabels==NULL)
886 for (
size_t j=0;
j<desiredLabels->size(); ++
j)
888 if ((*desiredLabels)[
j]==columnValues[
i]->label)
907 myMDSql->setObjectValues( -1, columnValues, desiredLabels);
913 const std::vector<MDLabel> *desiredLabels,
931 readXML(inFile, desiredLabels, blockName, decomposeStack);
932 else if(extFile==
"sqlite")
933 readDB(inFile, desiredLabels, blockName, decomposeStack);
935 readStar(_filename, desiredLabels, blockName, decomposeStack);
941 constexpr
size_t LINE_LENGTH = 1024;
943 std::vector<MDLabel> labels;
946 char lineBuffer[LINE_LENGTH];
949 size_t lineCounter = 0;
950 size_t columnsNumber = labels.size();
954 while (is.getline(lineBuffer, LINE_LENGTH))
957 line.assign(lineBuffer);
963 std::stringstream ss(line);
965 for (
size_t i = 0;
i < columnsNumber; ++
i)
991 struct stat file_status;
998 if (blockName.empty() || !outFile.
exists())
1005 if(stat(outFile.c_str(), &file_status) != 0)
1007 size_t size = file_status.st_size;
1010 fd = open(outFile.c_str(), O_RDWR, S_IREAD | S_IWRITE);
1014 map = (
char *) mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1015 if (map == MAP_FAILED)
1020 size_t blockNameSize = _szBlockName.size();
1022 bool found=
_memmem(map, size, _szBlockName.data(), blockNameSize) != NULL;
1023 if (munmap(map, size) == -1)
1034 const std::vector<MDLabel> *desiredLabels,
1035 const String & blockRegExp,
1036 bool decomposeStack)
1042 const std::vector<MDLabel> *desiredLabels,
1043 const String & blockRegExp,
1044 bool decomposeStack)
1046 myMDSql->copyTableFromFileDB(blockRegExp, filename, desiredLabels,
_maxRows);
1052 const std::vector<MDLabel> *desiredLabels) {
1054 std::stringstream ss;
1056 bool firstTime =
true;
1062 memcpy(buffer, block.
loop, n);
1066 if (
myMDSql->initializeInsert( desiredLabels, columnValues))
1068 while (iter < end) {
1072 line.assign(iter, newline - iter);
1075 if (!line.empty() && line[0] !=
'#') {
1080 std::stringstream ss(line);
1081 this->
_parseObjects(ss, columnValues, desiredLabels, firstTime);
1089 myMDSql->finalizePreparedStmt();
1103 while (getline(is, line,
'\n'))
1105 if (line[0] ==
'#' || line[0] ==
'\0' || line[0] ==
';')
1109 std::stringstream os(line);
1125 for (
const auto& row : md2)
1126 this->
setRow(row, row.id());
1129 #define SET_AND_FILL() generator.label=label; generator.fill(*this) 1141 for (
size_t id : mdCTFs.
ids())
1143 if (mdCTFs.
getValue(label, fn,
id))
1154 join1(md, mdCTFs, label);
1193 std::vector<MDObject> values;
1203 std::vector<MDLabel> vOldLabel(1);
1204 vOldLabel[0]=oldLabel;
1205 std::vector<MDLabel> vNewLabel(1);
1206 vNewLabel[0]=newLabel;
1211 const std::vector<MDLabel> &vNewLabel)
1213 myMDSql->renameColumn(vOldLabel,vNewLabel);
1220 mdValueOut.
setValue(
myMDSql->aggregateSingleDouble(op,aggregateLabel));
1227 mdValueOut.
setValue(
myMDSql->aggregateSingleSizeT(op,aggregateLabel));
1250 size_t aux =
myMDSql->aggregateSingleSizeT(op,aggregateLabel);
1251 int aux2 = (int) aux;
1258 std::vector<MDLabel> labels(2);
1259 std::vector<MDLabel> operateLabels(1);
1260 labels[0] = aggregateLabel;
1261 labels[1] = resultLabel;
1262 operateLabels[0]=operateLabel;
1264 std::vector<AggregateOperation> ops(1);
1266 mdIn.
myMDSql->aggregateMd(
this, ops, operateLabels);
1270 const std::vector<MDLabel> &operateLabels,
1271 const std::vector<MDLabel> &resultLabels)
1273 if (resultLabels.size() - ops.size() != 1)
1274 REPORT_ERROR(
ERR_MD,
"Labels vectors should contain one element more than operations");
1276 mdIn.
myMDSql->aggregateMd(
this, ops, operateLabels);
1281 const std::vector<MDLabel> &groupByLabels,
1285 std::vector<MDLabel> labels;
1286 labels = groupByLabels;
1287 labels.emplace_back(resultLabel);
1289 mdIn.
myMDSql->aggregateMdGroupBy(
this, op, groupByLabels, operateLabel, resultLabel);
1297 std::vector<MDLabel> labels;
1298 labels.emplace_back(label);
1303 const std::vector<MDLabel> &labels,
1308 if (
size() == 0 && mdIn.
size() == 0)
1314 mdIn.
myMDSql->setOperate(
this, labels, operation);
1323 if (mdIn.
size() == 0)
1327 std::vector<MDLabel> labels;
1328 labels.emplace_back(label);
1329 mdIn.
myMDSql->setOperate(
this, labels, operation);
1334 const std::vector<MDLabel> &labelsLeft,
1335 const std::vector<MDLabel> &labelsRight,
1338 if (
this == &mdInLeft ||
this == &mdInRight)
1346 for (
size_t j=0;
j<labelsRight.size(); ++
j)
1356 myMDSql->setOperate(&mdInLeft, &mdInRight, labelsLeft,labelsRight, operation);
1405 join2(mdInLeft, mdInRight, label, label, type);
1412 std::vector<MDLabel> labelsLeft, labelsRight;
1413 labelsLeft.emplace_back(labelLeft);
1414 labelsRight.emplace_back(labelRight);
1420 join2(mdInLeft, mdInRight, labels, labels, type);
1424 const std::vector<MDLabel> &labelsRight,
JoinType type)
1437 if (!
myMDSql->operate(expression))
1445 labelStr.c_str(), labelStr.c_str(), oldStr.c_str(), newStr.c_str());
1446 if (!
myMDSql->operate(expression))
1452 std::random_device rd;
1453 auto g = std::mt19937(rd());
1454 std::vector<size_t> objects;
1455 MDin.
myMDSql->selectObjects(objects);
1456 std::shuffle(objects.begin(), objects.end(),
g);
1468 MDQuery query(limit, offset, sortLabel,asc);
1469 MDin.
myMDSql->copyObjects(
this, &query);
1478 size_t ipos=sortLabel.find(
':');
1482 if(limit != -1 || offset != 0)
1487 if (ipos!=String::npos)
1490 std::vector< String > results;
1507 std::vector<double> vectorValues;
1509 for (
size_t id : MDin.
ids())
1511 MDin.
getValue(label, vectorValues,
id);
1512 if (column >= vectorValues.size())
1542 size_t mdSize =
size();
1544 REPORT_ERROR(
ERR_MD,
"MetaDataDb::split: Couldn't split a metadata in more parts than its size");
1548 for (
size_t i = 0;
i <
n;
i++)
1556 int n,
int part,
size_t mdSize,
1559 size_t first, last, n_images;
1563 mdIn.
myMDSql->copyObjects(
this,
new MDQuery(n_images, first, sortLabel));
1568 if (dynamic_cast<const MetaDataDb*>(&mdIn) !=
nullptr)
1569 return _selectSplitPart(dynamic_cast<const MetaDataDb&>(mdIn), n, part, sortLabel);
1570 throw std::logic_error(
"Not yet implemented");
1575 size_t mdSize = mdIn.
size();
1577 REPORT_ERROR(
ERR_MD,
"selectSplitPart: Couldn't split a metadata in more parts than its size");
1578 if (part < 0 || part >= n)
1586 if (dynamic_cast<const MetaDataDb*>(&mdIn) !=
nullptr)
1587 return _selectRandomSubset(dynamic_cast<const MetaDataDb&>(mdIn), numberOfObjects, sortLabel);
1588 throw std::logic_error(
"Not yet implemented");
1597 mdAux2.
selectPart(mdAux, 0, numberOfObjects);
1598 sort(mdAux2,sortLabel);
1604 if (dynamic_cast<const MetaDataDb*>(&mdIn) !=
nullptr)
1605 return _selectPart(dynamic_cast<const MetaDataDb&>(mdIn), startPosition, numberOfObjects, sortLabel);
1606 throw std::logic_error(
"Not yet implemented");
1612 size_t mdSize = mdIn.
size();
1613 if (startPosition < 0 || startPosition >= mdSize)
1614 REPORT_ERROR(
ERR_MD,
"selectPart: 'startPosition' should be between 0 and size()-1");
1617 mdIn.
myMDSql->copyObjects(
this,
new MDQuery(numberOfObjects, startPosition, sortLabel));
1627 if (!getcwd(buffer, 1023))
1633 if (aux_string[0] ==
'/')
1637 for (
size_t id : this->
ids())
1639 aux_string_path = path_str;
1644 size_t id = auxFile.find(
'@',0);
1645 auxFile.insert(
id+1,aux_string_path);
1660 myMDSql->copyTableToFileDB(blockname,fn);
1671 ofs <<
"<" << blockname <<
">"<<
'\n';
1672 for (
size_t id : this->
ids())
1675 for (
size_t i = 0;
i <
size;
i++)
1682 myMDSql->getObjectValue(
id, mdValue);
1687 ofs <<
" />" <<
'\n';
1689 ofs <<
"</" << blockname <<
">"<<
'\n';
1696 if (desiredLabels != NULL)
1709 for (
const auto& row : *
this)
1710 vd.emplace_back(dynamic_cast<const MDRowSql&>(row));
1717 for (
size_t i=0;
i<rowMetadata.size();
i++)
Just to locate unclassified errors.
No active object in MetaData.
virtual bool empty() const =0
Case or algorithm not implemented yet.
static MDLabel str2Label(const String &labelName)
const int & getValue2(int) const
static bool insert(const std::vector< std::vector< const MDObject *>> &records, sqlite3 *db, const std::string &table)
String getBlockName() const
#define REPORT_ERROR(nerr, ErrormMsg)
void resizeNoCopy(const MultidimArray< T1 > &v)
std::vector< SelLine >::iterator find(std::vector< SelLine > &text, const std::string &img_name)
void trim(std::string &s)
int size() const override
size_t divide_equally(size_t N, size_t size, size_t rank, size_t &first, size_t &last)
virtual std::vector< MDLabel > labels() const =0
String WordWrap(const String &inputString, size_t lineLength)
Incorrect MultidimArray size.
Memory has not been deallocated.
#define FOR_ALL_DIRECT_ELEMENTS_IN_ARRAY1D(v)
Bad amount of memory requested.
std::vector< String > StringVector
Incorrect number of objects in Metadata.
static MDLabelType labelType(const MDLabel label)
String getExtension() const
static bool update(const std::vector< const MDObject *> &values, sqlite3 *db, const std::string &table, size_t id)
#define DIRECT_A1D_ELEM(v, i)
void * _memmem(const void *haystack, size_t haystack_len, const void *needle, size_t needle_len)
FileName addPrefix(const String &prefix) const
int splitString(const String &input, const String &delimiter, StringVector &results, bool includeEmpties)
void setValue(const MDObject &object) override
Incorrect argument received.
basic_istream< char, std::char_traits< char > > istream
void sort(struct DCEL_T *dcel)
virtual int size() const =0
void setValue(MDLabel label, const T &d, bool addLabel=true)
String escapeForRegularExpressions(const String &str)
FileName removeBlockName() const
String formatString(const char *format,...)
void setValue(const int &iv)
void toStream(std::ostream &os, bool withFormat=false, bool isSql=false, bool escape=true) const
int textToInteger(const char *str)
Environment PATH cannot be read.
static void str2LabelVector(const String &labelsStr, std::vector< MDLabel > &labels)
static String label2Str(const MDLabel &label)
static bool select(size_t rowId, sqlite3 *db, const std::string &table, std::vector< MDObject > &values)
Incorrect value received.
void indexSort(MultidimArray< int > &indx) const
static bool addColumns(const std::vector< MDLabel > &columns, sqlite3 *db, const std::string &table)