Example #1
0
    def load(self):
        """
        Load from memcache or bigtable.
        
        Remark : If you only want to load a single entry of Agency , please use
        getCachedEntityOr404 instead.
        """
        cache_key = self.get_cache_key()
        
        cache = memcache.get(cache_key)

        if cache == None:
            cache = {}
            entity = getCachedEntityOr404(Agency,id_or_name = self.id)
            agency = entity["instance"]
            
            cache["agency"] = entity
            
            property = getattr(Trip,"route")
            route_entity_list = []
            trip_key_list = {}
            
            gql = db.GqlQuery("SELECT * FROM gogogo_route where agency=:1",agency)
            
            for row in gql:		
                e = createEntity(row)
                route_entity_list.append(e)
                gql2 = db.GqlQuery("SELECT __key__ FROM gogogo_trip where route = :1",row)
                
                route_key = row.key().id_or_name()
                for trip in gql2:
                    if not route_key in trip_key_list:
                        trip_key_list[route_key] = []
                    trip_key_list[route_key].append(trip)

            cache["routes"] = route_entity_list
                        
            cache["trips"] = trip_key_list
            
            memcache.add(cache_key, cache, _default_cache_time)
            
        self.agency = cache["agency"]
        self.routes = cache["routes"]
        self.trips = cache["trips"]
Example #2
0
def getCachedEntityOr404(model = None,key=None,key_name=None , id = None , id_or_name = None):
	"""
	Get cached entity object generated by createEntity()
	"""
	
	if id_or_name:
		try:
			id = int(id_or_name)
		except ValueError:
			key_name = id_or_name	
	
	cache_key = getCacheEntityKey(model = model ,key=key , key_name = key_name, id = id)
	
	entity = memcache.get(cache_key)

	if entity == None:
		object = getCachedObjectOr404(model, key=key,key_name=key_name, id = id)
		entity = createEntity(object)		
		
		if not memcache.add(cache_key, entity, _default_cache_time):
			logging.error("Memcache set %s failed." % cache_key)
	
	return entity
Example #3
0
	def load(self):
		"""
		Load route and all related objects from memecache or bigtable.
		
		If you only want to load a single entry of Route , please use
		getCachedEntityOr404 instead.
		"""
		
		cache_key = self.get_cache_key()
		
		cache = memcache.get(cache_key)
		
		if cache == None:
			route_entity = getCachedEntityOr404(Route,id_or_name = self.id)
            
            #TODO - should get from CachedEntity
			agency_entity = createEntity(route_entity['instance'].agency)
			
			trip_list = []
			gql = db.GqlQuery("SELECT __key__ from gogogo_trip where route = :1",route_entity['instance'])
			for key in gql:
				id = key.id_or_name()
				trip = TripLoader(id)
				trip.load()
				trip_list.append(trip)
			
			cache = {}	
			cache['route'] = route_entity
			cache['agency'] = agency_entity
			cache['trip_list'] = trip_list
			
			memcache.add(cache_key, cache, _default_cache_time)
			
		self.entity = cache['route']
		self.agency = cache['agency']
		self.trip_list = cache['trip_list']
Example #4
0
    def load(self,stop_table = None):
        """
        Load trip and all related objects from memecache or bigtable.
        
        If you only want to load a single entry of Trip , please use
        getCachedEntityOr404 instead.
        
        @param stop_table A dict of stop entities. It is used as cache to speed up the stop loading.
        """
        
        cache_key = self.get_cache_key()
        
        cache = memcache.get(cache_key)
        
        if cache == None:
            cache = {}
            
            trip_entity = getCachedEntityOr404(Trip,id_or_name = self.id)
            trip = trip_entity['instance']
            cache['trip'] = trip_entity
                        
            cache['route'] = getCachedEntityOr404(Route,id_or_name = trip_entity['route'] )
            cache['agency'] = getCachedEntityOr404(Agency,id_or_name = cache['route']['agency'] )
            
            first = None
            last = None
            try:
                first = db.get(trip.stop_list[0])
                last = db.get(trip.stop_list[len(trip.stop_list)-1])
            except IndexError:
                last = first
            
            stop_entity_list = []
            for key in trip.stop_list:
                stop_id = key.id_or_name()
                if stop_table and stop_id in stop_table:
                    stop_entity_list.append(stop_table[stop_id])
                    continue
                try:
                    stop = getCachedEntityOr404(Stop,id_or_name= stop_id)
                    stop_entity_list.append(stop)
                except Http404:
                    logging.error("Stop %s not found" % str(stop_id))
            
            if first != None:
                cache['first'] = createEntity(first)
            else:
                cache['first'] = None
            if last != None:	
                cache['last'] = createEntity(last)
            else:
                cache['last'] = None
            cache['trip'] = trip_entity
            cache['stop_entity_list'] = stop_entity_list
            
            #Fare Trip
            faretrip_entity_list = []
            for faretrip in trip.faretrip_set:
                entity = createEntity(faretrip)
                del entity['instance']
                min = sys.maxint
                max = 0
                for fare in faretrip.fares:
                    if fare < min:
                        min = fare
                    if fare > max:
                        max = fare
                        
                if max > 0 :
                    entity["min_fare"] = min
                    entity["max_fare"] = max
                else:
                    entity["min_fare"] = -1
                    entity["max_fare"] = -1
                    
                faretrip_entity_list.append(entity)
            
            cache['faretrip_entity_list'] = faretrip_entity_list
            
            memcache.add(cache_key, cache, _default_cache_time)

        self.agency = cache['agency']
        self.route = cache['route']
        self.trip  = cache['trip']
            
        # First station/stop	
        self.first = cache['first']
        
        # Last station/stop
        self.last = cache['last']
        self.entity = cache['trip']
        self.stop_entity_list = cache['stop_entity_list']
        self.faretrip_entity_list = cache['faretrip_entity_list']