Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix syntax error for edge id with comma #91

Merged
merged 3 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = ???
}