run apache-rat on S2GRAPH-219
Project: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/commit/73624153 Tree: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/tree/73624153 Diff: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/diff/73624153 Branch: refs/heads/master Commit: 73624153278edcee70769304a755956e22ac8bd8 Parents: 1881df0 a7ed4e8 Author: DO YUNG YOON <steams...@apache.org> Authored: Mon Jun 18 16:02:49 2018 +0900 Committer: DO YUNG YOON <steams...@apache.org> Committed: Mon Jun 18 16:02:49 2018 +0900 ---------------------------------------------------------------------- .../s2graph/core/parsers/WhereParser.scala | 10 +++++++--- .../s2graph/core/parsers/WhereParserTest.scala | 4 ++++ .../graphql/middleware/GraphFormatWriter.scala | 19 +++++++++++++++++++ .../graphql/types/PlayJsonScalarType.scala | 19 +++++++++++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/73624153/s2graphql/src/main/scala/org/apache/s2graph/graphql/middleware/GraphFormatWriter.scala ---------------------------------------------------------------------- diff --cc s2graphql/src/main/scala/org/apache/s2graph/graphql/middleware/GraphFormatWriter.scala index 91fea62,0000000..6dcc368 mode 100644,000000..100644 --- a/s2graphql/src/main/scala/org/apache/s2graph/graphql/middleware/GraphFormatWriter.scala +++ b/s2graphql/src/main/scala/org/apache/s2graph/graphql/middleware/GraphFormatWriter.scala @@@ -1,101 -1,0 +1,120 @@@ ++/* ++ * Licensed to the Apache Software Foundation (ASF) under one ++ * or more contributor license agreements. See the NOTICE file ++ * distributed with this work for additional information ++ * regarding copyright ownership. The ASF licenses this file ++ * to you under the Apache License, Version 2.0 (the ++ * "License"); you may not use this file except in compliance ++ * with the License. You may obtain a copy of the License at ++ * ++ * http://www.apache.org/licenses/LICENSE-2.0 ++ * ++ * Unless required by applicable law or agreed to in writing, ++ * software distributed under the License is distributed on an ++ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ++ * KIND, either express or implied. See the License for the ++ * specific language governing permissions and limitations ++ * under the License. ++ */ ++ +package org.apache.s2graph.graphql.middleware + +import org.apache.s2graph.core.schema.ServiceColumn +import org.apache.s2graph.core.{GraphElement, S2EdgeLike, S2VertexLike} +import org.apache.s2graph.graphql.types.PlayJsonScalarType +import org.slf4s.LoggerFactory +import play.api.libs.json._ +import sangria.execution._ +import sangria.schema.Context + + +object GraphFormatted extends Middleware[Any] with MiddlewareAfterField[Any] with MiddlewareExtension[Any] { + implicit val logger = LoggerFactory.getLogger(this.getClass) + + type QueryVal = java.util.concurrent.ConcurrentHashMap[GraphElement, Unit] + + type FieldVal = Long + + + def beforeQuery(context: MiddlewareQueryContext[Any, _, _]) = { + new java.util.concurrent.ConcurrentHashMap[GraphElement, Unit]() + } + + def afterQuery(queryVal: QueryVal, context: MiddlewareQueryContext[Any, _, _]) = () + + def toVertexId(v: S2VertexLike, c: ServiceColumn): String = { + val innerId = v.innerId.toIdString() + + s"${c.service.serviceName}.${c.columnName}.${innerId}" + } + + def toVertexJson(v: S2VertexLike, c: ServiceColumn): JsValue = { + Json.obj( + "id" -> toVertexId(v, c), + "label" -> v.innerId.toIdString() + ) + } + + def toEdgeJson(e: S2EdgeLike): JsValue = { + Json.obj( + "source" -> toVertexId(e.srcVertex, e.innerLabel.srcColumn), + "target" -> toVertexId(e.tgtVertex, e.innerLabel.tgtColumn), + "id" -> s"${toVertexId(e.srcVertex, e.innerLabel.srcColumn)}.${e.label()}.${toVertexId(e.tgtVertex, e.innerLabel.tgtColumn)}", + "label" -> e.label() + ) + } + + def afterQueryExtensions(queryVal: QueryVal, + context: MiddlewareQueryContext[Any, _, _] + ): Vector[Extension[_]] = { + + import scala.collection.JavaConverters._ + val elements = queryVal.keys().asScala.toVector + + val edges = elements.collect { case e: S2EdgeLike => e } + val vertices = elements.collect { case v: S2VertexLike => v -> v.serviceColumn } + val verticesFromEdges = edges.flatMap { e => + val label = e.innerLabel + Vector((e.srcVertex, label.srcColumn), (e.tgtVertex, label.srcColumn)) + } + + val verticesJson = (vertices ++ verticesFromEdges).map { case (v, c) => toVertexJson(v, c) }.distinct + val edgeJson = edges.map(toEdgeJson).distinct + + val jsElements = Json.obj( + "nodes" -> verticesJson, + "edges" -> edgeJson + ) + + val graph = Json.obj("graph" -> jsElements) + + /** + * nodes: [{id, label, x, y, size}, ..], + * edges: [{id, source, target, label}] + */ + implicit val iu = PlayJsonScalarType.PlayJsonInputUnmarshaller + Vector(Extension[JsValue](graph)) + } + + def beforeField(queryVal: QueryVal, mctx: MiddlewareQueryContext[Any, _, _], ctx: Context[Any, _]) = { + continue(System.currentTimeMillis()) + } + + def afterField(queryVal: QueryVal, fieldVal: FieldVal, value: Any, mctx: MiddlewareQueryContext[Any, _, _], ctx: Context[Any, _]) = { + // logger.info(s"${ctx.parentType.name}.${ctx.field.name} = ${value.getClass.getName}") + + value match { + case ls: Seq[_] => ls.foreach { + case e: GraphElement => queryVal.put(e, ()) + case _ => + } + case e: GraphElement => queryVal.put(e, ()) + case _ => + } + + None + } +} + + + http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/73624153/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/PlayJsonScalarType.scala ---------------------------------------------------------------------- diff --cc s2graphql/src/main/scala/org/apache/s2graph/graphql/types/PlayJsonScalarType.scala index 5149d36,0000000..fc7f1ce mode 100644,000000..100644 --- a/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/PlayJsonScalarType.scala +++ b/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/PlayJsonScalarType.scala @@@ -1,112 -1,0 +1,131 @@@ ++/* ++ * Licensed to the Apache Software Foundation (ASF) under one ++ * or more contributor license agreements. See the NOTICE file ++ * distributed with this work for additional information ++ * regarding copyright ownership. The ASF licenses this file ++ * to you under the Apache License, Version 2.0 (the ++ * "License"); you may not use this file except in compliance ++ * with the License. You may obtain a copy of the License at ++ * ++ * http://www.apache.org/licenses/LICENSE-2.0 ++ * ++ * Unless required by applicable law or agreed to in writing, ++ * software distributed under the License is distributed on an ++ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ++ * KIND, either express or implied. See the License for the ++ * specific language governing permissions and limitations ++ * under the License. ++ */ ++ +package org.apache.s2graph.graphql.types + +import play.api.libs.json._ +import sangria.ast +import sangria.execution.Executor +import sangria.marshalling.{ArrayMapBuilder, InputUnmarshaller, ResultMarshaller, ScalarValueInfo} +import sangria.schema._ +import sangria.validation.{BigIntCoercionViolation, IntCoercionViolation, ValueCoercionViolation} +import sangria.macros._ + +import scala.concurrent.ExecutionContext.Implicits.global + +object PlayJsonScalarType { + + implicit object CustomPlayJsonResultMarshaller extends ResultMarshaller { + type Node = JsValue + type MapBuilder = ArrayMapBuilder[Node] + + def emptyMapNode(keys: Seq[String]) = new ArrayMapBuilder[Node](keys) + + def addMapNodeElem(builder: MapBuilder, key: String, value: Node, optional: Boolean) = builder.add(key, value) + + def mapNode(builder: MapBuilder) = JsObject(builder.toMap) + + def mapNode(keyValues: Seq[(String, JsValue)]) = Json.toJson(keyValues.toMap) + + def arrayNode(values: Vector[JsValue]) = JsArray(values) + + def optionalArrayNodeValue(value: Option[JsValue]) = value match { + case Some(v) â v + case None â nullNode + } + + def scalarNode(value: Any, typeName: String, info: Set[ScalarValueInfo]) = value match { + case v: String â JsString(v) + case v: Boolean â JsBoolean(v) + case v: Int â JsNumber(v) + case v: Long â JsNumber(v) + case v: Float â JsNumber(BigDecimal(v)) + case v: Double â JsNumber(v) + case v: BigInt â JsNumber(BigDecimal(v)) + case v: BigDecimal â JsNumber(v) + case v: JsValue â v + case v â throw new IllegalArgumentException("Unsupported scalar value: " + v) + } + + def enumNode(value: String, typeName: String) = JsString(value) + + def nullNode = JsNull + + def renderCompact(node: JsValue) = Json.stringify(node) + + def renderPretty(node: JsValue) = Json.prettyPrint(node) + } + + implicit object PlayJsonInputUnmarshaller extends InputUnmarshaller[JsValue] { + def getRootMapValue(node: JsValue, key: String) = node.asInstanceOf[JsObject].value get key + + def isListNode(node: JsValue) = node.isInstanceOf[JsArray] + + def getListValue(node: JsValue) = node.asInstanceOf[JsArray].value + + def isMapNode(node: JsValue) = node.isInstanceOf[JsObject] + + def getMapValue(node: JsValue, key: String) = node.asInstanceOf[JsObject].value get key + + def getMapKeys(node: JsValue) = node.asInstanceOf[JsObject].fields.map(_._1) + + def isDefined(node: JsValue) = node != JsNull + + def getScalarValue(node: JsValue) = node match { + case JsBoolean(b) â b + case JsNumber(d) â d.toBigIntExact getOrElse d + case JsString(s) â s + case n â n + } + + def getScalaScalarValue(node: JsValue) = getScalarValue(node) + + def isEnumNode(node: JsValue) = node.isInstanceOf[JsString] + + def isScalarNode(node: JsValue) = true + + def isVariableNode(node: JsValue) = false + + def getVariableName(node: JsValue) = throw new IllegalArgumentException("variables are not supported") + + def render(node: JsValue) = Json.stringify(node) + } + + case object JsonCoercionViolation extends ValueCoercionViolation("Not valid JSON") + + implicit val JsonType = ScalarType[JsValue]("Json", + description = Some("Raw PlayJson value"), + coerceOutput = (value, _) â value, + coerceUserInput = { + case v: String â Right(JsString(v)) + case v: Boolean â Right(JsBoolean(v)) + case v: Int â Right(JsNumber(v)) + case v: Long â Right(JsNumber(v)) + case v: Float â Right(JsNumber(BigDecimal(v))) + case v: Double â Right(JsNumber(v)) + case v: BigInt â Right(JsNumber(BigDecimal(v))) + case v: BigDecimal â Right(JsNumber(v)) + case v: JsValue â Right(v) + }, + coerceInput = { + case sv: ast.StringValue => Right(Json.parse(sv.value)) + case _ â + Left(JsonCoercionViolation) + }) +}