Example #1
0
func getSlice(s *Seq) []int {
	v := vector.Vector(*s)
	l := len(v)
	slice := make([]int, l)
	for i := 0; i < l; i++ {
		slice[i] = v.At(i).(int)
	}
	return slice
}
Example #2
0
func (l *ObservedWidgetList) Clear() (err os.Error) {
	observer := l.observer
	if observer != nil {
		err = observer.onClearingWidgets()
		if err != nil {
			return
		}
	}

	oldLen := l.items.Len()
	l.items = vector.Vector(l.items[0:0])

	if observer != nil {
		err = observer.onClearedWidgets()
		if err != nil {
			l.items = vector.Vector(l.items[0:oldLen])
			return
		}
	}

	l.items.Resize(0, 8)

	return
}
Example #3
0
		}
	}{[]struct {
		b string
	}{{"a"}, {"b"}, {"c"}}},
		"abc",
	},
	{`{{#a}}{{b}}{{/a}}`, struct{ a []map[string]string }{[]map[string]string{{"b": "a"}, {"b": "b"}, {"b": "c"}}}, "abc"},

	{`{{#users}}{{Name}}{{/users}}`, map[string]interface{}{"users": []User{{"Mike", 1}}}, "Mike"},

	{`{{#users}}gone{{Name}}{{/users}}`, map[string]interface{}{"users": nil}, ""},
	{`{{#users}}gone{{Name}}{{/users}}`, map[string]interface{}{"users": (*User)(nil)}, ""},
	{`{{#users}}gone{{Name}}{{/users}}`, map[string]interface{}{"users": []User{}}, ""},

	{`{{#users}}{{Name}}{{/users}}`, map[string]interface{}{"users": []*User{&User{"Mike", 1}}}, "Mike"},
	{`{{#users}}{{Name}}{{/users}}`, map[string]interface{}{"users": vector.Vector([]interface{}{&User{"Mike", 12}})}, "Mike"},
	{`{{#users}}{{Name}}{{/users}}`, map[string]interface{}{"users": makeVector(1)}, "Mike"},
	{`{{Name}}`, User{"Mike", 1}, "Mike"},
	{`{{Name}}`, &User{"Mike", 1}, "Mike"},
	{"{{#users}}\n{{Name}}\n{{/users}}", map[string]interface{}{"users": makeVector(2)}, "Mike\nMike\n"},
	{"{{#users}}\r\n{{Name}}\r\n{{/users}}", map[string]interface{}{"users": makeVector(2)}, "Mike\r\nMike\r\n"},

	//inverted section tests
	{`{{a}}{{^b}}b{{/b}}{{c}}`, map[string]string{"a": "a", "c": "c"}, "abc"},
	{`{{a}}{{^b}}b{{/b}}{{c}}`, map[string]interface{}{"a": "a", "b": false, "c": "c"}, "abc"},
	{`{{^a}}b{{/a}}`, map[string]interface{}{"a": false}, "b"},
	{`{{^a}}b{{/a}}`, map[string]interface{}{"a": true}, ""},
	{`{{^a}}b{{/a}}`, map[string]interface{}{"a": "nonempty string"}, ""},

	//function tests
	{`{{#users}}{{Func1}}{{/users}}`, map[string]interface{}{"users": []User{{"Mike", 1}}}, "Mike"},
Example #4
0
// little helper function for sorting servers
func (ss *Storageserver) sortServers() {
	v := vector.Vector(ss.servers)
	sort.Sort(&v)
}
Example #5
0
func (ts *Tribserver) GetTribblesBySubscription(args *tribproto.GetTribblesArgs, reply *tribproto.GetTribblesReply) os.Error {
	//check if user exists
	suscribJson, stat, err := ts.tm.GET(args.Userid + SUBSCRIPTIONS)
	if err != nil {
		return err
	}
	if stat == storageproto.OK {
		suscribers := make(map[string]bool)
		err = json.Unmarshal(suscribJson, &suscribers)
		if err != nil {
			return err
		}

		//make minheap to store recent tribbles
		recentTribs := vector.Vector(make([]interface{}, 0, 100))
		heap.Init(&recentTribs)
		level := 0
		hasTribs := false

		//continue adding tribbles to heap until we reach a 100 or no more tribbles
		for recentTribs.Len() < 100 {
			hasTribs = false

			//for each suscription, get the last tribble
			for subscriber, _ := range suscribers {
				s := new(string)
				err = json.Unmarshal([]byte(subscriber), s)
				if err != nil {
					return err
				}

				lastJson, _, err := ts.tm.GET(*s + LAST)
				if err != nil {
					return err
				}
				last := new(int)
				err = json.Unmarshal(lastJson, last)
				if err != nil {
					return err
				}
				*last -= level
				tribbleId := fmt.Sprintf("%s:%d", *s, *last)
				if *last > 0 {
					hasTribs = true
					tribJson, stat, err := ts.tm.GET(tribbleId)
					if err != nil {
						return err
					}
					if stat == storageproto.OK {
						trib := new(tribproto.Tribble)
						err = json.Unmarshal(tribJson, trib)
						if err != nil {
							return err
						}

						//add to tribble if more recent than oldest tribble in heap
						if recentTribs.Len() >= 100 && recentTribs[0].(tribproto.Tribble).Posted < trib.Posted {
							heap.Pop(&recentTribs)
							heap.Push(&recentTribs, trib)

							//add to tribble if less than 100 in heap
						} else if recentTribs.Len() < 100 {
							heap.Push(&recentTribs, trib)
						}
					}
				}
			}
			level++
			if !hasTribs {
				break
			}
		}
		reply.Tribbles = make([]tribproto.Tribble, recentTribs.Len())
		for i := recentTribs.Len() - 1; i >= 0; i-- {
			reply.Tribbles[i] = *(heap.Pop(&recentTribs).(*tribproto.Tribble))
		}
	} else {
		reply.Status = tribproto.ENOSUCHUSER
	}
	return nil
}