Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
parse-util.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "util/parse-util.h"
16 #include "util/mem-info.h"
17 #include "util/string-parser.h"
18 
19 #include "common/names.h"
20 
21 namespace impala {
22 
23 int64_t ParseUtil::ParseMemSpec(const string& mem_spec_str, bool* is_percent,
24  int64_t relative_reference) {
25  if (mem_spec_str.empty()) return 0;
26 
27  *is_percent = false;
28  int64_t multiplier = -1;
29  int32_t number_str_len = mem_spec_str.size();
30 
31  // Look for an accepted suffix such as "MB", "M", or "%".
32  string::const_reverse_iterator suffix_char = mem_spec_str.rbegin();
33  if (*suffix_char == 'b' || *suffix_char == 'B') {
34  // Skip "B", the default is bytes anyways.
35  if (suffix_char == mem_spec_str.rend()) return -1;
36  suffix_char++;
37  number_str_len--;
38  }
39  switch (*suffix_char) {
40  case 'g':
41  case 'G':
42  // Gigabytes.
43  number_str_len--;
44  multiplier = 1024L * 1024L * 1024L;
45  break;
46  case 'm':
47  case 'M':
48  // Megabytes.
49  number_str_len--;
50  multiplier = 1024L * 1024L;
51  break;
52  case '%':
53  // Don't allow a suffix of "%B".
54  if (suffix_char != mem_spec_str.rbegin()) return -1;
55  number_str_len--;
56  *is_percent = true;
57  break;
58  // The default is bytes. If there was a trailing "B" it was handled above.
59  }
60 
62  int64_t bytes;
63  if (multiplier != -1) {
64  // Parse float - MB or GB
65  double limit_val = StringParser::StringToFloat<double>(mem_spec_str.data(),
66  number_str_len, &result);
67  if (result != StringParser::PARSE_SUCCESS) return -1;
68  bytes = multiplier * limit_val;
69  } else {
70  // Parse int - bytes or percent
71  int64_t limit_val = StringParser::StringToInt<int64_t>(mem_spec_str.data(),
72  number_str_len, &result);
73  if (result != StringParser::PARSE_SUCCESS) return -1;
74 
75  if (*is_percent) {
76  bytes = (static_cast<double>(limit_val) / 100.0) * relative_reference;
77  } else {
78  bytes = limit_val;
79  }
80  }
81  // Accept -1 as indicator for infinite memory that we report by a 0 return value.
82  if (bytes == -1) {
83  return 0;
84  }
85 
86  return bytes;
87 }
88 
89 }
static int64_t ParseMemSpec(const std::string &mem_spec_str, bool *is_percent, int64_t relative_reference)
Definition: parse-util.cc:23