Skip to content

Commit

Permalink
Merge pull request #217 from JetBrains/odb-upgrade
Browse files Browse the repository at this point in the history
ODB upgrade
  • Loading branch information
andrii0lomakin authored Nov 18, 2024
2 parents 658db49 + ed2b0fc commit 58d3e23
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 129 deletions.
2 changes: 1 addition & 1 deletion entity-store/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dependencies {
api(project(":xodus-openAPI"))
api("com.orientechnologies:orientdb-core:4.0.0-20241101.163455-177")
api("com.orientechnologies:orientdb-core:4.0.0-20241118.113248-197")

implementation(project(":xodus-utils"))
implementation(project(":xodus-environment"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
package jetbrains.exodus.entitystore.orientdb

import com.orientechnologies.orient.core.command.OCommandOutputListener
import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal
import com.orientechnologies.orient.core.db.ODatabaseSession
import com.orientechnologies.orient.core.db.ODatabaseSessionInternal
import com.orientechnologies.orient.core.db.ODatabaseType
import com.orientechnologies.orient.core.db.tool.ODatabaseExport
import com.orientechnologies.orient.core.db.tool.ODatabaseImport
Expand All @@ -39,7 +40,7 @@ class ODatabaseCompacter(

dbProvider.withSession { session ->
val exporter = ODatabaseExport(
session as ODatabaseDocumentInternal,
session as ODatabaseSessionInternal,
backupFile.outputStream(),
listener
)
Expand All @@ -55,7 +56,7 @@ class ODatabaseCompacter(
dbProvider.withSession { session ->
logger.info("Importing database from dump")
val importer = ODatabaseImport(
session as ODatabaseDocumentInternal,
session as ODatabaseSessionInternal,
backupFile.inputStream(),
listener
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package jetbrains.exodus.entitystore.orientdb
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal
import com.orientechnologies.orient.core.db.ODatabaseSession
import com.orientechnologies.orient.core.db.OrientDB
import com.orientechnologies.orient.core.tx.OTransaction
import com.orientechnologies.orient.core.tx.OTransactionNoTx

interface ODatabaseProvider {
val databaseLocation: String
Expand All @@ -33,7 +31,10 @@ interface ODatabaseProvider {
* Never use this method. If you use this method, make sure you 100% understand what happens,
* and do not hesitate to invite people to review your code.
*/
fun <T> executeInASeparateSession(currentSession: ODatabaseSession, action: (ODatabaseSession) -> T): T
fun <T> executeInASeparateSession(
currentSession: ODatabaseSession,
action: (ODatabaseSession) -> T
): T

/**
* Database-wise read-only mode.
Expand Down Expand Up @@ -68,16 +69,15 @@ fun <R> ODatabaseProvider.withCurrentOrNewSession(
}

internal fun ODatabaseSession.hasActiveTransaction(): Boolean {
return isActiveOnCurrentThread && transaction !is OTransactionNoTx
return isActiveOnCurrentThread && activeTxCount() > 0
}

internal fun ODatabaseSession.requireActiveTransaction(): OTransaction {
internal fun ODatabaseSession.requireActiveTransaction() {
require(hasActiveTransaction()) { "No active transaction is found. Happy debugging, pal!" }
return transaction
}

internal fun ODatabaseSession.requireNoActiveTransaction() {
assert(isActiveOnCurrentThread && transaction is OTransactionNoTx) { "Active transaction is detected. Changes in the schema must not happen in a transaction." }
assert(isActiveOnCurrentThread && activeTxCount() == 0) { "Active transaction is detected. Changes in the schema must not happen in a transaction." }
}

internal fun requireNoActiveSession() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ class OSchemaBuddyImpl(

override fun updateSequence(session: ODatabaseSession, sequenceName: String, currentValue: Long) {
session.executeInASeparateSessionIfCurrentHasTransaction(dbProvider) { sessionToWork ->
sessionToWork.begin()
getSequence(sessionToWork, sequenceName).updateParams(CreateParams().setCurrentValue(currentValue))
sessionToWork.commit()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface OStoreTransaction : StoreTransaction {
fun activateOnCurrentThread()


fun <T> getRecord(id: OEntityId): T?
fun <T> getRecord(id: OEntityId): T
where T: ORecord

fun newEntity(entityType: String, localEntityId: Long): OVertexEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
*/
package jetbrains.exodus.entitystore.orientdb

import com.orientechnologies.orient.core.db.ODatabase
import com.orientechnologies.orient.core.db.ODatabaseSession
import com.orientechnologies.orient.core.db.ODatabaseSessionInternal
import com.orientechnologies.orient.core.exception.ORecordNotFoundException
import com.orientechnologies.orient.core.metadata.schema.OClass
import com.orientechnologies.orient.core.metadata.sequence.OSequence
import com.orientechnologies.orient.core.record.ORecord
import com.orientechnologies.orient.core.record.OVertex
import com.orientechnologies.orient.core.sql.executor.OResultSet
import com.orientechnologies.orient.core.tx.OTransaction.TXSTATUS
import com.orientechnologies.orient.core.tx.OTransactionNoTx
import jetbrains.exodus.entitystore.*
import jetbrains.exodus.entitystore.orientdb.OVertexEntity.Companion.LOCAL_ENTITY_ID_PROPERTY_NAME
import jetbrains.exodus.entitystore.orientdb.iterate.OEntityOfTypeIterable
Expand Down Expand Up @@ -53,17 +53,22 @@ class OStoreTransactionImpl(
*/
private val transactionIdImpl by lazy {
requireActiveTransaction()
session.transaction.id.toLong()
(session as ODatabaseSessionInternal).transaction.id.toLong()
}

override fun getTransactionId(): Long {
return transactionIdImpl
}

override fun <T> getRecord(id: OEntityId): T?
override fun <T> getRecord(id: OEntityId): T
where T : ORecord {
requireActiveTransaction()
return session.getRecord(id.asOId())
try {
return session.load(id.asOId())
} catch (e: ORecordNotFoundException) {
throw EntityRemovedInDatabaseException(id.getTypeName(), id)
}

}

override fun query(sql: String, params: Map<String, Any>): OResultSet {
Expand All @@ -76,15 +81,15 @@ class OStoreTransactionImpl(
}

override fun isIdempotent(): Boolean {
return readOnly || session.transaction.recordOperations.none()
return readOnly || (session as ODatabaseSessionInternal).transaction.recordOperations.none()
}

override fun isReadonly(): Boolean {
return readOnly
}

override fun isFinished(): Boolean {
return session.status == ODatabase.STATUS.CLOSED
return session.status == ODatabaseSession.STATUS.CLOSED
}

override fun isCurrent(): Boolean {
Expand All @@ -96,9 +101,9 @@ class OStoreTransactionImpl(
}

override fun requireActiveTransaction() {
check(session.status == ODatabase.STATUS.OPEN) { "The transaction is finished, the internal session state: ${session.status}" }
check(session.status == ODatabaseSession.STATUS.OPEN) { "The transaction is finished, the internal session state: ${session.status}" }
check(session.isActiveOnCurrentThread) { "The active session is no the session the transaction was started in" }
check(session.transaction.status == TXSTATUS.BEGUN) { "The current OTransaction status is ${session.transaction.status}, but the status ${TXSTATUS.BEGUN} was expected." }
check((session as ODatabaseSessionInternal).transaction.status == TXSTATUS.BEGUN) { "The current OTransaction status is ${session.transaction.status}, but the status ${TXSTATUS.BEGUN} was expected." }
}

override fun requireActiveWritableTransaction() {
Expand All @@ -112,15 +117,15 @@ class OStoreTransactionImpl(
}

override fun activateOnCurrentThread() {
check(session.status == ODatabase.STATUS.OPEN) { "The transaction is finished, the internal session state: ${session.status}" }
check(session.status == ODatabaseSession.STATUS.OPEN) { "The transaction is finished, the internal session state: ${session.status}" }
check(!session.isActiveOnCurrentThread) { "The transaction is already active on the current thread" }
onActivated(session, this)
}

fun begin() {
check(session.status == ODatabase.STATUS.OPEN) { "The session status is ${session.status}. But ${ODatabase.STATUS.OPEN} is required." }
check(session.status == ODatabaseSession.STATUS.OPEN) { "The session status is ${session.status}. But ${ODatabaseSession.STATUS.OPEN} is required." }
check(session.isActiveOnCurrentThread) { "The session is not active on the current thread" }
check(session.transaction is OTransactionNoTx) { "The session must not have a transaction" }
check(session.activeTxCount() == 0) { "The session must not have a transaction" }
try {
session.begin()
// initialize transaction id
Expand Down Expand Up @@ -189,7 +194,7 @@ class OStoreTransactionImpl(
}

private fun cleanUpTxIfNeeded() {
if (session.status == ODatabase.STATUS.OPEN && session.transaction.status == TXSTATUS.INVALID) {
if (session.status == ODatabaseSession.STATUS.OPEN && session.activeTxCount() == 0) {
onFinished(session, this)
}
}
Expand Down Expand Up @@ -233,8 +238,12 @@ class OStoreTransactionImpl(
if (oId == ORIDEntityId.EMPTY_ID) {
throw EntityRemovedInDatabaseException(oId.getTypeName(), id)
}
val vertex: OVertex = session.load(oId.asOId()) ?: throw EntityRemovedInDatabaseException(oId.getTypeName(), id)
return OVertexEntity(vertex, store)
try {
val vertex: OVertex = session.load(oId.asOId())
return OVertexEntity(vertex, store)
} catch (e: ORecordNotFoundException) {
throw EntityRemovedInDatabaseException(oId.getTypeName(), id)
}
}

override fun getEntityTypes(): MutableList<String> {
Expand All @@ -252,7 +261,11 @@ class OStoreTransactionImpl(
return OSingleEntityIterable(this, entity)
}

override fun find(entityType: String, propertyName: String, value: Comparable<Nothing>): EntityIterable {
override fun find(
entityType: String,
propertyName: String,
value: Comparable<Nothing>
): EntityIterable {
requireActiveTransaction()
return OPropertyEqualIterable(this, entityType, propertyName, value)
}
Expand All @@ -277,7 +290,11 @@ class OStoreTransactionImpl(
return OPropertyContainsIterable(this, entityType, propertyName, value)
}

override fun findStartingWith(entityType: String, propertyName: String, value: String): EntityIterable {
override fun findStartingWith(
entityType: String,
propertyName: String,
value: String
): EntityIterable {
requireActiveTransaction()
return OPropertyStartsWithIterable(this, entityType, propertyName, value)
}
Expand All @@ -298,7 +315,10 @@ class OStoreTransactionImpl(
return OPropertyExistsIterable(this, entityType, propertyName)
}

override fun findWithPropSortedByValue(entityType: String, propertyName: String): EntityIterable {
override fun findWithPropSortedByValue(
entityType: String,
propertyName: String
): EntityIterable {
requireActiveTransaction()
return OPropertyExistsSortedIterable(this, entityType, propertyName)
}
Expand All @@ -313,7 +333,11 @@ class OStoreTransactionImpl(
return OLinkOfTypeToEntityIterable(this, linkName, entity.id as OEntityId, entityType)
}

override fun findLinks(entityType: String, entities: EntityIterable, linkName: String): EntityIterable {
override fun findLinks(
entityType: String,
entities: EntityIterable,
linkName: String
): EntityIterable {
requireActiveTransaction()
return OLinkIterableToEntityIterable(this, entities.asOQueryIterable(), linkName)
}
Expand All @@ -333,7 +357,11 @@ class OStoreTransactionImpl(
return OLinkExistsEntityIterable(this, entityType, linkName)
}

override fun sort(entityType: String, propertyName: String, ascending: Boolean): EntityIterable {
override fun sort(
entityType: String,
propertyName: String,
ascending: Boolean
): EntityIterable {
requireActiveTransaction()
return OPropertySortedIterable(this, entityType, propertyName, null, ascending)
}
Expand All @@ -345,7 +373,13 @@ class OStoreTransactionImpl(
ascending: Boolean
): EntityIterable {
requireActiveTransaction()
return OPropertySortedIterable(this, entityType, propertyName, rightOrder.asOQueryIterable(), ascending)
return OPropertySortedIterable(
this,
entityType,
propertyName,
rightOrder.asOQueryIterable(),
ascending
)
}

override fun sortLinks(
Expand All @@ -356,7 +390,12 @@ class OStoreTransactionImpl(
rightOrder: EntityIterable
): EntityIterable {
requireActiveTransaction()
return OLinkSortEntityIterable(this, sortedLinks.asOQueryIterable(), linkName, rightOrder.asOQueryIterable())
return OLinkSortEntityIterable(
this,
sortedLinks.asOQueryIterable(),
linkName,
rightOrder.asOQueryIterable()
)
}

override fun sortLinks(
Expand All @@ -370,11 +409,19 @@ class OStoreTransactionImpl(
): EntityIterable {
requireActiveTransaction()
// Not sure about skipping oppositeEntityType and oppositeLinkName values
return OLinkSortEntityIterable(this, sortedLinks.asOQueryIterable(), linkName, rightOrder.asOQueryIterable())
return OLinkSortEntityIterable(
this,
sortedLinks.asOQueryIterable(),
linkName,
rightOrder.asOQueryIterable()
)
}

@Deprecated("Deprecated in Java")
override fun mergeSorted(sorted: MutableList<EntityIterable>, comparator: Comparator<Entity>): EntityIterable {
override fun mergeSorted(
sorted: MutableList<EntityIterable>,
comparator: Comparator<Entity>
): EntityIterable {
throw UnsupportedOperationException("Not implemented")
}

Expand Down Expand Up @@ -418,7 +465,11 @@ class OStoreTransactionImpl(
schemaBuddy.renameOClass(session, oldName, newName)
}

override fun getOrCreateEdgeClass(linkName: String, outClassName: String, inClassName: String): OClass {
override fun getOrCreateEdgeClass(
linkName: String,
outClassName: String,
inClassName: String
): OClass {
requireActiveTransaction()
return schemaBuddy.getOrCreateEdgeClass(session, linkName, outClassName, inClassName)
}
Expand Down
Loading

0 comments on commit 58d3e23

Please sign in to comment.