Просмотр исходного кода

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 лет назад
Родитель
Сommit
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
 #    License for the specific language governing permissions and limitations
 #    under the License.
+import zlib
 
 from oslo_serialization import jsonutils
 from sqlalchemy.dialects import mysql
@@ -54,12 +55,18 @@ class Json(LongText):
 class Bson(Blob):
 
     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):
         if value is 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):