Skip to content

Commit

Permalink
gzip data (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccbrown authored Jun 18, 2020
1 parent e1bfd9d commit dcae1c5
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions server/database.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package server

import (
"bytes"
"compress/gzip"
"encoding/binary"
"io/ioutil"

json "github.com/json-iterator/go"
)
Expand All @@ -18,11 +21,16 @@ type Database interface {
Close() error
}

const gzipMarker = 0

func marshalActivity(a Activity) (key, value []byte, err error) {
buf, err := json.Marshal(a)
if err != nil {
buf := &bytes.Buffer{}
buf.Write([]byte{gzipMarker})
w := gzip.NewWriter(buf)
if err := json.NewEncoder(w).Encode(a); err != nil {
return nil, nil, err
}
w.Close()
k := make([]byte, 10)
binary.BigEndian.PutUint64(k, uint64(a.ActivityTime().Unix())<<24)
switch a.(type) {
Expand All @@ -34,10 +42,23 @@ func marshalActivity(a Activity) (key, value []byte, err error) {
k[5] = RedditPostType
}
binary.BigEndian.PutUint32(k[6:], a.ActivityKey())
return k, buf, nil
return k, buf.Bytes(), nil
}

func unmarshalActivity(key, value []byte) (Activity, error) {
if len(value) > 0 && value[0] == gzipMarker {
r, err := gzip.NewReader(bytes.NewReader(value[1:]))
if err != nil {
return nil, err
}
defer r.Close()
buf, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
value = buf
}

switch key[5] {
case ForumPostType:
post := &ForumPost{}
Expand Down

0 comments on commit dcae1c5

Please sign in to comment.