Skip to content

Commit

Permalink
mgo: optimize seeking to end of GridFS file
Browse files Browse the repository at this point in the history
  • Loading branch information
rogpeppe committed Aug 12, 2014
1 parent dc255bb commit aec61dd
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 11 deletions.
6 changes: 3 additions & 3 deletions bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ package mgo
// http://blog.mongodb.org/post/84922794768/mongodbs-new-bulk-api
//
type Bulk struct {
c *Collection
ordered bool
c *Collection
ordered bool
inserts []interface{}
}

Expand Down Expand Up @@ -44,7 +44,7 @@ func (c *Collection) Bulk() *Bulk {
}

// Unordered puts the bulk operation in unordered mode.
//
//
// In unordered mode the indvidual operations may be sent
// out of order, which means latter operations may proceed
// even if prior ones have failed.
Expand Down
2 changes: 1 addition & 1 deletion cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func (cluster *mongoCluster) server(addr string, tcpaddr *net.TCPAddr) *mongoSer

func resolveAddr(addr string) (*net.TCPAddr, error) {
// This hack allows having a timeout on resolution.
conn, err := net.DialTimeout("udp", addr, 10 * time.Second)
conn, err := net.DialTimeout("udp", addr, 10*time.Second)
if err != nil {
log("SYNC Failed to resolve server address: ", addr)
return nil, errors.New("failed to resolve server address: " + addr)
Expand Down
2 changes: 1 addition & 1 deletion cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ func (s *S) TestPoolLimitSimple(c *C) {
// Put the one socket back in the pool, freeing it for the copy.
session.Refresh()
delay := <-done
c.Assert(delay > 300 * time.Millisecond, Equals, true, Commentf("Delay: %s", delay))
c.Assert(delay > 300*time.Millisecond, Equals, true, Commentf("Delay: %s", delay))
}
}

Expand Down
8 changes: 8 additions & 0 deletions gridfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,14 @@ func (file *GridFile) Seek(offset int64, whence int) (pos int64, err error) {
if offset > file.doc.Length {
return file.offset, errors.New("seek past end of file")
}
if offset == file.doc.Length {
// If we're seeking to the end of the file,
// no need to read anything. This enables
// a client to find the size of the file using only the
// io.ReadSeeker interface with low overhead.
file.offset = offset
return file.offset, nil
}
chunk := int(offset / int64(file.doc.ChunkSize))
if chunk+1 == file.chunk && offset >= file.offset {
file.rbuf = file.rbuf[int(offset-file.offset):]
Expand Down
9 changes: 8 additions & 1 deletion gridfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import (
"os"
"time"

. "gopkg.in/check.v1"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
. "gopkg.in/check.v1"
)

func (s *S) TestGridFSCreate(c *C) {
Expand Down Expand Up @@ -485,6 +485,13 @@ func (s *S) TestGridFSSeek(c *C) {
c.Assert(err, IsNil)
c.Assert(b, DeepEquals, []byte("nopqr"))

o, err = file.Seek(0, os.SEEK_END)
c.Assert(err, IsNil)
c.Assert(o, Equals, int64(22))
n, err = file.Read(b)
c.Assert(err, Equals, io.EOF)
c.Assert(n, Equals, 0)

o, err = file.Seek(-10, os.SEEK_END)
c.Assert(err, IsNil)
c.Assert(o, Equals, int64(12))
Expand Down
6 changes: 3 additions & 3 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ type log_Logger interface {
}

var (
globalLogger log_Logger
globalDebug bool
globalMutex sync.Mutex
globalLogger log_Logger
globalDebug bool
globalMutex sync.Mutex
)

// RACE WARNING: There are known data races when logging, which are manually
Expand Down
1 change: 0 additions & 1 deletion raceoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
package mgo

const raceDetector = false

2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (server *mongoServer) pinger(loop bool) {
time.Sleep(delay)
}
op := op
socket, _, err := server.AcquireSocket(0, 3 * delay)
socket, _, err := server.AcquireSocket(0, 3*delay)
if err == nil {
start := time.Now()
_, _ = socket.SimpleQuery(&op)
Expand Down

0 comments on commit aec61dd

Please sign in to comment.