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 #ifndef TENSORFLOW_CONTRIB_BIGTABLE_KERNELS_BIGTABLE_RANGE_HELPERS_H_ 17 #define TENSORFLOW_CONTRIB_BIGTABLE_KERNELS_BIGTABLE_RANGE_HELPERS_H_ 18 19 #include <string> 20 21 #include "tensorflow/core/lib/core/stringpiece.h" 22 #include "tensorflow/core/platform/types.h" 23 24 namespace tensorflow { 25 26 // Represents a continuous range of keys defined by either a prefix or a range. 27 // 28 // Ranges are represented as "half-open", where the beginning key is included 29 // in the range, and the end_key is the first excluded key after the range. 30 // 31 // The range of keys can be specified either by a key prefix, or by an explicit 32 // begin key and end key. All methods on this class are valid no matter which 33 // way the range was specified. 34 // 35 // Example: 36 // MultiModeKeyRange range = MultiModeKeyRange::FromPrefix("myPrefix"); 37 // if (range.contains_key("myPrefixedKey")) { 38 // LOG(INFO) << "range from " << range.begin_key() << " to " 39 // << range.end_key() << "contains \"myPrefixedKey\""; 40 // } 41 // if (!range.contains_key("randomKey")) { 42 // LOG(INFO) << "range does not contain \"randomKey\""; 43 // } 44 // range = MultiModeKeyRange::FromRange("a_start_key", "z_end_key"); 45 class MultiModeKeyRange { 46 public: 47 static MultiModeKeyRange FromPrefix(string prefix); 48 static MultiModeKeyRange FromRange(string begin, string end); 49 50 // The first valid key in the range. 51 const string& begin_key() const; 52 // The first invalid key after the valid range. 53 const string& end_key() const; 54 // Returns true if the provided key is a part of the range, false otherwise. 55 bool contains_key(StringPiece key) const; 56 57 private: MultiModeKeyRange(string begin,string end)58 MultiModeKeyRange(string begin, string end) 59 : begin_(std::move(begin)), end_(std::move(end)) {} 60 61 const string begin_; 62 const string end_; 63 }; 64 65 } // namespace tensorflow 66 67 #endif // TENSORFLOW_CONTRIB_BIGTABLE_KERNELS_BIGTABLE_RANGE_HELPERS_H_ 68