title: Simple version/history on ndb/Google App Engine
body: <p>I am looking to create a system for tracking versions (history) of content of ndb.Models/Expandos on Google App Engine (Python).</p>
  
  <p>The content can be relatively lengthy and there may be many versions, but diffs between versions may be quite small. I expect others have done something like this and I would like to know how they went about it and what principles may guide the design &amp; development.</p>
  
  <p>It is not known at the time of deployment what the attributes of the data models would be (e.g. "title", "content", "body", "dated", etc.), but the types are known (dates, text, etc).</p>
  
  <p>My initial thought is to have the arranged something like this:</p>
  
  <pre><code>from google.appengine.ext import ndb
  
  class Version(ndb.Expando):
    version_id = ndb.IntegerProperty()
    # dated, etc.
    # data properties are not known in advance, hence Expando
  
   class MyDoc(ndb.Model):
     head     = ndb.KeyProperty(kind=Version)
  
     instance = ndb.kind=Property(kind=Version, repeated=True)
     # ^^^ may be a StructuredProperty?
  </code></pre>
  
  <p>The overview of the algorithms is:</p>
  
  <h3>Saving</h3>
  
  <p>Every time a user saves a document, put all the latest data into a new <code>Version</code> and point <code>head</code> to that instance.</p>
  
  <p>At that point, or some time after, go through old versions and change full saves to diffs (to save on space) with e.g. <a href="http://code.google.com/p/google-diff-match-patch/" rel="nofollow">diff-match-patch</a>. I would expect one full save per hour, day or some set time - or some set number of diffs.</p>
  
  <h3>Loading</h3>
  
  <p>Loading <code>head</code> is trivial.</p>
  
  <p>Older versions would be marked as a full save or a diff, and depending on which the data may be returned directly or compiled from the diff.</p>
  
  <h3>Thoughts?</h3>
  
  <p>I am sure others have tackled this problem, and I would love to know what ideas and implementations are out there about it. Obviously, there are full version control systems such as Git, Mercurial and Subversion and CVS - but those are both overkill for the intended purpose and would not work on Google App Engine.</p>