google-code-export / objectify-appengine

Automatically exported from code.google.com/p/objectify-appengine
MIT License
1 stars 0 forks source link

Feature Request: Allow option to have @Serialized fields be stored zipped. #101

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
To save space in the datastore, it should be fairly easy to have a @Serialized 
field for an object have an option  (e.g.: @Serialized(zipped=true)) that will 
have objectify transparently zip and unzip the blob field it stores the 
@Serialized objects in.

Some quick code you could use (after cleaning it up) to do this:

import java.util.*;
import java.util.zip.*;

public static byte[] zip(byte[] b, int compressionlevel) {
      Deflater compressor = new Deflater(compressionlevel);
      compressor.setInput(b);
      compressor.finish();

      ByteArrayOutputStream bos = new ByteArrayOutputStream(b.length);

      byte[] buf = new byte[1024];
      while (!compressor.finished()) {
         int count = compressor.deflate(buf);
         bos.write(buf, 0, count);
      }
      try {
         bos.close();
      } catch (IOException ignored) {
      }

      byte[] compressedData = bos.toByteArray();
      return compressedData;
   }

public static synchronized byte[] unzip(byte[] b) {
      if (b == null) {
         return null;
      }
      Inflater decompressor = new Inflater();
      decompressor.setInput(b);
      ByteArrayOutputStream bos = new ByteArrayOutputStream(b.length);

      try {
         byte[] buf = new byte[1024];
         while (!decompressor.finished()) {
            try {
               int count = decompressor.inflate(buf);
               if (count <= 0) {
                  break;
               }
               bos.write(buf, 0, count);
            } catch (DataFormatException e) {
               return null;
            }
         }

         byte[] decompressedData = bos.toByteArray();
         return decompressedData;
      } finally {
         try {
            decompressor.end();
            bos.close();
         } catch (IOException ignored) {
            decompressor = new Inflater();
         }
      }
   }

Original issue reported on code.google.com by kwste...@gmail.com on 26 Aug 2011 at 1:26

GoogleCodeExporter commented 9 years ago

Original comment by lhori...@gmail.com on 7 Sep 2011 at 6:28

GoogleCodeExporter commented 9 years ago
This will make it into Objectify 4.0 release.  @Serialize(zip=true)

Original comment by lhori...@gmail.com on 16 Dec 2011 at 2:40

GoogleCodeExporter commented 9 years ago
This has now been implemented in trunk

Original comment by lhori...@gmail.com on 17 Dec 2011 at 6:55