Skip to content

Commit

Permalink
Remove DefaultIndexReference static constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd authored and ragadeeshu committed May 21, 2018
1 parent 21f8820 commit 15b1b77
Show file tree
Hide file tree
Showing 23 changed files with 97 additions and 106 deletions.
Expand Up @@ -20,16 +20,13 @@
package org.neo4j.cypher.internal.spi.v2_3

import org.neo4j.cypher.internal.compiler.v2_3.spi.SchemaTypes
import org.neo4j.internal.kernel.api.{IndexReference => KernelIndexReference}
import org.neo4j.kernel.api.schema.constaints.{NodeExistenceConstraintDescriptor, RelExistenceConstraintDescriptor, UniquenessConstraintDescriptor}
import org.neo4j.kernel.api.schema.index.{IndexDescriptorFactory, IndexDescriptor => KernelIndexDescriptor}

trait SchemaDescriptorTranslation {
implicit def cypherToKernel(index: SchemaTypes.IndexDescriptor): KernelIndexDescriptor =
IndexDescriptorFactory.forLabel(index.labelId, index.propertyId)

implicit def kernelToCypher(index: KernelIndexDescriptor): SchemaTypes.IndexDescriptor =
if (index.schema().getPropertyIds.length == 1)
SchemaTypes.IndexDescriptor(index.schema().keyId, index.schema().getPropertyId)
implicit def kernelToCypher(index: KernelIndexReference): SchemaTypes.IndexDescriptor =
if (index.properties().length == 1)
SchemaTypes.IndexDescriptor(index.label(), index.properties()(0))
else
throw new UnsupportedOperationException("Cypher 2.3 does not support composite indexes")

Expand Down
Expand Up @@ -44,7 +44,7 @@ object TransactionBoundGraphStatistics {

// Probability of any node with the given label, to have a property with a given value
val indexEntrySelectivity = schemaRead.indexUniqueValuesSelectivity(
DefaultIndexReference.general(label, property.id))
schemaRead.indexReferenceUnchecked(label, property.id))
val frequencyOfNodesWithSameValue = 1.0 / indexEntrySelectivity
val indexSelectivity = frequencyOfNodesWithSameValue / labeledNodes

Expand All @@ -60,7 +60,7 @@ object TransactionBoundGraphStatistics {

// Probability of any node with the given label, to have a given property
val indexSize = schemaRead.indexSize(
DefaultIndexReference.general(label, property.id))
schemaRead.indexReferenceUnchecked(label, property.id))
val indexSelectivity = indexSize / labeledNodes

Selectivity.of(indexSelectivity)
Expand Down
Expand Up @@ -55,7 +55,6 @@ import org.neo4j.kernel.api.exceptions.schema.{AlreadyConstrainedException, Alre
import org.neo4j.kernel.api.schema.SchemaDescriptorFactory
import org.neo4j.kernel.api.schema.constaints.ConstraintDescriptorFactory
import org.neo4j.kernel.api.{SilentTokenNameLookup, StatementConstants}
import org.neo4j.kernel.impl.api.store.DefaultIndexReference
import org.neo4j.kernel.impl.core.EmbeddedProxySPI
import org.neo4j.values.storable.Values

Expand Down Expand Up @@ -210,7 +209,7 @@ final class TransactionBoundQueryContext(tc: TransactionalContextWrapper, val re
}

def indexSeek(index: SchemaTypes.IndexDescriptor, value: Any) =
seek(DefaultIndexReference.general(index.labelId, index.propertyId),
seek(tc.schemaRead.indexReferenceUnchecked(index.labelId, index.propertyId),
IndexQuery.exact(index.propertyId, value))

def indexSeekByRange(index: SchemaTypes.IndexDescriptor, value: Any) = value match {
Expand Down Expand Up @@ -302,19 +301,19 @@ final class TransactionBoundQueryContext(tc: TransactionalContextWrapper, val re
}

private def indexSeekByPrefixRange(index: SchemaTypes.IndexDescriptor, prefix: String): scala.Iterator[Node] =
seek(DefaultIndexReference.general(index.labelId, index.propertyId), IndexQuery.stringPrefix(index.propertyId, prefix))
seek(tc.schemaRead.indexReferenceUnchecked(index.labelId, index.propertyId), IndexQuery.stringPrefix(index.propertyId, prefix))

private def indexSeekByNumericalRange(index: SchemaTypes.IndexDescriptor, range: InequalitySeekRange[Number]): scala.Iterator[Node] = (range match {
case rangeLessThan: RangeLessThan[Number] =>
rangeLessThan.limit(BY_NUMBER).map { limit =>
val rangePredicate = IndexQuery.range(index.propertyId, null, false, limit.endPoint, limit.isInclusive)
seek(DefaultIndexReference.general(index.labelId, index.propertyId), rangePredicate)
seek(tc.schemaRead.indexReferenceUnchecked(index.labelId, index.propertyId), rangePredicate)
}

case rangeGreaterThan: RangeGreaterThan[Number] =>
rangeGreaterThan.limit(BY_NUMBER).map { limit =>
val rangePredicate = IndexQuery.range(index.propertyId, limit.endPoint, limit.isInclusive, null, false)
seek(DefaultIndexReference.general(index.labelId, index.propertyId), rangePredicate)
seek(tc.schemaRead.indexReferenceUnchecked(index.labelId, index.propertyId), rangePredicate)
}

case RangeBetween(rangeGreaterThan, rangeLessThan) =>
Expand All @@ -324,7 +323,7 @@ final class TransactionBoundQueryContext(tc: TransactionalContextWrapper, val re
.range(index.propertyId, greaterThanLimit.endPoint, greaterThanLimit.isInclusive,
lessThanLimit.endPoint,
lessThanLimit.isInclusive)
seek(DefaultIndexReference.general(index.labelId, index.propertyId), rangePredicate)
seek(tc.schemaRead.indexReferenceUnchecked(index.labelId, index.propertyId), rangePredicate)
}
}
}).getOrElse(Iterator.empty)
Expand All @@ -335,14 +334,14 @@ final class TransactionBoundQueryContext(tc: TransactionalContextWrapper, val re
rangeLessThan.limit(BY_STRING).map { limit =>
val rangePredicate = IndexQuery
.range(index.propertyId, null, false, limit.endPoint.asInstanceOf[String], limit.isInclusive)
seek(DefaultIndexReference.general(index.labelId, index.propertyId), rangePredicate)
seek(tc.schemaRead.indexReferenceUnchecked(index.labelId, index.propertyId), rangePredicate)
}.getOrElse(Iterator.empty)

case rangeGreaterThan: RangeGreaterThan[String] =>
rangeGreaterThan.limit(BY_STRING).map { limit =>
val rangePredicate = IndexQuery
.range(index.propertyId, limit.endPoint.asInstanceOf[String], limit.isInclusive, null, false)
seek(DefaultIndexReference.general(index.labelId, index.propertyId), rangePredicate)
seek(tc.schemaRead.indexReferenceUnchecked(index.labelId, index.propertyId), rangePredicate)
}.getOrElse(Iterator.empty)

case RangeBetween(rangeGreaterThan, rangeLessThan) =>
Expand All @@ -351,14 +350,14 @@ final class TransactionBoundQueryContext(tc: TransactionalContextWrapper, val re
val rangePredicate = IndexQuery
.range(index.propertyId, greaterThanLimit.endPoint.asInstanceOf[String], greaterThanLimit.isInclusive,
lessThanLimit.endPoint.asInstanceOf[String], lessThanLimit.isInclusive)
seek(DefaultIndexReference.general(index.labelId, index.propertyId), rangePredicate)
seek(tc.schemaRead.indexReferenceUnchecked(index.labelId, index.propertyId), rangePredicate)
}
}.getOrElse(Iterator.empty)
}

def indexScan(index: SchemaTypes.IndexDescriptor) = {
val cursor = allocateAndTraceNodeValueIndexCursor()
reads().nodeIndexScan(DefaultIndexReference.general(index.labelId, index.propertyId), cursor, IndexOrder.NONE)
reads().nodeIndexScan(tc.schemaRead.indexReferenceUnchecked(index.labelId, index.propertyId), cursor, IndexOrder.NONE)
new CursorIterator[Node] {
override protected def fetchNext(): Node = {
if (cursor.next()) proxySpi.newNodeProxy(cursor.nodeReference())
Expand All @@ -369,7 +368,7 @@ final class TransactionBoundQueryContext(tc: TransactionalContextWrapper, val re
}

override def lockingExactUniqueIndexSearch(index: SchemaTypes.IndexDescriptor, value: Any): Option[Node] = {
val nodeId: Long = tc.dataRead.lockingNodeUniqueIndexSeek(DefaultIndexReference.general(index.labelId, index.propertyId),
val nodeId: Long = tc.dataRead.lockingNodeUniqueIndexSeek(tc.schemaRead.indexReferenceUnchecked(index.labelId, index.propertyId),
IndexQuery.exact(index.propertyId, Values.of(value)))
if (StatementConstants.NO_SUCH_NODE == nodeId) None else Some(nodeOps.getById(nodeId))
}
Expand Down Expand Up @@ -719,8 +718,7 @@ final class TransactionBoundQueryContext(tc: TransactionalContextWrapper, val re

def addIndexRule(labelId: Int, propertyKeyId: Int): IdempotentResult[SchemaTypes.IndexDescriptor] = try {
IdempotentResult(
DefaultIndexReference.toDescriptor(
tc.kernelTransaction.schemaWrite().indexCreate(SchemaDescriptorFactory.forLabel(labelId, propertyKeyId)))
tc.kernelTransaction.schemaWrite().indexCreate(SchemaDescriptorFactory.forLabel(labelId, propertyKeyId))
)
} catch {
case _: AlreadyIndexedException =>
Expand All @@ -729,11 +727,11 @@ final class TransactionBoundQueryContext(tc: TransactionalContextWrapper, val re

if (tc.schemaRead.indexGetState(index) == InternalIndexState.FAILED)
throw new FailedIndexException(index.userDescription(tokenNameLookup))
IdempotentResult(DefaultIndexReference.toDescriptor(index), wasCreated = false)
IdempotentResult(index, wasCreated = false)
}

def dropIndexRule(labelId: Int, propertyKeyId: Int) =
tc.kernelTransaction.schemaWrite().indexDrop(DefaultIndexReference.general( labelId, propertyKeyId ))
tc.kernelTransaction.schemaWrite().indexDrop(tc.schemaRead.indexReferenceUnchecked( labelId, propertyKeyId ))

def createUniqueConstraint(labelId: Int, propertyKeyId: Int): IdempotentResult[SchemaTypes.UniquenessConstraint] = try {
tc.kernelTransaction.schemaWrite().uniquePropertyConstraintCreate(
Expand Down
Expand Up @@ -20,17 +20,14 @@
package org.neo4j.cypher.internal.spi.v3_1

import org.neo4j.cypher.internal.compiler.v3_1.spi.SchemaTypes
import org.neo4j.internal.kernel.api.{IndexReference => KernelIndexReference}
import org.neo4j.internal.kernel.api.schema.SchemaDescriptor
import org.neo4j.kernel.api.schema.constaints.{ConstraintDescriptorFactory, NodeExistenceConstraintDescriptor, RelExistenceConstraintDescriptor, UniquenessConstraintDescriptor => KernelUniquenessConstraint}
import org.neo4j.kernel.api.schema.index.{IndexDescriptorFactory, IndexDescriptor => KernelIndexDescriptor}

trait SchemaDescriptorTranslation {
implicit def toKernel(index: SchemaTypes.IndexDescriptor): KernelIndexDescriptor =
IndexDescriptorFactory.forLabel(index.labelId, index.propertyId)

implicit def toCypher(index: KernelIndexDescriptor): SchemaTypes.IndexDescriptor = {
assertSingleProperty(index.schema())
SchemaTypes.IndexDescriptor(index.schema().keyId, index.schema().getPropertyId())
implicit def toCypher(index: KernelIndexReference): SchemaTypes.IndexDescriptor = {
assertSingleProperty(index.properties())
SchemaTypes.IndexDescriptor(index.label(), index.properties()(0))
}

implicit def toKernel(constraint: SchemaTypes.UniquenessConstraint): KernelUniquenessConstraint =
Expand All @@ -52,6 +49,9 @@ trait SchemaDescriptorTranslation {
}

def assertSingleProperty(schema: SchemaDescriptor):Unit =
if (schema.getPropertyIds.length != 1)
assertSingleProperty(schema.getPropertyIds)

def assertSingleProperty(properties: Array[Int]): Unit =
if (properties.length != 1)
throw new UnsupportedOperationException("Cypher 3.1 does not support composite indexes or constraints")
}
Expand Up @@ -44,7 +44,7 @@ object TransactionBoundGraphStatistics {

// Probability of any node with the given label, to have a property with a given value
val indexEntrySelectivity = schemaRead.indexUniqueValuesSelectivity(
DefaultIndexReference.general(label.id, property.id)
schemaRead.indexReferenceUnchecked(label.id, property.id)
)
val frequencyOfNodesWithSameValue = 1.0 / indexEntrySelectivity
val indexSelectivity = frequencyOfNodesWithSameValue / labeledNodes
Expand All @@ -60,7 +60,7 @@ object TransactionBoundGraphStatistics {
val labeledNodes = read.countsForNodeWithoutTxState( label ).toDouble

// Probability of any node with the given label, to have a given property
val indexSize = schemaRead.indexSize(DefaultIndexReference.general(label.id, property.id))
val indexSize = schemaRead.indexSize(schemaRead.indexReferenceUnchecked(label.id, property.id))
val indexSelectivity = indexSize / labeledNodes

Selectivity.of(indexSelectivity)
Expand Down
Expand Up @@ -207,7 +207,7 @@ final class TransactionBoundQueryContext(txContext: TransactionalContextWrapper,
}

override def indexSeek(index: IndexDescriptor, value: Any) =
seek(DefaultIndexReference.general(index.labelId, index.propertyId),
seek(txContext.kernelTransaction.schemaRead().indexReferenceUnchecked(index.labelId, index.propertyId),
IndexQuery.exact(index.propertyId, value))

override def indexSeekByRange(index: IndexDescriptor, value: Any) = value match {
Expand Down Expand Up @@ -295,19 +295,19 @@ final class TransactionBoundQueryContext(txContext: TransactionalContextWrapper,
}

private def indexSeekByPrefixRange(index: IndexDescriptor, prefix: String): scala.Iterator[Node] =
seek(DefaultIndexReference.general(index.labelId, index.propertyId), IndexQuery.stringPrefix(index.propertyId, prefix))
seek(txContext.kernelTransaction.schemaRead().indexReferenceUnchecked(index.labelId, index.propertyId), IndexQuery.stringPrefix(index.propertyId, prefix))

private def indexSeekByNumericalRange(index: IndexDescriptor, range: InequalitySeekRange[Number]): scala.Iterator[Node] =(range match {
case rangeLessThan: RangeLessThan[Number] =>
rangeLessThan.limit(BY_NUMBER).map { limit =>
val rangePredicate = IndexQuery.range(index.propertyId, null, false, limit.endPoint, limit.isInclusive)
seek(DefaultIndexReference.general(index.labelId, index.propertyId), rangePredicate)
seek(txContext.kernelTransaction.schemaRead().indexReferenceUnchecked(index.labelId, index.propertyId), rangePredicate)
}

case rangeGreaterThan: RangeGreaterThan[Number] =>
rangeGreaterThan.limit(BY_NUMBER).map { limit =>
val rangePredicate = IndexQuery.range(index.propertyId, limit.endPoint, limit.isInclusive, null, false)
seek(DefaultIndexReference.general(index.labelId, index.propertyId), rangePredicate)
seek(txContext.kernelTransaction.schemaRead().indexReferenceUnchecked(index.labelId, index.propertyId), rangePredicate)
}

case RangeBetween(rangeGreaterThan, rangeLessThan) =>
Expand All @@ -317,7 +317,7 @@ final class TransactionBoundQueryContext(txContext: TransactionalContextWrapper,
.range(index.propertyId, greaterThanLimit.endPoint, greaterThanLimit.isInclusive,
lessThanLimit.endPoint,
lessThanLimit.isInclusive)
seek(DefaultIndexReference.general(index.labelId, index.propertyId), rangePredicate)
seek(txContext.kernelTransaction.schemaRead().indexReferenceUnchecked(index.labelId, index.propertyId), rangePredicate)
}
}
}).getOrElse(Iterator.empty)
Expand All @@ -329,14 +329,14 @@ final class TransactionBoundQueryContext(txContext: TransactionalContextWrapper,
rangeLessThan.limit(BY_STRING).map { limit =>
val rangePredicate = IndexQuery
.range(index.propertyId, null, false, limit.endPoint.asInstanceOf[String], limit.isInclusive)
seek(DefaultIndexReference.general(index.labelId, index.propertyId), rangePredicate)
seek(txContext.kernelTransaction.schemaRead().indexReferenceUnchecked(index.labelId, index.propertyId), rangePredicate)
}.getOrElse(Iterator.empty)

case rangeGreaterThan: RangeGreaterThan[String] =>
rangeGreaterThan.limit(BY_STRING).map { limit =>
val rangePredicate = IndexQuery
.range(index.propertyId, limit.endPoint.asInstanceOf[String], limit.isInclusive, null, false)
seek(DefaultIndexReference.general(index.labelId, index.propertyId), rangePredicate)
seek(txContext.kernelTransaction.schemaRead().indexReferenceUnchecked(index.labelId, index.propertyId), rangePredicate)
}.getOrElse(Iterator.empty)

case RangeBetween(rangeGreaterThan, rangeLessThan) =>
Expand All @@ -345,14 +345,14 @@ final class TransactionBoundQueryContext(txContext: TransactionalContextWrapper,
val rangePredicate = IndexQuery
.range(index.propertyId, greaterThanLimit.endPoint.asInstanceOf[String], greaterThanLimit.isInclusive,
lessThanLimit.endPoint.asInstanceOf[String], lessThanLimit.isInclusive)
seek(DefaultIndexReference.general(index.labelId, index.propertyId), rangePredicate)
seek(txContext.kernelTransaction.schemaRead().indexReferenceUnchecked(index.labelId, index.propertyId), rangePredicate)
}
}.getOrElse(Iterator.empty)
}

override def indexScan(index: IndexDescriptor) = {
val cursor = allocateAndTraceNodeValueIndexCursor()
reads().nodeIndexScan(DefaultIndexReference.general(index.labelId, index.propertyId), cursor, IndexOrder.NONE)
reads().nodeIndexScan(txContext.kernelTransaction.schemaRead().indexReferenceUnchecked(index.labelId, index.propertyId), cursor, IndexOrder.NONE)
new CursorIterator[Node] {
override protected def fetchNext(): Node = {
if (cursor.next()) entityAccessor.newNodeProxy(cursor.nodeReference())
Expand All @@ -363,14 +363,14 @@ final class TransactionBoundQueryContext(txContext: TransactionalContextWrapper,
}

override def indexScanByContains(index: IndexDescriptor, value: String) =
seek(DefaultIndexReference.general(index.labelId, index.propertyId), IndexQuery.stringContains(index.propertyId, value))
seek(txContext.kernelTransaction.schemaRead().indexReferenceUnchecked(index.labelId, index.propertyId), IndexQuery.stringContains(index.propertyId, value))

override def indexScanByEndsWith(index: IndexDescriptor, value: String) =
seek(DefaultIndexReference.general(index.labelId, index.propertyId), IndexQuery.stringSuffix(index.propertyId, value))
seek(txContext.kernelTransaction.schemaRead().indexReferenceUnchecked(index.labelId, index.propertyId), IndexQuery.stringSuffix(index.propertyId, value))

override def lockingUniqueIndexSeek(index: IndexDescriptor, value: Any): Option[Node] = {
indexSearchMonitor.lockingUniqueIndexSeek(index, value)
val nodeId = reads().lockingNodeUniqueIndexSeek(DefaultIndexReference.general(index.labelId, index.propertyId), IndexQuery.exact(index.propertyId, value))
val nodeId = reads().lockingNodeUniqueIndexSeek(txContext.kernelTransaction.schemaRead().indexReferenceUnchecked(index.labelId, index.propertyId), IndexQuery.exact(index.propertyId, value))
if (StatementConstants.NO_SUCH_NODE == nodeId) None else Some(nodeOps.getById(nodeId))
}

Expand Down Expand Up @@ -731,8 +731,7 @@ final class TransactionBoundQueryContext(txContext: TransactionalContextWrapper,

override def addIndexRule(labelId: Int, propertyKeyId: Int): IdempotentResult[IndexDescriptor] = try {
IdempotentResult(
DefaultIndexReference.toDescriptor(
txContext.kernelTransaction.schemaWrite().indexCreate(SchemaDescriptorFactory.forLabel(labelId, propertyKeyId)))
txContext.kernelTransaction.schemaWrite().indexCreate(SchemaDescriptorFactory.forLabel(labelId, propertyKeyId))
)
} catch {
case _: AlreadyIndexedException =>
Expand All @@ -742,11 +741,11 @@ final class TransactionBoundQueryContext(txContext: TransactionalContextWrapper,

if (read.indexGetState(index) == InternalIndexState.FAILED)
throw new FailedIndexException(index.userDescription(tokenNameLookup))
IdempotentResult(DefaultIndexReference.toDescriptor(index), wasCreated = false)
IdempotentResult(index, wasCreated = false)
}

override def dropIndexRule(labelId: Int, propertyKeyId: Int) =
txContext.kernelTransaction.schemaWrite().indexDrop(DefaultIndexReference.general( labelId, propertyKeyId ))
txContext.kernelTransaction.schemaWrite().indexDrop(txContext.kernelTransaction.schemaRead().indexReferenceUnchecked( labelId, propertyKeyId ))

override def createUniqueConstraint(labelId: Int, propertyKeyId: Int): IdempotentResult[UniquenessConstraint] = try {
txContext.kernelTransaction.schemaWrite().uniquePropertyConstraintCreate(
Expand Down

0 comments on commit 15b1b77

Please sign in to comment.