Exemplo n.º 1
0
func (s *ZStoreStub) handleQuery(req *zc.ZMsg, resp *zc.ZMsg) {
	payload, _ := req.GetPayload()
	if !req.CheckExists("zc-class", "zc-query") {
		resp.SetErr("invalid query request")
		log.Infof("invalid query request: %s", string(payload))
		return
	}

	className := req.GetString("zc-class")
	if !s.checkClass(className) {
		resp.SetErr("invalid class name")
		log.Infof("invalid query request: %s", string(payload))
		return
	}

	classStore := s.store[className]
	if classStore == nil {
		resp.SetAck()
		return
	}

	query := req.GetObject("zc-query")
	if !query.CheckExists("zc-eq") {
		resp.SetErr("no eq condition to find objects")
		return
	}

	eq := query.GetObject("zc-eq")
	selectKeys := query.GetStrings("zc-select")
	primaryKey := s.getPrimaryKey(className, eq)
	dataZO := classStore[primaryKey]
	if dataZO == nil {
		resp.SetAck()
		return
	}

	selectZO := zc.NewObject()
	if len(selectKeys) <= 0 {
		selectZO = dataZO
	} else {
		for _, key := range selectKeys {
			selectZO.Put(key, dataZO.Get(key))
		}
	}
	resp.SetAck()
	resp.AddObject("zc-objects", selectZO)
	return
}