Skip to content

Commit

Permalink
make gofmt
Browse files Browse the repository at this point in the history
  • Loading branch information
niemeyer committed Jan 13, 2011
1 parent e29066f commit 7e135a8
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 179 deletions.
22 changes: 13 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ GOFILES=\

include $(GOROOT)/src/Make.pkg

format:
${GOFMT} -w session.go
${GOFMT} -w cluster.go
${GOFMT} -w server.go
${GOFMT} -w socket.go
${GOFMT} -w stats.go
${GOFMT} -w log.go
${GOFMT} -w mongogo_test.go

testpackage: _testdb

_testdb:
Expand All @@ -36,3 +27,16 @@ stopdb:
@testdb/setup.sh stop

clean: stopdb

GOFMT=gofmt -spaces=true -tabwidth=4 -tabindent=false

BADFMT=$(shell $(GOFMT) -l $(GOFILES) $(wildcard *_test.go))

gofmt: $(BADFMT)
@for F in $(BADFMT); do $(GOFMT) -w $$F && echo $$F; done

ifneq ($(BADFMT),)
ifneq ($(MAKECMDGOALS),gofmt)
$(warning WARNING: make gofmt: $(BADFMT))
endif
endif
28 changes: 14 additions & 14 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package mongogo_test


import (
. "gocheck"
B "gobson"
. "gocheck"
B "gobson"
"mongogo"
)

Expand Down Expand Up @@ -42,7 +42,7 @@ func (s *S) TestTopologySyncWithSlaveSeed(c *C) {
coll := session.DB("mydb").C("mycollection")
coll.Insert(B.M{"a": 1, "b": 2})

result := struct{Ok bool}{}
result := struct{ Ok bool }{}
err = session.Run("getLastError", &result)
c.Assert(err, IsNil)
c.Assert(result.Ok, Equals, true)
Expand All @@ -67,7 +67,7 @@ func (s *S) TestRunString(c *C) {
session, err := mongogo.Mongo("localhost:40001")
c.Assert(err, IsNil)

result := struct{Ok int}{}
result := struct{ Ok int }{}
err = session.Run("ping", &result)
c.Assert(err, IsNil)
c.Assert(result.Ok, Equals, 1)
Expand All @@ -77,7 +77,7 @@ func (s *S) TestRunValue(c *C) {
session, err := mongogo.Mongo("localhost:40001")
c.Assert(err, IsNil)

result := struct{Ok int}{}
result := struct{ Ok int }{}
err = session.Run(B.M{"ping": 1}, &result)
c.Assert(err, IsNil)
c.Assert(result.Ok, Equals, 1)
Expand All @@ -90,7 +90,7 @@ func (s *S) TestInsertFindOne(c *C) {
coll := session.DB("mydb").C("mycollection")
coll.Insert(B.M{"a": 1, "b": 2})

result := struct{A, B int}{}
result := struct{ A, B int }{}

err = coll.Find(B.M{"a": 1}).One(&result)
c.Assert(err, IsNil)
Expand All @@ -117,7 +117,7 @@ func (s *S) TestInsertFindOneNotFound(c *C) {

coll := session.DB("mydb").C("mycollection")

result := struct{A, B int}{}
result := struct{ A, B int }{}
err = coll.Find(B.M{"a": 1}).One(&result)
c.Assert(err, Equals, mongogo.NotFound)
c.Assert(err, Matches, "Document not found")
Expand Down Expand Up @@ -150,7 +150,7 @@ func (s *S) TestInsertFindIter(c *C) {
}

n := len(m)
result := struct{N int}{}
result := struct{ N int }{}
for i := 0; i != n; i++ {
err = iter.Next(&result)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -206,7 +206,7 @@ func (s *S) TestSort(c *C) {
c.Assert(err, IsNil)

l := make([]int, 18)
r := struct{A, B int}{}
r := struct{ A, B int }{}
for i := 0; i != len(l); i += 2 {
err := iter.Next(&r)
c.Assert(err, IsNil)
Expand All @@ -215,7 +215,7 @@ func (s *S) TestSort(c *C) {
}

c.Assert(l, Equals,
[]int{0, 2, 1, 2, 2, 2, 0, 1, 1, 1, 2, 1, 0, 0, 1, 0, 2, 0})
[]int{0, 2, 1, 2, 2, 2, 0, 1, 1, 1, 2, 1, 0, 0, 1, 0, 2, 0})
}

func (s *S) TestBadSortWithMap(c *C) {
Expand All @@ -226,7 +226,7 @@ func (s *S) TestBadSortWithMap(c *C) {
defer func() {
error := recover()
c.Assert(error, Matches,
"Can't sort using map of len > 1. Use gobson.D instead.")
"Can't sort using map of len > 1. Use gobson.D instead.")
}()
coll.Find(B.M{}).Sort(B.M{"a": 1, "b": 2})
}
Expand All @@ -248,7 +248,7 @@ func (s *S) TestInsertFindIterTwiceWithSameQuery(c *C) {
result2, err := query.Skip(2).Iter()
c.Assert(err, IsNil)

result := struct{N int}{}
result := struct{ N int }{}
err = result2.Next(&result)
c.Assert(err, IsNil)
c.Assert(result.N, Equals, 42)
Expand All @@ -267,7 +267,7 @@ func (s *S) TestInsertFindIterWithoutResults(c *C) {
iter, err := coll.Find(B.M{"n": 0}).Iter()
c.Assert(err, IsNil)

result := struct{N int}{}
result := struct{ N int }{}
err = iter.Next(&result)
c.Assert(result.N, Equals, 0)
c.Assert(err == mongogo.NotFound, Equals, true)
Expand Down Expand Up @@ -314,7 +314,7 @@ func (s *S) TestPrefetching(c *C) {
nextn = 73
}

result := struct{N int}{}
result := struct{ N int }{}
for i := 0; i != nextn; i++ {
iter.Next(&result)
}
Expand Down
40 changes: 22 additions & 18 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)



// ---------------------------------------------------------------------------
// Entry point function to the cluster/session/server/socket hierarchy.

Expand All @@ -16,7 +15,7 @@ import (
// so the seed servers are used only to find out about the cluster topology.
func Mongo(servers string) (session *Session, err os.Error) {
userSeeds := strings.Split(servers, ",", -1)
cluster := &mongoCluster{userSeeds:userSeeds}
cluster := &mongoCluster{userSeeds: userSeeds}
cluster.masterSynced.M = (*rlocker)(&cluster.RWMutex)
go cluster.syncServers()
session = newSession(Strong, cluster, nil)
Expand All @@ -33,34 +32,33 @@ func Mongo(servers string) (session *Session, err os.Error) {

type mongoCluster struct {
sync.RWMutex
masterSynced cond
masterSynced cond
userSeeds, dynaSeeds []string
servers mongoServers
masters mongoServers
slaves mongoServers
servers mongoServers
masters mongoServers
slaves mongoServers
}

func (cluster *mongoCluster) removeServer(server *mongoServer) {
cluster.Lock()
removed := cluster.servers.Remove(server) ||
cluster.masters.Remove(server) ||
cluster.slaves.Remove(server)
cluster.masters.Remove(server) ||
cluster.slaves.Remove(server)
if removed {
log("Removing server ", server.Addr, " from cluster.")
}
cluster.Unlock()
}

type isMasterResult struct {
IsMaster bool
IsMaster bool
Secondary bool
Primary string
Hosts []string
Passives []string
Primary string
Hosts []string
Passives []string
}

func (cluster *mongoCluster) syncServer(server *mongoServer) (
hosts []string, err os.Error) {
func (cluster *mongoCluster) syncServer(server *mongoServer) (hosts []string, err os.Error) {

addr := server.Addr

Expand Down Expand Up @@ -152,7 +150,7 @@ func (cluster *mongoCluster) mergeServer(server *mongoServer) {

func (cluster *mongoCluster) getKnownAddrs() []string {
cluster.RLock()
max := len(cluster.userSeeds)+len(cluster.dynaSeeds)+cluster.servers.Len()
max := len(cluster.userSeeds) + len(cluster.dynaSeeds) + cluster.servers.Len()
seen := make(map[string]bool, max)
known := make([]string, 0, max)

Expand All @@ -163,9 +161,15 @@ func (cluster *mongoCluster) getKnownAddrs() []string {
}
}

for _, addr := range cluster.userSeeds { add(addr) }
for _, addr := range cluster.dynaSeeds { add(addr) }
for _, serv := range cluster.servers.Slice() { add(serv.Addr) }
for _, addr := range cluster.userSeeds {
add(addr)
}
for _, addr := range cluster.dynaSeeds {
add(addr)
}
for _, serv := range cluster.servers.Slice() {
add(serv.Addr)
}
cluster.RUnlock()

return known
Expand Down
69 changes: 35 additions & 34 deletions cond.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,30 @@ import (


type Locker interface {
Lock()
Unlock()
Lock()
Unlock()
}

type rlocker sync.RWMutex

func (r *rlocker) Lock() { (*sync.RWMutex)(r).RLock() }
func (r *rlocker) Unlock() { (*sync.RWMutex)(r).RUnlock() }

// Cond enables waiting reliably for an arbitrary condition to become
// true. The M Locker object must be provided and will be unlocked and
// locked on calls to Wait().
type cond struct {
M Locker
m sync.Mutex
n uint32
sema *uint32
M Locker
m sync.Mutex
n uint32
sema *uint32
}

// NewCond returns a new Cond variable with M initialized to m. The Mutex and
// RWMutex values implement the Locker interface and are thus suitable as an
// argument.
func newCond(m Locker) *cond {
return &cond{M: m}
return &cond{M: m}
}

// Wait blocks the calling goroutine and waits until it is awaken by
Expand All @@ -39,41 +40,41 @@ func newCond(m Locker) *cond {
// c.M.Lock() on it first. M will be unlocked before sleeping, and will
// be reacquired before Wait returns.
func (c *cond) Wait() {
c.m.Lock()
if c.sema == nil {
c.sema = new(uint32)
}
s := c.sema
c.n++
c.m.Unlock()
c.M.Unlock()
runtime.Semacquire(s)
c.M.Lock()
c.m.Lock()
if c.sema == nil {
c.sema = new(uint32)
}
s := c.sema
c.n++
c.m.Unlock()
c.M.Unlock()
runtime.Semacquire(s)
c.M.Lock()
}

// Signal awakes one goroutine currently waiting on the condition variable.
// There is no guarantee about which goroutine will be awaken first.
func (c *cond) Signal() {
c.m.Lock()
if c.n > 0 {
c.n--
runtime.Semrelease(c.sema)
}
c.m.Unlock()
c.m.Lock()
if c.n > 0 {
c.n--
runtime.Semrelease(c.sema)
}
c.m.Unlock()
}

// Broadcast awakes all goroutines currently waiting on the condition
// variable.
func (c *cond) Broadcast() {
c.m.Lock()
n := c.n
if n > 0 {
s := c.sema
for i := uint32(0); i < n; i++ {
runtime.Semrelease(s)
}
c.sema = nil // Prevent races.
c.n = 0
}
c.m.Unlock()
c.m.Lock()
n := c.n
if n > 0 {
s := c.sema
for i := uint32(0); i < n; i++ {
runtime.Semrelease(s)
}
c.sema = nil // Prevent races.
c.n = 0
}
c.m.Unlock()
}
2 changes: 1 addition & 1 deletion queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package mongogo


type queue struct {
elems []interface{}
elems []interface{}
nelems, popi, pushi int
}

Expand Down
Loading

0 comments on commit 7e135a8

Please sign in to comment.