#!/usr/bin/env python # encoding: utf-8 """ compressible_text_property.py A string property that will automatically be stored compressed if larger than a given length threshold Created by Michael Tyson on 2011-01-07. Copyright (c) 2011 A Tasty Pixel. All rights reserved. BSD LICENSE Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of the A Tasty Pixel nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ from google.appengine.ext import db from google.appengine.api import datastore_types import zlib Text = datastore_types.Text LENGTH_THRESHOLD = 500000 # Bytes EXPECTED_ZLIB_HEADER = u"x\x9c" class CompressibleTextProperty(db.TextProperty): """A string property that will automatically be stored compressed if larger than a given length threshold This is designed to be used with textual properties that may exceed App Engine's 1MB entity size limit. Note that, if compressed, property will not be searchable. """ def validate(self, value): """Validate text property; Nicked verbatim from TextProperty. Returns: A valid value. Raises: BadValueError if property is not instance of 'Text'. """ if value is not None and not isinstance(value, Text): try: value = db.Text(value) except TypeError, err: raise BadValueError('Property %s must be convertible ' 'to a Text instance (%s)' % (self.name, err)) value = super(db.TextProperty, self).validate(value) if value is not None and not isinstance(value, Text): raise BadValueError('Property %s must be a Text instance' % self.name) return value def get_value_for_datastore(self, model_instance): """For writing to the datastore: Performs compression if length is greater than the threshold""" value = super(CompressibleTextProperty, self).get_value_for_datastore(model_instance) if len(value) > LENGTH_THRESHOLD and not value.startswith(EXPECTED_ZLIB_HEADER): value = unicode(zlib.compress(value), 'ISO-8859-1') return Text(value) def make_value_from_datastore(self, value): """For reading from the datastore: Decompresses if compressed data detected""" if value is None: return None if value.startswith(EXPECTED_ZLIB_HEADER): value = zlib.decompress(value.encode('ISO-8859-1')) return value data_type = Text