[轉]Snappy壓縮庫安裝和使用之一


Snappy壓縮庫安裝和使用之一

原文地址:http://blog.csdn.net/luo6620378xu/article/details/8521223

    近日需要在畢業設計中引入一個壓縮庫,要求壓縮與解壓縮速度快,但是壓縮率可以不那么苛刻。查找資料發現Google的snappy庫比較合適,而且該庫開源,由C++寫成。所以就拿來使用一下,下面權作記錄。下面引出的任何涉及Google公司的源代碼,版權歸Google公司所有,我權作學習交流。文章安排如下,首先簡要介紹Snappy,之后安裝之,然后以實際例子介紹如何使用,接着bzip2和gzip做了性能比較,最后提出一些使用上面的疑問。

(一)簡要介紹

    去官網下載之http://code.google.com/p/snappy/。在Project Home處有這么一段英文,我想許多地方都引用和翻譯了這段。我也嘗試翻譯一下。

 

Snappy is a compression/decompression library. 

It does not aim for maximum compression,

 or compatibility with any other compression library;

 instead, it aims for veryhigh speeds and reasonable compression.

 For instance, compared to the fastest mode of zlib, 

Snappy is an order of magnitude faster for most inputs,

 but the resulting compressed files are anywhere from 20% to 100% bigger.

 On a single core of a Core i7 processor in 64-bit mode, 

Snappy compresses at about 250 MB/sec or more and

 decompresses at about 500 MB/sec or more.

Snappy is widely used inside Google, in everything from BigTable 

and MapReduce to our internal RPC systems.

 

 

譯文:Snappy是一個壓縮/解壓縮庫。它不是以最大壓縮率,或者與其他壓縮庫兼容為目標;它旨在獲得高速的壓縮和合理的壓縮率。例如,Snappy對大多數的輸入比zlib的最快模式要快幾個數量級,但是其壓縮過后的文件通常會比zlib大20%到100%。在Core i7的單核64位模式下,Snappy壓縮速度大概可以達到250MB/s或者更快,解壓縮可以達到大約500MB/s或更快。

Snappy在Google內部廣泛使用,從BigTable,MapReduce到公司內部的RPC系統。

 

 

(二)安裝過程

下面描述安裝過程:

    下載snappy-1.0.5.tar.gz,snappy的安裝過程與傳統的安裝過程一樣。解壓后的INSTALL文件有詳細的安裝說明。

gunzip snappy-1.0.5.tar.gz

tar xf snappy-1.0.5.tar

cd snappy-1.0.5

./configure 

make

make install

安裝完成后,生成的動態庫和靜態庫位於/usr/local/lib處,編程需要用到的頭文件位於/usr/local/include處。注意需要將這些庫文件cp至/usr/lib處,不然就算在鏈接的時候加上-L/usr/local/lib,在運行時也會報錯。./main: error while loading shared libraries: libsnappy.so.1: 

cannot open shared object file: No such file or directory

 

當然這是我的LD_LIBRARY_PATH環境變量的設置問題。

 

 

 

 

(三)使用snappy

解壓出來的README文件介紹了一簡單的使用方式。snappy是各種庫標示符所在的命名空間。C++使用需要包含#include <snappy.h>頭文件,C語言使用需要包含#include<snapyy-c.h>頭文件。Snappy使用較為簡單,我指的是跟bzip2的庫比起來。所有的函數接口都暴露在上述兩個頭文件中,頭文件中有詳細的使用說明,並有簡單的示例,而且英文通俗易懂。摘抄如下(Google公司版權所有):

snappy.h

 

[cpp]  view plain  copy
 
  1. // Copyright 2005 and onwards Google Inc.  
  2. //  
  3. // Redistribution and use in source and binary forms, with or without  
  4. // modification, are permitted provided that the following conditions are  
  5. // met:  
  6. //  
  7. //     * Redistributions of source code must retain the above copyright  
  8. // notice, this list of conditions and the following disclaimer.  
  9. //     * Redistributions in binary form must reproduce the above  
  10. // copyright notice, this list of conditions and the following disclaimer  
  11. // in the documentation and/or other materials provided with the  
  12. // distribution.  
  13. //     * Neither the name of Google Inc. nor the names of its  
  14. // contributors may be used to endorse or promote products derived from  
  15. // this software without specific prior written permission.  
  16. //  
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  
  18. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT  
  19. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  
  20. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  
  21. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  
  22. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT  
  23. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  
  24. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  
  25. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
  28. //  
  29. // A light-weight compression algorithm.  It is designed for speed of  
  30. // compression and decompression, rather than for the utmost in space  
  31. // savings.  
  32. //  
  33. // For getting better compression ratios when you are compressing data  
  34. // with long repeated sequences or compressing data that is similar to  
  35. // other data, while still compressing fast, you might look at first  
  36. // using BMDiff and then compressing the output of BMDiff with  
  37. // Snappy.  
  38.   
  39. #ifndef UTIL_SNAPPY_SNAPPY_H__  
  40. #define UTIL_SNAPPY_SNAPPY_H__  
  41.   
  42. #include <stddef.h>  
  43. #include <string>  
  44.   
  45. #include "snappy-stubs-public.h"  
  46.   
  47. namespace snappy {  
  48.   class Source;  
  49.   class Sink;  
  50.   
  51.   // ------------------------------------------------------------------------  
  52.   // Generic compression/decompression routines.  
  53.   // ------------------------------------------------------------------------  
  54.   
  55.   // Compress the bytes read from "*source" and append to "*sink". Return the  
  56.   // number of bytes written.  
  57.   size_t Compress(Source* source, Sink* sink);  
  58.   
  59.   bool GetUncompressedLength(Source* source, uint32* result);  
  60.   
  61.   // ------------------------------------------------------------------------  
  62.   // Higher-level string based routines (should be sufficient for most users)  
  63.   // ------------------------------------------------------------------------  
  64.   
  65.   // Sets "*output" to the compressed version of "input[0,input_length-1]".  
  66.   // Original contents of *output are lost.  
  67.   //  
  68.   // REQUIRES: "input[]" is not an alias of "*output".  
  69.   size_t Compress(const char* input, size_t input_length, string* output);  
  70.   
  71.   // Decompresses "compressed[0,compressed_length-1]" to "*uncompressed".  
  72.   // Original contents of "*uncompressed" are lost.  
  73.   //  
  74.   // REQUIRES: "compressed[]" is not an alias of "*uncompressed".  
  75.   //  
  76.   // returns false if the message is corrupted and could not be decompressed  
  77.   bool Uncompress(const char* compressed, size_t compressed_length,  
  78.                   string* uncompressed);  
  79.   
  80.   
  81.   // ------------------------------------------------------------------------  
  82.   // Lower-level character array based routines.  May be useful for  
  83.   // efficiency reasons in certain circumstances.  
  84.   // ------------------------------------------------------------------------  
  85.   
  86.   // REQUIRES: "compressed" must point to an area of memory that is at  
  87.   // least "MaxCompressedLength(input_length)" bytes in length.  
  88.   //  
  89.   // Takes the data stored in "input[0..input_length]" and stores  
  90.   // it in the array pointed to by "compressed".  
  91.   //  
  92.   // "*compressed_length" is set to the length of the compressed output.  
  93.   //  
  94.   // Example:  
  95.   //    char* output = new char[snappy::MaxCompressedLength(input_length)];  
  96.   //    size_t output_length;  
  97.   //    RawCompress(input, input_length, output, &output_length);  
  98.   //    ... Process(output, output_length) ...  
  99.   //    delete [] output;  
  100.   void RawCompress(const char* input,  
  101.                    size_t input_length,  
  102.                    char* compressed,  
  103.                    size_t* compressed_length);  
  104.   
  105.   // Given data in "compressed[0..compressed_length-1]" generated by  
  106.   // calling the Snappy::Compress routine, this routine  
  107.   // stores the uncompressed data to  
  108.   //    uncompressed[0..GetUncompressedLength(compressed)-1]  
  109.   // returns false if the message is corrupted and could not be decrypted  
  110.   bool RawUncompress(const char* compressed, size_t compressed_length,  
  111.                      char* uncompressed);  
  112.   
  113.   // Given data from the byte source 'compressed' generated by calling  
  114.   // the Snappy::Compress routine, this routine stores the uncompressed  
  115.   // data to  
  116.   //    uncompressed[0..GetUncompressedLength(compressed,compressed_length)-1]  
  117.   // returns false if the message is corrupted and could not be decrypted  
  118.   bool RawUncompress(Source* compressed, char* uncompressed);  
  119.   
  120.   // Returns the maximal size of the compressed representation of  
  121.   // input data that is "source_bytes" bytes in length;  
  122.   size_t MaxCompressedLength(size_t source_bytes);  
  123.   
  124.   // REQUIRES: "compressed[]" was produced by RawCompress() or Compress()  
  125.   // Returns true and stores the length of the uncompressed data in  
  126.   // *result normally.  Returns false on parsing error.  
  127.   // This operation takes O(1) time.  
  128.   bool GetUncompressedLength(const char* compressed, size_t compressed_length,  
  129.                              size_t* result);  
  130.   
  131.   // Returns true iff the contents of "compressed[]" can be uncompressed  
  132.   // successfully.  Does not return the uncompressed data.  Takes  
  133.   // time proportional to compressed_length, but is usually at least  
  134.   // a factor of four faster than actual decompression.  
  135.   bool IsValidCompressedBuffer(const char* compressed,  
  136.                                size_t compressed_length);  
  137.   
  138.   // *** DO NOT CHANGE THE VALUE OF kBlockSize ***  
  139.   //  
  140.   // New Compression code chops up the input into blocks of at most  
  141.   // the following size.  This ensures that back-references in the  
  142.   // output never cross kBlockSize block boundaries.  This can be  
  143.   // helpful in implementing blocked decompression.  However the  
  144.   // decompression code should not rely on this guarantee since older  
  145.   // compression code may not obey it.  
  146.   static const int kBlockLog = 15;  
  147.   static const size_t kBlockSize = 1 << kBlockLog;  
  148.   
  149.   static const int kMaxHashTableBits = 14;  
  150.   static const size_t kMaxHashTableSize = 1 << kMaxHashTableBits;  
  151.   
  152. }  // end namespace snappy  
  153.   
  154.   
  155. #endif  // UTIL_SNAPPY_SNAPPY_H__  

 

 

snapp-c.h

 

 

[cpp]  view plain  copy
 
  1. /* 
  2.  * Copyright 2011 Martin Gieseking <martin.gieseking@uos.de>. 
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without 
  5.  * modification, are permitted provided that the following conditions are 
  6.  * met: 
  7.  * 
  8.  *     * Redistributions of source code must retain the above copyright 
  9.  * notice, this list of conditions and the following disclaimer. 
  10.  *     * Redistributions in binary form must reproduce the above 
  11.  * copyright notice, this list of conditions and the following disclaimer 
  12.  * in the documentation and/or other materials provided with the 
  13.  * distribution. 
  14.  *     * Neither the name of Google Inc. nor the names of its 
  15.  * contributors may be used to endorse or promote products derived from 
  16.  * this software without specific prior written permission. 
  17.  * 
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  19.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  20.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
  21.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
  22.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
  23.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
  24.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
  25.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
  26.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
  28.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
  29.  * 
  30.  * Plain C interface (a wrapper around the C++ implementation). 
  31.  */  
  32.   
  33. #ifndef UTIL_SNAPPY_OPENSOURCE_SNAPPY_C_H_  
  34. #define UTIL_SNAPPY_OPENSOURCE_SNAPPY_C_H_  
  35.   
  36. #ifdef __cplusplus  
  37. extern "C" {  
  38. #endif  
  39.   
  40. #include <stddef.h>  
  41.   
  42. /* 
  43.  * Return values; see the documentation for each function to know 
  44.  * what each can return. 
  45.  */  
  46. typedef enum {  
  47.   SNAPPY_OK = 0,  
  48.   SNAPPY_INVALID_INPUT = 1,  
  49.   SNAPPY_BUFFER_TOO_SMALL = 2,  
  50. } snappy_status;  
  51.   
  52. /* 
  53.  * Takes the data stored in "input[0..input_length-1]" and stores 
  54.  * it in the array pointed to by "compressed". 
  55.  * 
  56.  * <compressed_length> signals the space available in "compressed". 
  57.  * If it is not at least equal to "snappy_max_compressed_length(input_length)", 
  58.  * SNAPPY_BUFFER_TOO_SMALL is returned. After successful compression, 
  59.  * <compressed_length> contains the true length of the compressed output, 
  60.  * and SNAPPY_OK is returned. 
  61.  * 
  62.  * Example: 
  63.  *   size_t output_length = snappy_max_compressed_length(input_length); 
  64.  *   char* output = (char*)malloc(output_length); 
  65.  *   if (snappy_compress(input, input_length, output, &output_length) 
  66.  *       == SNAPPY_OK) { 
  67.  *     ... Process(output, output_length) ... 
  68.  *   } 
  69.  *   free(output); 
  70.  */  
  71. snappy_status snappy_compress(const char* input,  
  72.                               size_t input_length,  
  73.                               char* compressed,  
  74.                               size_t* compressed_length);  
  75.   
  76. /* 
  77.  * Given data in "compressed[0..compressed_length-1]" generated by 
  78.  * calling the snappy_compress routine, this routine stores 
  79.  * the uncompressed data to 
  80.  *   uncompressed[0..uncompressed_length-1]. 
  81.  * Returns failure (a value not equal to SNAPPY_OK) if the message 
  82.  * is corrupted and could not be decrypted. 
  83.  * 
  84.  * <uncompressed_length> signals the space available in "uncompressed". 
  85.  * If it is not at least equal to the value returned by 
  86.  * snappy_uncompressed_length for this stream, SNAPPY_BUFFER_TOO_SMALL 
  87.  * is returned. After successful decompression, <uncompressed_length> 
  88.  * contains the true length of the decompressed output. 
  89.  * 
  90.  * Example: 
  91.  *   size_t output_length; 
  92.  *   if (snappy_uncompressed_length(input, input_length, &output_length) 
  93.  *       != SNAPPY_OK) { 
  94.  *     ... fail ... 
  95.  *   } 
  96.  *   char* output = (char*)malloc(output_length); 
  97.  *   if (snappy_uncompress(input, input_length, output, &output_length) 
  98.  *       == SNAPPY_OK) { 
  99.  *     ... Process(output, output_length) ... 
  100.  *   } 
  101.  *   free(output); 
  102.  */  
  103. snappy_status snappy_uncompress(const char* compressed,  
  104.                                 size_t compressed_length,  
  105.                                 char* uncompressed,  
  106.                                 size_t* uncompressed_length);  
  107.   
  108. /* 
  109.  * Returns the maximal size of the compressed representation of 
  110.  * input data that is "source_length" bytes in length. 
  111.  */  
  112. size_t snappy_max_compressed_length(size_t source_length);  
  113.   
  114. /* 
  115.  * REQUIRES: "compressed[]" was produced by snappy_compress() 
  116.  * Returns SNAPPY_OK and stores the length of the uncompressed data in 
  117.  * *result normally. Returns SNAPPY_INVALID_INPUT on parsing error. 
  118.  * This operation takes O(1) time. 
  119.  */  
  120. snappy_status snappy_uncompressed_length(const char* compressed,  
  121.                                          size_t compressed_length,  
  122.                                          size_t* result);  
  123.   
  124. /* 
  125.  * Check if the contents of "compressed[]" can be uncompressed successfully. 
  126.  * Does not return the uncompressed data; if so, returns SNAPPY_OK, 
  127.  * or if not, returns SNAPPY_INVALID_INPUT. 
  128.  * Takes time proportional to compressed_length, but is usually at least a 
  129.  * factor of four faster than actual decompression. 
  130.  */  
  131. snappy_status snappy_validate_compressed_buffer(const char* compressed,  
  132.                                                 size_t compressed_length);  
  133.   
  134. #ifdef __cplusplus  
  135. }  // extern "C"  
  136. #endif  
  137.   
  138. #endif  /* UTIL_SNAPPY_OPENSOURCE_SNAPPY_C_H_ */  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM