Esempio n. 1
0
 def delete(self):
     config = CounterShardConfiguration.get_by_key_name(self.name)
     if config:
         config.delete()
     for c in CounterShard.find_by_name(self.name):
         c.delete()
     logging.debug("deleted %s" % self.name)
Esempio n. 2
0
    def set(self, value):
        """ sets value of counter """
        if value == 0:
            self.delete()
            return

        # calculates value to set for all counters
        counter_q = CounterShard.find_by_name(self.name)
        counter_count = counter_q.count()

        if not counter_count:
            # no counters exist, set value to single shard in transaction
            def counter_call(counter):
                counter.count = value

            self.__counter_tx(counter_call)
            return

        value_per_counter = float(value) / float(counter_count)

        values = []
        # TODO: is there a better way to detect floats than this?
        if (value_per_counter - int(value_per_counter)) > 0:
            # value is a float, I need to found all but last value down, and round last value up
            values.extend([
                math.floor(value_per_counter.floor)
                for i in xrange(counter_count)
            ])
            values[-1] = math.ceil(value_per_counter)
        logging.debug("setting counter values across shards: %s" % values)

        # sets values for all counter shards
        for shard, count in zip(counter_q, values):
            shard.count = count
            shard.put()
Esempio n. 3
0
 def delete(self):
     config = CounterShardConfiguration.get_by_key_name(self.name)
     if config:
         config.delete()
     for c in CounterShard.find_by_name(self.name):
         c.delete()
     logging.debug("deleted %s" % self.name)
Esempio n. 4
0
    def set(self, value):
        """ sets value of counter """
        if value == 0:
            self.delete()
            return
        
        # calculates value to set for all counters    
        counter_q = CounterShard.find_by_name(self.name)
        counter_count = counter_q.count()
        
        if not counter_count:
            # no counters exist, set value to single shard in transaction
            def counter_call(counter):
                counter.count = value
            self.__counter_tx(counter_call)
            return
            
        value_per_counter = float(value) / float(counter_count)
        
        values = []
        # TODO: is there a better way to detect floats than this?
        if (value_per_counter - int(value_per_counter)) > 0:
            # value is a float, I need to found all but last value down, and round last value up
            values.extend([math.floor(value_per_counter.floor) for i in xrange(counter_count)])
            values[-1] = math.ceil(value_per_counter)
        logging.debug("setting counter values across shards: %s" % values)

        # sets values for all counter shards
        for shard, count in zip(counter_q, values):
            shard.count = count
            shard.put()
Esempio n. 5
0
 def __shards(self):
     return CounterShard.find_by_name(self.name)
Esempio n. 6
0
 def __shards(self):
     return CounterShard.find_by_name(self.name)