Skip to content

Commit

Permalink
Fix for #28: No increment in CanDB.update during creation, resulting …
Browse files Browse the repository at this point in the history
…in Natural subtraction underflow during deletion
  • Loading branch information
ByronBecker committed Mar 20, 2024
1 parent 4386531 commit ddce421
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/CanDB.mo
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Text "mo:base/Text";

import Prim "mo:⛔";

import { size } "mo:btree/BTree";

import E "Entity";
import RT "RangeTreeV2";

Expand Down Expand Up @@ -277,7 +279,14 @@ module {
public func update(db: DB, options: UpdateOptions): ?E.Entity {
let ovAttributeMap = RT.update(db.data, options.sk, options.updateAttributeMapFunction);
switch(ovAttributeMap) {
case null { null };
// entity did not previously exist, and was created
case null {
// Normally we would increment the count here, but due to the bug found in https://github.com/ORIGYN-SA/CanDB/issues/28
// This will apply a hot fix to the db count if it was off by more than 1
db.count := size(db.data);
null
};
// entity previously existed, return the original entity
case (?map) {
?{
pk = db.pk;
Expand Down

0 comments on commit ddce421

Please sign in to comment.