Skip to content

Commit 853138c

Browse files
committed
GremlinQuery is used in NodeBase. YTDBMultiEntityIterable replaced by Gremlin equivalent
1 parent 1249571 commit 853138c

14 files changed

Lines changed: 362 additions & 244 deletions

File tree

entity-store/src/main/kotlin/jetbrains/exodus/entitystore/youtrackdb/YTDBGremlinStoreTransactionImpl.kt

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ import com.jetbrains.youtrack.db.internal.core.metadata.sequence.DBSequence
2929
import com.jetbrains.youtrack.db.internal.core.tx.FrontendTransaction
3030
import jetbrains.exodus.entitystore.*
3131
import jetbrains.exodus.entitystore.youtrackdb.YTDBVertexEntity.Companion.LOCAL_ENTITY_ID_PROPERTY_NAME
32-
import jetbrains.exodus.entitystore.youtrackdb.gremlin.GremlinEntityIterable
3332
import jetbrains.exodus.entitystore.youtrackdb.gremlin.GremlinBlock
3433
import jetbrains.exodus.entitystore.youtrackdb.gremlin.GremlinBlock.SortDirection
34+
import jetbrains.exodus.entitystore.youtrackdb.gremlin.GremlinEntityIterable
3535
import jetbrains.exodus.entitystore.youtrackdb.gremlin.GremlinEntityIterableImpl
3636
import jetbrains.exodus.entitystore.youtrackdb.gremlin.GremlinQuery
37-
import jetbrains.exodus.entitystore.youtrackdb.iterate.link.*
38-
import jetbrains.exodus.entitystore.youtrackdb.iterate.property.*
37+
import jetbrains.exodus.entitystore.youtrackdb.iterate.property.YTDBSequenceImpl
3938
import jetbrains.exodus.entitystore.youtrackdb.query.YTDBQueryCancellingPolicy
4039
import jetbrains.exodus.env.ReadonlyTransactionException
4140
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource
@@ -275,10 +274,12 @@ class YTDBGremlinStoreTransactionImpl(
275274
return GremlinEntityIterable.where(entityType, this, GremlinBlock.All)
276275
}
277276

278-
// todo: remove YTDBMultipleEntitiesIterable
279277
override fun getSingletonIterable(entity: Entity): EntityIterable {
280278
requireActiveTransaction()
281-
return YTDBMultipleEntitiesIterable(this, listOf(entity))
279+
return GremlinEntityIterable.query(
280+
this, GremlinQuery.all
281+
.then(GremlinBlock.IdEqual((entity.id as YTDBEntityId).asOId()))
282+
)
282283
}
283284

284285
override fun find(
@@ -346,11 +347,13 @@ class YTDBGremlinStoreTransactionImpl(
346347
propertyName: String
347348
): EntityIterable {
348349
requireActiveTransaction()
349-
return GremlinEntityIterable.where(
350-
entityType, this,
351-
GremlinBlock.PropNotNull(propertyName)
350+
return GremlinEntityIterable.query(
351+
this,
352+
GremlinQuery.all
353+
.then(GremlinBlock.HasLabel(entityType))
354+
.then(GremlinBlock.PropNotNull(propertyName))
352355
// todo: move SortBy out of Condition
353-
.andThen(GremlinBlock.SortBy(propertyName, SortDirection.ASC))
356+
.then(GremlinBlock.Sort(GremlinBlock.Sort.ByProp(propertyName), SortDirection.ASC))
354357
)
355358
}
356359

@@ -364,9 +367,9 @@ class YTDBGremlinStoreTransactionImpl(
364367
return GremlinEntityIterable.query(
365368
this,
366369
GremlinQuery.all
367-
.andThen(GremlinBlock.PropEqual(LOCAL_ENTITY_ID_PROPERTY_NAME, entity.id.localId))
368-
.andThen(GremlinBlock.InLink(linkName))
369-
.andThen(GremlinBlock.HasLabel(entityType))
370+
.then(GremlinBlock.PropEqual(LOCAL_ENTITY_ID_PROPERTY_NAME, entity.id.localId))
371+
.then(GremlinBlock.InLink(linkName))
372+
.then(GremlinBlock.HasLabel(entityType))
370373
)
371374
}
372375

@@ -380,15 +383,14 @@ class YTDBGremlinStoreTransactionImpl(
380383
return GremlinEntityIterable.query(
381384
this,
382385
entityQuery
383-
.andThen(GremlinBlock.InLink(linkName))
384-
.andThen(GremlinBlock.HasLabel(entityType))
386+
.then(GremlinBlock.InLink(linkName))
387+
.then(GremlinBlock.HasLabel(entityType))
385388
)
386389
}
387390

388-
// todo:
389391
override fun findWithLinks(entityType: String, linkName: String): EntityIterable {
390392
requireActiveTransaction()
391-
return YTDBLinkExistsEntityIterable(this, entityType, linkName)
393+
return GremlinEntityIterable.where(entityType, this, GremlinBlock.HasLink(linkName))
392394
}
393395

394396
override fun findWithLinks(
@@ -398,7 +400,7 @@ class YTDBGremlinStoreTransactionImpl(
398400
oppositeLinkName: String
399401
): EntityIterable {
400402
requireActiveTransaction()
401-
return YTDBLinkExistsEntityIterable(this, entityType, linkName)
403+
return GremlinEntityIterable.where(entityType, this, GremlinBlock.HasLink(linkName))
402404
}
403405

404406
override fun sort(
@@ -407,7 +409,17 @@ class YTDBGremlinStoreTransactionImpl(
407409
ascending: Boolean
408410
): EntityIterable {
409411
requireActiveTransaction()
410-
return YTDBPropertySortedIterable(this, entityType, propertyName, null, ascending)
412+
return GremlinEntityIterable.query(
413+
this,
414+
GremlinQuery.all
415+
.then(GremlinBlock.HasLabel(entityType))
416+
.then(
417+
GremlinBlock.Sort(
418+
GremlinBlock.Sort.ByProp(propertyName),
419+
if (ascending) SortDirection.ASC else SortDirection.DESC
420+
)
421+
)
422+
)
411423
}
412424

413425
override fun sort(
@@ -417,12 +429,15 @@ class YTDBGremlinStoreTransactionImpl(
417429
ascending: Boolean
418430
): EntityIterable {
419431
requireActiveTransaction()
420-
return YTDBPropertySortedIterable(
432+
return GremlinEntityIterableImpl(
421433
this,
422-
entityType,
423-
propertyName,
424-
rightOrder.asOQueryIterable(),
425-
ascending
434+
rightOrder.asGremlinIterable().query
435+
.then(
436+
GremlinBlock.Sort(
437+
GremlinBlock.Sort.ByProp(propertyName),
438+
if (ascending) SortDirection.ASC else SortDirection.DESC
439+
)
440+
)
426441
)
427442
}
428443

@@ -434,11 +449,12 @@ class YTDBGremlinStoreTransactionImpl(
434449
rightOrder: EntityIterable
435450
): EntityIterable {
436451
requireActiveTransaction()
437-
return YTDBLinkSortEntityIterable(
452+
return GremlinEntityIterable.query(
438453
this,
439-
sortedLinks.asOQueryIterable(),
440-
linkName,
441-
rightOrder.asOQueryIterable()
454+
sortedLinks.asGremlinIterable().query
455+
.then(GremlinBlock.InLink(linkName))
456+
.intersect(rightOrder.asGremlinIterable().query)
457+
.then(GremlinBlock.HasLabel(entityType))
442458
)
443459
}
444460

@@ -452,12 +468,14 @@ class YTDBGremlinStoreTransactionImpl(
452468
oppositeLinkName: String
453469
): EntityIterable {
454470
requireActiveTransaction()
471+
// todo: check if we need this
455472
// Not sure about skipping oppositeEntityType and oppositeLinkName values
456-
return YTDBLinkSortEntityIterable(
473+
return GremlinEntityIterable.query(
457474
this,
458-
sortedLinks.asOQueryIterable(),
459-
linkName,
460-
rightOrder.asOQueryIterable()
475+
sortedLinks.asGremlinIterable().query
476+
.then(GremlinBlock.InLink(linkName))
477+
.intersect(rightOrder.asGremlinIterable().query)
478+
.then(GremlinBlock.HasLabel(entityType))
461479
)
462480
}
463481

entity-store/src/main/kotlin/jetbrains/exodus/entitystore/youtrackdb/gremlin/GremlinEntityIterable.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import jetbrains.exodus.entitystore.asGremlinIterable
2323
import jetbrains.exodus.entitystore.youtrackdb.YTDBEntityId
2424
import jetbrains.exodus.entitystore.youtrackdb.YTDBEntityStore
2525
import jetbrains.exodus.entitystore.youtrackdb.YTDBStoreTransaction
26+
import org.apache.tinkerpop.gremlin.process.traversal.P
2627
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal
28+
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__
2729

2830
interface GremlinEntityIterable : EntityIterable {
2931

@@ -33,8 +35,8 @@ interface GremlinEntityIterable : EntityIterable {
3335
query(
3436
tx,
3537
GremlinQuery.all
36-
.andThen(condition)
37-
.andThen(GremlinBlock.HasLabel(entityType))
38+
.then(condition)
39+
.then(GremlinBlock.HasLabel(entityType))
3840
)
3941

4042
@JvmStatic
@@ -59,7 +61,7 @@ class GremlinEntityIterableImpl(
5961
private var cachedSize: Long = -1
6062

6163
private fun modify(block: GremlinBlock): GremlinEntityIterableImpl =
62-
GremlinEntityIterableImpl(tx, this.query.andThen(block))
64+
GremlinEntityIterableImpl(tx, this.query.then(block))
6365

6466
private fun iterator(traversal: GraphTraversal<*, YTDBVertex>): GremlinEntityIterator =
6567
GremlinEntityIterator(
@@ -184,7 +186,7 @@ class GremlinEntityIterableImpl(
184186
entities
185187
.asGremlinIterable()
186188
.query
187-
.andThen(GremlinBlock.InLink(linkName))
189+
.then(GremlinBlock.InLink(linkName))
188190
)
189191
.distinct()
190192
}

0 commit comments

Comments
 (0)