func (ls *libstore) deletelist() {
	now := time.Now()
	// ls.mutex.Lock()
	// // fmt.Println("deleteinvalid")
	// defer ls.mutex.Unlock()

	for key, list := range ls.keyValudhistory {
		ls.initiateKeyValueMutex(key)
		ls.keyValueMutex[key].Lock()
		for list.Len() > 0 {
			e := list.Front()
			if e.Value.(time.Time).Add(time.Duration(storagerpc.QueryCacheSeconds)*time.Second).After(now) && list.Len() <= storagerpc.QueryCacheThresh {
				break
			}
			list.Remove(list.Front())
		}
		ls.keyValueMutex[key].Unlock()
	}

	for key, list := range ls.keyListhistory {
		ls.initiateKeyListMutex(key)
		ls.keyListMutex[key].Lock()
		for list.Len() > 0 {
			e := list.Front()
			if e.Value.(time.Time).Add(time.Duration(storagerpc.QueryCacheSeconds)*time.Second).After(now) && list.Len() <= storagerpc.QueryCacheThresh {
				break
			}
			list.Remove(list.Front())
		}
		ls.keyListMutex[key].Unlock()
	}
}
Exemple #2
0
// Reads a list of ASN1Cert types from |r|
func readASN1CertList(r io.Reader, totalLenBytes int, elementLenBytes int) ([]ASN1Cert, error) {
	listBytes, err := readVarBytes(r, totalLenBytes)
	if err != nil {
		return []ASN1Cert{}, err
	}
	list := list.New()
	listReader := bytes.NewReader(listBytes)
	var entry []byte
	for err == nil {
		entry, err = readVarBytes(listReader, elementLenBytes)
		if err != nil {
			if err != io.EOF {
				return []ASN1Cert{}, err
			}
		} else {
			list.PushBack(entry)
		}
	}
	ret := make([]ASN1Cert, list.Len())
	i := 0
	for e := list.Front(); e != nil; e = e.Next() {
		ret[i] = e.Value.([]byte)
		i++
	}
	return ret, nil
}
func (ss *storageServer) GetList(args *storagerpc.GetArgs, reply *storagerpc.GetListReply) error {
	if !ss.isInServerRange(args.Key) {
		reply.Status = storagerpc.WrongServer
		return nil
	}

	//get the lock for current key
	ss.mutex.Lock()
	lock, exist := ss.lockMap[args.Key]
	if !exist {
		lock = new(sync.Mutex)
		ss.lockMap[args.Key] = lock
	}
	ss.mutex.Unlock()

	//Lock current key we are going to work on
	lock.Lock()

	//process lease request
	grantLease := false
	if args.WantLease {
		//add to lease map
		var libServerList *list.List
		libServerList, exist := ss.leaseMap[args.Key]
		if !exist {
			libServerList = new(list.List)
		}
		leaseRecord := LeaseRecord{args.HostPort, time.Now()}
		libServerList.PushBack(leaseRecord)
		ss.leaseMap[args.Key] = libServerList
		grantLease = true
	}
	reply.Lease = storagerpc.Lease{grantLease, storagerpc.LeaseSeconds}

	//retrieve list
	list, exist := ss.keyListMap[args.Key]
	if !exist {
		reply.Status = storagerpc.KeyNotFound
	} else {
		//convert list format from "map" to "[]string"
		listSize := list.Len()
		replyList := make([]string, listSize)

		i := 0
		for e := list.Front(); e != nil; e = e.Next() {
			val := (e.Value).(string)
			replyList[i] = val
			i = i + 1
		}

		reply.Value = replyList
		reply.Status = storagerpc.OK
	}

	lock.Unlock()
	return nil
}
Exemple #4
0
func MatchAll(m *Matcher, text string) []Match {
	list := list.New()
	ch := m.Match(text)
	for n := range ch {
		list.PushBack(n)
	}
	all := make([]Match, list.Len())
	idx := 0
	for e := list.Front(); e != nil; e = e.Next() {
		all[idx] = e.Value.(Match)
		idx++
	}
	return all
}
Exemple #5
0
func daythirteen() {
	file, _ := os.Open("input-day13")
	list := list.New()
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		text := scanner.Text()
		list.PushFront(text)
	}
	arr := make([]string, list.Len())
	count := 0
	for e := list.Front(); e != nil; e = e.Next() {
		arr[count] = e.Value.(string)
		count++
	}
	fmt.Printf("Best = %d\n", ProcSeatingArray(arr, 0))
	fmt.Printf("Best = %d\n", ProcSeatingArray(arr, 1))
}
Exemple #6
0
func reconstructPath(came_from map[Coord]Coord, current_node Coord) []*Move {
	list := list.New()
	for present := true; present; _, present = came_from[current_node] {
		next_node := came_from[current_node]
		list.PushFront(findMove(current_node, next_node))

		current_node = next_node
	}

	// Convert to a list and return
	rtn := make([]*Move, list.Len())
	i := 0
	for e := list.Front(); e != nil; e = e.Next() {
		move := e.Value.(Move)
		rtn[i] = &move
		i++
	}
	return rtn
}
Exemple #7
0
//根据taskId查看某一个task
func task(w http.ResponseWriter, r *http.Request) {

	taskId := r.FormValue("taskId")

	mark, list := dbutil.FetchTaskById(taskId)
	if list.Len() == 0 {
		io.WriteString(w, "without this task !")
		return
	}

	if mark == 1 {
		var listHtml string = "<body><ol>"
		for e := list.Front(); e != nil; e = e.Next() {
			task, ok := e.Value.(dbutil.Task)
			if ok {
				//获取task信息
				host := task.Host
				port := task.Port
				filterLength := task.FilterLength
				filterKey := task.FilterKey
				taskId := task.TaskId
				priority := task.Priority
				status := task.Status
				createTime := task.CreateTime
				listHtml += "<li>" + "host:" + host + ";port:" + port + ";filterLength:" + strconv.FormatInt(int64(filterLength), 10) + ";filter_key:" + filterKey + ";taskId:" + taskId +
					";priority:" + strconv.FormatInt(int64(priority), 10) + ";status:" + strconv.FormatInt(int64(status), 10) + ";create_time:" + createTime + "</li>"
			} else {
				//task的类型不正确
				return
			}
			listHtml = listHtml + "</ol></body>"
		}
		io.WriteString(w, listHtml)
	} else {
		io.WriteString(w, "error occure !")
		logs.Log("error occure in taskView!")
	}
}
Exemple #8
0
// The command module takes as input a map, and runs that command.
//
// Parameters in the input map are:
//   :command, a list or vector, giving the command to run. Required.
//   :env, a map of names to values; if specified, these are added to the environment when running the command; optional
//   :input, a string to pass to the command's stdin; optional
func (this Command) Exec(input types.Value) (output types.Value, err error) {
	var command []string
	var commandMap types.Map
	if m, ok := input.(types.Map); ok {
		commandMap = m
	} else {
		err = errors.New("input must be a map")
		return
	}

	var commandEntry = commandMap[types.Keyword("command")]
	if commandEntry == nil {
		err = errors.New("require at least :command in input")
		return
	} else if l, ok := commandEntry.(*types.List); ok {
		var list = (*list.List)(l)
		command = make([]string, list.Len())
		var i = 0
		for e := list.Front(); e != nil; e = e.Next() {
			if elem, ok := e.Value.(types.String); ok {
				command[i] = string(elem)
				i++
			} else {
				err = errors.New(":command must be a list of strings")
				return
			}
		}
	} else if v, ok := commandEntry.(types.Vector); ok {
		var vect = ([]types.Value)(v)
		command = make([]string, len(vect))
		for i, e := range vect {
			if elem, ok := e.(types.String); ok {
				command[i] = string(elem)
			} else {
				err = errors.New(":command must be a vector of strings")
				return
			}
		}
	} else {
		err = errors.New("expected a list or vector as :command")
		return
	}
	var cmd = exec.Command(command[0], command[1:]...)

	var envEntry = commandMap[types.Keyword("env")]
	if envEntry != nil {
		if e, ok := envEntry.(types.Map); ok {
			var env = make([]string, len(e))
			var i = 0
			for k := range e {
				v := e[k]
				if kk, ok := k.(types.String); ok && len(kk) > 0 {
					if vv, ok := v.(types.String); ok && len(vv) > 0 {
						env[i] = fmt.Sprint(string(kk), "=", string(vv))
					} else {
						err = errors.New(":env values must be nonempty strings")
						return
					}
				} else {
					err = errors.New(":env keys must be nonempty strings")
					return
				}
				i++
			}
			cmd.Env = env
		} else {
			err = errors.New(":env must be a map if specified")
			return
		}
	}

	var stdinEntry = commandMap[types.Keyword("input")]
	if stdinEntry != nil {
		if stdin, ok := stdinEntry.(types.String); ok {
			cmd.Stdin = strings.NewReader(string(stdin))
		} else {
			err = errors.New(":input must be a string if specified")
			return
		}
	}

	var stderr = new(bytes.Buffer)
	var stdout = new(bytes.Buffer)
	cmd.Stderr = stderr
	cmd.Stdout = stdout
	err = cmd.Run()
	if err != nil {
		output = types.Map{
			types.Keyword("success"):     types.Bool(false),
			types.Keyword("err"):         types.String(stderr.String()),
			types.Keyword("out"):         types.String(stdout.String()),
			types.Keyword("system-time"): types.Float(cmd.ProcessState.SystemTime().Seconds()),
			types.Keyword("user-time"):   types.Float(cmd.ProcessState.UserTime().Seconds()),
		}
		return
	}

	output = types.Map{
		types.Keyword("success"):     types.Bool(true),
		types.Keyword("err"):         types.String(stderr.String()),
		types.Keyword("out"):         types.String(stdout.String()),
		types.Keyword("system-time"): types.Float(cmd.ProcessState.SystemTime().Seconds()),
		types.Keyword("user-time"):   types.Float(cmd.ProcessState.UserTime().Seconds()),
	}
	return
}
func (ls *libstore) GetList(key string) ([]string, error) {
	now := time.Now()
	ls.initiateKeyListMutex(key)
	ls.keyListMutex[key].Lock()
	defer ls.keyListMutex[key].Unlock()

	value, ok := ls.keylist[key]

	if ok {
		validUntil := value.lease.startFrom.Add(time.Duration(value.lease.validSeconds) * time.Second)

		if now.After(validUntil) {
			delete(ls.keylist, key)
			ok = !ok
		}
	}
	list, list_ok := ls.keyListhistory[key]
	if list_ok {
		for list.Len() > 0 {
			e := list.Front()
			pretime := e.Value.(time.Time)
			judgetime := pretime.Add(time.Duration(storagerpc.QueryCacheSeconds) * time.Second)
			if judgetime.After(now) && list.Len() <= storagerpc.QueryCacheThresh {
				break
			}
			list.Remove(list.Front())
		}

		list.PushBack(now) // do not know wether count cache or not
	}
	if ok { //use cache
		return value.list, nil
	} else { // use the storage

		want := false
		if ls.mode == Always {
			want = true
		}
		if ls.mode == Normal && list_ok && ls.keyListhistory[key].Len() >= storagerpc.QueryCacheThresh {
			want = true
		}

		args := &storagerpc.GetArgs{
			Key:       key,
			WantLease: want,
			HostPort:  ls.myHostPort,
		}
		reply := &storagerpc.GetListReply{}

		server := ls.Findshashring(key)
		client, ok := ls.clientmap[server]
		if !ok {

			clienttemp, err := rpc.DialHTTP("tcp", server)
			if err != nil {
				return nil, errors.New("meet error when DialHTTP")
			}
			ls.clientmap[server] = clienttemp
			client = clienttemp
		}

		err := client.Call("StorageServer.GetList", args, &reply)
		if err != nil {

			return nil, errors.New("error when libstore call get to storageserver")
		}

		if reply.Status == storagerpc.OK {
			if reply.Lease.Granted == true { // update cache
				ls.keylist[key] = &listlib{
					list: reply.Value,
					lease: leasetype{
						startFrom:    now,
						validSeconds: reply.Lease.ValidSeconds,
					},
				}
			}
		} else if reply.Status == storagerpc.KeyNotFound {
			return make([]string, 0), nil
		} else {
			return nil, errors.New("reply not ready getlist: " + string(reply.Status))
		}
		return reply.Value, nil
	}
}
Exemple #10
0
func (stack *Stack) Len() int {
	list := (*list.List)(stack)
	return list.Len()
}