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_lib.h" 17 #include "tensorflow/core/framework/op_kernel.h" 18 19 namespace tensorflow { 20 namespace data { 21 namespace { 22 23 class BigtablePrefixKeyDatasetOp : public DatasetOpKernel { 24 public: 25 using DatasetOpKernel::DatasetOpKernel; 26 MakeDataset(OpKernelContext * ctx,DatasetBase ** output)27 void MakeDataset(OpKernelContext* ctx, DatasetBase** output) override { 28 string prefix; 29 OP_REQUIRES_OK(ctx, ParseScalarArgument<string>(ctx, "prefix", &prefix)); 30 31 BigtableTableResource* resource; 32 OP_REQUIRES_OK(ctx, 33 LookupResource(ctx, HandleFromInput(ctx, 0), &resource)); 34 core::ScopedUnref scoped_unref(resource); 35 36 *output = new Dataset(ctx, resource, std::move(prefix)); 37 } 38 39 private: 40 class Dataset : public DatasetBase { 41 public: Dataset(OpKernelContext * ctx,BigtableTableResource * table,string prefix)42 explicit Dataset(OpKernelContext* ctx, BigtableTableResource* table, 43 string prefix) 44 : DatasetBase(DatasetContext(ctx)), 45 table_(table), 46 prefix_(std::move(prefix)) { 47 table_->Ref(); 48 } 49 ~Dataset()50 ~Dataset() override { table_->Unref(); } 51 MakeIteratorInternal(const string & prefix) const52 std::unique_ptr<IteratorBase> MakeIteratorInternal( 53 const string& prefix) const override { 54 return std::unique_ptr<IteratorBase>( 55 new Iterator({this, strings::StrCat(prefix, "::BigtablePrefixKey")})); 56 } 57 output_dtypes() const58 const DataTypeVector& output_dtypes() const override { 59 static DataTypeVector* dtypes = new DataTypeVector({DT_STRING}); 60 return *dtypes; 61 } 62 output_shapes() const63 const std::vector<PartialTensorShape>& output_shapes() const override { 64 static std::vector<PartialTensorShape>* shapes = 65 new std::vector<PartialTensorShape>({{}}); 66 return *shapes; 67 } 68 DebugString() const69 string DebugString() const override { 70 return "BigtablePrefixKeyDatasetOp::Dataset"; 71 } 72 table() const73 BigtableTableResource* table() const { return table_; } 74 75 protected: AsGraphDefInternal(SerializationContext * ctx,DatasetGraphDefBuilder * b,Node ** output) const76 Status AsGraphDefInternal(SerializationContext* ctx, 77 DatasetGraphDefBuilder* b, 78 Node** output) const override { 79 return errors::Unimplemented("%s does not support serialization", 80 DebugString()); 81 } 82 83 private: 84 class Iterator : public BigtableReaderDatasetIterator<Dataset> { 85 public: Iterator(const Params & params)86 explicit Iterator(const Params& params) 87 : BigtableReaderDatasetIterator<Dataset>(params) {} 88 MakeRowRange()89 ::google::cloud::bigtable::RowRange MakeRowRange() override { 90 return ::google::cloud::bigtable::RowRange::Prefix(dataset()->prefix_); 91 } MakeFilter()92 ::google::cloud::bigtable::Filter MakeFilter() override { 93 return ::google::cloud::bigtable::Filter::Chain( 94 ::google::cloud::bigtable::Filter::CellsRowLimit(1), 95 ::google::cloud::bigtable::Filter::StripValueTransformer()); 96 } ParseRow(IteratorContext * ctx,const::google::cloud::bigtable::Row & row,std::vector<Tensor> * out_tensors)97 Status ParseRow(IteratorContext* ctx, 98 const ::google::cloud::bigtable::Row& row, 99 std::vector<Tensor>* out_tensors) override { 100 Tensor output_tensor(ctx->allocator({}), DT_STRING, {}); 101 output_tensor.scalar<string>()() = string(row.row_key()); 102 out_tensors->emplace_back(std::move(output_tensor)); 103 return Status::OK(); 104 } 105 }; 106 107 BigtableTableResource* const table_; 108 const string prefix_; 109 }; 110 }; 111 112 REGISTER_KERNEL_BUILDER(Name("BigtablePrefixKeyDataset").Device(DEVICE_CPU), 113 BigtablePrefixKeyDatasetOp); 114 115 } // namespace 116 } // namespace data 117 } // namespace tensorflow 118