Пример #1
0
// Get changes.
//
// @param  query map[string]interface{}
// @return []map[string]interface{}, error
func (this *Database) GetChanges(
	query map[string]interface{}, docIds []string) (map[string]interface{}, error) {
	query = util.Param(query)
	if docIds != nil {
		query["filter"] = "_doc_ids"
	}

	body := util.ParamList("doc_ids", docIds)
	data, err := this.Client.Post(this.Name+"/_changes", query, body, nil).
		GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	ret := util.Map()
	ret["last_seq"] = util.Dig("last_seq", data)
	ret["results"] = util.MapList(0) // set empty as default
	if results := data.(map[string]interface{})["results"].([]interface{}); results != nil {
		ret["results"] = util.MapList(results) // @overwrite
		for i, result := range results {
			ret["results"].([]map[string]interface{})[i] = map[string]interface{}{
				"id":      util.Dig("id", result),
				"seq":     util.Dig("seq", result),
				"deleted": util.Dig("deleted", result),
				"changes": util.Dig("changes", result),
			}
		}
	}

	return ret, nil
}
Пример #2
0
// Update documents.
//
// @param  document []map[string]interface{}
// @return []map[string]interface{}, error
// @panics
func (this *Database) UpdateDocumentAll(
	documents []interface{}) ([]map[string]interface{}, error) {
	docs := util.MapList(documents)
	for i, doc := range documents {
		if docs[i] == nil {
			docs[i] = util.Map()
		}
		for key, value := range doc.(map[string]interface{}) {
			docs[i][key] = value
		}
		// these are required params
		if docs[i]["_id"] == nil || docs[i]["_rev"] == nil {
			panic("Both _id & _rev fields are required!")
		}
	}

	body := util.ParamList("docs", docs)
	data, err := this.Client.Post(this.Name+"/_bulk_docs", nil, body, nil).GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	ret := util.MapList(data)
	for i, doc := range data.([]interface{}) {
		if ret[i] == nil {
			ret[i] = util.Map()
		}

		for key, value := range doc.(map[string]interface{}) {
			ret[i][key] = value
		}
	}

	return ret, nil
}
Пример #3
0
// Find attachments
//
// @param  attEncInfo bool
// @param  attsSince []string
// return  []map[string]interface{}, error
func (this *Document) FindAttachments(
	attEncInfo bool, attsSince []string) ([]map[string]interface{}, error) {
	query := util.Map()
	query["attachments"] = true
	query["att_encoding_info"] = attEncInfo

	if attsSince != nil {
		attsSinceArray := util.MapSliceString(attsSince)
		for _, attsSinceValue := range attsSince {
			attsSinceArray = append(attsSinceArray, util.QuoteEncode(attsSinceValue))
		}
	}

	data, err := this.Find(query)
	if err != nil {
		return nil, err
	}

	ret := util.MapList(nil)
	if data["_attachments"] != nil {
		for _, attc := range data["_attachments"].(map[string]interface{}) {
			ret = append(ret, attc.(map[string]interface{}))
		}
	}

	return ret, nil
}
Пример #4
0
// Get UUID.
//
// @param  body map[string]interface{}
// @return map[string]interface{}, error
// @panics
func (this *Server) Replicate(body map[string]interface{}) (map[string]interface{}, error) {
	body = util.Param(body)
	if body["source"] == nil || body["target"] == nil {
		panic("Both source & target required!")
	}

	data, err := this.Client.Post("/_replicate", nil, body, nil).GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	ret := util.Map()
	for key, value := range data.(map[string]interface{}) {
		// grap, set & pass history field
		if key == "history" {
			ret["history"] = util.MapList(value)
			for i, history := range value.([]interface{}) {
				ret["history"].([]map[string]interface{})[i] = util.Map()
				for kkey, vvalue := range history.(map[string]interface{}) {
					ret["history"].([]map[string]interface{})[i][kkey] = vvalue
				}
			}
			continue
		}
		ret[key] = value
	}

	return ret, nil
}
Пример #5
0
// View temp.
//
// @param  _map string
// @param  _red interface{}
// @return map[string]interface{}, error
func (this *Database) ViewTemp(_map string, _red interface{}) (map[string]interface{}, error) {
	body := util.ParamList(
		"map", _map,
		"reduce", util.IsEmptySet(_red, nil), // prevent "missing function" error
	)

	// short?
	type ddl DatabaseDocumentList

	data, err := this.Client.Post(this.Name+"/_temp_view", nil, body, nil).GetBodyData(&ddl{})
	if err != nil {
		return nil, err
	}

	ret := util.Map()
	ret["offset"] = data.(*ddl).Offset
	ret["total_rows"] = data.(*ddl).TotalRows

	rows := data.(*ddl).Rows
	ret["rows"] = util.MapList(len(rows))

	// append docs
	for i, row := range rows {
		ret["rows"].([]map[string]interface{})[i] = map[string]interface{}{
			"id":    row.Id,
			"key":   row.Key,
			"value": row.Value,
		}
	}

	return ret, nil
}
Пример #6
0
// Create database.
//
// @param  target      string
// @param  targetCreate bool
// @return bool
func (this *Database) Replicate(
	target string, targetCreate bool) (map[string]interface{}, error) {
	// prepare body
	body := util.ParamList(
		"source", this.Name,
		"target", target,
		"create_target", targetCreate,
	)

	data, err := this.Client.Post("/_replicate", nil, body, nil).GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	ret := util.Map()
	for key, value := range data.(map[string]interface{}) {
		// grap, set & pass history field
		if key == "history" {
			ret[key] = util.MapList(value)
			for i, history := range value.([]interface{}) {
				ret[key].([]map[string]interface{})[i] = util.Map()
				for kkey, vvalue := range history.(map[string]interface{}) {
					ret[key].([]map[string]interface{})[i][kkey] = vvalue
				}
			}
			continue
		}
		ret[key] = value
	}

	return ret, nil
}
Пример #7
0
// Create documents.
//
// @param  document []map[string]interface{}
// @return []map[string]interface{}, error
func (this *Database) CreateDocumentAll(
	documents []interface{}) ([]map[string]interface{}, error) {
	docs := util.MapList(documents)
	for i, doc := range documents {
		if docs[i] == nil {
			docs[i] = util.Map()
		}
		// filter documents
		for key, value := range doc.(map[string]interface{}) {
			// this is create method, no update allowed
			if key == "_rev" || key == "_deleted" {
				continue
			}
			docs[i][key] = value
		}
	}

	body := util.ParamList("docs", docs)
	data, err := this.Client.Post(this.Name+"/_bulk_docs", nil, body, nil).GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	ret := util.MapList(data)
	for i, doc := range data.([]interface{}) {
		if ret[i] == nil {
			ret[i] = util.Map()
		}

		for key, value := range doc.(map[string]interface{}) {
			ret[i][key] = value
		}
	}

	return ret, nil
}
Пример #8
0
// Get active tasks.
//
// @return []map[string]interface{}, error
func (this *Server) GetActiveTasks() ([]map[string]interface{}, error) {
	data, err := this.Client.Get("/_active_tasks", nil, nil).GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	ret := util.MapList(data)
	for i, data := range data.([]interface{}) {
		ret[i] = util.Map()
		for key, value := range data.(map[string]interface{}) {
			ret[i][key] = value
		}
	}

	return ret, nil
}
Пример #9
0
// Get documents.
//
// @param  query map[string]interface{}
// @param  keys  []string
// @return map[string]interface{}, error
func (this *Database) GetDocumentAll(
	query map[string]interface{}, keys []string) (map[string]interface{}, error) {
	query = util.Param(query)
	if query["include_docs"] == nil {
		query["include_docs"] = true
	}

	// short?
	type ddl DatabaseDocumentList

	// make a reusable lambda
	_func := func(data interface{}, err error) (map[string]interface{}, error) {
		if err != nil {
			return nil, err
		}

		ret := util.Map()
		ret["offset"] = data.(*ddl).Offset
		ret["total_rows"] = data.(*ddl).TotalRows

		rows := data.(*ddl).Rows
		ret["rows"] = util.MapList(len(rows))

		// append docs
		for i, row := range rows {
			ret["rows"].([]map[string]interface{})[i] = map[string]interface{}{
				"id":    row.Id,
				"key":   row.Key,
				"value": map[string]string{"rev": row.Value["rev"].(string)},
				"doc":   row.Doc,
			}
		}

		return ret, nil
	}

	if keys == nil {
		return _func( // get all
			this.Client.Get(this.Name+"/_all_docs", query, nil).GetBodyData(&ddl{}))
	} else {
		body := util.ParamList("keys", keys)
		return _func( // get all only matched keys
			this.Client.Post(this.Name+"/_all_docs", query, body, nil).GetBodyData(&ddl{}))
	}
}