예제 #1
0
 def remove_object(self, obj, data):
     title = data["title"]
     keys = []
     for partial_title in partial_complete(title):
         partial_key = create_key(partial_title)
         self.client.srem(partial_key, data)
         key = "%s%s" % (self.prefix, partial_key)
         self.client.zrem(key, "^")
예제 #2
0
 def test_partial_complete(self):
     self.assertEqual(list(partial_complete('1')), ['1'])
     self.assertEqual(list(partial_complete('1 2')), ['1 2'])
     self.assertEqual(list(partial_complete('1 2 3')), ['1 2 3'])
     self.assertEqual(list(partial_complete('1 2 3 4')), ['1 2 3', '2 3 4'])
     self.assertEqual(list(partial_complete('1 2 3 4 5')), ['1 2 3', '2 3 4', '3 4 5'])
     
     self.assertEqual(
         list(partial_complete('The Best of times, the blurst of times')),
         ['best of times,', 'of times, blurst', 'times, blurst of', 'blurst of times'])
     
     self.assertEqual(list(partial_complete('a the An')), [''])
     self.assertEqual(list(partial_complete('a')), [''])
예제 #3
0
    def store_object(self, obj, data):
        """
        Given a title & some data that needs to be stored, make it available
        for autocomplete via the suggest() method
        """
        title = data["title"]
        for partial_title in partial_complete(title):
            for (key, value, score) in self.autocomplete_keys(partial_title):
                self.client.zadd("%s%s" % (self.prefix, key), value, score)

            self.client.sadd(key, data["data"])
예제 #4
0
 def test_partial_complete(self):
     self.assertEqual(list(partial_complete('1')), ['1'])
     self.assertEqual(list(partial_complete('1 2')), ['1 2'])
     self.assertEqual(list(partial_complete('1 2 3')), ['1 2', '2 3', '1 2 3'])
     self.assertEqual(list(partial_complete('1 2 3 4')), ['1 2', '2 3', '3 4', '1 2 3', '2 3 4'])
     self.assertEqual(list(partial_complete('1 2 3 4 5')), ['1 2', '2 3', '3 4', '4 5', '1 2 3', '2 3 4', '3 4 5'])
     
     self.assertEqual(
         list(partial_complete('The Best of times, the blurst of times')),
         ['best times,', 'times, blurst', 'blurst times', 'best times, blurst', 'times, blurst times']
     )
     
     self.assertEqual(list(partial_complete('a the An')), [''])
     self.assertEqual(list(partial_complete('a')), [''])
예제 #5
0
    def remove_object(self, obj, data):
        title = data["title"]
        keys = []

        obj_data = self.get_object_data(obj)

        # ...how to figure out if its the final item...
        for partial_title in partial_complete(title):
            # get a list of all the keys that would have been set for the tries
            autocomplete_keys = list(self.autocomplete_keys(partial_title))

            # flag for whether ours is the last object at this lookup
            is_last = False

            # grab all the members of this lookup set
            partial_key = create_key(partial_title)
            objects_at_key = self.client.smembers(partial_key)

            # check the data at this lookup set to see if ours was the only obj
            # referenced at this point
            if obj_data not in objects_at_key:
                # something weird happened and our data isn't even here
                continue
            elif len(objects_at_key) == 1:
                # only one object stored here, remove the terminal flag
                zset_key = "%s%s" % (self.prefix, partial_key)
                self.client.zrem(zset_key, "^")

                # see if there are any other references to keys here
                is_last = self.client.zcard(zset_key) == 0

            if is_last:
                for (key, value, score) in reversed(autocomplete_keys):
                    key = "%s%s" % (self.prefix, key)

                    # another lookup ends here, so bail
                    if "^" in self.client.zrange(key, 0, -1):
                        self.client.zrem(key, value)
                        break
                    else:
                        self.client.delete(key)

                # we can just blow away the lookup key
                self.client.delete(partial_key)
            else:
                # remove only our object's data
                self.client.srem(partial_key, obj_data)

        # finally, remove the data from the data key
        self.client.delete("objdata:%s" % obj_data)
예제 #6
0
    def remove_object(self, obj, data):
        title = data['title']
        keys = []

        obj_data = self.get_object_data(obj)
        
        #...how to figure out if its the final item...
        for partial_title in partial_complete(title):
            # get a list of all the keys that would have been set for the tries
            autocomplete_keys = list(self.autocomplete_keys(partial_title))
            
            # flag for whether ours is the last object at this lookup
            is_last = False
            
            # grab all the members of this lookup set
            partial_key = create_key(partial_title)
            objects_at_key = self.client.smembers(partial_key)
            
            # check the data at this lookup set to see if ours was the only obj
            # referenced at this point
            if obj_data not in objects_at_key:
                # something weird happened and our data isn't even here
                continue
            elif len(objects_at_key) == 1:
                # only one object stored here, remove the terminal flag
                zset_key = '%s%s' % (self.prefix, partial_key)
                self.client.zrem(zset_key, '^')
                
                # see if there are any other references to keys here
                is_last = self.client.zcard(zset_key) == 0
            
            if is_last:
                for (key, value, score) in reversed(autocomplete_keys):
                    key = '%s%s' % (self.prefix, key)
                    
                    # another lookup ends here, so bail
                    if '^' in self.client.zrange(key, 0, -1):
                        self.client.zrem(key, value)
                        break
                    else:
                        self.client.delete(key)
                
                # we can just blow away the lookup key
                self.client.delete(partial_key)
            else:
                # remove only our object's data
                self.client.srem(partial_key, obj_data)
        
        # finally, remove the data from the data key
        self.client.delete('objdata:%s' % obj_data)
예제 #7
0
    def store_object(self, obj, data):
        """
        Given a title & some data that needs to be stored, make it available
        for autocomplete via the suggest() method
        """
        title = data["title"]

        # store actual object data
        obj_data = self.get_object_data(obj)
        self.client.set("objdata:%s" % obj_data, data["data"])

        # create tries using sorted sets and add obj_data to the lookup set
        for partial_title in partial_complete(title):
            # store a reference to our object in the lookup set
            self.client.sadd(create_key(partial_title), obj_data)

            for (key, value, score) in self.autocomplete_keys(partial_title):
                self.client.zadd("%s%s" % (self.prefix, key), value, score)
예제 #8
0
 def store_object(self, obj, data):
     """
     Given a title & some data that needs to be stored, make it available
     for autocomplete via the suggest() method
     """
     title = data['title']
     
     # store actual object data
     obj_data = self.get_object_data(obj)
     self.client.set('objdata:%s' % obj_data, data['data'])
     
     # create tries using sorted sets and add obj_data to the lookup set
     for partial_title in partial_complete(title):
         # store a reference to our object in the lookup set
         self.client.sadd(create_key(partial_title), obj_data)
         
         for (key, value, score) in self.autocomplete_keys(partial_title):
             self.client.zadd('%s%s' % (self.prefix, key), value, score)
예제 #9
0
    def store_object(self, obj, data):
        """
        Given a title & some data that needs to be stored, make it available
        for autocomplete via the suggest() method
        """
        self.remove_object(obj, data)

        title = data['title']
        for partial_title in partial_complete(title):
            key = create_key(partial_title)
            autocomplete_obj = AutocompleteObject(
                title=key,
                object_id=obj.pk,
                content_type=ContentType.objects.get_for_model(obj),
                pub_date=data['pub_date'],
                data=data['data'])
            autocomplete_obj.save()
            autocomplete_obj.sites = data['sites']
예제 #10
0
 def store_object(self, obj, data):
     """
     Given a title & some data that needs to be stored, make it available
     for autocomplete via the suggest() method
     """
     self.remove_object(obj, data)
     
     title = data['title']
     for partial_title in partial_complete(title):
         key = create_key(partial_title)
         obj = AutocompleteObject(
             title=key,
             object_id=obj.pk,
             content_type=ContentType.objects.get_for_model(obj),
             pub_date=data['pub_date'],
             data=data['data']
         )
         obj.save()
         obj.sites = data['sites']