コード例 #1
0
ファイル: msg.go プロジェクト: petar/ShelfLife
// AddMsg adds a new message to the database. The author is the currently
// logged in user. The message is attached to the object given by the string
// argument "AttachTo". Optionally, the message is in response to another message
// with message ID "ReplyTo". AddMsg returns the message ID of the newly added
// message, in the return field "ID".
func (a *API) AddMsg(args *rpc.Args, r *rpc.Ret) (err os.Error) {
	authorDoc, authorID, err := a.whoAmI(args)
	if err != nil {
		return err
	}
	attachTo, err := args.QueryString("AttachTo")
	if err != nil || attachTo == "" {
		return ErrArg
	}
	replyTo, _ := args.QueryString("ReplyTo")
	body, err := args.QueryString("Body")
	if err != nil || body == "" {
		return ErrArg
	}
	msgID, err := a.db.AddMsg(authorID, attachTo, ObjectIDOfWebString(replyTo), body)
	if err != nil {
		return err
	}
	j := msgJoinJSON{
		ID:        WebStringOfObjectID(msgID),
		Body:      body,
		AuthorID:  WebStringOfObjectID(authorID),
		AuthorNym: authorDoc.Login,
		AttachTo:  attachTo,
		ReplyTo:   replyTo,
		Modified:  time.NanosecondsToLocalTime(int64(bson.Now())).Format(msgFormat),
	}
	r.SetInterface("Msg", j)
	return nil
}
コード例 #2
0
ファイル: gridfs.go プロジェクト: petar/ShelfLife
func (file *GridFile) insertFile() {
	hexsum := hex.EncodeToString(file.wsum.Sum())
	for file.wpending > 0 {
		debugf("GridFile %p: waiting for %d pending chunks to insert file", file, file.wpending)
		file.c.Wait()
	}
	if file.err == nil {
		file.doc.UploadDate = bson.Now()
		file.doc.MD5 = hexsum
		file.err = file.gfs.Files.Insert(file.doc)
		file.gfs.Chunks.EnsureIndexKey([]string{"files_id", "n"})
	}
}
コード例 #3
0
ファイル: gridfs_test.go プロジェクト: petar/ShelfLife
func (s *S) TestGridFSCreate(c *C) {
	session, err := mgo.Mongo("localhost:40011")
	c.Assert(err, IsNil)
	defer session.Close()

	db := session.DB("mydb")

	before := bson.Now()

	gfs := db.GridFS("fs")
	file, err := gfs.Create("")
	c.Assert(err, IsNil)

	n, err := file.Write([]byte("some data"))
	c.Assert(err, IsNil)
	c.Assert(n, Equals, 9)

	err = file.Close()
	c.Assert(err, IsNil)

	after := bson.Now()

	// Check the file information.
	result := M{}
	err = db.C("fs.files").Find(nil).One(result)
	c.Assert(err, IsNil)

	fileId, ok := result["_id"].(bson.ObjectId)
	c.Assert(ok, Equals, true)
	c.Assert(fileId.Valid(), Equals, true)
	result["_id"] = "<id>"

	fileTs, ok := result["uploadDate"].(bson.Timestamp)
	c.Assert(ok, Equals, true)
	c.Assert(fileTs >= before && fileTs <= after, Equals, true)
	result["uploadDate"] = "<timestamp>"

	expected := M{
		"_id":        "<id>",
		"length":     9,
		"chunkSize":  262144,
		"uploadDate": "<timestamp>",
		"md5":        "1e50210a0202497fb79bc38b6ade6c34",
	}
	c.Assert(result, Equals, expected)

	// Check the chunk.
	result = M{}
	err = db.C("fs.chunks").Find(nil).One(result)
	c.Assert(err, IsNil)

	chunkId, ok := result["_id"].(bson.ObjectId)
	c.Assert(ok, Equals, true)
	c.Assert(chunkId.Valid(), Equals, true)
	result["_id"] = "<id>"

	expected = M{
		"_id":      "<id>",
		"files_id": fileId,
		"n":        0,
		"data":     []byte("some data"),
	}
	c.Assert(result, Equals, expected)

	// Check that an index was created.
	indexes, err := db.C("fs.chunks").Indexes()
	c.Assert(err, IsNil)
	c.Assert(len(indexes), Equals, 2)
	c.Assert(indexes[1].Key, Equals, []string{"files_id", "n"})
}