//Dictionary is the cache for providing the dictionary to the platform on demand
Dictionary is the cache for providing the dictionary to the platform on demand
// Dictionary is the cache for providing the dictionary to the platform on demand
func Dictionary(in chan DICTRequest) {
	dict := make(map[string]DICT)
	for {
		req := <-in
		switch req.Type {
		case DICTAdd:
			req.DICT.LastUsed = time.Now()
			dict[req.ID] = req.DICT
			go SendTokenizerToChannel(
				TokenizerInputChannel,
				Request{
					ID:        req.ID,
					Type:      TokenizerAdd,
					Out:       make(chan Request),
					Tokenizer: Tokenizer{Map: req.DICT.Map},
				})
			break
		case DICTGet:
			req.DICT, req.Valid = dict[req.ID]
			if !req.Valid {
				req.DICT, req.Valid = getDICT(req.ID, false)
			}
			req.DICT = req.DICT.Copy()
			req.DICT.LastUsed = time.Now()
			dict[req.ID] = req.DICT
			go SendDICTToChannel(req.Out, req)
			break
		case DICTPreCache:
			req.DICT, req.Valid = dict[req.ID]
			if !req.Valid {
				req.DICT, req.Valid = getDICT(req.ID, false)
			}
			if !req.Valid {
				break
			}
			req.DICT.LastUsed = time.Now()
			dict[req.ID] = req.DICT
			SendTokenizerToChannel(
				TokenizerInputChannel,
				Request{
					ID:        req.ID,
					Type:      TokenizerAdd,
					Out:       make(chan Request),
					Tokenizer: Tokenizer{Map: req.DICT.Map},
				})
			go SendDICTToChannel(req.Out, req)
			break
		case DICTRemoveCheck:
			t := time.Now()
			for k, v := range dict {
				if v.LastUsed.Add(DICTExpiry).After(t) {
					continue
				}
				delete(dict, k)
				go SendTokenizerToChannel(
					TokenizerInputChannel,
					Request{
						ID:   k,
						Type: TokenizerRemove,
					})
			}
			break
		case DICTRemove:
			delete(dict, req.ID)
			go SendTokenizerToChannel(
				TokenizerInputChannel,
				Request{
					ID:   req.ID,
					Type: TokenizerRemove,
				})
			break
		case DICTUpdate:
			delete(dict, req.ID)
			d, ok := getDICT(req.ID, true)
			if !ok {
				break
			}
			req.DICT.LastUsed = time.Now()
			dict[req.ID] = d
			SendTokenizerToChannel(
				TokenizerInputChannel,
				Request{
					ID:        req.ID,
					Type:      TokenizerAdd,
					Out:       make(chan Request),
					Tokenizer: Tokenizer{Map: d.Map},
				})
			break
		}
	}
}
func Dictionary ( in chan DICTRequest ) { dict := make ( map [ string ] DICT ) for { req := <- in switch req . Type { case DICTAdd : req . DICT . LastUsed = time . Now ( ) dict [ req . ID ] = req . DICT go SendTokenizerToChannel ( TokenizerInputChannel , Request { ID : req . ID , Type : TokenizerAdd , Out : make ( chan Request ) , Tokenizer : Tokenizer { Map : req . DICT . Map } , } ) break case DICTGet : req . DICT , req . Valid = dict [ req . ID ] if ! req . Valid { req . DICT , req . Valid = getDICT ( req . ID , false ) } req . DICT = req . DICT . Copy ( ) req . DICT . LastUsed = time . Now ( ) dict [ req . ID ] = req . DICT go SendDICTToChannel ( req . Out , req ) break case DICTPreCache : req . DICT , req . Valid = dict [ req . ID ] if ! req . Valid { req . DICT , req . Valid = getDICT ( req . ID , false ) } if ! req . Valid { break } req . DICT . LastUsed = time . Now ( ) dict [ req . ID ] = req . DICT SendTokenizerToChannel ( TokenizerInputChannel , Request { ID : req . ID , Type : TokenizerAdd , Out : make ( chan Request ) , Tokenizer : Tokenizer { Map : req . DICT . Map } , } ) go SendDICTToChannel ( req . Out , req ) break case DICTRemoveCheck : t := time . Now ( ) for k , v := range dict { if v . LastUsed . Add ( DICTExpiry ) . After ( t ) { continue } delete ( dict , k ) go SendTokenizerToChannel ( TokenizerInputChannel , Request { ID : k , Type : TokenizerRemove , } ) } break case DICTRemove : delete ( dict , req . ID ) go SendTokenizerToChannel ( TokenizerInputChannel , Request { ID : req . ID , Type : TokenizerRemove , } ) break case DICTUpdate : delete ( dict , req . ID ) d , ok := getDICT ( req . ID , true ) if ! ok { break } req . DICT . LastUsed = time . Now ( ) dict [ req . ID ] = d SendTokenizerToChannel ( TokenizerInputChannel , Request { ID : req . ID , Type : TokenizerAdd , Out : make ( chan Request ) , Tokenizer : Tokenizer { Map : d . Map } , } ) break } } }
Dictionary is the cache for providing the dictionary to the platform on demand
Dictionary is the cache for providing the dictionary to the platform on demand
/*
	 * We will go into an infinte loop
	 * Will wait for the requests to come through the channel
	 * Based on the type of the request we will remove them from memory
	 */ //we will only give the copies of the dict item to avoid mutation //we will iterate over the cache and check the last usage