new cachable objects + factories
[MogileFS-Server.git] / lib / MogileFS / Factory / Class.pm
bloba4ec368045676b01b76add0b2faf8ac053e61c31
1 package MogileFS::Factory::Class;
2 use strict;
3 use warnings;
4 use base 'MogileFS::Factory';
6 use MogileFS::NewClass;
8 # This class is a reimplementation since classids and classnames
9 # are not globally unique... uses the same interface.
10 # Stupid/wasteful.
11 sub set {
12 my ($self, $domain, $args) = @_;
13 my $domain_factory = MogileFS::Factory::Domain->get_factory;
14 # FIXME: Inject the dmid into the class somehow.
15 my $class = MogileFS::NewClass->new_from_args($args, $domain_factory);
16 $self->{by_id}->{$domain->id}->{$class->id} = $class;
17 $self->{by_name}->{$domain->id}->{$class->name} = $class;
18 return $class;
21 # Example of what we could use for testing.
22 # Test creates the object, injects its own factory, then hands it to us.
23 sub set_from_obj {
24 my ($self, $obj) = @_;
27 sub remove {
28 my $self = shift;
29 my $class = shift;
30 my $domid = $class->domain->id;
31 my $clsid = $class->id;
32 if (exists $self->{by_id}->{$domid}->{$clsid}) {
33 delete $self->{by_id}->{$domid}->{$clsid};
34 delete $self->{by_name}->{$domid}->{$class->name};
38 sub get_by_id {
39 my ($self, $domain, $id) = @_;
40 return $self->{by_id}->{$domain->id}->{$id};
43 sub get_by_name {
44 my ($self, $domain, $name) = @_;
45 return $self->{by_name}->{$domain->id}->{$name};
48 sub get_ids {
49 my ($self, $domain) = @_;
50 return keys %{$self->{by_id}->{$domain->id}};
53 sub get_names {
54 my ($self, $domain) = @_;
55 return keys %{$self->{by_name}->{$domain->id}};
58 sub get_all {
59 my ($self, $domain) = @_;
60 return values %{$self->{by_id}->{$domain->id}};
63 sub map_by_id {
64 my ($self, $domain) = @_;
65 my $set = $self->{by_id}->{$domain->id};
66 return { map { $_ => $set->{$_} } keys %{$set} };
69 sub map_by_name {
70 my ($self, $domain) = @_;
71 my $set = $self->{by_name}->{$domain->id};
72 return { map { $_ => $set->{$_} } keys %{$set} };