Пример #1
0
func (stats *StatsBackend) SInter(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) < 2 {
		client.WriteError(errors.New("SINTER takes at least 2 parameters (SINTER key [key ...])"))
		client.Flush()
		return nil
	} else {
		keys := make([]string, len(d))
		for i, v := range d {
			keys[i] = string(v)
		}
		results, err := stats.mem.SInter(keys)
		if err != nil {
			return err
		}

		if results == nil {
			client.WriteNull()
			client.Flush()
			return nil
		}

		client.WriteLen('*', len(results))
		for k, _ := range results {
			client.WriteString(k)
		}
		client.Flush()
		return nil
	}
}
Пример #2
0
func (stats *StatsBackend) SMembers(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) == 0 {
		client.WriteError(errors.New("SMEMBERS takes 1 parameter (SMEMBERS key)"))
		client.Flush()
		return nil
	} else {
		values, err := stats.mem.SMembers(string(d[0]))
		if err != nil {
			return err
		}

		if values == nil {
			client.WriteNull()
			client.Flush()
			return nil
		}

		client.WriteLen('*', len(values))
		for k, _ := range values {
			client.WriteString(k)
		}
		client.Flush()
		return nil
	}
}
Пример #3
0
func (stats *StatsBackend) SDiff(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) < 2 {
		client.WriteError(errors.New("SDIFF requires at least 2 parameters (SDIFF key [key ...])"))
		client.Flush()
		return nil
	} else {
		results, err := stats.mem.SMembers(string(d[0]))
		if err != nil {
			return err
		} else if results == nil {
			client.WriteNull()
			client.Flush()
			return nil
		} else {
			for _, v := range d[1:] {
				results, err = stats.mem.SDiff(results, string(v))
				if err != nil {
					return err
				} else if len(results) == 0 {
					break
				}
			}

			client.WriteLen('*', len(results))
			for k, _ := range results {
				client.WriteString(k)
			}
			client.Flush()
			return nil
		}
	}
}
Пример #4
0
func (t *TermBackend) SaveFile(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) >= 2 {
		fileName := string(d[0])
		content := d[1]
		err := ioutil.WriteFile(fileName, content, 0644)
		if err != nil {
			client.WriteError(err)
			client.Flush()
		} else {
			client.WriteString("saved " + fileName + " successfully")
			client.Flush()
		}
	} else {
		client.WriteError(errors.New("save takes at least 2 parameters (save filename filecontents...)"))
		client.Flush()
	}

	return nil
}
Пример #5
0
func (p *RedisProtocol) RunClient(client server.ProtocolClient) {
	// defer panics to the loggable event routine
	defer func() {
		if e := recover(); e != nil {
			buf := make([]byte, 4096)
			n := runtime.Stack(buf, false)
			buf = buf[0:n]
			p.ctx.Events <- server.BroadcastEvent{"fatal", "client run panic", errors.New(fmt.Sprintf("%v", e)), buf}
		}

		client.Close()
		return
	}()

	reqErr := client.RequestErrorChan()
	for {
		data, err := client.ReadBulkPayload()
		if err != nil {
			if err != io.EOF {
				p.ctx.Events <- server.BroadcastEvent{"error", "read error", err, nil}
			}
			return
		}

		err = p.handleData(data, client, reqErr)
		if err != nil {
			if err == errQuit {
				client.WriteString("OK")
				client.Flush()
				return
			} else {
				p.ctx.Events <- server.BroadcastEvent{"error", "accept error", err, nil}
				client.WriteError(err)
				client.Flush()
			}
		}
	}
}
Пример #6
0
func (stats *StatsBackend) Keys(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	key := ""
	if len(d) > 0 {
		pattern, err := stats.readString(d[0])
		if err != nil {
			return err
		}
		key = pattern
	}

	results, err := stats.mem.Keys(key)
	if err != nil {
		client.WriteError(err)
		client.Flush()
		return nil
	}
	client.WriteLen('*', len(results))
	for _, k := range results {
		client.WriteString(k)
	}
	client.Flush()
	return nil
}
Пример #7
0
func (b *DefaultBackend) echo(data interface{}, client server.ProtocolClient) error {
	d, okInterface := data.([]interface{})
	if okInterface {
		if len(d) == 0 {
			client.WriteString("")
			client.Flush()
			return nil
		} else {
			if len(d) == 1 {
				fmt.Printf("%v", d[0])
				client.WriteInterface(d[0])
			} else {
				client.WriteArray(d)
			}
			client.Flush()
			return nil
		}
	} else {
		b, _ := data.([][]byte)
		if len(b) == 0 {
			client.WriteString("")
			client.Flush()
			return nil
		} else {
			if len(b) == 1 {
				client.WriteString(string(b[0]))
			} else {
				s := make([]string, len(b))
				for i, k := range b {
					s[i] = string(k)
				}
				client.WriteString(strings.Join(s, " "))
			}
			client.Flush()
			return nil
		}
	}
}
Пример #8
0
func (b *DefaultBackend) ping(data interface{}, client server.ProtocolClient) error {
	client.WriteString(PONG)
	client.Flush()
	return nil
}