#33The SharedMemory storeEasy

The SharedMemory store

Background

Shared memory is a single store every executor reads and writes, so a document fetched once is visible to all. Its core is a dict keyed by search query:

class SharedMemory:
    def __init__(self):
        self._store = {}
    def get(self, key):   return self._store.get(key)   # None if absent
    def set(self, key, value): self._store[key] = value
    def has(self, key):   return key in self._store
    def snapshot(self):   return dict(self._store)       # a read-only copy

(In the real lesson get/set are async and guarded by an asyncio.Lock; here we model the store itself.)

Problem statement

Implement the SharedMemory class with get, set, has, and snapshot.

Methods

  • get(key) → the stored value, or None if absent.
  • set(key, value) → store value under key.
  • has(key)True iff key is stored.
  • snapshot() → a copy of the store (mutating it must not affect the store).

Examples

m = SharedMemory()
m.set("AlphaCode", ["doc1"])
m.get("AlphaCode")   # ["doc1"]
m.has("Codex")       # False
m.get("Codex")       # None

Constraints

  • get returns None for missing keys.
  • snapshot returns an independent dict copy.

Notes

  • One store, one source of truth — the foundation for deduping retrievals across parallel executors.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Reference example: set then get
  • Sample: missing key returns None
  • Reference example: snapshot is an independent copy