tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
jobs_dataset.cc
1 // -*- mode: c++; fill-column: 80; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 /*
3  Copyright (c) 2013 Juan Jose Garcia Ripoll
4 
5  Tensor is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published
7  by the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Library General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #include <string>
21 #include <memory>
22 #include <tensor/jobs.h>
23 #include <tensor/sdf.h>
24 
25 namespace jobs {
26 
27  template <typename T>
28  std::string NumberToString ( T Number )
29  {
30  /*
31  * Input:
32  * T = a number
33  * Output:
34  * a string with a representation of the number
35  * Remarks:
36  * There is no way to control the length of this string or
37  * its accuracy.
38  */
39  std::ostringstream ss;
40  ss << Number;
41  return ss.str();
42  }
43 
44  static std::string
45  dataset_name(const std::string &base, int jobid)
46  {
47  return base + "/" + NumberToString(jobid);
48  }
49 
50  Job::dataset
51  Job::open_dataset(const std::string &filename) const
52  {
53  if (sdf::file_exists(filename)) {
54  if (!sdf::isdir(filename)) {
55  std::cerr << "Cannot create Job dataset on existing file\n";
56  abort();
57  }
58  } else {
59  // In a shared environment with multiple jobs, we cannot
60  // be certain that another job creates the same directory
61  // between the previous check and here.
62  sdf::make_directory(filename);
63  if (!sdf::file_exists(filename)) {
64  std::cerr << "Cannot create Job dataset\n";
65  abort();
66  }
67  }
68  std::string dataset_record_name = dataset_name(filename, current_job());
69  sdf::OutDataFile *output;
70  if (sdf::file_exists(dataset_record_name)) {
71  std::cout << "File " << dataset_record_name << " already exists" << std::endl;
72  output = NULL;
73  } else {
74  output = new sdf::OutDataFile(dataset_record_name,
75  sdf::DataFile::SDF_PARANOID);
76  }
77  return dataset(output);
78  }
79 
80  bool
81  Job::dataset_record_exists(const std::string &filename) const
82  {
83  return sdf::file_exists(dataset_name(filename, current_job()));
84  }
85 
86 }