當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Msg.IDString方法代碼示例

本文整理匯總了Golang中github.com/compose/transporter/pkg/message.Msg.IDString方法的典型用法代碼示例。如果您正苦於以下問題:Golang Msg.IDString方法的具體用法?Golang Msg.IDString怎麽用?Golang Msg.IDString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/compose/transporter/pkg/message.Msg的用法示例。


在下文中一共展示了Msg.IDString方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: applyOp

func (e *Elasticsearch) applyOp(msg *message.Msg) (*message.Msg, error) {
	if msg.Op == message.Command {
		err := e.runCommand(msg)
		if err != nil {
			e.pipe.Err <- NewError(ERROR, e.path, fmt.Sprintf("elasticsearch error (%s)", err), msg.Data)
		}
		return msg, nil
	}

	// TODO there might be some inconsistency here.  elasticsearch uses the _id field for an primary index,
	//  and we're just mapping it to a string here.
	id, err := msg.IDString("_id")
	if err != nil {
		id = ""
	}

	_, _type, err := msg.SplitNamespace()
	if err != nil {
		e.pipe.Err <- NewError(ERROR, e.path, fmt.Sprintf("unable to determine type from msg.Namespace (%s)", msg.Namespace), msg)
		return msg, nil
	}
	switch msg.Op {
	case message.Delete:
		e.indexer.Delete(e.index, _type, id, false)
		err = nil
	default:
		err = e.indexer.Index(e.index, _type, id, "", "", nil, msg.Data, false)
	}
	if err != nil {
		e.pipe.Err <- NewError(ERROR, e.path, fmt.Sprintf("elasticsearch error (%s)", err), msg.Data)
	}
	return msg, nil
}
開發者ID:travisjeffery,項目名稱:transporter,代碼行數:33,代碼來源:elasticsearch.go

示例2: addBulkCommand

func (a *Appbase) addBulkCommand(msg *message.Msg) (*message.Msg, error) {
	id, err := msg.IDString("_id")
	if err != nil {
		id = ""
	}

	switch msg.Op {
	case message.Delete:
		bulkRequest := elastic.NewBulkDeleteRequest().Index(a.appName).Type(a.typename).Id(id)
		a.AddBulkRequestSize(bulkRequest)
		a.bulkService.Add(bulkRequest)
		break
	case message.Update:
		bulkRequest := elastic.NewBulkUpdateRequest().Index(a.appName).Type(a.typename).Id(id).Doc(msg.Data)
		a.AddBulkRequestSize(bulkRequest)
		a.bulkService.Add(bulkRequest)
		break
	default:
		bulkRequest := elastic.NewBulkIndexRequest().Index(a.appName).Type(a.typename).Id(id).Doc(msg.Data)
		a.AddBulkRequestSize(bulkRequest)
		a.bulkService.Add(bulkRequest)
		break
	}

	a.commitBulk(false)

	return msg, nil
}
開發者ID:cleitonmarx,項目名稱:transporter,代碼行數:28,代碼來源:appbase.go

示例3: applyOp

// applyOp applies one operation to the database
func (cloudant *Cloudant) applyOp(msg *message.Msg) (*message.Msg, error) {
	var err error

	doc := msg.Map()
	database := cloudant.client.DB(cloudant.database)

	switch msg.Op {
	case message.Delete:
		id, err := msg.IDString("_id")
		if err != nil {
			cloudant.pipe.Err <- NewError(ERROR, cloudant.path, "cloudant error (cannot delete an object with a nil id)", msg.Data)
			return msg, err
		}
		rev, err := msg.IDString("_rev")
		if err != nil {
			cloudant.pipe.Err <- NewError(ERROR, cloudant.path, "cloudant error (cannot delete an object with a nil rev)", msg.Data)
			return msg, err
		}
		tombstone, err := database.Delete(id, rev)
		if err == nil && cloudant.debug {
			fmt.Printf("Deleted document {_id=%s, _rev=%s} => tombstode rev: %s\n", id, rev, tombstone)
		}

	case message.Insert: // If the doc contains _id this will be respected
		_, err := msg.IDString("_rev") // If the doc contains a _rev, it can't be an insert, as this would mean a conflict.
		if err == nil {
			cloudant.pipe.Err <- NewError(ERROR, cloudant.path, "cloudant error (cannot insert doc that already has a rev)", msg.Data)
			return msg, err
		}
		newid, newrev, err := database.Post(doc)
		if err == nil && cloudant.debug {
			fmt.Printf("Inserted new document as {_id=%s, _rev=%s}\n", newid, newrev)
		}

	case message.Update:
		id, err := msg.IDString("_id")
		if err != nil {
			cloudant.pipe.Err <- NewError(ERROR, cloudant.path, "cloudant error (cannot update an object with a nil id)", msg.Data)
			return msg, err
		}
		rev, _ := msg.IDString("_rev")
		newrev, err := database.Put(id, doc, rev)
		if err == nil && cloudant.debug {
			fmt.Printf("Updated document as {_id=%s, _rev=%s}\n", id, newrev)
		}
	}

	if err != nil {
		errmsg := fmt.Sprintf("cloudant error (database '%s') %+v %+v", cloudant.database, err, msg)
		if cloudant.debug {
			fmt.Println(errmsg)
		}
		cloudant.pipe.Err <- NewError(ERROR, cloudant.path, errmsg, err)
	}

	return msg, err
}
開發者ID:xpqz,項目名稱:transporter,代碼行數:58,代碼來源:cloudant.go

示例4: applyOp

// applyOp applies one operation to the database
func (r *Rethinkdb) applyOp(msg *message.Msg) (*message.Msg, error) {
	var (
		resp gorethink.WriteResponse
		err  error
	)

	_, msgTable, err := msg.SplitNamespace()
	if err != nil {
		r.pipe.Err <- NewError(ERROR, r.path, fmt.Sprintf("rethinkdb error (msg namespace improperly formatted, must be database.table, got %s)", msg.Namespace), msg.Data)
		return msg, nil
	}
	if !msg.IsMap() {
		r.pipe.Err <- NewError(ERROR, r.path, "rethinkdb error (document must be a json document)", msg.Data)
		return msg, nil
	}
	doc := msg.Map()

	switch msg.Op {
	case message.Delete:
		id, err := msg.IDString("id")
		if err != nil {
			r.pipe.Err <- NewError(ERROR, r.path, "rethinkdb error (cannot delete an object with a nil id)", msg.Data)
			return msg, nil
		}
		resp, err = gorethink.Table(msgTable).Get(id).Delete().RunWrite(r.client)
	case message.Insert:
		resp, err = gorethink.Table(msgTable).Insert(doc).RunWrite(r.client)
	case message.Update:
		resp, err = gorethink.Table(msgTable).Insert(doc, gorethink.InsertOpts{Conflict: "replace"}).RunWrite(r.client)
	}
	if err != nil {
		r.pipe.Err <- NewError(ERROR, r.path, "rethinkdb error (%s)", err)
		return msg, nil
	}

	err = r.handleResponse(&resp)
	if err != nil {
		r.pipe.Err <- NewError(ERROR, r.path, "rethinkdb error (%s)", err)
	}

	return msg, nil
}
開發者ID:albertzak,項目名稱:transporter,代碼行數:43,代碼來源:rethinkdb.go

示例5: applyOp

func (e *Elasticsearch) applyOp(msg *message.Msg) (*message.Msg, error) {
	if msg.Op == message.Command {
		err := e.runCommand(msg)
		if err != nil {
			e.pipe.Err <- NewError(ERROR, e.path, fmt.Sprintf("elasticsearch error (%s)", err), msg.Data)
		}
		return msg, nil
	}

	// TODO there might be some inconsistency here.  elasticsearch uses the _id field for an primary index,
	//  and we're just mapping it to a string here.
	id, err := msg.IDString("_id")
	if err != nil {
		id = ""
	}

	// Possible fix for #151 issue: code added to make it working with elastic 2 and mongo (now it's possible to use _id)
	if len(id) > 0 {
		var message = msg.Map()
		delete(message, "_id")
		msg.Data = message
	}

	_, _type, err := msg.SplitNamespace()
	if err != nil {
		e.pipe.Err <- NewError(ERROR, e.path, fmt.Sprintf("unable to determine type from msg.Namespace (%s)", msg.Namespace), msg)
		return msg, nil
	}
	switch msg.Op {
	case message.Delete:
		e.indexer.Delete(e.index, _type, id)
		err = nil
	default:
		err = e.indexer.Index(e.index, _type, id, "", "", nil, msg.Data)
	}
	if err != nil {
		e.pipe.Err <- NewError(ERROR, e.path, fmt.Sprintf("elasticsearch error (%s)", err), msg.Data)
	}
	return msg, nil
}
開發者ID:albertzak,項目名稱:transporter,代碼行數:40,代碼來源:elasticsearch.go


注:本文中的github.com/compose/transporter/pkg/message.Msg.IDString方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。