IODA Bundle
Retriever.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 1996-2013 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 
12 #include <ctype.h>
13 #include <fstream>
14 #include <algorithm> // std::find
15 
16 #include "eckit/config/Resource.h"
17 #include "eckit/parser/StringTools.h"
18 #include "eckit/io/PartFileHandle.h"
19 
20 #include "odb_api/FileCollector.h"
21 #include "odb_api/FileMapper.h"
22 #include "odb_api/InMemoryDataHandle.h"
23 #include "odb_api/Partition.h"
24 #include "odb_api/Writer.h"
25 #include "odb_api/Reader.h"
26 #include "odb_api/Retriever.h"
27 
28 using namespace std;
29 using namespace eckit;
30 using namespace odc;
31 using namespace odc::tool;
32 
33 void Retriever::checkKeywordsHaveValues(const std::map<std::string,std::vector<std::string> >& request, const vector<string>& keywords)
34 {
35  typedef std::map<std::string,std::vector<std::string> > ms;
36  ms r;
37  for (ms::const_iterator it (request.begin()); it != request.end(); ++it)
38  r[eckit::StringTools::lower(it->first)] = it->second;
39 
40  for (size_t i (0); i < keywords.size(); ++i)
41  {
42  const vector<string>& values ( r[ keywords[i] ]);
43  if (values.size() < 1)
44  throw eckit::UserError( "At least one value required for keyword '" + keywords[i] + "'");
45  Log::info() << ":: - " << keywords[i] << " " << values << endl;
46  }
47 }
48 
49 void Retriever::retrieve(MultiHandle& output,
50  const std::vector<std::string>& keywords,
51  const std::map<std::string,std::vector<std::string> >& r)
52 {
53  std::map<std::string,std::vector<std::string> > request(unquoteValues(r));
54 
55  if (r.count("odbpathnameschema") == 0)
56  throw UserError("RETRIEVE: odbpathnameschema not set");
57 
58  FileMapper mapper(r.at("odbpathnameschema")[0]);
59 
60  vector<string> odbServerRoots (eckit::StringTools::split(":", r.at("odbserverroots")[0]));
61  for (size_t i(0); i < odbServerRoots.size(); ++i)
62  odbServerRoots[i] = FileCollector::expandTilde(odbServerRoots[i]);
63  mapper.addRoots(odbServerRoots);
64  mapper.checkRoots();
65 
66  checkKeywordsHaveValues(request, keywords);
67 
68  const vector<string> partitionNumbers ( request["part_number"] );
69 
70  if (partitionNumbers.size())
71  {
72  const string partitionsInfo (FileCollector::expandTilde(request["partitionsinfo"][0]));
73 
74  vector<size_t> parts;
75  for (size_t i(0); i < partitionNumbers.size(); ++i)
76  parts.push_back( atoll(partitionNumbers[i].c_str()) );
77 
78  sendPartitions (output, PathName(partitionsInfo), parts);
79  //sendSavedPartitions (output, PathName(partitionsInfo + ".files"), parts);
80  }
81  else
82  {
83  // Check server_side
84  vector<string> serverSide (request ["server_side"]);
85  if (serverSide.size())
86  {
87  Log::error() << "ODB API server side no longer exists" << endl;
88  throw UserError("ODB API server side no longer exists");
89  }
90  else
91  {
92  Log::debug() << "No server side processing" << endl;
93  FileCollector fileCollector (mapper, output);
94  fileCollector.findFiles(keywords, request);
95 
96  if(output.estimate() == Length(0))
97  Log::userWarning() << "Data not found" << endl;
98  }
99  }
100 }
101 
102 void Retriever::sendSavedPartitions(MultiHandle& output, const PathName& savedPartitionsListFile, const std::vector<size_t>& partitionNumbers)
103 {
104  vector<std::string> files (StringTool::readLines(savedPartitionsListFile, true));
105  for (size_t i (0); i < files.size(); ++i)
106  {
107  if (std::find(partitionNumbers.begin(), partitionNumbers.end(), i) != partitionNumbers.end())
108  output += PathName(files[i]).fileHandle();
109  }
110 }
111 
112 void Retriever::sendPartitions(MultiHandle& output, const PathName& partitionsInfo, const std::vector<size_t>& partitionNumbers)
113 {
114  for (size_t i(0); i < partitionNumbers.size(); ++i)
115  {
116  Partition partition (partitionsInfo, partitionNumbers[i]);
117 
118  InMemoryDataHandle* dh (new InMemoryDataHandle);
119  dh->openForWrite(0);
120 
121  partition.write(*dh);
122 
123  output += dh;
124  }
125 }
126 
127 
128 std::map<std::string,std::vector<std::string> > Retriever::unquoteValues(const std::map<std::string,std::vector<std::string> >& request)
129 {
130  std::map<std::string,std::vector<std::string> > r;
131 
132  for ( std::map<std::string,std::vector<std::string> >::const_iterator it (request.begin()); it != request.end(); ++it)
133  {
134  const std::string& key (it->first);
135  const std::vector<std::string>& values (it->second);
136  std::vector<std::string> vs;
137  for (size_t i(0); i < values.size(); ++i)
138  vs.push_back(StringTool::unQuote(values[i]));
139  r[key] = vs;
140  }
141 
142  return r;
143 }
static std::string expandTilde(const std::string &s)
Definition: ColumnInfo.h:23
void checkKeywordsHaveValues(const RequestDict &request, const std::vector< std::string > &keywords)
Definition: RequestUtils.cc:38
Definition: encode.cc:30