def get(self): from user_sn import confirm u = confirm(self) from cache import get_cache_by_name, can_operate_on, ContentCopy from Content import content_by_id c = get_cache_by_name(self.request.get("cache")) if not can_operate_on(c,u): raise Exception("You can't sync this cache.") content = content_by_id(self.request.get("content")) logging.info(c.key()) logging.info(content.key()) copy = ContentCopy.all().filter("where =",c).filter("content =",content).get() logging.info(copy) if copy is None: self.repsonse.out.write("NO_SUCH_THING") return result = can_purge(u,copy) if result==PURGE_AT_WILL: self.response.out.write("PURGE_AT_WILL") logging.info("PURGE_AT_WILL") elif result==MOVE_TO_SOFTCACHE: self.response.out.write("MOVE_TO_SOFTCACHE") logging.info("MOVE_TO_SOFTCACHE") else: self.response.out.write("LEAVE_IN_PLACE") logging.info("LEAVE_IN_PLACE")
def can_purge(user,contentcopy): from cache import ContentCopy, TYPE_COMPUTER allCopies = ContentCopy.all().filter("content =",contentcopy.content).fetch(limit = 1000) #now fetch all the copies allCopies = filter(available_soon_cc,allCopies) #only the ones that are online if len(allCopies) > LEAVE_COPIES_ON_DARKNET+1: copies_on_team = 0 chk_team = team_of(contentcopy) for copy in allCopies: if copy.key()==contentcopy.key(): continue #not interested in the copy we want to purge if chk_team.key()==team_of(copy).key(): copies_on_team += 1 if copies_on_team >= LEAVE_COPIES_ON_TEAM: if not user.team_leader_flag: #wait for the team leader to purge the crap. team_leader_copies = filter(lambda x: x.where.type==TYPE_COMPUTER, copies_on_team) from user_sn import all_team_leaders_for leaders = all_team_leaders_for(user.team) leaders = map(lambda x: x.key(),leaders) team_leader_copies = filter(lambda x: x.where.last_touched.key() in leaders) if len(team_leader_copies) != 0: logging.info("Team leader has a copy. Not purging.") return MOVE_TO_SOFTCACHE else: return PURGE_AT_WILL #this is conservative logic. If there's an outstanding request on the sneakernet, leave the file in place from request import Request reqs = Request.all().filter("file =",contentcopy.content).get() if reqs==None: return MOVE_TO_SOFTCACHE return LEAVE_IN_PLACE
def get(self): from cache import can_operate_on, ContentCopy, get_cache_by_name, Cache, TYPE_COMPUTER from user_sn import confirm u = confirm(self) cname = self.request.get("cache") logging.info("Syncing a cache named %s" % cname) c = get_cache_by_name(cname) if c == None: logging.info("No cached named %s, making a new one" % cname) c = Cache(friendlyName=cname, type=TYPE_COMPUTER, last_touched=u, space_left=-1, person_responsible=u) c.put() if not can_operate_on(c, u): raise Exception("You're not permitted to sync this cache.") # fetch everything that's "supposed" to be on the key copies = ContentCopy.all().filter("where =", c).fetch(limit=1000) self.response.out.write("OK\n") for copy in copies: self.response.out.write(copy.content.file_id + "\n")
def copies_that_can_fulfill(request): from cache import ContentCopy return ContentCopy.all().filter("content =", request.file).fetch(limit=1000)
def post(self): from user_sn import confirm from cache import ( can_operate_on, get_copy, ContentCopy, get_cache_by_name, TYPE_COMPUTER, TYPE_EXTERN_FD, TYPE_LOCAL_FD, TYPE_EXTERNAL_RING, ) from Content import content_by_id u = confirm(self) cname = self.request.get("cache") c = get_cache_by_name(cname) if not can_operate_on(c, u): raise Exception("You're not permitted to sync this cache.") deletes = self.request.get("deletes") for item in deletes.split("\n"): if item == "": continue logging.info("deleting %s" % item) get_copy(cname, item).delete() adds = self.request.get("adds") for item in adds.split("\n"): if item == "": continue logging.info("adding %s" % item) # see if there's an RC associated with this from request import RequestCopy content = content_by_id(item) rcs = RequestCopy.all().filter("file =", content) if c.type == TYPE_COMPUTER: rcs = rcs.filter("dest =", u).fetch(limit=1000) elif c.type == TYPE_LOCAL_FD: rcs = ( rcs.filter("dest =", c.permanent_location.team_responsible) .filter("dest_int =", TYPE_LOCAL_FD) .fetch(limit=1000) ) elif c.type == TYPE_EXTERN_FD: # the common case (closing the RC associated with a swap) is handled in runComplete. rcs = ( RequestCopy.all() .filter("file =", content) .filter("dest_int =", TYPE_EXTERNAL_RING) .fetch(limit=1000) ) for rc in rcs: logging.info("Closing %s" % rc.key()) rc.delete() co = ContentCopy(where=c, content=content) if co.content is None: logging.warning("Not adding %s" % item) else: co.put() c.space_left = int(self.request.get("size")) c.last_touched = u c.put()