active_set 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) Rick Olson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ # Active Set
2
+
3
+ This tracks the number of active objects during a certain time period in
4
+ a Redis sorted set. Object activity is defined by your application, and
5
+ is out of the scope of this library.
6
+
7
+ ## INSTALL
8
+
9
+ gem install active_set
10
+
11
+ ## USAGE
12
+
13
+ When an object is active, it gets added to a Redis set with a timestamp.
14
+
15
+ set = ActiveSet.new(:objects)
16
+ set.add(1, Time.now) # Time.now is default if no time is given.
17
+
18
+ This is equivalent to the following Redis command:
19
+
20
+ ZADD active:objects 1305054408 "1"
21
+
22
+ We can count the number of items in the set:
23
+
24
+ set.count # => 1
25
+ # ZCARD active:objects
26
+
27
+ You can also count the active items since a given time:
28
+
29
+ set.count(1302462660)
30
+ # ZCOUNT active:objects 1302462660 +inf
31
+
32
+ The set should be trimmed periodically so that old objects aren't
33
+ counted.
34
+
35
+ set.trim(1302462660)
36
+ # ZREMRANGEBYSCORE active:objects -inf (1302462660
37
+
38
+ If no trim date is given, 30 days is assumed.
39
+
40
+ You can also check if an object is in the active set, and get it's last
41
+ timestamp.
42
+
43
+ set.include?(1)
44
+ set.timestamp_for(1) # => Time
45
+ # ZSCORE active:objects "1"
46
+
47
+ ## Contribute
48
+
49
+ If you'd like to hack on ActiveSet, start by forking the repo on GitHub:
50
+
51
+ `https://github.com/technoweenie/redis_active_set`
52
+
53
+ The best way to get your changes merged back into core is as follows:
54
+
55
+ * Clone down your fork
56
+ * Create a thoughtfully named topic branch to contain your change
57
+ * Hack away
58
+ * Add tests and make sure everything still passes by running rake
59
+ * If you are adding new functionality, document it in the README
60
+ * Do not change the version number, I will do that on my end
61
+ * If necessary, rebase your commits into logical chunks, without errors
62
+ * Push the branch up to GitHub
63
+ * Send a pull request to the `technoweenie/redis_active_set` project.
64
+
@@ -0,0 +1,135 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def version
16
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
17
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18
+ end
19
+
20
+ def date
21
+ Date.today.to_s
22
+ end
23
+
24
+ def rubyforge_project
25
+ name
26
+ end
27
+
28
+ def gemspec_file
29
+ "#{name}.gemspec"
30
+ end
31
+
32
+ def gem_file
33
+ "#{name}-#{version}.gem"
34
+ end
35
+
36
+ def replace_header(head, header_name)
37
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
38
+ end
39
+
40
+ #############################################################################
41
+ #
42
+ # Standard tasks
43
+ #
44
+ #############################################################################
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/testtask'
49
+ Rake::TestTask.new(:test) do |test|
50
+ test.libs << 'lib' << 'test'
51
+ test.pattern = 'test/**/*_test.rb'
52
+ test.verbose = true
53
+ end
54
+
55
+ desc "Open an irb session preloaded with this library"
56
+ task :console do
57
+ sh "irb -rubygems -r ./lib/#{name}.rb"
58
+ end
59
+
60
+ #############################################################################
61
+ #
62
+ # Custom tasks (add your own tasks here)
63
+ #
64
+ #############################################################################
65
+
66
+
67
+
68
+ #############################################################################
69
+ #
70
+ # Packaging tasks
71
+ #
72
+ #############################################################################
73
+
74
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
75
+ task :release => :build do
76
+ unless `git branch` =~ /^\* master$/
77
+ puts "You must be on the master branch to release!"
78
+ exit!
79
+ end
80
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
81
+ sh "git tag v#{version}"
82
+ sh "git push origin master"
83
+ sh "git push origin v#{version}"
84
+ sh "gem push pkg/#{name}-#{version}.gem"
85
+ end
86
+
87
+ desc "Build #{gem_file} into the pkg directory"
88
+ task :build => :gemspec do
89
+ sh "mkdir -p pkg"
90
+ sh "gem build #{gemspec_file}"
91
+ sh "mv #{gem_file} pkg"
92
+ end
93
+
94
+ desc "Generate #{gemspec_file}"
95
+ task :gemspec => :validate do
96
+ # read spec file and split out manifest section
97
+ spec = File.read(gemspec_file)
98
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
99
+
100
+ # replace name version and date
101
+ replace_header(head, :name)
102
+ replace_header(head, :version)
103
+ replace_header(head, :date)
104
+ #comment this out if your rubyforge_project has a different name
105
+ replace_header(head, :rubyforge_project)
106
+
107
+ # determine file list from git ls-files
108
+ files = `git ls-files`.
109
+ split("\n").
110
+ sort.
111
+ reject { |file| file =~ /^\./ }.
112
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
113
+ map { |file| " #{file}" }.
114
+ join("\n")
115
+
116
+ # piece file back together and write
117
+ manifest = " s.files = %w[\n#{files}\n ]\n"
118
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
119
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
120
+ puts "Updated #{gemspec_file}"
121
+ end
122
+
123
+ desc "Validate #{gemspec_file}"
124
+ task :validate do
125
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
126
+ unless libfiles.empty?
127
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
128
+ exit!
129
+ end
130
+ unless Dir['VERSION*'].empty?
131
+ puts "A `VERSION` file at root level violates Gem best practices."
132
+ exit!
133
+ end
134
+ end
135
+
@@ -0,0 +1,75 @@
1
+ ## This is the rakegem gemspec template. Make sure you read and understand
2
+ ## all of the comments. Some sections require modification, and others can
3
+ ## be deleted if you don't need them. Once you understand the contents of
4
+ ## this file, feel free to delete any comments that begin with two hash marks.
5
+ ## You can find comprehensive Gem::Specification documentation, at
6
+ ## http://docs.rubygems.org/read/chapter/20
7
+ Gem::Specification.new do |s|
8
+ s.specification_version = 2 if s.respond_to? :specification_version=
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubygems_version = '1.3.5'
11
+
12
+ ## Leave these as is they will be modified for you by the rake gemspec task.
13
+ ## If your rubyforge_project name is different, then edit it and comment out
14
+ ## the sub! line in the Rakefile
15
+ s.name = 'active_set'
16
+ s.version = '0.1.0'
17
+ s.date = '2011-05-10'
18
+ s.rubyforge_project = 'active_set'
19
+
20
+ ## Make sure your summary is short. The description may be as long
21
+ ## as you like.
22
+ s.summary = "Tracks the number of active objects during a certain time period in a Redis sorted set."
23
+ s.description = s.summary
24
+
25
+ ## List the primary authors. If there are a bunch of authors, it's probably
26
+ ## better to set the email to an email list or something. If you don't have
27
+ ## a custom homepage, consider using your GitHub URL or the like.
28
+ s.authors = ["Rick Olson"]
29
+ s.email = 'technoweenie@example.com'
30
+ s.homepage = 'http://github.com/technoweenie/active_set'
31
+
32
+ ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
33
+ ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
34
+ s.require_paths = %w[lib]
35
+
36
+ ## This sections is only necessary if you have C extensions.
37
+ #s.require_paths << 'ext'
38
+ #s.extensions = %w[ext/extconf.rb]
39
+
40
+ ## If your gem includes any executables, list them here.
41
+ #s.executables = ["name"]
42
+ #s.default_executable = 'name'
43
+
44
+ ## Specify any RDoc options here. You'll want to add your README and
45
+ ## LICENSE files to the extra_rdoc_files list.
46
+ s.rdoc_options = ["--charset=UTF-8"]
47
+ s.extra_rdoc_files = %w[README.md LICENSE]
48
+
49
+ ## List your runtime dependencies here. Runtime dependencies are those
50
+ ## that are needed for an end user to actually USE your code.
51
+ s.add_dependency('redis', ["< 3.0", ">= 2.0"])
52
+
53
+ ## List your development dependencies here. Development dependencies are
54
+ ## those that are only needed during development
55
+ #s.add_development_dependency('DEVDEPNAME', [">= 1.1.0", "< 2.0.0"])
56
+
57
+ ## Leave this section as-is. It will be automatically generated from the
58
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
59
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
60
+ # = MANIFEST =
61
+ s.files = %w[
62
+ LICENSE
63
+ README.md
64
+ Rakefile
65
+ active_set.gemspec
66
+ lib/active_set.rb
67
+ test/active_test.rb
68
+ ]
69
+ # = MANIFEST =
70
+
71
+ ## Test files will be grabbed from the file list. Make sure the path glob
72
+ ## matches what you actually use.
73
+ s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
74
+ end
75
+
@@ -0,0 +1,97 @@
1
+ require 'redis'
2
+
3
+ class ActiveSet
4
+ VERSION = "0.1.0"
5
+ SECONDS_PER_DAY = 86400
6
+
7
+ attr_reader :key
8
+
9
+ # Initializes a new Set of active objects.
10
+ #
11
+ # name - String name to identify this ActiveSet.
12
+ # options - Hash of options.
13
+ # :prefix - String prefix used to namespace the Redis key.
14
+ # :days - Default Fixnum of days of items to allow in the
15
+ # set. Default to 30.
16
+ # :sec - Fixnum of seconds of items to allow in the set.
17
+ # Defaults to using MATH to calculate seconds in
18
+ # 30 days.
19
+ # :redis - An existing Redis connection. If not set, the
20
+ # rest of this options Hash is used to initialize
21
+ # a new Redis connection.
22
+ def initialize(name, options = {})
23
+ @name = name
24
+ @prefix = options.delete(:prefix) || :active
25
+ @days = options.delete(:days) || 30
26
+ @sec = options.delete(:sec) || (SECONDS_PER_DAY * @days)
27
+ @redis = options.delete(:redis)
28
+ @key = "#{@prefix}:#{@name}"
29
+ @redis ||= Redis.new(options)
30
+ end
31
+
32
+ # Public: Adds a new object to the Set.
33
+ #
34
+ # entry - The String identifier of the object.
35
+ # time - Optional Time specifying when the object was last active.
36
+ #
37
+ # Returns nothing.
38
+ def add(entry, time = Time.now)
39
+ @redis.zadd(@key, time.to_i, entry)
40
+ end
41
+
42
+ # Public: Checks to see if the object is in the Set.
43
+ #
44
+ # entry - The String identifier of the object.
45
+ #
46
+ # Returns true if the object is in the set, or false.
47
+ def include?(entry)
48
+ !@redis.zscore(@key, entry).nil?
49
+ end
50
+
51
+ # Public: Gets the timestamp for when the given object was active.
52
+ #
53
+ # entry - The String identifier of the object.
54
+ #
55
+ # Returns the Time the object was last active, or nil.
56
+ def timestamp_for(entry)
57
+ sec = @redis.zscore(@key, entry)
58
+ sec ? Time.at(sec.to_i) : nil
59
+ end
60
+
61
+ # Public: Counts the number of objects in the set.
62
+ #
63
+ # since - An optional Time to specify the cutoff time to
64
+ # count. If provided, any object updated since the timestamp
65
+ # is counted.
66
+ #
67
+ # Returns a Fixnum.
68
+ def count(since = nil)
69
+ (since ?
70
+ @redis.zcount(@key, since.to_i, "+inf") :
71
+ @redis.zcard(@key)).to_i
72
+ end
73
+
74
+ # Public: Trims the Set.
75
+ #
76
+ # time - Optional Time specifying the earliest cutoff point. Any
77
+ # object with a later timestamp is purged.
78
+ #
79
+ # Returns nothing.
80
+ def trim(time = earliest_time)
81
+ @redis.zremrangebyscore(@key, "-inf", "(#{time.to_i}")
82
+ end
83
+
84
+ # Public: Clears the Set.
85
+ #
86
+ # Returns nothing.
87
+ def clear
88
+ @redis.del(@key)
89
+ end
90
+
91
+ # Calculates the earliest time used as a cutoff point for #trim.
92
+ #
93
+ # Returns Fixnum seconds.
94
+ def earliest_time
95
+ Time.now.to_i - @sec
96
+ end
97
+ end
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require File.expand_path("../../lib/active_set", __FILE__)
4
+
5
+ class ActiveSetTest < Test::Unit::TestCase
6
+ def setup
7
+ @set = ActiveSet.new :lols, :prefix => 'active_test'
8
+ @set.clear
9
+ @key = @set.key
10
+ end
11
+
12
+ def test_adds_item
13
+ assert_equal 0, @set.count
14
+ assert !@set.include?('abc')
15
+ @set.add 'abc'
16
+ assert_equal 1, @set.count
17
+ assert @set.include?('abc')
18
+ end
19
+
20
+ def test_adds_item_with_custom_time
21
+ time = Time.local(2001)
22
+ @set.add 'abc', time
23
+ assert_equal time, @set.timestamp_for('abc')
24
+ end
25
+
26
+ def test_counts_items
27
+ assert_equal 0, @set.count
28
+ @set.add 'abc'
29
+ assert_equal 1, @set.count
30
+ @set.add 'def', Time.local(2001)
31
+ assert_equal 2, @set.count
32
+ @set.add 'abc'
33
+ assert_equal 2, @set.count
34
+ assert_equal 1, @set.count(Time.local(2002))
35
+ end
36
+
37
+ def test_trims_set
38
+ @set.add 'abc'
39
+ @set.add 'def', Time.local(2001)
40
+ assert_equal 2, @set.count
41
+ @set.trim
42
+ assert_equal 1, @set.count
43
+ assert @set.include?('abc')
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_set
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Rick Olson
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-10 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: redis
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - <
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ version: "3.0"
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ hash: 3
37
+ segments:
38
+ - 2
39
+ - 0
40
+ version: "2.0"
41
+ type: :runtime
42
+ version_requirements: *id001
43
+ description: Tracks the number of active objects during a certain time period in a Redis sorted set.
44
+ email: technoweenie@example.com
45
+ executables: []
46
+
47
+ extensions: []
48
+
49
+ extra_rdoc_files:
50
+ - README.md
51
+ - LICENSE
52
+ files:
53
+ - LICENSE
54
+ - README.md
55
+ - Rakefile
56
+ - active_set.gemspec
57
+ - lib/active_set.rb
58
+ - test/active_test.rb
59
+ has_rdoc: true
60
+ homepage: http://github.com/technoweenie/active_set
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --charset=UTF-8
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ requirements: []
87
+
88
+ rubyforge_project: active_set
89
+ rubygems_version: 1.3.7
90
+ signing_key:
91
+ specification_version: 2
92
+ summary: Tracks the number of active objects during a certain time period in a Redis sorted set.
93
+ test_files: []
94
+