Skip to content

Commit

Permalink
fix view test returning also the tuple when deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
gunplar committed Oct 15, 2024
1 parent 3b65bd4 commit c4961c8
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import naksha.model.objects.NakshaCollection
import naksha.model.objects.NakshaFeature
import naksha.model.request.*
import naksha.psql.*
import naksha.psql.executors.TupleCachingUtils.cachedTupleNumber
import naksha.psql.executors.write.*
import kotlin.jvm.JvmField

Expand Down Expand Up @@ -188,17 +189,23 @@ class PgWriter(
when (write.op) {
WriteOp.CREATE -> cachedTupleNumber(
write,
CreateCollection(session).execute(mapOf(write), write)
CreateCollection(session).execute(mapOf(write), write),
tuples,
tupleCache
)

WriteOp.UPSERT -> cachedTupleNumber(
write,
upsertCollection(mapOf(write), write)
upsertCollection(mapOf(write), write),
tuples,
tupleCache
)

WriteOp.UPDATE -> cachedTupleNumber(
write,
updateCollection(mapOf(write), write)
updateCollection(mapOf(write), write),
tuples,
tupleCache
)

WriteOp.DELETE, WriteOp.PURGE -> DropCollection(session).execute(
Expand All @@ -216,7 +223,9 @@ class PgWriter(
when (write.op) {
WriteOp.CREATE -> cachedTupleNumber(
write,
InsertFeature(session, writeExecutor).execute(collection, write)
InsertFeature(session, writeExecutor).execute(collection, write),
tuples,
tupleCache
)

WriteOp.UPSERT ->
Expand All @@ -227,7 +236,9 @@ class PgWriter(
) {
cachedTupleNumber(
write,
InsertFeature(session, writeExecutor).execute(collection, write)
InsertFeature(session, writeExecutor).execute(collection, write),
tuples,
tupleCache
)
} else {
cachedTupleNumber(
Expand All @@ -236,7 +247,9 @@ class PgWriter(
session,
previousMetadataProvider,
writeExecutor
).execute(collection, write)
).execute(collection, write),
tuples,
tupleCache
)
}

Expand All @@ -245,12 +258,16 @@ class PgWriter(
UpdateFeature(session, previousMetadataProvider, writeExecutor).execute(
collection,
write
)
),
tuples,
tupleCache
)

WriteOp.DELETE -> DeleteFeature(session, writeExecutor).execute(
collection,
write
write,
tuples,
tupleCache
)

WriteOp.PURGE -> TODO()
Expand Down Expand Up @@ -281,12 +298,6 @@ class PgWriter(
)
}

private fun cachedTupleNumber(write: WriteExt, tuple: Tuple): TupleNumber {
tuples[write.i] = tuple
tupleCache.store(tuple)
return tuple.tupleNumber
}

private fun mapOf(write: WriteExt): PgMap {
val mapId = write.mapId
if (mapId !in storage) throw NakshaException(MAP_NOT_FOUND, "No such map: '$mapId'")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package naksha.psql.executors

import naksha.model.*

object TupleCachingUtils {
fun cachedTupleNumber(write: WriteExt, tuple: Tuple, tupleList: TupleList, tupleCache: TupleCache): TupleNumber {
tupleList[write.i] = tuple
tupleCache.store(tuple)
return tuple.tupleNumber
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package naksha.psql.executors.write

import naksha.model.*
import naksha.model.Metadata.Metadata_C.geoGrid
import naksha.model.Metadata.Metadata_C.hash
import naksha.model.objects.NakshaFeature
import naksha.model.request.ReadFeatures
import naksha.model.request.SuccessResponse
import naksha.psql.PgCollection
import naksha.psql.PgSession
import naksha.psql.executors.PgReader
import naksha.psql.executors.PgWriter
import naksha.psql.executors.TupleCachingUtils.cachedTupleNumber
import naksha.psql.executors.WriteExt
import naksha.psql.executors.write.WriteFeatureUtils.newFeatureTupleNumber
import naksha.psql.executors.write.WriteFeatureUtils.resolveFlags
import naksha.psql.executors.write.WriteFeatureUtils.tuple

class DeleteFeature(
private val session: PgSession,
private val writeExecutor: WriteExecutor
) {
fun execute(collection: PgCollection, write: WriteExt): TupleNumber {
fun execute(collection: PgCollection, write: WriteExt, tupleList: TupleList, tupleCache: TupleCache): TupleNumber {
val featureId = write.featureId ?: throw NakshaException(NakshaError.ILLEGAL_ARGUMENT, "No feature ID provided")

val tupleNumber = newFeatureTupleNumber(collection, featureId, session)
Expand Down Expand Up @@ -51,7 +56,38 @@ class DeleteFeature(
}

writeExecutor.removeFeatureFromHead(collection, featureId)
val feature = response.features.first()!! //already checked that feature list is not empty
val metadata = response.tuples.first()?.tuple?.meta!!
val tuple = tuple(
session.storage,
tupleNumber,
feature = feature,
metadata = metaForDeleted(metadata,feature,flags),
write.attachment,
flags
)
return cachedTupleNumber(write, tuple, tupleList, tupleCache)
}
return tupleNumber
}

private fun metaForDeleted(previousMetadata: Metadata,
feature: NakshaFeature,
flags: Flags): Metadata {
val versionTime = session.versionTime()
return previousMetadata.copy(
updatedAt = versionTime,
authorTs = if (session.options.author == null) previousMetadata.authorTs else versionTime,
prevVersion = previousMetadata.version,
uid = session.uid.getAndAdd(1),
puid = previousMetadata.puid,
hash = hash(feature, session.options.excludePaths, session.options.excludeFn),
changeCount = previousMetadata.changeCount + 1,
geoGrid = geoGrid(feature),
flags = flags,
appId = session.options.appId,
author = session.options.author ?: previousMetadata.author,
id = feature.id
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static List<ResultTuple> sampleXyzResponse(int size, IStorage storage) {
List<ResultTuple> returnList = new ArrayList<>();
for (int i = 0; i < size; i++) {
byte[] bytesFeature = PgUtil.encodeFeature(new NakshaFeature(), 0, null);
Tuple tuple = new Tuple(storage, tupleNum, FetchMode.FETCH_ALL, metadata, metadata.getId(), metadata.getFlags(), bytesFeature, null, null, null, null);
Tuple tuple = new Tuple(storage, tupleNum, FetchMode.FETCH_ALL, metadata, metadata.getId()+i, metadata.getFlags(), bytesFeature, null, null, null, null);
returnList.add(new ResultTuple(storage, tupleNum, ExecutedOp.READ, tuple));
}
return returnList;
Expand All @@ -65,7 +65,7 @@ public static List<ResultTuple> sampleXyzWriteResponse(int size, IStorage storag
List<ResultTuple> returnList = new ArrayList<>();
for (int i = 0; i < size; i++) {
byte[] bytesFeature = PgUtil.encodeFeature(new NakshaFeature(), 0, null);
Tuple tuple = new Tuple(storage, tupleNum, FetchMode.FETCH_ALL, metadata, metadata.getId(), metadata.getFlags(), bytesFeature, null, null, null, null);
Tuple tuple = new Tuple(storage, tupleNum, FetchMode.FETCH_ALL, metadata, metadata.getId()+i, metadata.getFlags(), bytesFeature, null, null, null, null);
returnList.add(new ResultTuple(storage, tupleNum, op,tuple));
}
return returnList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void testDeleteApiNotation() {
when(storage.newWriteSession(sessionOptions)).thenReturn(session);

final WriteRequest request = new WriteRequest();
final NakshaFeature feature = new NakshaFeature("id0");
final NakshaFeature feature = new NakshaFeature("sampleTuple0");
request.add(write.deleteFeature(null,VIEW_COLLECTION,feature,false));
SuccessResponse successResponse1 = new SuccessResponse(sampleXyzWriteResponse(1, storage, ExecutedOp.DELETED));
when(session.execute(request)).thenReturn(successResponse1);
Expand Down

0 comments on commit c4961c8

Please sign in to comment.