Skip to content

Commit

Permalink
go translator and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentyn Kahamlyk authored and Valentyn Kahamlyk committed Aug 23, 2023
1 parent 0688db6 commit 4c06920
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 689 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
public final class GolangTranslator implements Translator.ScriptTranslator {
private final String traversalSource;
private final TypeTranslator typeTranslator;
private final static String GO_PACKAGE_NAME = "gremlingo.";
private final static String GO_PACKAGE_NAME = "gremlingo.";

private GolangTranslator(final String traversalSource, final TypeTranslator typeTranslator) {
this.traversalSource = traversalSource;
Expand Down Expand Up @@ -133,14 +133,15 @@ protected String getSyntax(final String o) {
protected String getSyntax(final Boolean o) {
return o.toString();
}

@Override
protected String getSyntax(final Date o) {
return "time.Unix(" + o.getTime() + ", 0)";
return "time.UnixMilli(" + o.getTime() + ")";

Check warning on line 139 in gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/translator/GolangTranslator.java

View check run for this annotation

Codecov / codecov/patch

gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/translator/GolangTranslator.java#L139

Added line #L139 was not covered by tests
}

@Override
protected String getSyntax(final Timestamp o) {
return "time.Unix(" + o.getTime() + ", 0)";
return "time.UnixMilli(" + o.getTime() + ")";

Check warning on line 144 in gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/translator/GolangTranslator.java

View check run for this annotation

Codecov / codecov/patch

gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/translator/GolangTranslator.java#L144

Added line #L144 was not covered by tests
}

@Override
Expand Down
8 changes: 7 additions & 1 deletion gremlin-go/build/generate.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ radishGremlinFile.withWriter('UTF-8') { Writer writer ->
'\n' +
'import (\n' +
'\t \"errors\"\n' +
'\t \"time\"\n' +
'\t \"math\"\n' +
'\t \"github.com/apache/tinkerpop/gremlin-go/v3/driver\"\n' +
')\n'
Expand All @@ -108,7 +109,12 @@ radishGremlinFile.withWriter('UTF-8') { Writer writer ->
// solution may become necessary as testing of nulls expands.
def staticTranslate = [
g_injectXnull_nullX: " \"g_injectXnull_nullX\": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(nil, nil)}}, ",
g_VX1X_valuesXageX_injectXnull_nullX: " \"g_VX1X_valuesXageX_injectXnull_nullX\": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V(p[\"xx1\"]).Values(\"age\").Inject(nil, nil)}}, "
g_VX1X_valuesXageX_injectXnull_nullX: " \"g_VX1X_valuesXageX_injectXnull_nullX\": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V(p[\"xx1\"]).Values(\"age\").Inject(nil, nil)}}, ",
g_injectXdatetimeXstrXX_dateAddXDT_hour_2X: " \"g_injectXdatetimeXstrXX_dateAddXDT_hour_2X\": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(time.UnixMilli(1690934400000)).DateAdd(gremlingo.DT.Hour, int32(2))}}, ",
g_injectXdatetimeXstrXX_dateAddXhour_2X: " \"g_injectXdatetimeXstrXX_dateAddXhour_2X\": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(time.UnixMilli(1690934400000)).DateAdd(gremlingo.DT.Hour, int32(2))}},",
g_injectXdatetimeXstrXX_dateAddXhour_1X: " \"g_injectXdatetimeXstrXX_dateAddXhour_1X\": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(time.UnixMilli(1690934400000)).DateAdd(gremlingo.DT.Hour, int32(-1))}},",
g_injectXdatetimeXstrXX_dateAddXminute_10X: " \"g_injectXdatetimeXstrXX_dateAddXminute_10X\": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(time.UnixMilli(1690934400000)).DateAdd(gremlingo.DT.Minute, int32(10))}},",
g_injectXdatetimeXstrXX_dateAddXsecond_20X: " \"g_injectXdatetimeXstrXX_dateAddXsecond_20X\": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(time.UnixMilli(1690934400000)).DateAdd(gremlingo.DT.Second, int32(20))}},"
]

gremlins.each { k,v ->
Expand Down
15 changes: 15 additions & 0 deletions gremlin-go/driver/anonymousTraversal.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ func (anonymousTraversal *anonymousTraversal) As(args ...interface{}) *GraphTrav
return anonymousTraversal.graphTraversal().As(args...)
}

// AsDate adds the AsDate step to the GraphTraversal.
func (anonymousTraversal *anonymousTraversal) AsDate(args ...interface{}) *GraphTraversal {
return anonymousTraversal.graphTraversal().AsDate(args...)

Check warning on line 322 in gremlin-go/driver/anonymousTraversal.go

View check run for this annotation

Codecov / codecov/patch

gremlin-go/driver/anonymousTraversal.go#L321-L322

Added lines #L321 - L322 were not covered by tests
}

// Barrier adds the barrier step to the GraphTraversal.
func (anonymousTraversal *anonymousTraversal) Barrier(args ...interface{}) *GraphTraversal {
return anonymousTraversal.graphTraversal().Barrier(args...)
Expand Down Expand Up @@ -397,6 +402,16 @@ func (anonymousTraversal *anonymousTraversal) CyclicPath(args ...interface{}) *G
return anonymousTraversal.graphTraversal().CyclicPath(args...)
}

// DateAdd adds the DateAdd step to the GraphTraversal.
func (anonymousTraversal *anonymousTraversal) DateAdd(args ...interface{}) *GraphTraversal {
return anonymousTraversal.graphTraversal().DateAdd(args...)

Check warning on line 407 in gremlin-go/driver/anonymousTraversal.go

View check run for this annotation

Codecov / codecov/patch

gremlin-go/driver/anonymousTraversal.go#L406-L407

Added lines #L406 - L407 were not covered by tests
}

// DateDiff adds the DateDiff step to the GraphTraversal.
func (anonymousTraversal *anonymousTraversal) DateDiff(args ...interface{}) *GraphTraversal {
return anonymousTraversal.graphTraversal().DateDiff(args...)

Check warning on line 412 in gremlin-go/driver/anonymousTraversal.go

View check run for this annotation

Codecov / codecov/patch

gremlin-go/driver/anonymousTraversal.go#L411-L412

Added lines #L411 - L412 were not covered by tests
}

// Dedup adds the dedup step to the GraphTraversal.
func (anonymousTraversal *anonymousTraversal) Dedup(args ...interface{}) *GraphTraversal {
return anonymousTraversal.graphTraversal().Dedup(args...)
Expand Down
12 changes: 11 additions & 1 deletion gremlin-go/driver/cucumber/cucumberSteps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"strings"
"sync"
"testing"
"time"

gremlingo "github.com/apache/tinkerpop/gremlin-go/v3/driver"
"github.com/cucumber/godog"
Expand All @@ -42,10 +43,10 @@ type tinkerPopGraph struct {
}

var parsers map[*regexp.Regexp]func(string, string) interface{}
var toListLock sync.Mutex

func init() {
parsers = map[*regexp.Regexp]func(string, string) interface{}{
regexp.MustCompile(`^dt\[(.*)]$`): toDateTime,
regexp.MustCompile(`^d\[(.*)]\.[bslfdmn]$`): toNumeric,
regexp.MustCompile(`^d\[(.*)]\.[i]$`): toInt32,
regexp.MustCompile(`^vp\[(.+)]$`): toVertexProperty,
Expand Down Expand Up @@ -103,6 +104,15 @@ func parseValue(value string, graphName string) interface{} {
}
}

// Parse dateTime.
func toDateTime(stringVal, graphName string) interface{} {
val, err := strconv.ParseInt(stringVal, 10, 64)
if err != nil {
return nil
}
return time.Unix(val, 0)
}

// Parse numeric.
func toNumeric(stringVal, graphName string) interface{} {
if strings.Contains(stringVal, ".") {
Expand Down
14 changes: 14 additions & 0 deletions gremlin-go/driver/cucumber/gremlin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package gremlingo

import (
"errors"
"time"
"math"
"github.com/apache/tinkerpop/gremlin-go/v3/driver"
)
Expand Down Expand Up @@ -440,6 +441,12 @@ var translationMap = map[string][]func(g *gremlingo.GraphTraversalSource, p map[
"g_addVXpersonX_propertyXname_joshX_propertyXage_nullX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.AddV("person").Property("name", "josh").Property("age", nil)}, func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().Has("person", "age", nil)}},
"g_addVXpersonX_propertyXname_markoX_propertyXfriendWeight_null_acl_nullX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.AddV("person").Property("name", "marko").Property("friendWeight", nil, "acl", nil)}, func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().Has("person", "name", "marko").Has("friendWeight", nil)}, func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().Has("person", "name", "marko").Properties("friendWeight").Has("acl", nil)}, func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().Has("person", "name", "marko").Properties("friendWeight").Count()}},
"g_V_hasXperson_name_aliceX_propertyXsingle_age_unionXage_constantX1XX_sumX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.AddV("person").Property("name", "alice").Property(gremlingo.Cardinality.Single, "age", 50)}, func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().Has("person", "name", "alice").Property("age", gremlingo.T__.Union(gremlingo.T__.Values("age"), gremlingo.T__.Constant(1)).Sum())}, func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().Has("person", "age", 50)}, func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().Has("person", "age", 51)}},
"g_injectXstrX_asDate": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject("2022-08-02T00:00:00Z").AsDate()}},
"g_injectX1000X_asDate": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(1000).AsDate()}},
"g_injectX2000LX_asDate": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(p["xx1"]).AsDate()}},
"g_injectX3000dX_asDate": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(p["xx1"]).AsDate()}},
"g_injectX1_2X_asDate": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(p["xx1"]).AsDate()}},
"g_injectXnullX_asDate": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(nil).AsDate()}},
"g_call": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Call()}},
"g_callXlistX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Call("--list")}},
"g_callXlistX_withXstring_stringX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Call("--list").With("service", "tinker.search")}},
Expand Down Expand Up @@ -500,6 +507,13 @@ var translationMap = map[string][]func(g *gremlingo.GraphTraversalSource, p map[
"g_E_sampleX1X_count": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.E().Sample(1).Count()}},
"g_V_sampleX1X_byXageX_count": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().Sample(1).By("age").Count()}},
"g_V_order_byXnoX_count": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().Order().By("no").Count()}},
"g_injectXdatetimeXstrXX_dateAddXDT_hour_2X": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(time.UnixMilli(1690934400000)).DateAdd(gremlingo.DT.Hour, int32(2))}},
"g_injectXdatetimeXstrXX_dateAddXhour_2X": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(time.UnixMilli(1690934400000)).DateAdd(gremlingo.DT.Hour, int32(2))}},
"g_injectXdatetimeXstrXX_dateAddXhour_1X": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(time.UnixMilli(1690934400000)).DateAdd(gremlingo.DT.Hour, int32(-1))}},
"g_injectXdatetimeXstrXX_dateAddXminute_10X": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(time.UnixMilli(1690934400000)).DateAdd(gremlingo.DT.Minute, int32(10))}},
"g_injectXdatetimeXstrXX_dateAddXsecond_20X": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(time.UnixMilli(1690934400000)).DateAdd(gremlingo.DT.Second, int32(20))}},
"g_injectXdatetimeXstr1XX_dateDiffXdatetimeXstr2XX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(time.UnixMilli(1690934400000)).DateDiff(time.UnixMilli(1691539200000))}},
"g_injectXdatetimeXstr1XX_dateDiffXinjectXdatetimeXstr2XXX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(time.UnixMilli(1691452800000)).DateDiff(gremlingo.T__.Inject(time.UnixMilli(1690848000000)))}},
"g_V_EX11X": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().E(p["eid11"])}},
"g_EX11X_E": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.E(p["eid11"]).E()}},
"g_V_EXnullX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().E(nil)}},
Expand Down
17 changes: 9 additions & 8 deletions gremlin-go/driver/graphBinary.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const (
traversalStrategyType dataType = 0x29
bulkSetType dataType = 0x2a
mergeType dataType = 0x2e
dtType dataType = 0x2f
metricsType dataType = 0x2c
traversalMetricsType dataType = 0x2d
durationType dataType = 0x81
Expand Down Expand Up @@ -224,11 +225,12 @@ func instructionWriter(instructions []instruction, buffer *bytes.Buffer, typeSer

// Format: {steps_length}{step_0}…{step_n}{sources_length}{source_0}…{source_n}
// Where:
// {steps_length} is an Int value describing the amount of steps.
// {step_i} is composed of {name}{values_length}{value_0}…{value_n}, where:
// {name} is a String. This is also known as the operator.
// {values_length} is an Int describing the amount values.
// {value_i} is a fully qualified typed value composed of {type_code}{type_info}{value_flag}{value} describing the step argument.
//
// {steps_length} is an Int value describing the amount of steps.
// {step_i} is composed of {name}{values_length}{value_0}…{value_n}, where:
// {name} is a String. This is also known as the operator.
// {values_length} is an Int describing the amount values.
// {value_i} is a fully qualified typed value composed of {type_code}{type_info}{value_flag}{value} describing the step argument.
func bytecodeWriter(value interface{}, buffer *bytes.Buffer, typeSerializer *graphBinaryTypeSerializer) ([]byte, error) {
var bc Bytecode
switch typedVal := value.(type) {
Expand Down Expand Up @@ -709,6 +711,8 @@ func (serializer *graphBinaryTypeSerializer) getType(val interface{}) (dataType,
return scopeType, nil
case merge:
return mergeType, nil
case dt:
return dtType, nil
case p, Predicate:
return pType, nil
case textP, TextPredicate:
Expand Down Expand Up @@ -988,13 +992,10 @@ func readMap(data *[]byte, i *int) (interface{}, error) {
switch reflect.TypeOf(k).Kind() {
case reflect.Map:
mapData[&k] = v
break
case reflect.Slice:
mapData[fmt.Sprint(k)] = v
break
default:
mapData[k] = v
break
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions gremlin-go/driver/graphTraversal.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ func (g *GraphTraversal) As(args ...interface{}) *GraphTraversal {
return g
}

// AsDate adds the asDate step to the GraphTraversal.
func (g *GraphTraversal) AsDate(args ...interface{}) *GraphTraversal {
g.Bytecode.AddStep("asDate", args...)
return g
}

// Barrier adds the barrier step to the GraphTraversal.
func (g *GraphTraversal) Barrier(args ...interface{}) *GraphTraversal {
// Force int32 serialization for valid number values for server compatibility
Expand Down Expand Up @@ -192,6 +198,18 @@ func (g *GraphTraversal) CyclicPath(args ...interface{}) *GraphTraversal {
return g
}

// DateAdd adds the dateAdd step to the GraphTraversal.
func (g *GraphTraversal) DateAdd(args ...interface{}) *GraphTraversal {
g.Bytecode.AddStep("dateAdd", args...)
return g
}

// DateDiff adds the dateDiff step to the GraphTraversal.
func (g *GraphTraversal) DateDiff(args ...interface{}) *GraphTraversal {
g.Bytecode.AddStep("dateDiff", args...)
return g
}

// Dedup adds the dedup step to the GraphTraversal.
func (g *GraphTraversal) Dedup(args ...interface{}) *GraphTraversal {
g.Bytecode.AddStep("dedup", args...)
Expand Down
2 changes: 2 additions & 0 deletions gremlin-go/driver/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func initSerializers() {
cardinalityType: enumWriter,
columnType: enumWriter,
directionType: enumWriter,
dtType: enumWriter,
operatorType: enumWriter,
orderType: enumWriter,
pickType: enumWriter,
Expand Down Expand Up @@ -315,6 +316,7 @@ func initDeserializers() {
bulkSetType: bulkSetReader,
tType: enumReader,
directionType: enumReader,
dtType: enumReader,
bindingType: bindingReader,

// Metrics
Expand Down
21 changes: 21 additions & 0 deletions gremlin-go/driver/traversal.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,27 @@ var MaterializeProperties = materializeProperties{
Tokens: "tokens",
}

type dt string

type dts struct {
// time period second
Second dt
// time period minute
Minute dt
// time period hour
Hour dt
// time period day
Day dt
}

// Merge is a set of operations for Vertex and Edge merging.
var DT = dts{
Second: "second",
Minute: "minute",
Hour: "hour",
Day: "day",
}

type merge string

type merges struct {
Expand Down
1 change: 1 addition & 0 deletions gremlin-python/build/generate.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ radishGremlinFile.withWriter('UTF-8') { Writer writer ->

writer.writeLine(
'from radish import world\n' +
'import datetime\n' +
'from gremlin_python.process.anonymous_traversal import traversal\n' +
'from gremlin_python.process.traversal import TraversalStrategy\n' +
'from gremlin_python.process.graph_traversal import __\n' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.
import calendar
import datetime

import json
import uuid
import math
Expand Down
Loading

0 comments on commit 4c06920

Please sign in to comment.