예제 #1
0
파일: submission.go 프로젝트: rakeen/cactus
func (s *Submission) Put() error {
	s.Modified = time.Now()

	if s.Id == 0 {
		s.Created = s.Modified

		b, err := bson.Marshal(s)
		if err != nil {
			return err
		}

		res, err := db.Exec("INSERT INTO submissions(author_id, struct, created, modified) VALUES(?, ?, ?, ?)", s.AuthorId, b, s.Created, s.Modified)
		if err != nil {
			return err
		}
		chZerk <- s.AuthorId

		s.Id, err = res.LastInsertId()
		return err

	} else {
		b, err := bson.Marshal(s)
		if err != nil {
			return err
		}

		_, err = db.Exec("UPDATE submissions SET author_id=?, struct=?, modified=? WHERE id=?", s.AuthorId, b, s.Modified, s.Id)
		chZerk <- s.AuthorId
		return err
	}
}
예제 #2
0
파일: standing.go 프로젝트: rakeen/cactus
func (s *Standing) Put() error {
	s.Modified = time.Now()

	if s.Id == 0 {
		s.Created = s.Modified

		b, err := bson.Marshal(s)
		if err != nil {
			return err
		}

		res, err := db.Exec("INSERT INTO standings(author_id, score, penalty, struct, created, modified) VALUES(?, ?, ?, ?, ?, ?)", s.AuthorId, s.Score, s.Penalty, b, s.Created, s.Modified)
		if err != nil {
			return err
		}

		s.Id, err = res.LastInsertId()
		return err

	} else {
		b, err := bson.Marshal(s)
		if err != nil {
			return err
		}

		_, err = db.Exec("UPDATE standings SET author_id=?, score=?, penalty=?, struct=?, modified=? WHERE id=?", s.AuthorId, s.Score, s.Penalty, b, s.Modified, s.Id)
		return err
	}
}
예제 #3
0
func (c *Clarification) Put() error {
	c.Modified = time.Now()

	if c.Id == 0 {
		c.Created = c.Modified

		b, err := bson.Marshal(c)
		if err != nil {
			return err
		}

		res, err := db.Exec("INSERT INTO clarifications(asker_id, response, struct, created, modified) VALUES(?, ?, ?, ?, ?)", c.AskerId, c.Response, b, c.Created, c.Modified)
		if err != nil {
			return err
		}

		c.Id, err = res.LastInsertId()
		return err

	} else {
		b, err := bson.Marshal(c)
		if err != nil {
			return err
		}

		_, err = db.Exec("UPDATE clarifications SET asker_id=?, response=?, struct=?, modified=? WHERE id=?", c.AskerId, c.Response, b, c.Modified, c.Id)
		return err
	}
}
예제 #4
0
파일: account.go 프로젝트: rakeen/cactus
func (a *Account) Put() error {
	a.Modified = time.Now()

	if a.Id == 0 {
		a.Created = a.Modified

		b, err := bson.Marshal(a)
		if err != nil {
			return err
		}

		res, err := db.Exec("INSERT INTO accounts(handle_lower, name_lower, level, struct, created, modified) VALUES(?, ?, ?, ?, ?, ?)", strings.ToLower(a.Handle), strings.ToLower(a.Name), a.Level, b, a.Created, a.Modified)
		if err != nil {
			return err
		}

		a.Id, err = res.LastInsertId()
		if err != nil {
			return err
		}
		chZerk <- a.Id
		return nil

	} else {
		b, err := bson.Marshal(a)
		if err != nil {
			return err
		}

		_, err = db.Exec("UPDATE accounts SET handle_lower=?, name_lower=?, level=?, struct=?, modified=? WHERE id=?", strings.ToLower(a.Handle), strings.ToLower(a.Name), a.Level, b, a.Modified, a.Id)
		chZerk <- a.Id
		return err
	}
}
예제 #5
0
파일: execution.go 프로젝트: rakeen/cactus
func (e *Execution) Put() error {
	e.Modified = time.Now()

	if e.Id == 0 {
		e.Created = e.Modified

		b, err := bson.Marshal(e)
		if err != nil {
			return err
		}

		res, err := db.Exec("INSERT INTO executions(struct, created, modified) VALUES(?, ?, ?)", b, e.Created, e.Modified)
		if err != nil {
			return err
		}

		e.Id, err = res.LastInsertId()
		return err

	} else {
		b, err := bson.Marshal(e)
		if err != nil {
			return err
		}

		_, err = db.Exec("UPDATE executions SET struct=?, modified=? WHERE id=?", b, e.Modified, e.Id)
		return err
	}
}
예제 #6
0
파일: problem.go 프로젝트: rakeen/cactus
func (p *Problem) Put() error {
	p.Modified = time.Now()

	if p.Id == 0 {
		p.Created = p.Modified

		b, err := bson.Marshal(p)
		if err != nil {
			return err
		}

		res, err := db.Exec("INSERT INTO problems(slug, char, struct, created, modified) VALUES(?, ?, ?, ?, ?)", p.Slug, p.Char, b, p.Created, p.Modified)
		if err != nil {
			return err
		}

		p.Id, err = res.LastInsertId()
		return err

	} else {
		b, err := bson.Marshal(p)
		if err != nil {
			return err
		}

		_, err = db.Exec("UPDATE problems SET slug=?, char=?, struct=?, modified=? WHERE id=?", p.Slug, p.Char, b, p.Modified, p.Id)
		return err
	}
}
예제 #7
0
func TestDecodeReadsOnlyOne(t *testing.T) {
	req := rpc.Request{
		ServiceMethod: "Foo.Bar",
		Seq:           3,
	}

	type T struct {
		Value string
	}

	tv := T{"test"}

	b, err := bson.Marshal(req)

	if err != nil {
		t.Fatal(err)
	}

	buf := bytes.NewBuffer(b)

	b, err = bson.Marshal(tv)

	if err != nil {
		t.Fatal(err)
	}

	buf.Write(b)
	dec := NewDecoder(buf)

	r := new(rpc.Request)
	err = dec.Decode(r)

	if *r != req {
		t.Fatal("Values don't match")
	}

	if err != nil {
		t.Fatal(err)
	}

	// We should be able to read a second message off this io.Reader
	tmp := new(T)
	err = dec.Decode(tmp)

	if err != nil {
		t.Fatal(err)
	}

	if *tmp != tv {
		t.Fatal("Values don't match")
	}

}
예제 #8
0
파일: bson_test.go 프로젝트: RyanDeng/qbot
func (s *S) TestGetterErrors(c *C) {
	e := errors.New("oops")

	obj1 := &docWithGetterField{}
	obj1.Field = &typeWithGetter{sampleItems[0].obj, e}
	data, err := bson.Marshal(obj1)
	c.Assert(err, ErrorMatches, "oops")
	c.Assert(data, IsNil)

	obj2 := &typeWithGetter{sampleItems[0].obj, e}
	data, err = bson.Marshal(obj2)
	c.Assert(err, ErrorMatches, "oops")
	c.Assert(data, IsNil)
}
예제 #9
0
func fnBsonEncodeFn(buf *bytes.Buffer, ts *TestStruc) error {
	bs, err := bson.Marshal(ts)
	if err == nil {
		buf.Write(bs)
	}
	return err
}
예제 #10
0
파일: node.go 프로젝트: prinsmike/Shock
func (node *Node) Save() (err error) {
	db, err := DBConnect()
	if err != nil {
		return
	}
	defer db.Close()
	node.UpdateVersion()
	if len(node.Revisions) == 0 || node.Revisions[len(node.Revisions)-1].Version != node.Version {
		n := Node{node.Id, node.Version, node.File, node.Attributes, node.Indexes, node.Acl, node.VersionParts, node.Type, nil, node.Relatives}
		node.Revisions = append(node.Revisions, n)
	}
	bsonPath := fmt.Sprintf("%s/%s.bson", node.Path(), node.Id)
	os.Remove(bsonPath)
	nbson, err := bson.Marshal(node)
	if err != nil {
		return
	}
	err = ioutil.WriteFile(bsonPath, nbson, 0644)
	if err != nil {
		return
	}
	err = db.Upsert(node)
	if err != nil {
		return
	}
	return
}
예제 #11
0
// ServiceClient.sendToInstance() tries to make an RPC request on a particular connection to an instance
func (c *ServiceClient) sendToInstance(sr ServiceResource, requestInfo *skynet.RequestInfo, funcName string, in interface{}) (result []byte, serviceErr, err error) {
	ts("sendToInstance", requestInfo)
	defer te("sendToInstance", requestInfo)

	sin := skynet.ServiceRPCIn{
		RequestInfo: requestInfo,
		Method:      funcName,
		ClientID:    sr.clientID,
	}

	sin.In, err = bson.Marshal(in)
	if err != nil {
		return
	}

	sout := skynet.ServiceRPCOut{}

	err = sr.rpcClient.Call(sr.service.Config.Name+".Forward", sin, &sout)
	if err != nil {
		sr.Close()
		dbg("(sr.rpcClient.Call)", err)
		c.Log.Item(err)
	}

	if sout.ErrString != "" {
		serviceErr = serviceError{sout.ErrString}
	}

	result = sout.Out

	return
}
예제 #12
0
파일: bson_test.go 프로젝트: RyanDeng/qbot
func (s *S) TestMarshalErrorItems(c *C) {
	for _, item := range marshalErrorItems {
		data, err := bson.Marshal(item.obj)
		c.Assert(err, ErrorMatches, item.data)
		c.Assert(data, IsNil)
	}
}
예제 #13
0
파일: update.go 프로젝트: wtangiit/Shock
func (node *Node) Save() (err error) {
	node.UpdateVersion()
	if len(node.Revisions) == 0 || node.Revisions[len(node.Revisions)-1].Version != node.Version {
		n := Node{node.Id, node.Version, node.File, node.Attributes, node.Public, node.Indexes, node.Acl, node.VersionParts, node.Tags, nil, node.Linkages, node.CreatedOn, node.LastModified}
		node.Revisions = append(node.Revisions, n)
	}
	if node.CreatedOn == "" {
		node.CreatedOn = time.Now().Format(time.UnixDate)
	} else {
		node.LastModified = time.Now().Format(time.UnixDate)
	}

	bsonPath := fmt.Sprintf("%s/%s.bson", node.Path(), node.Id)
	os.Remove(bsonPath)
	nbson, err := bson.Marshal(node)
	if err != nil {
		return
	}
	err = ioutil.WriteFile(bsonPath, nbson, 0644)
	if err != nil {
		return
	}
	err = dbUpsert(node)
	if err != nil {
		return
	}
	return
}
예제 #14
0
func BenchmarkBsonUnmarshal(b *testing.B) {
	popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
	total := 0

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		b.StopTimer()

		p := NewPopulatedPreAccept(popr)

		data, err := bson.Marshal(p)
		if err != nil {
			panic(err)
		}

		total += len(data)

		msg := &PreAccept{}

		b.StartTimer()
		if err := bson.Unmarshal(data, msg); err != nil {
			panic(err)
		}

	}
	b.SetBytes(int64(total / b.N))
}
예제 #15
0
// Marshal and pack data into array of bytes for sending.
func (_ *BSONProtocol) MarshalAndPack(name string, structPtr interface{}) ([]byte, error) {
	outMsg := &OutgoingBSONMessage{
		Event: name,
		Data:  structPtr,
	}
	return bson.Marshal(outMsg)
}
예제 #16
0
파일: data.go 프로젝트: everfore/bconv
// Bson to Json,InterfaceP{} by BsonBytes
// failed
func _Bson2Json(m *bson.M, i interface{}) error {
	b, err := bson.Marshal(m)
	if isError(err) {
		return err
	}
	return bson.Unmarshal(b, &i)
}
예제 #17
0
// ServiceClient.sendToInstance() tries to make an RPC request on a particular connection to an instance
func (c *ServiceClient) sendToInstance(sr ServiceResource, requestInfo *skynet.RequestInfo, funcName string, in interface{}) (result []byte, err error) {
	if requestInfo == nil {
		requestInfo = &skynet.RequestInfo{
			RequestID: skynet.UUID(),
		}
	}

	sin := service.ServiceRPCIn{
		RequestInfo: requestInfo,
		Method:      funcName,
	}

	sin.In, err = bson.Marshal(in)
	if err != nil {
		return
	}

	sout := service.ServiceRPCOut{}

	err = sr.rpcClient.Call(sr.service.Config.Name+".Forward", sin, &sout)
	if err != nil {
		sr.Close()
		c.Log.Item(err)
	}

	if sout.ErrString != "" {
		err = serviceError{sout.ErrString}
	}

	result = sout.Out

	return
}
예제 #18
0
// ServiceClient.sendToInstance() tries to make an RPC request on a particular connection to an instance
func (c *ServiceClient) sendToInstance(sr ServiceResource,
	requestInfo *skynet.RequestInfo, funcName string, in interface{}) (
	result []byte, serviceErr, err error) {

	sin := skynet.ServiceRPCIn{
		RequestInfo: requestInfo,
		Method:      funcName,
		ClientID:    sr.clientID,
	}

	sin.In, err = bson.Marshal(in)
	if err != nil {
		err = fmt.Errorf("Error calling bson.Marshal: %v", err)
		return
	}

	sout := skynet.ServiceRPCOut{}

	err = sr.rpcClient.Call(sr.service.Name+".Forward", sin, &sout)
	if err != nil {
		sr.Close()

		// Log failure
		log.Printf(log.ERROR, "Error calling sr.rpcClient.Call: "+err.Error())
	}

	if sout.ErrString != "" {
		serviceErr = serviceError{sout.ErrString}
	}

	result = sout.Out

	return
}
예제 #19
0
func (s *URLSuite) TestBSON(c *C) {
	type doc struct {
		URL *charm.URL
	}
	url := charm.MustParseURL("cs:series/name")
	data, err := bson.Marshal(doc{url})
	c.Assert(err, IsNil)
	var v doc
	err = bson.Unmarshal(data, &v)
	c.Assert(v.URL, DeepEquals, url)

	data, err = bson.Marshal(doc{})
	c.Assert(err, IsNil)
	err = bson.Unmarshal(data, &v)
	c.Assert(v.URL, IsNil)
}
예제 #20
0
func Encode(obj interface{}) (p *WirePkt, err error) {
	p = new(WirePkt)
	switch obj.(type) {
	case *IdentifyClient:
		p.Type = TypeIdentifyClient
	case *TaskRequest:
		p.Type = TypeTaskRequest
	case *TaskResponse:
		p.Type = TypeTaskResponse
	case *Acknowledgement:
		p.Type = TypeAcknowledgement
	default:
		Warn("Encoding unknown type!")
		return nil, ErrUnknownType
	}
	p.Payload, err = bson.Marshal(obj)
	if err != nil {
		return nil, err
	}
	if len(p.Payload) >= MaximumPayloadSize {
		return nil, ErrObjectTooLarge
	}
	p.Length = uint16(len(p.Payload))

	return p, nil
}
예제 #21
0
파일: bson_test.go 프로젝트: RyanDeng/qbot
func (s *S) TestMarshalAllItems(c *C) {
	for i, item := range allItems {
		data, err := bson.Marshal(item.obj)
		c.Assert(err, IsNil)
		c.Assert(string(data), Equals, wrapInDoc(item.data), Commentf("Failed on item %d: %#v", i, item))
	}
}
예제 #22
0
func init() {
	log.SetFlags(log.Ltime | log.Lshortfile)

	thtw = *th.NewThriftTweet()
	// create structs, maps by using json data to do initial population
	json.Unmarshal([]byte(jsons), &tw)
	json.Unmarshal([]byte(jsons), &pbtw)
	json.Unmarshal([]byte(jsons), &twl)
	json.Unmarshal([]byte(jsons), &thtw)
	//log.Println("pbtw\n", pbtw)
	// create by values per serialization type
	bsonTweet, _ = bson.Marshal(&tw)
	jsonTweet = []byte(jsons)
	protoTw, _ = proto.Marshal(&pbtw)
	//ptw2, err := proto.Marshal(&pbtw)
	//log.Println(err)
	//log.Println(ptw2)
	msgpackTw, _ = msgpack.Marshal(tw)
	enc := gob.NewEncoder(&gobTw)
	err := enc.Encode(tw)
	if err != nil {
		panic("error")
	}
	//log.Println(string(gobTw.Bytes()))

	buf := thrift.NewTMemoryBuffer()
	thbp := thrift.NewTBinaryProtocol(buf, false, true)
	thtw.Write(thbp)
	thriftTw = buf.Bytes()
	tw2 := th.NewThriftTweet()
	tw2.Read(thbp)

}
예제 #23
0
파일: bson_test.go 프로젝트: RyanDeng/qbot
func (s *S) TestMarshalOneWayItems(c *C) {
	for _, item := range marshalItems {
		data, err := bson.Marshal(item.obj)
		c.Assert(err, IsNil)
		c.Assert(string(data), Equals, wrapInDoc(item.data))
	}
}
예제 #24
0
func TestIntegrationList_Pagination(t *testing.T) {
	storage, err := goejdb.Open("TestIntegrationList", goejdb.JBOWRITER|goejdb.JBOCREAT)
	if err != nil {
		t.Error(err)
	}
	defer storage.Del()
	defer os.Remove("TestIntegrationList")

	chatColl, err := storage.CreateColl("12345", nil)
	if err != nil {
		t.Error(err)
	}
	defer os.Remove("TestIntegrationList_12345")

	for i := 25; i > 0; i-- {
		bsonBytes, _ := bson.Marshal(quotes.Quote{When: i})
		chatColl.SaveBson(bsonBytes)
	}

	manager := manageStorage{storage}

	res, err2 := manager.List("12345", 10)
	if err2 != nil {
		t.Error(err2)
	}

	assert.Equal(t, 10, len(*res))
}
예제 #25
0
func TestSend(t *testing.T) {
	client, server := net.Pipe()
	go doServiceHandshake(server, true, t)

	cn, err := NewConnectionFromNetConn("TestRPCService", client)
	c := cn.(*Conn)

	s := rpc.NewServer()
	var ts TestRPCService
	s.Register(&ts)
	go s.ServeCodec(bsonrpc.NewServerCodec(server))

	var tp TestParam
	tp.Val1 = "Hello World"
	tp.Val2 = 10

	ri := &skynet.RequestInfo{}

	ts.TestMethod = func(in skynet.ServiceRPCIn, out *skynet.ServiceRPCOut) (err error) {
		out.Out, err = bson.Marshal(&tp)

		var t TestParam

		if err != nil {
			return
		}

		if in.ClientID != c.clientID {
			return errors.New("Failed to set ClientID on request")
		}

		if in.Method != "Foo" {
			return errors.New("Failed to set Method on request")
		}

		if *in.RequestInfo != *ri {
			return errors.New("Failed to set RequestInfo on request")
		}

		err = bson.Unmarshal(in.In, &t)
		if err != nil {
			return
		}

		if t.Val1 != tp.Val1 || tp.Val2 != tp.Val2 {
			return errors.New("Request failed to send proper data")
		}

		return
	}

	err = c.Send(ri, "Foo", tp, &tp)
	if err != nil {
		t.Error(err)
		return
	}

	c.Close()
	server.Close()
}
예제 #26
0
func TestDecode(t *testing.T) {
	req := rpc.Request{
		ServiceMethod: "Foo.Bar",
		Seq:           3,
	}

	b, err := bson.Marshal(req)

	if err != nil {
		t.Fatal(err)
	}

	buf := bytes.NewBuffer(b)
	dec := NewDecoder(buf)

	r := new(rpc.Request)
	err = dec.Decode(r)

	if err != nil {
		t.Fatal(err)
	}

	if *r != req {
		t.Fatal("Values don't match")
	}
}
예제 #27
0
파일: kmgBson.go 프로젝트: keysonZZZ/kmg
func WriteFile(path string, obj interface{}) (err error) {
	out, err := bson.Marshal(obj)
	if err != nil {
		return err
	}
	return ioutil.WriteFile(path, out, os.FileMode(0777))
}
예제 #28
0
파일: bson_test.go 프로젝트: RyanDeng/qbot
func (s *S) TestMarshalSampleItems(c *C) {
	for i, item := range sampleItems {
		data, err := bson.Marshal(item.obj)
		c.Assert(err, IsNil)
		c.Assert(string(data), Equals, item.data, Commentf("Failed on item %d", i))
	}
}
예제 #29
0
파일: bson_test.go 프로젝트: RyanDeng/qbot
func (s *S) TestMarshalShortWithGetter(c *C) {
	obj := typeWithIntGetter{42}
	data, err := bson.Marshal(obj)
	c.Assert(err, IsNil)
	m := bson.M{}
	err = bson.Unmarshal(data, m)
	c.Assert(m["v"], Equals, 42)
}
예제 #30
0
파일: bson_test.go 프로젝트: RyanDeng/qbot
func (s *S) TestUnmarshalZeroesMap(c *C) {
	data, err := bson.Marshal(bson.M{"b": 2})
	c.Assert(err, IsNil)
	m := bson.M{"a": 1}
	err = bson.Unmarshal(data, &m)
	c.Assert(err, IsNil)
	c.Assert(m, DeepEquals, bson.M{"b": 2})
}