diff --git a/exchange-common/src/main/scala/com/vesoft/exchange/common/writer/ServerBaseWriter.scala b/exchange-common/src/main/scala/com/vesoft/exchange/common/writer/ServerBaseWriter.scala index 2638c37a..19fe4b4c 100644 --- a/exchange-common/src/main/scala/com/vesoft/exchange/common/writer/ServerBaseWriter.scala +++ b/exchange-common/src/main/scala/com/vesoft/exchange/common/writer/ServerBaseWriter.scala @@ -57,39 +57,35 @@ abstract class ServerBaseWriter extends Writer { def toExecuteSentence(name: String, edges: Edges): String = { val values = edges.values .map { edge => - (for (element <- edge.source.split(",")) - yield { - val source = edges.sourcePolicy match { - case Some(KeyPolicy.HASH) => - ENDPOINT_TEMPLATE.format(KeyPolicy.HASH.toString, element) - case Some(KeyPolicy.UUID) => - ENDPOINT_TEMPLATE.format(KeyPolicy.UUID.toString, element) - case None => - element - case _ => - throw new IllegalArgumentException( - s"invalidate source policy ${edges.sourcePolicy.get}") - } - - val target = edges.targetPolicy match { - case Some(KeyPolicy.HASH) => - ENDPOINT_TEMPLATE.format(KeyPolicy.HASH.toString, edge.destination) - case Some(KeyPolicy.UUID) => - ENDPOINT_TEMPLATE.format(KeyPolicy.UUID.toString, edge.destination) - case None => - edge.destination - case _ => - throw new IllegalArgumentException( - s"invalidate target policy ${edges.targetPolicy.get}") - } + val source = edges.sourcePolicy match { + case Some(KeyPolicy.HASH) => + ENDPOINT_TEMPLATE.format(KeyPolicy.HASH.toString, edge.source) + case Some(KeyPolicy.UUID) => + ENDPOINT_TEMPLATE.format(KeyPolicy.UUID.toString, edge.source) + case None => + edge.source + case _ => + throw new IllegalArgumentException( + s"invalidate source policy ${edges.sourcePolicy.get}") + } - if (edge.ranking.isEmpty) - EDGE_VALUE_WITHOUT_RANKING_TEMPLATE - .format(source, target, edge.propertyValues) - else - EDGE_VALUE_TEMPLATE.format(source, target, edge.ranking.get, edge.propertyValues) - }).mkString(", ") + val target = edges.targetPolicy match { + case Some(KeyPolicy.HASH) => + ENDPOINT_TEMPLATE.format(KeyPolicy.HASH.toString, edge.destination) + case Some(KeyPolicy.UUID) => + ENDPOINT_TEMPLATE.format(KeyPolicy.UUID.toString, edge.destination) + case None => + edge.destination + case _ => + throw new IllegalArgumentException( + s"invalidate target policy ${edges.targetPolicy.get}") + } + if (edge.ranking.isEmpty) + EDGE_VALUE_WITHOUT_RANKING_TEMPLATE + .format(source, target, edge.propertyValues) + else + EDGE_VALUE_TEMPLATE.format(source, target, edge.ranking.get, edge.propertyValues) } .mkString(", ") BATCH_INSERT_TEMPLATE.format(Type.EDGE.toString, name, edges.propertyNames, values) diff --git a/exchange-common/src/test/scala/com/vesoft/exchange/common/writer/ServerBaseWriterSuite.scala b/exchange-common/src/test/scala/com/vesoft/exchange/common/writer/ServerBaseWriterSuite.scala new file mode 100644 index 00000000..d187d57a --- /dev/null +++ b/exchange-common/src/test/scala/com/vesoft/exchange/common/writer/ServerBaseWriterSuite.scala @@ -0,0 +1,101 @@ +/* Copyright (c) 2022 vesoft inc. All rights reserved. + * + * This source code is licensed under Apache 2.0 License. + */ + +package com.vesoft.exchange.common.writer + +import com.vesoft.exchange.common +import com.vesoft.exchange.common.{Edge, Edges, Vertex, Vertices} +import org.junit.Test + +import scala.collection.mutable.ListBuffer + +class ServerBaseWriterSuite extends ServerBaseWriter { + + @Test + def toExecuteSentenceSuiteForVertex(): Unit = { + val vertices: ListBuffer[Vertex] = new ListBuffer[Vertex] + val tagName = "person" + val propNames = List("name", "age", "gender", "high", "weight") + + val props1 = List("\"Tom\"", 10, 0, 172.5, 55) + val props2 = List("\"Jena\"", 12, 1, 165.5, 45) + vertices.append(Vertex("\"vid1\"", props1)) + vertices.append(Vertex("\"vid2\"", props2)) + val nebulaVertices = Vertices(propNames, vertices.toList) + + val sentence = toExecuteSentence(tagName, nebulaVertices) + val expectSentence = + "INSERT VERTEX `person`(`name`,`age`,`gender`,`high`,`weight`) VALUES " + + "\"vid1\": (\"Tom\", 10, 0, 172.5, 55), " + + "\"vid2\": (\"Jena\", 12, 1, 165.5, 45)" + assert(sentence.equals(expectSentence)) + } + + @Test + def toExecuteSentenceSuiteForVertexWithSymbol(): Unit = { + val vertices: ListBuffer[Vertex] = new ListBuffer[Vertex] + val tagName = "person,test_with^symbol#" + val propNames = List("name_1", "age-1", "gender&1", "high%1", "weight,1") + + val props1 = List("\"Tom\"", 10, 0, 172.5, 55) + val props2 = List("\"Jena\"", 12, 1, 165.5, 45) + vertices.append(Vertex("\"vid_1\"", props1)) + vertices.append(Vertex("\"vid,2\"", props2)) + val nebulaVertices = Vertices(propNames, vertices.toList) + + val sentence = toExecuteSentence(tagName, nebulaVertices) + val expectSentence = + "INSERT VERTEX `person,test_with^symbol#`(`name_1`,`age-1`,`gender&1`,`high%1`,`weight,1`) VALUES " + + "\"vid_1\": (\"Tom\", 10, 0, 172.5, 55), " + + "\"vid,2\": (\"Jena\", 12, 1, 165.5, 45)" + assert(sentence.equals(expectSentence)) + } + + @Test + def toExecuteSentenceSuiteForEdge(): Unit = { + val edges: ListBuffer[Edge] = new ListBuffer[Edge] + val edgeType = "friend" + val propNames = List("src_name", "dst_name", "time", "address", "relation") + + val props1 = List("\"Tom\"", "\"Jena\"", "2022-08-25", "hangzhou", "friend") + val props2 = List("\"Jena\"", "\"Bob\"", "2022-08-25", "shanghai", "friend") + edges.append(Edge("\"vid1\"", "\"vid2\"", Some(0L), props1)) + edges.append(Edge("\"vid2\"", "\"vid3\"", Some(1L), props2)) + val nebulaEdges = Edges(propNames, edges.toList) + val sentence = toExecuteSentence(edgeType, nebulaEdges) + val expectSentence = "INSERT EDGE `friend`(`src_name`,`dst_name`,`time`,`address`,`relation`) VALUES" + + " \"vid1\"->\"vid2\"@0: (\"Tom\", \"Jena\", 2022-08-25, hangzhou, friend), " + + "\"vid2\"->\"vid3\"@1: (\"Jena\", \"Bob\", 2022-08-25, shanghai, friend)" + assert(sentence.equals(expectSentence)) + } + + @Test + def toExecuteSentenceSuiteForEdgeWithSymbol(): Unit = { + val edges: ListBuffer[Edge] = new ListBuffer[Edge] + val edgeType = "friend" + val propNames = List("src_name", "dst_name", "time", "address", "relation") + + val props1 = List("\"Tom\"", "\"Jena\"", "2022-08-25", "hangzhou", "friend") + val props2 = List("\"Jena\"", "\"Bob\"", "2022-08-25", "shanghai", "friend") + edges.append(Edge("\"vid_1\"", "\"vid_2\"", Some(0L), props1)) + edges.append(Edge("\"vid_2,test-1\"", "\"vid&3^test*a\"", Some(1L), props2)) + val nebulaEdges = Edges(propNames, edges.toList) + val sentence = toExecuteSentence(edgeType, nebulaEdges) + val expectSentence = "INSERT EDGE `friend`(`src_name`,`dst_name`,`time`,`address`,`relation`) VALUES " + + "\"vid_1\"->\"vid_2\"@0: (\"Tom\", \"Jena\", 2022-08-25, hangzhou, friend), " + + "\"vid_2,test-1\"->\"vid&3^test*a\"@1: (\"Jena\", \"Bob\", 2022-08-25, shanghai, friend)" + assert(sentence.equals(expectSentence)) + } + + override def writeVertices(vertices: Vertices): String = ??? + + override def writeEdges(edges: common.Edges): String = ??? + + override def writeNgql(ngql: String): String = ??? + + override def prepare(): Unit = ??? + + override def close(): Unit = ??? +}