1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
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 
16 #include "tensorflow/contrib/bigtable/kernels/bigtable_range_helpers.h"
17 
18 #include "tensorflow/core/platform/logging.h"
19 
20 namespace tensorflow {
21 
22 namespace {
23 
MakePrefixEndKey(const string & prefix)24 string MakePrefixEndKey(const string& prefix) {
25   string end = prefix;
26   while (true) {
27     if (end.empty()) {
28       return end;
29     }
30     ++end[end.size() - 1];
31     if (end[end.size() - 1] == 0) {
32       // Handle wraparound case.
33       end = end.substr(0, end.size() - 1);
34     } else {
35       return end;
36     }
37   }
38 }
39 
40 }  // namespace
41 
FromPrefix(string prefix)42 /* static */ MultiModeKeyRange MultiModeKeyRange::FromPrefix(string prefix) {
43   string end = MakePrefixEndKey(prefix);
44   VLOG(1) << "Creating MultiModeKeyRange from Prefix: " << prefix
45           << ", with end key: " << end;
46   return MultiModeKeyRange(std::move(prefix), std::move(end));
47 }
48 
FromRange(string begin,string end)49 /* static */ MultiModeKeyRange MultiModeKeyRange::FromRange(string begin,
50                                                             string end) {
51   return MultiModeKeyRange(std::move(begin), std::move(end));
52 }
53 
begin_key() const54 const string& MultiModeKeyRange::begin_key() const { return begin_; }
55 
end_key() const56 const string& MultiModeKeyRange::end_key() const { return end_; }
57 
contains_key(StringPiece key) const58 bool MultiModeKeyRange::contains_key(StringPiece key) const {
59   if (StringPiece(begin_) > key) {
60     return false;
61   }
62   if (StringPiece(end_) <= key && !end_.empty()) {
63     return false;
64   }
65   return true;
66 }
67 
68 }  // namespace tensorflow
69