From aa769730b7ff7ed5c2012831ed947a182a8ec400 Mon Sep 17 00:00:00 2001 From: James Hill Date: Thu, 17 Sep 2015 14:57:51 +1200 Subject: [PATCH] Prevent dups on manual retrieve during start cache Fixes Issue #16 If a user fired off a retrieve from the command palette during Sublime startup caching (for a doc type that was being cached), you would wind up with duplicates. To fix this, the cache now implements MutableSet as well as MutableSequence, and existing methods check for existence of duplicates. The whole structure could use some optimisation, but will get us over the line for now --- salesforce_reference/cache.py | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/salesforce_reference/cache.py b/salesforce_reference/cache.py index 434caae..c27250d 100644 --- a/salesforce_reference/cache.py +++ b/salesforce_reference/cache.py @@ -1,7 +1,7 @@ import collections from functools import total_ordering -class SalesforceReferenceCache(collections.MutableSequence): +class SalesforceReferenceCache(collections.MutableSequence,collections.MutableSet): """ A cache of SalesforceReferenceEntry objects, sorted by Title. This order will be maintained throughout append operations @@ -32,17 +32,21 @@ def entries_by_doc_type(self): def __getitem__(self, key): return self.__entries[key] - def __setitem__(self, key, value): - self.__entries[key] = value - self.__maintain_cache() + def __setitem__(self, key, item): + # Enforce set behaviour + if item not in self.__entries: + self.__entries[key] = item + self.__maintain_cache() def __delitem__(self, key): del self.__entries[key] self.__maintain_cache() def __len__(self): return len(self.__entries) - def insert(self, key,val): - self.__entries.insert(key,val) - self.__maintain_cache() + def insert(self, key, item): + # Enforce set behaviour + if item not in self.__entries: + self.__entries.insert(key,item) + self.__maintain_cache() def __maintain_cache(self): self.__entries.sort() self.__index_entries_by_doc_type() @@ -66,6 +70,18 @@ def __groupby(self, the_list, key=lambda x: x): d[key(item)].append(item) return d.items() + """MutableSet methods""" + def add(self,item): + if item not in self.__entries: + self.__entries.append(item) + self.__maintain_cache() + def discard(self,item): + try: + del self.__entries[self.__entries.index(item)] + self.__maintain_cache() + except ValueError: + pass + """str and repr implemented for debugging""" def __str__(self): return str(self.__entries)