go fmt for all source codes

This commit is contained in:
Chris Lu
2013-01-17 00:56:56 -08:00
parent ca9056d673
commit b0c7df0c3b
35 changed files with 478 additions and 493 deletions

View File

@@ -24,7 +24,6 @@ type Command struct {
// Flag is a set of flags specific to this command. // Flag is a set of flags specific to this command.
Flag flag.FlagSet Flag flag.FlagSet
} }
// Name returns the command's name: the first word in the usage line. // Name returns the command's name: the first word in the usage line.

View File

@@ -2,8 +2,8 @@ package main
import ( import (
"bufio" "bufio"
"os"
"fmt" "fmt"
"os"
) )
func init() { func init() {
@@ -18,8 +18,7 @@ var cmdShell = &Command{
`, `,
} }
var ( var ()
)
func runShell(command *Command, args []string) bool { func runShell(command *Command, args []string) bool {
r := bufio.NewReader(os.Stdin) r := bufio.NewReader(os.Stdin)
@@ -28,11 +27,11 @@ func runShell(command *Command, args []string) bool {
prompt := func() { prompt := func() {
o.WriteString("> ") o.WriteString("> ")
o.Flush() o.Flush()
}; }
readLine := func() string { readLine := func() string {
ret, err := r.ReadString('\n') ret, err := r.ReadString('\n')
if err != nil { if err != nil {
fmt.Fprint(e,err); fmt.Fprint(e, err)
os.Exit(1) os.Exit(1)
} }
return ret return ret

View File

@@ -156,7 +156,9 @@ func GetHandler(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip") w.Header().Set("Content-Encoding", "gzip")
} else { } else {
n.Data = storage.UnGzipData(n.Data) if n.Data, err = storage.UnGzipData(n.Data); err != nil {
debug("lookup error:", err, r.URL.Path)
}
} }
} }
} }

View File

@@ -3,8 +3,8 @@ package directory
import ( import (
"encoding/hex" "encoding/hex"
"pkg/storage" "pkg/storage"
"strings"
"pkg/util" "pkg/util"
"strings"
) )
type FileId struct { type FileId struct {

View File

@@ -1,8 +1,8 @@
package operation package operation
import ( import (
"net/http"
"log" "log"
"net/http"
) )
func Delete(url string) error { func Delete(url string) error {

View File

@@ -2,11 +2,11 @@ package operation
import ( import (
"encoding/json" "encoding/json"
"errors"
_ "fmt"
"net/url" "net/url"
"pkg/storage" "pkg/storage"
"pkg/util" "pkg/util"
_ "fmt"
"errors"
) )
type Location struct { type Location struct {

View File

@@ -3,13 +3,13 @@ package operation
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
_ "fmt" _ "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"errors"
) )
type UploadResult struct { type UploadResult struct {

View File

@@ -127,4 +127,3 @@ func TestReserveOneVolume(t *testing.T) {
t.Log("reserved", c) t.Log("reserved", c)
} }
} }

View File

@@ -2,10 +2,10 @@ package sequence
import ( import (
"encoding/gob" "encoding/gob"
"log"
"os" "os"
"path" "path"
"sync" "sync"
"log"
) )
const ( const (

View File

@@ -1,10 +1,10 @@
package storage package storage
import ( import (
"testing"
"log" "log"
"os" "os"
"pkg/util" "pkg/util"
"testing"
) )
func TestMemoryUsage(t *testing.T) { func TestMemoryUsage(t *testing.T) {

View File

@@ -15,25 +15,10 @@ func IsGzippable(ext, mtype string) bool {
if strings.HasPrefix(mtype, "text/") { if strings.HasPrefix(mtype, "text/") {
return true return true
} }
if ext == ".zip" { switch ext {
case ".zip", ".rar", ".gz", ".bz2", ".xz":
return false return false
} case ".pdf", ".txt", ".html", ".css", ".js", ".json":
if ext == ".rar" {
return false
}
if ext == ".gz" {
return false
}
if ext == ".pdf" {
return true
}
if ext == ".css" {
return true
}
if ext == ".js" {
return true
}
if ext == ".json" {
return true return true
} }
if strings.HasPrefix(mtype, "application/") { if strings.HasPrefix(mtype, "application/") {
@@ -46,18 +31,21 @@ func IsGzippable(ext, mtype string) bool {
} }
return false return false
} }
func GzipData(input []byte) []byte {
func GzipData(input []byte) ([]byte, error) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
w, _ := gzip.NewWriterLevel(buf, flate.BestCompression) w, _ := gzip.NewWriterLevel(buf, flate.BestCompression)
if _, err := w.Write(input); err != nil { if _, err := w.Write(input); err != nil {
println("error compressing data:", err) println("error compressing data:", err)
return nil, err
} }
if err := w.Close(); err != nil { if err := w.Close(); err != nil {
println("error closing compressed data:", err) println("error closing compressed data:", err)
return nil, err
} }
return buf.Bytes() return buf.Bytes(), nil
} }
func UnGzipData(input []byte) []byte { func UnGzipData(input []byte) ([]byte, error) {
buf := bytes.NewBuffer(input) buf := bytes.NewBuffer(input)
r, _ := gzip.NewReader(buf) r, _ := gzip.NewReader(buf)
defer r.Close() defer r.Close()
@@ -65,5 +53,5 @@ func UnGzipData(input []byte) []byte {
if err != nil { if err != nil {
println("error uncompressing data:", err) println("error uncompressing data:", err)
} }
return output return output, err
} }

View File

@@ -64,7 +64,9 @@ func NewNeedle(r *http.Request) (n *Needle, fname string, e error) {
mtype = contentType mtype = contentType
} }
if IsGzippable(ext, mtype) { if IsGzippable(ext, mtype) {
data = GzipData(data) if data, e = GzipData(data); e != nil {
return
}
n.SetGzipped() n.SetGzipped()
} }
if ext == ".gz" { if ext == ".gz" {

View File

@@ -65,13 +65,13 @@ func (s *Store) AddVolume(volumeListString string, replicationType string) error
} }
return e return e
} }
func (s *Store) addVolume(vid VolumeId, replicationType ReplicationType) error { func (s *Store) addVolume(vid VolumeId, replicationType ReplicationType) (err error) {
if s.volumes[vid] != nil { if s.volumes[vid] != nil {
return errors.New("Volume Id " + vid.String() + " already exists!") return errors.New("Volume Id " + vid.String() + " already exists!")
} }
log.Println("In dir", s.dir, "adds volume =", vid, ", replicationType =", replicationType) log.Println("In dir", s.dir, "adds volume =", vid, ", replicationType =", replicationType)
s.volumes[vid] = NewVolume(s.dir, vid, replicationType) s.volumes[vid], err = NewVolume(s.dir, vid, replicationType)
return nil return err
} }
func (s *Store) CheckCompactVolume(volumeIdString string, garbageThresholdString string) (error, bool) { func (s *Store) CheckCompactVolume(volumeIdString string, garbageThresholdString string) (error, bool) {
@@ -107,7 +107,7 @@ func (s *Store) loadExistingVolumes() {
base := name[:len(name)-len(".dat")] base := name[:len(name)-len(".dat")]
if vid, err := NewVolumeId(base); err == nil { if vid, err := NewVolumeId(base); err == nil {
if s.volumes[vid] == nil { if s.volumes[vid] == nil {
v := NewVolume(s.dir, vid, CopyNil) if v, e := NewVolume(s.dir, vid, CopyNil); e == nil {
s.volumes[vid] = v s.volumes[vid] = v
log.Println("In dir", s.dir, "read volume =", vid, "replicationType =", v.replicaType, "version =", v.version, "size =", v.Size()) log.Println("In dir", s.dir, "read volume =", vid, "replicationType =", v.replicaType, "version =", v.version, "size =", v.Size())
} }
@@ -116,6 +116,7 @@ func (s *Store) loadExistingVolumes() {
} }
} }
} }
}
func (s *Store) Status() []*VolumeInfo { func (s *Store) Status() []*VolumeInfo {
var stats []*VolumeInfo var stats []*VolumeInfo
for k, v := range s.volumes { for k, v := range s.volumes {

View File

@@ -24,9 +24,9 @@ type Volume struct {
accessLock sync.Mutex accessLock sync.Mutex
} }
func NewVolume(dirname string, id VolumeId, replicationType ReplicationType) (v *Volume) { func NewVolume(dirname string, id VolumeId, replicationType ReplicationType) (v *Volume, e error) {
v = &Volume{dir: dirname, Id: id, replicaType: replicationType} v = &Volume{dir: dirname, Id: id, replicaType: replicationType}
v.load() e = v.load()
return return
} }
func (v *Volume) load() error { func (v *Volume) load() error {
@@ -79,21 +79,19 @@ func (v *Volume) maybeWriteSuperBlock() {
v.dataFile.Write(header) v.dataFile.Write(header)
} }
} }
func (v *Volume) readSuperBlock() error { func (v *Volume) readSuperBlock() (err error) {
v.dataFile.Seek(0, 0) v.dataFile.Seek(0, 0)
header := make([]byte, SuperBlockSize) header := make([]byte, SuperBlockSize)
if _, e := v.dataFile.Read(header); e != nil { if _, e := v.dataFile.Read(header); e != nil {
return fmt.Errorf("cannot read superblock: %s", e) return fmt.Errorf("cannot read superblock: %s", e)
} }
var err error
v.version, v.replicaType, err = ParseSuperBlock(header) v.version, v.replicaType, err = ParseSuperBlock(header)
return err return err
} }
func ParseSuperBlock(header []byte) (version Version, replicaType ReplicationType, e error) { func ParseSuperBlock(header []byte) (version Version, replicaType ReplicationType, err error) {
version = Version(header[0]) version = Version(header[0])
var err error
if replicaType, err = NewReplicationTypeFromByte(header[1]); err != nil { if replicaType, err = NewReplicationTypeFromByte(header[1]); err != nil {
e = fmt.Errorf("cannot read replica type: %s", err) err = fmt.Errorf("cannot read replica type: %s", err)
} }
return return
} }

View File

@@ -5,6 +5,7 @@ import (
) )
type VolumeId uint32 type VolumeId uint32
func NewVolumeId(vid string) (VolumeId, error) { func NewVolumeId(vid string) (VolumeId, error) {
volumeId, err := strconv.ParseUint(vid, 10, 64) volumeId, err := strconv.ParseUint(vid, 10, 64)
return VolumeId(volumeId), err return VolumeId(volumeId), err

View File

@@ -1,7 +1,6 @@
package storage package storage
import ( import ()
)
type Version uint8 type Version uint8

View File

@@ -1,7 +1,6 @@
package topology package topology
import ( import ()
)
type DataCenter struct { type DataCenter struct {
NodeImpl NodeImpl

View File

@@ -1,9 +1,9 @@
package topology package topology
import ( import (
_ "fmt"
"strconv" "strconv"
"testing" "testing"
_ "fmt"
) )
func TestXYZ(t *testing.T) { func TestXYZ(t *testing.T) {

View File

@@ -1,7 +1,6 @@
package topology package topology
import ( import ()
)
func (t *Topology) ToMap() interface{} { func (t *Topology) ToMap() interface{} {
m := make(map[string]interface{}) m := make(map[string]interface{})

View File

@@ -31,4 +31,3 @@ func Uint32toBytes(b []byte, v uint32){
func Uint8toBytes(b []byte, v uint8) { func Uint8toBytes(b []byte, v uint8) {
b[0] = byte(v) b[0] = byte(v)
} }