From f64df95f2363bd811fdb474aab7bd74a36935130 Mon Sep 17 00:00:00 2001 From: Barak Michener Date: Thu, 16 Apr 2015 18:02:35 -0400 Subject: [PATCH] move decompressor to internal --- cayley.go | 3 ++- cayley_test.go | 3 ++- http/write.go | 3 ++- internal/decompressor.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ quad/decompressor.go | 46 ---------------------------------------------- 5 files changed, 52 insertions(+), 49 deletions(-) create mode 100644 internal/decompressor.go delete mode 100644 quad/decompressor.go diff --git a/cayley.go b/cayley.go index eae0ff9..41b3244 100644 --- a/cayley.go +++ b/cayley.go @@ -33,6 +33,7 @@ import ( "github.com/google/cayley/db" "github.com/google/cayley/graph" "github.com/google/cayley/http" + "github.com/google/cayley/internal" "github.com/google/cayley/quad" "github.com/google/cayley/quad/cquads" "github.com/google/cayley/quad/nquads" @@ -286,7 +287,7 @@ func decompressAndLoad(qw graph.QuadWriter, cfg *config.Config, path, typ string r = res.Body } - r, err = quad.Decompressor(r) + r, err = internal.Decompressor(r) if err != nil { if err == io.EOF { return nil diff --git a/cayley_test.go b/cayley_test.go index 18cae90..f488a3c 100644 --- a/cayley_test.go +++ b/cayley_test.go @@ -32,6 +32,7 @@ import ( "github.com/google/cayley/config" "github.com/google/cayley/db" "github.com/google/cayley/graph" + "github.com/google/cayley/internal" "github.com/google/cayley/quad" "github.com/google/cayley/query/gremlin" ) @@ -705,7 +706,7 @@ var testDecompressor = []struct { func TestDecompressor(t *testing.T) { for _, test := range testDecompressor { - r, err := decompressor(test.input) + r, err := internal.Decompressor(test.input) if err != test.err { t.Fatalf("Unexpected error for %s, got:%v expect:%v", test.message, err, test.err) } diff --git a/http/write.go b/http/write.go index 0c71f13..de2c24b 100644 --- a/http/write.go +++ b/http/write.go @@ -25,6 +25,7 @@ import ( "github.com/barakmich/glog" "github.com/julienschmidt/httprouter" + "github.com/google/cayley/internal" "github.com/google/cayley/quad" "github.com/google/cayley/quad/cquads" ) @@ -82,7 +83,7 @@ func (api *API) ServeV1WriteNQuad(w http.ResponseWriter, r *http.Request, params blockSize = int64(api.config.LoadSize) } - quadReader, err := quad.Decompressor(formFile) + quadReader, err := internal.Decompressor(formFile) // TODO(kortschak) Make this configurable from the web UI. dec := cquads.NewDecoder(quadReader) diff --git a/internal/decompressor.go b/internal/decompressor.go new file mode 100644 index 0000000..1d3ab00 --- /dev/null +++ b/internal/decompressor.go @@ -0,0 +1,46 @@ +// Copyright 2014 The Cayley Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package internal + +import ( + "bufio" + "bytes" + "compress/bzip2" + "compress/gzip" + "io" +) + +const ( + gzipMagic = "\x1f\x8b" + b2zipMagic = "BZh" +) + +// Decompressor detects the file type of an io.Reader between +// bzip, gzip, or raw quad file. +func Decompressor(r io.Reader) (io.Reader, error) { + br := bufio.NewReader(r) + buf, err := br.Peek(3) + if err != nil { + return nil, err + } + switch { + case bytes.Compare(buf[:2], []byte(gzipMagic)) == 0: + return gzip.NewReader(br) + case bytes.Compare(buf[:3], []byte(b2zipMagic)) == 0: + return bzip2.NewReader(br), nil + default: + return br, nil + } +} diff --git a/quad/decompressor.go b/quad/decompressor.go deleted file mode 100644 index 35ee714..0000000 --- a/quad/decompressor.go +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2014 The Cayley Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package quad - -import ( - "bufio" - "bytes" - "compress/bzip2" - "compress/gzip" - "io" -) - -const ( - gzipMagic = "\x1f\x8b" - b2zipMagic = "BZh" -) - -// Decompressor detects the file type of an io.Reader between -// bzip, gzip, or raw quad file. -func Decompressor(r io.Reader) (io.Reader, error) { - br := bufio.NewReader(r) - buf, err := br.Peek(3) - if err != nil { - return nil, err - } - switch { - case bytes.Compare(buf[:2], []byte(gzipMagic)) == 0: - return gzip.NewReader(br) - case bytes.Compare(buf[:3], []byte(b2zipMagic)) == 0: - return bzip2.NewReader(br), nil - default: - return br, nil - } -}