Skip to content

Commit

Permalink
fix: currntly support delfate compression by using zlib
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad committed Sep 16, 2024
1 parent 617e53b commit e41c3e9
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
12 changes: 8 additions & 4 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package meilisearch

import (
"bytes"
"compress/flate"
"compress/gzip"
"compress/zlib"
"encoding/json"
"github.com/andybalholm/brotli"
"io"
Expand Down Expand Up @@ -38,7 +38,7 @@ func newEncoding(ce ContentEncoding, level EncodingCompressionLevel) encoder {
return &flateEncoder{
flWriterPool: &sync.Pool{
New: func() interface{} {
w, err := flate.NewWriter(io.Discard, level.Int())
w, err := zlib.NewWriterLevel(io.Discard, level.Int())
return &flateWriter{
writer: w,
err: err,
Expand Down Expand Up @@ -124,7 +124,7 @@ type flateEncoder struct {
}

type flateWriter struct {
writer *flate.Writer
writer *zlib.Writer
err error
}

Expand Down Expand Up @@ -152,7 +152,11 @@ func (d *flateEncoder) Encode(rc io.Reader) (*bytes.Buffer, error) {
}

func (d *flateEncoder) Decode(data []byte, vPtr interface{}) error {
r := flate.NewReader(bytes.NewBuffer(data))
r, err := zlib.NewReader(bytes.NewBuffer(data))
if err != nil {
return err
}

defer func() {
_ = r.Close()
}()
Expand Down
4 changes: 2 additions & 2 deletions encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"compress/flate"
"compress/gzip"
"compress/zlib"
"encoding/json"
"errors"
"github.com/andybalholm/brotli"
Expand Down Expand Up @@ -54,7 +55,6 @@ func Test_Encode_ErrorOnNewWriter(t *testing.T) {
New: func() interface{} {
return &flateWriter{
writer: nil,
err: errors.New("new writer error"),
}
},
},
Expand Down Expand Up @@ -93,7 +93,7 @@ func Test_Encode_ErrorInCopyZeroAlloc(t *testing.T) {
d := &flateEncoder{
flWriterPool: &sync.Pool{
New: func() interface{} {
w, err := flate.NewWriter(io.Discard, flate.DefaultCompression)
w, err := zlib.NewWriterLevel(io.Discard, flate.DefaultCompression)
return &flateWriter{
writer: w,
err: err,
Expand Down
24 changes: 24 additions & 0 deletions index_document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ func Test_AddOrUpdateDocumentsWithContentEncoding(t *testing.T) {
},
},
},
{
Name: "TestIndexBasicAddDocumentsWithDeflate",
ContentEncoding: DeflateEncoding,
Request: []map[string]interface{}{
{"ID": "123", "Name": "Pride and Prejudice"},
},
Response: struct {
WantResp *TaskInfo
DocResp DocumentsResult
}{WantResp: &TaskInfo{
TaskUID: 0,
Status: "enqueued",
Type: TaskTypeDocumentAdditionOrUpdate,
},
DocResp: DocumentsResult{
Results: []map[string]interface{}{
{"ID": "123", "Name": "Pride and Prejudice"},
},
Limit: 3,
Offset: 0,
Total: 1,
},
},
},
{
Name: "TestIndexBasicAddDocumentsWithBrotli",
ContentEncoding: BrotliEncoding,
Expand Down
33 changes: 33 additions & 0 deletions index_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,39 @@ func TestIndex_SearchWithContentEncoding(t *testing.T) {
Limit: 20,
},
},
{
Name: "SearchResultWithDeflateEncoding",
ContentEncoding: DeflateEncoding,
Query: "prince",
Request: &SearchRequest{
IndexUID: "indexUID",
},
Response: &SearchResponse{
Hits: []interface{}{
map[string]interface{}{
"book_id": float64(456), "title": "Le Petit Prince",
},
map[string]interface{}{
"Tag": "Epic fantasy", "book_id": float64(4), "title": "Harry Potter and the Half-Blood Prince",
},
},
EstimatedTotalHits: 2,
Offset: 0,
Limit: 20,
},
FacetRequest: &FacetSearchRequest{
FacetName: "tag",
FacetQuery: "Novel",
},
FacetResponse: &FacetSearchResponse{
FacetHits: []interface{}{
map[string]interface{}{
"value": "Novel", "count": float64(5),
},
},
FacetQuery: "Novel",
},
},
{
Name: "SearchResultWithBrotliEncoding",
ContentEncoding: BrotliEncoding,
Expand Down
6 changes: 6 additions & 0 deletions meilisearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2186,6 +2186,12 @@ func Test_ListIndex(t *testing.T) {
Indexes: []string{"foo", "bar"},
WantErr: false,
},
{
Name: "Basic get list of indexes with deflate encoding",
Indexes: []string{"foo", "bar"},
ContentEncoding: DeflateEncoding,
WantErr: false,
},
{
Name: "Basic get list of indexes with encoding",
Indexes: []string{"foo", "bar"},
Expand Down

0 comments on commit e41c3e9

Please sign in to comment.