| 37 | }; |
| 38 | |
| 39 | void SampleNode::ComputeAsync(OpKernelContext* ctx, DoneCallback done) { |
| 40 | auto count = ctx->input(0); |
| 41 | auto node_type = ctx->input(1); |
| 42 | |
| 43 | OP_REQUIRES_ASYNC(ctx, TensorShapeUtils::IsScalar(count.shape()), |
| 44 | errors::InvalidArgument("count must be a scalar, saw shape: ", |
| 45 | count.shape().DebugString()), done); |
| 46 | |
| 47 | OP_REQUIRES_ASYNC(ctx, TensorShapeUtils::IsScalar(node_type.shape()), |
| 48 | errors::InvalidArgument("node_type must be a scalar, saw shape: ", |
| 49 | node_type.shape().DebugString()), done); |
| 50 | |
| 51 | int32_t count_value = (count.scalar<int32>())(); |
| 52 | int32_t type_value = (node_type.scalar<int32>())(); |
| 53 | |
| 54 | TensorShape output_shape; |
| 55 | output_shape.AddDim(count_value); |
| 56 | |
| 57 | Tensor* output = nullptr; |
| 58 | OP_REQUIRES_OK(ctx, ctx->allocate_output(0, output_shape, &output)); |
| 59 | |
| 60 | char buffer[4096]; |
| 61 | if (!condition_.empty()) { |
| 62 | int ret = snprintf( |
| 63 | buffer, sizeof(buffer), "sampleN(node_type, count).has(%s).as(id)", |
| 64 | condition_.c_str()); |
| 65 | if (ret < 0 || static_cast<size_t>(ret) > sizeof(buffer)) { |
| 66 | EULER_LOG(ERROR) << "Can not build query, the condition is too long," |
| 67 | << " condition: " << condition_; |
| 68 | done(); |
| 69 | return; |
| 70 | } |
| 71 | } else { |
| 72 | snprintf(buffer, sizeof(buffer), "sampleN(node_type, count).as(id)"); |
| 73 | } |
| 74 | |
| 75 | // build euler gremlin query |
| 76 | auto query = new euler::Query(buffer); |
| 77 | auto t_node_type = query->AllocInput("node_type", {}, euler::kInt32); |
| 78 | euler::Tensor* t_count = query->AllocInput("count", {}, euler::kInt32); |
| 79 | *(t_node_type->Raw<int32_t>()) = type_value; |
| 80 | *(t_count->Raw<int32_t>()) = count_value; |
| 81 | |
| 82 | auto callback = [query, output, done] () { |
| 83 | auto res = query->GetResult("id:0"); |
| 84 | auto res_data = res->Raw<uint64_t>(); |
| 85 | auto data = output->flat<int64>().data(); |
| 86 | if (res->NumElements() == 0) { |
| 87 | EULER_LOG(FATAL) << "SampleNode Result Size 0! " |
| 88 | << "Maybe caused by empty node_type or bad filter condition"; |
| 89 | } |
| 90 | std::copy(res_data, res_data + res->NumElements(), data); |
| 91 | delete query; |
| 92 | done(); |
| 93 | }; |
| 94 | |
| 95 | euler::QueryProxy::GetInstance()->RunAsyncGremlin(query, callback); |
| 96 | } |
nothing calls this directly
no test coverage detected