首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> J2EE

Java 實現資料庫字串欄位透明壓縮儲存:GZIP + JavaBean 攔截器模式

     編輯:J2EE

背景與動機

論壇、部落格等系統的內容欄位通常用 CLOB 儲存,極少參與 LIKE 查詢,且全文檢索建議用 Lucene 等專用引擎。這類大量文本資料壓縮率極高,若能透明壓縮入庫,可大幅節省儲存空間與 I/O 頻寬。

核心壓縮/解壓工具類

public class StringZip {
    public static String zipString(String str) {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            GZIPOutputStream os = new GZIPOutputStream(bos);
            os.write(str.getBytes("UTF-8"));  // 建議指定編碼
            os.close();
            bos.close();
            byte[] bs = bos.toByteArray();
            return new String(bs, "ISO-8859-1");  // 以單位節對應 Latin-1 保存二進位
        } catch (Exception ex) {
            return str;  // 失敗回退原文
        }
    }

    public static String unzipString(String str) {
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
            GZIPInputStream is = new GZIPInputStream(bis);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int len;
            while ((len = is.read(buf)) != -1) bos.write(buf, 0, len);
            is.close(); bis.close(); bos.close();
            return new String(bos.toByteArray(), "UTF-8");
        } catch (Exception ex) {
            return str;
        }
    }
}

JavaBean 透明攔截實現

在實體類別新增 ziped 標記欄位,於 Getter/Setter 內自動判斷壓縮與否:

private String content;
private boolean ziped;

public String getContent() {
    return ziped ? StringZip.unzipString(content) : content;
}

public void setContent(String content) {
    if (content == null || content.length() < 512) {  // 短文本不壓縮
        this.content = content;
        this.ziped = false;
    } else {
        String compressed = StringZip.zipString(content);
        if (compressed.length() >= content.length()) {  // 壓縮後反而變大
            this.content = content;
            this.ziped = false;
        } else {
            this.content = compressed;
            this.ziped = true;
        }
    }
}

關鍵設計考量

  • 長度門檻:短文本 (如 < 512 字元) 壓縮開銷大於收益,直接存原文。
  • 反向檢查:極少數情況 (如高度隨機字串) 壓縮後體積增大,需回退原文。
  • 編碼一致性:壓縮端用 UTF-8 轉 byte[],儲存時以 ISO-8859-1 將每個 byte 映射為單一 char,讀取時逆向操作,避免字元集轉換損壞二進位資料。
  • ORM 透明性:Hibernate/iBATIS 只見 String 欄位,無感知壓縮邏輯,零侵入遷移既有系統。

效能預期

一般文章、評論文本壓縮率可達 70%~90%,實際數值請依業務資料自行基準測試。CPU 開銷極低,換取儲存與網路頻寬收益顯著。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved