view MoinMoin/storage/stores/ @ 1917:16ea00a08d51

create mixin classes that transform bytes <-> file stores often (but maybe not always), the underlying storage system just offers either a bytes- or files-oriented interface, not both. but on the backend layer, we need a BytesStore for the metadata and a FileStore for the data.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 20 Jan 2013 21:55:30 +0100
parents f83b66646d75
line wrap: on
line source
# Copyright: 2011 MoinMoin:ThomasWaldmann
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

MoinMoin - memory store

Stores k/v pairs into memory (RAM, non-persistent!).

Note: likely this is mostly useful for unit tests.

from __future__ import absolute_import, division

from . import (BytesMutableStoreBase, FileMutableStoreBase,
               BytesMutableStoreMixin, FileMutableStoreMixin)

class BytesStore(BytesMutableStoreBase):
    A simple dict-based in-memory store. No persistence!
    def from_uri(cls, uri):
        return cls()

    def __init__(self):
        self._st = None
        self.__st = None

    def create(self):
        self.__st = {}

    def destroy(self):
        self.__st = None

    def open(self):
        self._st = self.__st

    def close(self):
        self._st = None

    def __iter__(self):
        for key in self._st:
            yield key

    def __delitem__(self, key):
        del self._st[key]

    def __getitem__(self, key):
        return self._st[key]

    def __setitem__(self, key, value):
        self._st[key] = value

class FileStore(FileMutableStoreMixin, BytesStore, FileMutableStoreBase):
    """memory FileStore"""