Skip to content

Commit

Permalink
improved the management of metadat in undo
Browse files Browse the repository at this point in the history
  • Loading branch information
N-thony committed Oct 23, 2024
1 parent 28beaa2 commit 2c6a091
Showing 1 changed file with 46 additions and 28 deletions.
74 changes: 46 additions & 28 deletions instat/static/InstatObject/R/data_object_R6.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ DataSheet$set("public", "set_enable_disable_undo", function(disable_undo) {
})

DataSheet$set("public", "save_state_to_undo_history", function() {
self$set_undo_history(list(private$data))
self$set_undo_history(private$data, attributes(private$data))
})

DataSheet$set("public", "is_undo", function() {
Expand Down Expand Up @@ -209,19 +209,31 @@ DataSheet$set("public", "has_undo_history", function() {

DataSheet$set("public", "undo_last_action", function() {

# Perform the undo action
# Check if there's any action to undo
if (length(private$undo_history) > 0) {
# Get the last state from the undo history
previous_state <- private$undo_history[[length(private$undo_history)]]
self$set_data(as.data.frame(previous_state)) # Restore the previous state

private$undo_history <- private$undo_history[-length(private$undo_history)] # Remove the latest state
# Restore the data and its attributes
restored_data <- previous_state$data # Extract the dataframe
restored_attributes <- previous_state$attributes # Extract the attributes

# Set the dataframe in the DataSheet
self$set_data(as.data.frame(restored_data))

# Restore attributes
restored_attributes <- previous_state$attributes # Extract the attributes
for (property in names(restored_attributes)) {
self$append_to_metadata(property, restored_attributes[[property]])
}
# Remove the latest state from the undo history
private$undo_history <- private$undo_history[-length(private$undo_history)]

# Trigger garbage collection to free memory
gc()
} else {
message("No more actions to undo.")
}

})


Expand Down Expand Up @@ -302,33 +314,39 @@ DataSheet$set("public", "set_scalars", function(new_scalars) {
)

# Set undo_history with memory management
DataSheet$set("public", "set_undo_history", function(new_undo_history) {
if (!is.list(new_undo_history)) stop("undo_history must be of type: list")
if(!private$disable_undo){
# Define memory and undo_history limits
MAX_undo_history_SIZE <- 10 # Limit to last 10 undo_history states
MAX_MEMORY_LIMIT_MB <- 1024 # Limit the memory usage for undo undo_history

# Check current memory usage
current_memory <- monitor_memory()

# If memory exceeds limit, remove the oldest entry
if (current_memory > MAX_MEMORY_LIMIT_MB) {
message(paste("Memory limit exceeded:", round(current_memory, 2), "MB. Removing oldest entry."))
private$undo_history <- private$undo_history[-1] # Remove the oldest entry
gc() # Trigger garbage collection to free memory
}

# Limit undo_history size
if (length(private$undo_history) >= MAX_undo_history_SIZE) {
private$undo_history <- private$undo_history[-1] # Remove the oldest entry
gc() # Trigger garbage collection to free memory
}
DataSheet$set("public", "set_undo_history", function(new_data, attributes = list()) {
if (!is.data.frame(new_data)) stop("new_data must be of type: data.frame")

private$undo_history <- append(private$undo_history, list(new_undo_history))
if (!private$disable_undo) {
# Define memory and undo_history limits
MAX_undo_history_SIZE <- 10 # Limit to last 10 undo_history states
MAX_MEMORY_LIMIT_MB <- 1024 # Limit the memory usage for undo_history

# Check current memory usage
current_memory <- monitor_memory()

# If memory exceeds limit, remove the oldest entry
if (current_memory > MAX_MEMORY_LIMIT_MB) {
message(paste("Memory limit exceeded:", round(current_memory, 2), "MB. Removing oldest entry."))
private$undo_history <- private$undo_history[-1] # Remove the oldest entry
gc() # Trigger garbage collection to free memory
}

# Limit undo_history size
if (length(private$undo_history) >= MAX_undo_history_SIZE) {
private$undo_history <- private$undo_history[-1] # Remove the oldest entry
gc() # Trigger garbage collection to free memory
}

# Package the new data and attributes into a list
new_undo_entry <- list(data = new_data, attributes = attributes)

# Append the new entry to the undo history
private$undo_history <- append(private$undo_history, list(new_undo_entry))
}
})


DataSheet$set("public", "set_keys", function(new_keys) {
if(!is.list(new_keys)) stop("new_keys must be of type: list")
self$append_to_changes(list(Set_property, "keys"))
Expand Down

0 comments on commit 2c6a091

Please sign in to comment.