예제 #1
0
// Method Get Key Range
func (self *NetworkClient) GetKeyRange(startKey []byte, endKey []byte) (<-chan [][]byte, <-chan error, error) {
	var startKeyInclusive bool = true
	var endKeyInclusive bool = true
	var maxReturned int32 = 200
	var reverse bool = false

	cmd := &kproto.Command{
		Header: &kproto.Command_Header{
			ConnectionID: proto.Int64(self.connectionId),
			Sequence:     proto.Int64(self.sequence),
			MessageType:  kproto.Command_GETKEYRANGE.Enum(),
		},
		Body: &kproto.Command_Body{
			Range: &kproto.Command_Range{
				StartKey:          startKey,
				StartKeyInclusive: &startKeyInclusive,
				EndKey:            endKey,
				EndKeyInclusive:   &endKeyInclusive,
				MaxReturned:       &maxReturned,
				Reverse:           &reverse,
			},
		},
	}

	cmd_bytes, err := proto.Marshal(cmd)
	if err != nil {
		return nil, nil, err
	}

	msg := &kproto.Message{
		AuthType: kproto.Message_HMACAUTH.Enum(),
		HmacAuth: &kproto.Message_HMACauth{
			Identity: proto.Int64(self.userId),
			Hmac:     calculate_hmac(self.secret, cmd_bytes),
		},
		CommandBytes: cmd_bytes,
	}

	err = network.Send(self.conn, msg, nil)
	if err != nil {
		return nil, nil, err
	}

	// TODO: return single chan
	status := make(chan error, 1)
	array := make(chan [][]byte, 1)
	pending := PendingOperation{sequence: self.sequence, receiver: status, array: array}

	self.notifier <- pending

	self.sequence += 1

	return array, status, nil
}
예제 #2
0
// Method Get Previous
func (self *NetworkClient) GetPreviousKey(key []byte) (<-chan []byte, <-chan error, error) {
	metadataOnly := true

	cmd := &kproto.Command{
		Header: &kproto.Command_Header{
			ConnectionID: proto.Int64(self.connectionId),
			Sequence:     proto.Int64(self.sequence),
			MessageType:  kproto.Command_GETPREVIOUS.Enum(),
		},
		Body: &kproto.Command_Body{
			KeyValue: &kproto.Command_KeyValue{
				Key:          key,
				MetadataOnly: &metadataOnly,
			},
		},
	}

	cmd_bytes, err := proto.Marshal(cmd)
	if err != nil {
		return nil, nil, err
	}

	msg := &kproto.Message{
		AuthType: kproto.Message_HMACAUTH.Enum(),
		HmacAuth: &kproto.Message_HMACauth{
			Identity: proto.Int64(self.userId),
			Hmac:     calculate_hmac(self.secret, cmd_bytes),
		},
		CommandBytes: cmd_bytes,
	}

	err = network.Send(self.conn, msg, nil)
	if err != nil {
		return nil, nil, err
	}

	// TODO: return single chan
	status := make(chan error, 1)
	value := make(chan []byte, 1)
	pending := PendingOperation{sequence: self.sequence, receiver: status, value: value}

	self.notifier <- pending

	self.sequence += 1

	return value, status, nil
}
예제 #3
0
func (self *NetworkClient) Delete(key []byte) (<-chan error, error) {
	cmd := &kproto.Command{
		Header: &kproto.Command_Header{
			ConnectionID: proto.Int64(self.connectionId),
			Sequence:     proto.Int64(self.sequence),
			MessageType:  kproto.Command_DELETE.Enum(),
		},
		Body: &kproto.Command_Body{
			KeyValue: &kproto.Command_KeyValue{
				Key:             key,
				Algorithm:       kproto.Command_SHA1.Enum(),
				Tag:             make([]byte, 0),
				Synchronization: kproto.Command_WRITEBACK.Enum(),
			},
		},
	}

	cmd_bytes, err := proto.Marshal(cmd)
	if err != nil {
		return nil, err
	}

	msg := &kproto.Message{
		AuthType: kproto.Message_HMACAUTH.Enum(),
		HmacAuth: &kproto.Message_HMACauth{
			Identity: proto.Int64(self.userId),
			Hmac:     calculate_hmac(self.secret, cmd_bytes),
		},
		CommandBytes: cmd_bytes,
	}

	err = network.Send(self.conn, msg, nil)
	if err != nil {
		return nil, err
	}

	rx := make(chan error, 1)
	pending := PendingOperation{sequence: self.sequence, receiver: rx}

	self.notifier <- pending

	self.sequence += 1

	return rx, nil
}