func (store *JobStore) SetAllocBuffer(key string, data []byte) error {

	store.Lock()
	defer store.Unlock()
	tempalloc := &models.JobsAlloc{
		Version: 0,
		Jobs:    make([]*models.JobData, 0),
	}

	if err := models.JobsAllocDeCode(data, tempalloc); err != nil {
		return err
	}

	if tempalloc.Version == 0 || tempalloc.Version == store.alloc.Version {
		return nil
	}

	if tempalloc.Version < store.alloc.Version && store.alloc.Version != 0 {
		return fmt.Errorf("josalloc version invalid. tempalloc:%d storealloc:%d", tempalloc.Version, store.alloc.Version)
	}

	for i := len(tempalloc.Jobs) - 1; i >= 0; i-- {
		if tempalloc.Jobs[i].Key != key { //筛选非本实例的任务
			tempalloc.Jobs = append(tempalloc.Jobs[:i], tempalloc.Jobs[i+1:]...)
		}
	}

	var found bool
	for i := len(store.alloc.Jobs) - 1; i >= 0; i-- {
		found = false
		jobdata := store.alloc.Jobs[i]
		for j := 0; j < len(tempalloc.Jobs); j++ {
			if jobdata.JobId == tempalloc.Jobs[j].JobId {
				found = true
				break
			}
		}
		if !found { //找出被删除的任务
			store.alloc.Jobs = append(store.alloc.Jobs[:i], store.alloc.Jobs[i+1:]...)
			store.getter.Remove(jobdata.JobId) //从下载器中删除
			if jobbase, ret := store.jobs[jobdata.JobId]; ret {
				jobbase.Version = 0 //remove job, re-set job version to zero, wait re-alloc.
				logger.INFO("[#cache#] CACHE_EVENT_JOBREMOVE ###REMOVE %v", jobdata)
				go store.changedCallback(CACHE_EVENT_JOBREMOVE, jobbase)
			}
		}
	}

	for i := 0; i < len(tempalloc.Jobs); i++ {
		found = false
		jobdata := tempalloc.Jobs[i]
		for j := 0; j < len(store.alloc.Jobs); j++ { //找出版本号已改变的任务(任务更新)
			if jobdata.JobId == store.alloc.Jobs[j].JobId {
				if jobdata.Version > store.alloc.Jobs[j].Version {
					store.alloc.Jobs[j] = jobdata
					jobbase := store.tryGet(jobdata)
					if jobbase != nil {
						logger.INFO("[#cache#] CACHE_EVENT_JOBSET ###CHANGE %v", jobdata)
						go store.changedCallback(CACHE_EVENT_JOBSET, jobbase)
					}
				}
				found = true
				break
			}
		}
		if !found { //加入新添加的任务
			store.alloc.Jobs = append(store.alloc.Jobs, jobdata)
			jobbase := store.tryGet(jobdata)
			if jobbase != nil {
				logger.INFO("[#cache#] CACHE_EVENT_JOBSET ###CREATE %v", jobdata)
				go store.changedCallback(CACHE_EVENT_JOBSET, jobbase)
			}
		}
	}
	store.alloc = tempalloc
	return nil
}
//SetAllocBuffer is exported
//alloc changed, set alloc data.
SetAllocBuffer is exported
alloc changed, set alloc data.
// SetAllocBuffer is exported // alloc changed set alloc data .
func (store *JobStore) SetAllocBuffer(key string, data []byte) error {
	store.Lock()
	defer store.Unlock()
	tempalloc := &models.JobsAlloc{
		Version: 0,
		Jobs:    make([]*models.JobData, 0),
	}
	if err := models.JobsAllocDeCode(data, tempalloc); err != nil {
		return err
	}
	if tempalloc.Version == 0 || tempalloc.Version == store.alloc.Version {
		return nil
	}
	if tempalloc.Version < store.alloc.Version && store.alloc.Version != 0 {
		return fmt.Errorf("josalloc version invalid. tempalloc:%d storealloc:%d", tempalloc.Version, store.alloc.Version)
	}
	for i := len(tempalloc.Jobs) - 1; i >= 0; i-- {
		if tempalloc.Jobs[i].Key != key { 
			tempalloc.Jobs = append(tempalloc.Jobs[:i], tempalloc.Jobs[i+1:]...)
		}
	}
	var found bool
	for i := len(store.alloc.Jobs) - 1; i >= 0; i-- {
		found = false
		jobdata := store.alloc.Jobs[i]
		for j := 0; j < len(tempalloc.Jobs); j++ {
			if jobdata.JobId == tempalloc.Jobs[j].JobId {
				found = true
				break
			}
		}
		if !found { 
			store.alloc.Jobs = append(store.alloc.Jobs[:i], store.alloc.Jobs[i+1:]...)
			store.getter.Remove(jobdata.JobId) 
			if jobbase, ret := store.jobs[jobdata.JobId]; ret {
				jobbase.Version = 0 
				logger.INFO("[#cache#] CACHE_EVENT_JOBREMOVE ###REMOVE %v", jobdata)
				go store.changedCallback(CACHE_EVENT_JOBREMOVE, jobbase)
			}
		}
	}
	for i := 0; i < len(tempalloc.Jobs); i++ {
		found = false
		jobdata := tempalloc.Jobs[i]
		for j := 0; j < len(store.alloc.Jobs); j++ { 
			if jobdata.JobId == store.alloc.Jobs[j].JobId {
				if jobdata.Version > store.alloc.Jobs[j].Version {
					store.alloc.Jobs[j] = jobdata
					jobbase := store.tryGet(jobdata)
					if jobbase != nil {
						logger.INFO("[#cache#] CACHE_EVENT_JOBSET ###CHANGE %v", jobdata)
						go store.changedCallback(CACHE_EVENT_JOBSET, jobbase)
					}
				}
				found = true
				break
			}
		}
		if !found { 
			store.alloc.Jobs = append(store.alloc.Jobs, jobdata)
			jobbase := store.tryGet(jobdata)
			if jobbase != nil {
				logger.INFO("[#cache#] CACHE_EVENT_JOBSET ###CREATE %v", jobdata)
				go store.changedCallback(CACHE_EVENT_JOBSET, jobbase)
			}
		}
	}
	store.alloc = tempalloc
	return nil
}
func ( store * JobStore ) SetAllocBuffer ( key string , data [ ] byte ) error { store . Lock ( ) defer store . Unlock ( ) tempalloc := & models . JobsAlloc { Version : 0 , Jobs : make ( [ ] * models . JobData , 0 ) , } if err := models . JobsAllocDeCode ( data , tempalloc ) ; err != nil { return err } if tempalloc . Version == 0 || tempalloc . Version == store . alloc . Version { return nil } if tempalloc . Version < store . alloc . Version && store . alloc . Version != 0 { return fmt . Errorf ( " " , tempalloc . Version , store . alloc . Version ) } for i := len ( tempalloc . Jobs ) - 1 ; i >= 0 ; i -- { if tempalloc . Jobs [ i ] . Key != key { tempalloc . Jobs = append ( tempalloc . Jobs [ : i ] , tempalloc . Jobs [ i + 1 : ] ... ) } } var found bool for i := len ( store . alloc . Jobs ) - 1 ; i >= 0 ; i -- { found = false jobdata := store . alloc . Jobs [ i ] for j := 0 ; j < len ( tempalloc . Jobs ) ; j ++ { if jobdata . JobId == tempalloc . Jobs [ j ] . JobId { found = true break } } if ! found { store . alloc . Jobs = append ( store . alloc . Jobs [ : i ] , store . alloc . Jobs [ i + 1 : ] ... ) store . getter . Remove ( jobdata . JobId ) if jobbase , ret := store . jobs [ jobdata . JobId ] ; ret { jobbase . Version = 0 logger . INFO ( " " , jobdata ) go store . changedCallback ( CACHE_EVENT_JOBREMOVE , jobbase ) } } } for i := 0 ; i < len ( tempalloc . Jobs ) ; i ++ { found = false jobdata := tempalloc . Jobs [ i ] for j := 0 ; j < len ( store . alloc . Jobs ) ; j ++ { if jobdata . JobId == store . alloc . Jobs [ j ] . JobId { if jobdata . Version > store . alloc . Jobs [ j ] . Version { store . alloc . Jobs [ j ] = jobdata jobbase := store . tryGet ( jobdata ) if jobbase != nil { logger . INFO ( " " , jobdata ) go store . changedCallback ( CACHE_EVENT_JOBSET , jobbase ) } } found = true break } } if ! found { store . alloc . Jobs = append ( store . alloc . Jobs , jobdata ) jobbase := store . tryGet ( jobdata ) if jobbase != nil { logger . INFO ( " " , jobdata ) go store . changedCallback ( CACHE_EVENT_JOBSET , jobbase ) } } } store . alloc = tempalloc return nil }
SetAllocBuffer is exported
alloc changed, set alloc data.
SetAllocBuffer is exported alloc changed set alloc data .
//筛选非本实例的任务 //找出被删除的任务 //从下载器中删除 //remove job, re-set job version to zero, wait re-alloc. //找出版本号已改变的任务(任务更新) //加入新添加的任务