Class: ActiveShard::ShardCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_shard/shard_collection.rb

Overview

Represents a group of shards. Most commonly used to group shards belonging to the same schema.

Handles some basic caching of common use cases to (hopefully) increase performance in most situations.

Instance Method Summary collapse

Constructor Details

#initialize(shard_definitions = []) ⇒ ShardCollection

Initializes a shard group

Parameters:



20
21
22
# File 'lib/active_shard/shard_collection.rb', line 20

def initialize( shard_definitions=[] )
  add_shards( shard_definitions )
end

Instance Method Details

#add_shard(*args) ⇒ ShardDefinition

Adds a shard definition to the collection

Returns:

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/active_shard/shard_collection.rb', line 54

def add_shard( *args )
  shard_def = ( args.first.is_a?(ShardDefinition) ? args.first : ShardDefinition.new( *args ) )

  duplicate_exists = shard_name_exists?( shard_def.name )

  raise NameNotUniqueError,
        "Shard named '#{shard_def.name.to_s}' exists for schema '#{shard_def.schema.to_s}'" if duplicate_exists

  definitions << shard_def
  definitions_by_schema( shard_def.schema ) << shard_def
  definitions_by_name[ shard_def.name.to_sym ] = shard_def

  shard_def
end

#add_shards(shard_definitions) ⇒ Array

Adds a list of shard definitions to this collection.

Returns:

  • (Array)

    a list of shards that were added



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/active_shard/shard_collection.rb', line 36

def add_shards( shard_definitions )
  added_shards = []

  shard_definitions.each do |definition|
    begin
      added_shards << add_shard( definition )
    rescue NameNotUniqueError
      # bury
    end
  end

  added_shards
end

#any_shardSymbol

Returns a random shard name from the group

Returns:

  • (Symbol)

    a shard name



123
124
125
# File 'lib/active_shard/shard_collection.rb', line 123

def any_shard
  definitions[ rand( definitions.size ) ].name
end

#by_schema(schema_name) ⇒ ShardCollection

Returns a ShardCollection with definitions from this collection that match the provided schema name

Parameters:

  • schema_name (Symbol)

    schema name

Returns:



84
85
86
# File 'lib/active_shard/shard_collection.rb', line 84

def by_schema( schema_name )
  self.class.new( definitions_by_schema( schema_name ) )
end

#each(&block) ⇒ Object



127
128
129
# File 'lib/active_shard/shard_collection.rb', line 127

def each( &block )
  definitions.each( &block )
end

#lengthObject



131
132
133
# File 'lib/active_shard/shard_collection.rb', line 131

def length
  definitions.length
end

#remove_shard(shard_name) ⇒ ShardDefinition?

Removes a shard definition from the collection based on the provided shard name.

Parameters:

  • shard_name (Symbol)

Returns:

  • (ShardDefinition, nil)

    the ShardDefinition removed, or nil if none was found



100
101
102
103
104
105
106
107
108
# File 'lib/active_shard/shard_collection.rb', line 100

def remove_shard( shard_name )
  shard_def = definitions_by_name.delete( shard_name.to_sym )
  return nil if shard_def.nil?

  definitions.delete( shard_def )
  definitions_by_schema( shard_def.schema ).delete( shard_def )

  shard_def
end

#schemasObject



88
89
90
# File 'lib/active_shard/shard_collection.rb', line 88

def schemas
  definitions.map { |d| d.schema }.uniq.compact
end

#shard(name) ⇒ ShardDefinition

Returns a Shard Definition by the shard name

Returns:



28
29
30
# File 'lib/active_shard/shard_collection.rb', line 28

def shard( name )
  definitions_by_name[ name.to_sym ]
end

#shard_definitionsArray<ShardDefinition>

All shard definitions in collection

Returns:



73
74
75
# File 'lib/active_shard/shard_collection.rb', line 73

def shard_definitions
  definitions.dup
end

#shard_name_exists?(shard_name) ⇒ Boolean

Returns whether or not a Shard exists in this collection with the provided schema and shard names.

Returns:

  • (Boolean)


115
116
117
# File 'lib/active_shard/shard_collection.rb', line 115

def shard_name_exists?( shard_name )
  !( definitions_by_name[ shard_name.to_sym ].nil? )
end

#sizeObject



135
136
137
# File 'lib/active_shard/shard_collection.rb', line 135

def size
  length
end