Skip to content

Commit

Permalink
feat: default to stable memory for datastore and users as well (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker authored Mar 15, 2024
1 parent cff3c6a commit 07bd583
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 25 deletions.
7 changes: 4 additions & 3 deletions src/frontend/src/lib/api/satellites.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import type {
RulesType,
SetRule
} from '$declarations/satellite/satellite.did';
import type { MemoryText, PermissionText } from '$lib/constants/rules.constants';
import { MemoryHeap } from '$lib/constants/rules.constants';
import { MemoryStable, type MemoryText, type PermissionText } from '$lib/constants/rules.constants';
import type { CustomDomains } from '$lib/types/custom-domain';
import type { OptionIdentity } from '$lib/types/itentity';
import type { ListParams } from '$lib/types/list';
Expand Down Expand Up @@ -90,7 +89,9 @@ export const setRule = async ({
write: permissionFromText(write),
updated_at: isNullish(rule) ? [] : [rule.updated_at],
max_size: toNullable(nonNullish(maxSize) && maxSize > 0 ? BigInt(maxSize) : undefined),
memory: isNullish(rule) ? [memoryFromText(memory)] : [fromNullable(rule.memory) ?? MemoryHeap],
memory: isNullish(rule)
? [memoryFromText(memory)]
: [fromNullable(rule.memory) ?? MemoryStable],
mutable_permissions: toNullable(mutablePermissions)
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@
const initMemory = (text: MemoryText) => (memory = text);
$: initMemory(
// Before the introduction of the stable memory, the memory used was "Heap". That's why we fallback for display purpose on Stable only if new to support old satellites
memoryToText(
fromNullable(rule?.memory ?? []) ??
(typeStorage && mode === 'new' ? MemoryStable : MemoryHeap)
)
memoryToText(fromNullable(rule?.memory ?? []) ?? (mode === 'new' ? MemoryStable : MemoryHeap))
);
let currentImmutable: boolean;
Expand Down Expand Up @@ -165,8 +162,8 @@
<Value ref="memory">
<svelte:fragment slot="label">{$i18n.collections.memory}</svelte:fragment>
<select id="memory" name="write" bind:value={memory} disabled={mode === 'edit'}>
<option value="Heap">{$i18n.collections.heap}</option>
<option value="Stable">{$i18n.collections.stable}</option>
<option value="Heap">{$i18n.collections.heap}</option>
</select>
</Value>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/libs/satellite/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Default for HeapState {
Rule {
read: rule.read,
write: rule.write,
memory: Some(rule.memory.unwrap_or(Memory::Heap)),
memory: Some(rule.memory.unwrap_or(Memory::Stable)),
mutable_permissions: Some(rule.mutable_permissions.unwrap_or(false)),
max_size: rule.max_size,
created_at: now,
Expand Down
2 changes: 1 addition & 1 deletion src/libs/satellite/src/rules/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub const DEFAULT_DB_COLLECTIONS: [(&str, SetRule); 1] = [(
SetRule {
read: Managed,
write: Managed,
memory: Some(Memory::Heap),
memory: Some(Memory::Stable),
mutable_permissions: Some(false),
max_size: None,
updated_at: None,
Expand Down
15 changes: 3 additions & 12 deletions src/libs/satellite/src/rules/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,18 @@ pub fn set_rule_db(collection: CollectionKey, rule: SetRule) -> Result<(), Strin
set_rule_impl(
collection.clone(),
rule.clone(),
Memory::Heap,
&mut state.borrow_mut().heap.db.rules,
)
})?;

// If the collection does not exist yet we initialize it
init_collection_store(&collection, &rule.memory.unwrap_or(Memory::Heap));
init_collection_store(&collection, &rule.memory.unwrap_or(Memory::Stable));

Ok(())
}

pub fn set_rule_storage(collection: CollectionKey, rule: SetRule) -> Result<(), String> {
STATE.with(|state| {
set_rule_impl(
collection,
rule,
Memory::Stable,
&mut state.borrow_mut().heap.storage.rules,
)
})
STATE.with(|state| set_rule_impl(collection, rule, &mut state.borrow_mut().heap.storage.rules))
}

pub fn del_rule_db(collection: CollectionKey, rule: DelRule) -> Result<(), String> {
Expand Down Expand Up @@ -80,7 +72,6 @@ pub fn del_rule_storage(collection: CollectionKey, rule: DelRule) -> Result<(),
fn set_rule_impl(
collection: CollectionKey,
user_rule: SetRule,
default_memory: Memory,
rules: &mut Rules,
) -> Result<(), String> {
let current_rule = rules.get(&collection);
Expand All @@ -103,7 +94,7 @@ fn set_rule_impl(
updated_at,
read: user_rule.read,
write: user_rule.write,
memory: Some(user_rule.memory.unwrap_or(default_memory)),
memory: Some(user_rule.memory.unwrap_or(Memory::Stable)),
mutable_permissions: Some(user_rule.mutable_permissions.unwrap_or(true)),
max_size: user_rule.max_size,
};
Expand Down
2 changes: 1 addition & 1 deletion src/libs/satellite/src/rules/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub mod rules {

#[derive(CandidType, Serialize, Deserialize, Default, Clone, Debug)]
pub enum Memory {
#[default]
Heap,
#[default]
Stable,
}

Expand Down
4 changes: 2 additions & 2 deletions src/tests/satellite.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Satellite', () => {
});

expect(collection).toEqual('test');
expect(memory).toEqual(toNullable({ Heap: null }));
expect(memory).toEqual(toNullable({ Stable: null }));
expect(read).toEqual({ Managed: null });
expect(write).toEqual({ Managed: null });
expect(created_at).toBeGreaterThan(0n);
Expand All @@ -82,7 +82,7 @@ describe('Satellite', () => {
] = await list_rules({ Db: null });

expect(collection).toEqual('test');
expect(memory).toEqual(toNullable({ Heap: null }));
expect(memory).toEqual(toNullable({ Stable: null }));
expect(read).toEqual({ Managed: null });
expect(write).toEqual({ Managed: null });
expect(mutable_permissions).toEqual([true]);
Expand Down
28 changes: 28 additions & 0 deletions src/tests/satellite.upgrade.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,32 @@ describe('satellite upgrade v0.0.16', () => {

await testUsers([...users, ...moreUsers]);
});

it('should keep listing existing heap collections as such', async () => {
const { set_rule, list_rules } = actor;

await set_rule({ Db: null }, 'test', {
memory: toNullable({ Heap: null }),
updated_at: toNullable(),
max_size: toNullable(),
read: { Managed: null },
mutable_permissions: toNullable(),
write: { Managed: null }
});

const testCollection = async () => {
const [[collection, { memory }], _] = await list_rules({
Db: null
});

expect(collection).toEqual('test');
expect(memory).toEqual(toNullable({ Heap: null }));
};

await testCollection();

await upgrade();

await testCollection();
});
});

0 comments on commit 07bd583

Please sign in to comment.