-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate events to stable memory (#40)
- Loading branch information
Showing
10 changed files
with
136 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use crate::state; | ||
use ic_cdk::heartbeat; | ||
|
||
#[heartbeat] | ||
fn heartbeat() { | ||
state::mutate(|s| s.events.migrate(1000)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod heartbeat; | ||
mod init; | ||
mod post_upgrade; | ||
mod pre_upgrade; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,31 @@ | ||
use ic_cdk::api::stable::StableWriter; | ||
use ic_stable_structures::{ | ||
memory_manager::{MemoryId, MemoryManager, VirtualMemory}, | ||
DefaultMemoryImpl, | ||
}; | ||
use std::cell::RefCell; | ||
|
||
const UPGRADES: MemoryId = MemoryId::new(0); | ||
const EVENTS_INDEX: MemoryId = MemoryId::new(1); | ||
const EVENTS_DATA: MemoryId = MemoryId::new(2); | ||
|
||
pub type Memory = VirtualMemory<DefaultMemoryImpl>; | ||
|
||
thread_local! { | ||
static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> | ||
= RefCell::new(MemoryManager::init_with_bucket_size(DefaultMemoryImpl::default(), 128)); | ||
} | ||
|
||
// This forces the buckets to be the specified size rather than preserving the previous bucket size | ||
pub fn reset_memory_manager() { | ||
let mut writer = StableWriter::default(); | ||
writer.write(&[0, 0, 0]).unwrap(); | ||
MEMORY_MANAGER.replace(MemoryManager::init_with_bucket_size( | ||
DefaultMemoryImpl::default(), | ||
128, | ||
)); | ||
static MEMORY_MANAGER: MemoryManager<DefaultMemoryImpl> | ||
= MemoryManager::init_with_bucket_size(DefaultMemoryImpl::default(), 128); | ||
} | ||
|
||
pub fn get_upgrades_memory() -> Memory { | ||
get_memory(UPGRADES) | ||
} | ||
|
||
pub fn get_events_index_memory() -> Memory { | ||
get_memory(EVENTS_INDEX) | ||
} | ||
|
||
pub fn get_events_data_memory() -> Memory { | ||
get_memory(EVENTS_DATA) | ||
} | ||
|
||
fn get_memory(id: MemoryId) -> Memory { | ||
MEMORY_MANAGER.with_borrow(|m| m.get(id)) | ||
MEMORY_MANAGER.with(|m| m.get(id)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters