This Storm document is included in Storm's source code at tests/infoheritance.txt so that it can be tested and be kept up-to-date.

Introduction

Storm doesn't support classes that have columns in multiple tables. This makes using inheritance rather difficult. The infoheritance pattern described here provides a way to get the benefits of inheritance without running into the problems Storm has with multi-table classes.

Defining a sample model

Let's consider an inheritance hierarchy to migrate to Storm.

   1 class Person(object):
   2 
   3     def __init__(self, name):
   4         self.name = name
   5 
   6 
   7 class SecretAgent(Person):
   8 
   9     def __init__(self, name, passcode):
  10         super(SecretAgent, self).__init__(name)
  11         self.passcode = passcode
  12 
  13 
  14 class Teacher(Person):
  15 
  16     def __init__(self, name, school):
  17         super(Employee, self).__init__(name):
  18         self.school = school

We want to use three tables to store data for these objects: person, secret_agent and teacher. We can't simply convert instance attributes to Storm properties and add __storm_table__ definitions because a single object may not have columns that come from more than one table. We can't have Teacher getting its name column from the person table and its school column from the teacher table, for example.

The infoheritance pattern

The infoheritance pattern uses composition instead of inheritance to work around the multiple table limitation. A base Storm class is used to represent all objects in the hierarchy. Each instance of this base class has an info property that yields an instance of a specific info class. An info class provides the additional data and behaviour you'd normally implement in a subclass. Following is the design from above converted to use the pattern.

   1 >>> from storm.locals import Storm, Store, Int, Unicode, Reference
   2 
   3 >>> person_info_types = {}
   4 
   5 >>> def register_person_info_type(info_type, info_class):
   6 ...     existing_info_class = person_info_types.get(info_type)
   7 ...     if existing_info_class is not None:
   8 ...         raise RuntimeError("%r has the same info_type of %r" %
   9 ...                            (info_class, existing_info_class))
  10 ...     person_info_types[info_type] = info_class
  11 ...     info_class.info_type = info_type
  12 
  13 
  14 >>> class Person(Storm):
  15 ...
  16 ...     __storm_table__ = "person"
  17 ...
  18 ...     id = Int(allow_none=False, primary=True)
  19 ...     name = Unicode(allow_none=False)
  20 ...     info_type = Int(allow_none=False)
  21 ...     _info = None
  22 ...
  23 ...     def __init__(self, store, name, info_class, **kwargs):
  24 ...         self.name = name
  25 ...         self.info_type = info_class.info_type
  26 ...         store.add(self)
  27 ...         self._info = info_class(self, **kwargs)
  28 ...
  29 ...     @property
  30 ...     def info(self):
  31 ...         if self._info is not None:
  32 ...             return self._info
  33 ...         assert self.id is not None
  34 ...         info_class = person_info_types[self.info_type]
  35 ...         if not hasattr(info_class, "__storm_table__"):
  36 ...             info = info_class.__new__(info_class)
  37 ...             info.person = self
  38 ...         else:
  39 ...             info = Store.of(self).get(info_class, self.id)
  40 ...         self._info = info
  41 ...         return info
  42 
  43 
  44 >>> class PersonInfo(object):
  45 ...
  46 ...     def __init__(self, person):
  47 ...         self.person = person
  48 
  49 
  50 >>> class StoredPersonInfo(PersonInfo):
  51 ...
  52 ...     person_id = Int(allow_none=False, primary=True)
  53 ...     person = Reference(person_id, Person.id)
  54 
  55 
  56 >>> class SecretAgent(StoredPersonInfo):
  57 ...
  58 ...     __storm_table__ = "secret_agent"
  59 ...
  60 ...     passcode = Unicode(allow_none=False)
  61 ...
  62 ...     def __init__(self, person, passcode=None):
  63 ...         super(SecretAgent, self).__init__(person)
  64 ...         self.passcode = passcode
  65 
  66 
  67 >>> class Teacher(StoredPersonInfo):
  68 ...
  69 ...     __storm_table__ = "teacher"
  70 ...
  71 ...     school = Unicode(allow_none=False)
  72 ...
  73 ...     def __init__(self, person, school=None):
  74 ...         super(Teacher, self).__init__(person)
  75 ...         self.school = school
  76 >>>

The pattern works by having a base class, Person, keep a reference to an info class, PersonInfo. Info classes need to be registered so that Person can discover them and load them when necessary. Note that info types have the same ID as their parent object. This isn't strictly necessary, but it makes certain things easy, such as being able to look up info objects directly by ID when given a person object. Person objects are required to be in a store to ensure that an ID is available and can used by the info class.

Registering info classes

Let's register our info classes. Each class must be registered with a unique info type key. This key is stored in the database, so be sure to use a stable value.

   1 >>> register_person_info_type(1, SecretAgent)
   2 >>> register_person_info_type(2, Teacher)
   3 >>>

Let's create a database to store person objects before we continue.

   1 >>> from storm.locals import create_database
   2 
   3 >>> database = create_database("sqlite:")
   4 >>> store = Store(database)
   5 >>> result = store.execute("""
   6 ...     CREATE TABLE person (
   7 ...         id INTEGER PRIMARY KEY,
   8 ...         info_type INTEGER NOT NULL,
   9 ...         name TEXT NOT NULL)
  10 ... """)
  11 >>> result = store.execute("""
  12 ...     CREATE TABLE secret_agent (
  13 ...         person_id INTEGER PRIMARY KEY,
  14 ...         passcode TEXT NOT NULL)
  15 ... """)
  16 >>> result = store.execute("""
  17 ...     CREATE TABLE teacher (
  18 ...         person_id INTEGER PRIMARY KEY,
  19 ...         school TEXT NOT NULL)
  20 ... """)
  21 >>>

Creating info classes

We can easily create person objects now.

   1 >>> secret_agent = Person(store, u"Dick Tracy",
   2 ...                       SecretAgent, passcode=u"secret!")
   3 >>> teacher = Person(store, u"Mrs. Cohen",
   4 ...                  Teacher, school=u"Cameron Elementary School")
   5 >>> store.commit()
   6 >>>

And we can easily find them again.

   1 >>> del secret_agent
   2 >>> del teacher
   3 >>> store.rollback()
   4 
   5 >>> [type(person.info) for person in store.find(Person).order_by(Person.name)]
   6 [<class 'SecretAgent'>, <class 'Teacher'>]
   7 >>>

Retrieving info classes

Now that we have our basic hierarchy in place we're going to want to retrieve objects by info type. Let's implement a function to make finding Persons easier.

   1 >>> def get_persons(store, info_classes=None):
   2 ...     where = []
   3 ...     if info_classes:
   4 ...         info_types = [info_class.info_type for info_class in info_classes]
   5 ...         where = [Person.info_type.is_in(info_types)]
   6 ...     result = store.find(Person, *where)
   7 ...     result.order_by(Person.name)
   8 ...     return result
   9 
  10 >>> secret_agent = get_persons(store, info_classes=[SecretAgent]).one()
  11 >>> secret_agent.name, secret_agent.info.passcode
  12 (u'Dick Tracy', u'secret!')
  13 
  14 >>> teacher = get_persons(store, info_classes=[Teacher]).one()
  15 >>> teacher.name, teacher.info.school
  16 (u'Mrs. Cohen', u'Cameron Elementary School')
  17 
  18 >>>

Great, we can easily find different kinds of Persons.

In-memory info objects

This design also allows for in-memory info objects. Let's add one to our hierarchy.

   1 >>> class Ghost(PersonInfo):
   2 ...
   3 ...     friendly = True
   4 
   5 >>> register_person_info_type(3, Ghost)
   6 >>>

We create and load in-memory objects the same way we do stored ones.

   1 >>> ghost = Person(store, u"Casper", Ghost)
   2 >>> store.commit()
   3 >>> del ghost
   4 >>> store.rollback()
   5 
   6 >>> ghost = get_persons(store, info_classes=[Ghost]).one()
   7 >>> ghost.name, ghost.info.friendly
   8 (u'Casper', True)
   9 
  10 >>>

This pattern is very handy when using Storm with code that would naturally be implemented using inheritance.

Infoheritance (last edited 2008-11-17 10:20:53 by smurf)