Skip to content

Commit

Permalink
Refactor DatasetResource and DatasetAccessResource (#3210)
Browse files Browse the repository at this point in the history
This PR refactors the codes style of the `DatasetResource` and
`DatasetAccessResource`.

Specifically, the refactoring principles are:
1. Move all the function definition in `object`, that are only being
used within the `class`, to the `class`
2. Uniform the way of interacting with jooq: use either jooq raw query,
or Dao, not both.
3. Flatten single-kv-pair case classes.
4. Flatten the single-line function.
  • Loading branch information
bobbai00 authored Jan 14, 2025
1 parent 3f66f4e commit 00aa822
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 335 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ object DatasetSearchQueryBuilder extends SearchQueryBuilder {
),
dataset.getOwnerUid == uid,
List(),
DatasetResource.calculateLatestDatasetVersionSize(dataset.getDid)
DatasetResource.calculateDatasetVersionSize(dataset.getDid)
)
DashboardClickableFileEntry(
resourceType = SearchQueryBuilder.DATASET_RESOURCE_TYPE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import edu.uci.ics.texera.web.resource.dashboard.user.dataset.DatasetAccessResou
context,
getOwner
}
import org.jooq.DSLContext
import org.jooq.{Condition, DSLContext}
import org.jooq.impl.DSL
import org.jooq.types.UInteger

import java.util
Expand All @@ -28,36 +29,28 @@ object DatasetAccessResource {
.createDSLContext()

def userHasReadAccess(ctx: DSLContext, did: UInteger, uid: UInteger): Boolean = {
val datasetDao = new DatasetDao(ctx.configuration())
val isDatasetPublic = Option(datasetDao.fetchOneByDid(did))
.flatMap(dataset => Option(dataset.getIsPublic))
.contains(1.toByte)

isDatasetPublic ||
userHasWriteAccess(ctx, did, uid) ||
datasetIsPublic(ctx, did) ||
getDatasetUserAccessPrivilege(ctx, did, uid) == DatasetUserAccessPrivilege.READ
}

def userOwnDataset(ctx: DSLContext, did: UInteger, uid: UInteger): Boolean = {
ctx
.selectOne()
.from(DATASET)
.where(DATASET.DID.eq(did))
.and(DATASET.OWNER_UID.eq(uid))
.fetch()
.isNotEmpty
val datasetDao = new DatasetDao(ctx.configuration())

Option(datasetDao.fetchOneByDid(did))
.exists(_.getOwnerUid == uid)
}

def userHasWriteAccess(ctx: DSLContext, did: UInteger, uid: UInteger): Boolean = {
userOwnDataset(ctx, did, uid) ||
getDatasetUserAccessPrivilege(ctx, did, uid) == DatasetUserAccessPrivilege.WRITE
}

def datasetIsPublic(ctx: DSLContext, did: UInteger): Boolean = {
Option(
ctx
.select(DATASET.IS_PUBLIC)
.from(DATASET)
.where(DATASET.DID.eq(did))
.fetchOneInto(classOf[Boolean])
).getOrElse(false)
}

def getDatasetUserAccessPrivilege(
ctx: DSLContext,
did: UInteger,
Expand All @@ -67,21 +60,23 @@ object DatasetAccessResource {
ctx
.select(DATASET_USER_ACCESS.PRIVILEGE)
.from(DATASET_USER_ACCESS)
.where(DATASET_USER_ACCESS.DID.eq(did))
.and(DATASET_USER_ACCESS.UID.eq(uid))
.fetchOne()
)
.map(_.getValue(DATASET_USER_ACCESS.PRIVILEGE))
.getOrElse(DatasetUserAccessPrivilege.NONE)
.where(
DATASET_USER_ACCESS.DID
.eq(did)
.and(DATASET_USER_ACCESS.UID.eq(uid))
)
.fetchOneInto(classOf[DatasetUserAccessPrivilege])
).getOrElse(DatasetUserAccessPrivilege.NONE)
}

def getOwner(ctx: DSLContext, did: UInteger): User = {
val ownerUid = ctx
.select(DATASET.OWNER_UID)
.from(DATASET)
.where(DATASET.DID.eq(did))
.fetchOneInto(classOf[UInteger])
new UserDao(ctx.configuration()).fetchOneByUid(ownerUid)
val datasetDao = new DatasetDao(ctx.configuration())
val userDao = new UserDao(ctx.configuration())

Option(datasetDao.fetchOneByDid(did))
.flatMap(dataset => Option(dataset.getOwnerUid))
.map(ownerUid => userDao.fetchOneByUid(ownerUid))
.orNull
}
}

Expand Down
Loading

0 comments on commit 00aa822

Please sign in to comment.