Exemple #1
0
//creates a new response from a dynmap
func NewResponseDynMap(mp *dynmap.DynMap) *Response {
	strest := mp.MustDynMap("strest", dynmap.New())
	status := mp.MustDynMap("status", dynmap.New())
	mp.Remove("strest")
	mp.Remove("status")

	response := &Response{
		DynMap:        *mp,
		txnId:         strest.MustString("txn.id", ""),
		txnStatus:     strest.MustString("txn.status", "completed"),
		statusCode:    status.MustInt("code", 200),
		statusMessage: status.MustString("message", "OK"),
	}
	return response
}
Exemple #2
0
// Say hello
func (this *BinProtocol) WriteHello(writer io.Writer, hello *dynmap.DynMap) error {
	err := binary.Write(writer, binary.BigEndian, BINCONST.ParamEncoding["json"])
	if err != nil {
		return err
	}

	hello.PutIfAbsent("v", StrestVersion)
	hello.PutIfAbsent("useragent", "golang")
	b, err := hello.MarshalJSON()
	if err != nil {
		return err
	}
	_, err = WriteString(writer, string(b))
	return err
}
// Creates a new router entry from the dynmap passed in
func ToRouterEntry(mp *dynmap.DynMap) (*RouterEntry, error) {
	e := &RouterEntry{
		Self:          false,
		PartitionsMap: make(map[int]bool),
	}
	var ok bool
	e.Address, ok = mp.GetString("address")
	if !ok {
		return nil, fmt.Errorf("No Address in Entry: %s", mp)
	}

	e.JsonPort = mp.MustInt("ports.json", 0)
	e.HttpPort = mp.MustInt("ports.http", 0)
	e.LastSeenAt = mp.MustTime("last_seen_at", *new(time.Time))
	e.Partitions, ok = mp.GetIntSlice("partitions")
	if !ok {
		e.Partitions = make([]int, 0)
	}
	for _, p := range e.Partitions {
		e.PartitionsMap[p] = true
	}
	e.DynMap = e.ToDynMap()
	return e, nil
}
// Creates a new router table from the dynmap passed in
func ToRouterTable(mp *dynmap.DynMap) (*RouterTable, error) {
	t := &RouterTable{}

	var ok bool
	t.Service, ok = mp.GetString("service")
	if !ok {
		return nil, fmt.Errorf("No Service in the table %s", mp)
	}

	t.Revision, ok = mp.GetInt64("revision")
	if !ok {
		return nil, fmt.Errorf("No Revision in the table %s", mp)
	}

	t.TotalPartitions, ok = mp.GetInt("total_partitions")
	if !ok {
		return nil, fmt.Errorf("No total_partitions in the table %s", mp)
	}

	t.ReplicationFactor, ok = mp.GetInt("replication_factor")
	if !ok {
		return nil, fmt.Errorf("No replication_factor in the table %s", mp)
	}

	t.PartitionKeys, ok = mp.GetStringSlice("partition_keys")
	if !ok {
		//do nothing, right?
	}

	//fill the entries
	t.Entries = make([]*RouterEntry, 0)
	entryMaps, ok := mp.GetDynMapSlice("entries")
	if !ok {
		return nil, fmt.Errorf("Bad entries in the table %s", mp)
	}
	for _, em := range entryMaps {
		routerEntry, err := ToRouterEntry(em)
		if err != nil {
			return nil, err
		}
		t.Entries = append(t.Entries, routerEntry)
	}

	// set up the partition to entry mapping
	partitionCount := 0
	entriesPartition := make([]*RouterEntry, t.TotalPartitions)
	for _, e := range t.Entries {
		for _, p := range e.Partitions {
			if p >= t.TotalPartitions {
				return nil, fmt.Errorf("Bad Partition entry (greater then total partitions): %s", e)
			}
			entriesPartition[p] = e
			partitionCount++
		}
	}

	if partitionCount != t.TotalPartitions {
		return nil, fmt.Errorf("Bad table, some partitions un accounted for")
	}

	t.DynMap = t.toDynMap()

	t.EntriesPartition = make([][]*RouterEntry, t.TotalPartitions)
	//Now setup the replication partitions.
	for _, e := range t.Entries {

		for _, p := range e.Partitions {
			pRep, err := t.repPartitions(p, e)
			if err != nil {
				return nil, fmt.Errorf("Bad table (%s)", err)
			}
			entries := make([]*RouterEntry, len(pRep)+1)
			entries[0] = e
			for i := 1; i < len(entries); i++ {
				entries[i] = entriesPartition[pRep[i-1]]
				e.PartitionsMap[pRep[i-1]] = false
			}
			t.EntriesPartition[p] = entries
		}
	}

	return t, nil
}
Exemple #5
0
//creates a new request from a dynmap
func NewRequestDynMap(mp *dynmap.DynMap) *Request {
	request := &Request{
		// version : mp.MustFloat32("strest.v", mp.StrestVersion), //TODO
		version:   StrestVersion,
		uri:       mp.MustString("strest.uri", ""),
		method:    mp.MustString("strest.method", "GET"),
		txnId:     mp.MustString("strest.txn.id", ""),
		txnAccept: mp.MustString("strest.txn.accept", "single"),
		params:    mp.MustDynMap("strest.params", dynmap.New()),
	}

	shardMp, ok := mp.GetDynMap("strest.shard")
	if ok {
		request.Shard = &ShardRequest{
			Partition: shardMp.MustInt("partition", -1),
			Key:       shardMp.MustString("key", ""),
			Revision:  shardMp.MustInt64("revision", int64(-1)),
		}
	} else {
		request.Shard = &ShardRequest{
			Partition: request.params.MustInt(PARAM_SHARD_PARTITION, -1),
			Key:       request.params.MustString(PARAM_SHARD_KEY, ""),
			Revision:  request.params.MustInt64(PARAM_SHARD_REVISION, int64(-1)),
		}
	}
	return request
}