Example #1
0
func (this *Dag) MakeDotGraph(fn string) {
	file, err := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE, 0644)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	sb := str.NewStringBuilder()
	sb.WriteString("digraph depgraph {\n\trankdir=LR;\n")
	for _, node := range this.nodes {
		node.dotGraph(sb)
	}
	sb.WriteString("}\n")
	file.WriteString(sb.String())
}
Example #2
0
// 184 ns/op
func BenchmarkStringBuilderConcat(b *testing.B) {
	const (
		s1 = "SELECT shardId FROM "
		s2 = " WHERE entityId=?"
	)

	b.ReportAllocs()
	table := "UserLookup"
	sb := str.NewStringBuilder()
	for i := 0; i < b.N; i++ {
		sb.WriteString(s1)
		sb.WriteString(table)
		sb.WriteString(s2)
		sb.String()
		sb.Reset()
	}
}
Example #3
0
func (this *StandardServerSelector) pickShardedServer(pool string,
	table string, hintId int) (*mysql, error) {
	const (
		sb1 = "SELECT shardId,shardLock FROM "
		sb2 = " WHERE entityId=?"
	)

	if hintId == 0 {
		return nil, ErrInvalidHintId
	}

	// get mysql conn from cache
	key := this.lookupCacheKey(pool, hintId)
	if conn, present := this.lookupCache.Get(key); present {
		return conn.(*mysql), nil
	}

	// cache missed, get lookup mysql conn
	my, err := this.ServerByBucket(this.conf.LookupPool)
	if err != nil {
		return nil, err
	}

	// TODO maybe string concat is better performant
	sb := str.NewStringBuilder()
	sb.WriteString(sb1)
	lookupTable := this.conf.LookupTable(pool)
	if lookupTable == "" {
		return nil, ErrLookupTableNotFound
	}
	sb.WriteString(lookupTable)
	sb.WriteString(sb2)
	rows, err := my.Query(sb.String(), hintId)
	if err != nil {
		log.Error("sql=%s id=%d: %s", sb.String(), hintId, err.Error())
		return nil, err
	}

	defer rows.Close()

	// only 1 row in lookup table
	if !rows.Next() {
		return nil, ErrShardLookupNotFound
	}

	var (
		shardId     string
		shardLocked int
	)
	if err = rows.Scan(&shardId, &shardLocked); err != nil {
		log.Error("sql=%s id=%d: %s", sb.String(), hintId, err.Error())
		return nil, err
	}
	if shardLocked > 0 {
		return nil, ErrEntityLocked
	}
	if err = rows.Err(); err != nil {
		log.Error("sql=%s id=%d: %s", sb.String(), hintId, err.Error())
		return nil, err
	}

	//bucket := fmt.Sprintf("%s%d", pool, (hintId/200000)+1)
	bucket := pool + shardId
	my, present := this.clients[bucket]
	if !present {
		return nil, ErrServerNotFound
	}

	this.lookupCache.Set(key, my)
	log.Debug("lookupCache[%s] set {pool^%s}", key, bucket)

	return my, nil
}