瀏覽代碼

Compress Bson fields

When using the replicator, we need to save the previous state of
the disk in the DB. Depending on disk size and chunk size, that
can add up to a lot of data. This change compresses the json using
zlib to ease storage needs on the DB.
Gabriel-Adrian Samfira 6 年之前
父節點
當前提交
e1c4c96fc4
共有 1 個文件被更改,包括 9 次插入2 次删除
  1. 9 2
      coriolis/db/sqlalchemy/types.py

+ 9 - 2
coriolis/db/sqlalchemy/types.py

@@ -14,6 +14,7 @@
 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 #    License for the specific language governing permissions and limitations
 #    License for the specific language governing permissions and limitations
 #    under the License.
 #    under the License.
+import zlib
 
 
 from oslo_serialization import jsonutils
 from oslo_serialization import jsonutils
 from sqlalchemy.dialects import mysql
 from sqlalchemy.dialects import mysql
@@ -54,12 +55,18 @@ class Json(LongText):
 class Bson(Blob):
 class Bson(Blob):
 
 
     def process_bind_param(self, value, dialect):
     def process_bind_param(self, value, dialect):
-        return jsonutils.dumps(value).encode('utf-8')
+        return zlib.compress(
+                jsonutils.dumps(value).encode('utf-8'))
 
 
     def process_result_value(self, value, dialect):
     def process_result_value(self, value, dialect):
         if value is None:
         if value is None:
             return None
             return None
-        return jsonutils.loads(value.decode('utf-8'))
+        data = None
+        try:
+            data = zlib.decompress(value)
+        except:
+            data = value
+        return jsonutils.loads(data)
 
 
 
 
 class List(types.TypeDecorator):
 class List(types.TypeDecorator):