home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / perl5 / Debconf / DbDriver / Cache.pm < prev    next >
Encoding:
Perl POD Document  |  2006-07-24  |  4.7 KB  |  272 lines

  1. #!/usr/bin/perl -w
  2. # This file was preprocessed, do not edit!
  3.  
  4.  
  5. package Debconf::DbDriver::Cache;
  6. use strict;
  7. use Debconf::Log qw{:all};
  8. use base 'Debconf::DbDriver';
  9.  
  10.  
  11. use fields qw(cache dirty);
  12.  
  13.  
  14. sub iterator {
  15.     my $this=shift;
  16.     my $subiterator=shift;
  17.  
  18.     my @items=keys %{$this->{cache}};
  19.     my $iterator=Debconf::Iterator->new(callback => sub {
  20.         while (my $item = pop @items) {
  21.             next unless defined $this->{cache}->{$item};
  22.             return $item;
  23.         }
  24.         return unless $subiterator;
  25.         my $ret;
  26.         do {
  27.             $ret=$subiterator->iterate;
  28.         } while defined $ret and exists $this->{cache}->{$ret};
  29.         return $ret;
  30.     });
  31.     return $iterator;
  32. }
  33.  
  34.  
  35. sub exists {
  36.     my $this=shift;
  37.     my $item=shift;
  38.  
  39.     return $this->{cache}->{$item}
  40.         if exists $this->{cache}->{$item};
  41.     return 0;
  42. }
  43.  
  44.  
  45. sub init {
  46.     my $this=shift;
  47.  
  48.     $this->{cache} = {} unless exists $this->{cache};
  49. }
  50.  
  51.  
  52. sub cacheadd {
  53.     my $this=shift;
  54.     my $item=shift;
  55.     my $entry=shift;
  56.  
  57.     return if exists $this->{cache}->{$item};
  58.  
  59.     $this->{cache}->{$item}=$entry;
  60.     $this->{dirty}->{$item}=0;
  61. }
  62.  
  63.  
  64. sub cachedata {
  65.     my $this=shift;
  66.     my $item=shift;
  67.     
  68.     return $this->{cache}->{$item};
  69. }
  70.  
  71.  
  72. sub cached {
  73.     my $this=shift;
  74.     my $item=shift;
  75.  
  76.     unless (exists $this->{cache}->{$item}) {
  77.         debug "db $this->{name}" => "cache miss on $item";
  78.         $this->load($item);
  79.     }
  80.     return $this->{cache}->{$item};
  81. }
  82.  
  83.  
  84. sub shutdown {
  85.     my $this=shift;
  86.     
  87.     return if $this->{readonly};
  88.  
  89.     my $ret=1;
  90.     foreach my $item (keys %{$this->{cache}}) {
  91.         if (not defined $this->{cache}->{$item}) {
  92.             $ret=undef unless defined $this->remove($item);
  93.             delete $this->{cache}->{$item};
  94.         }
  95.         elsif ($this->{dirty}->{$item}) {
  96.             $ret=undef unless defined $this->save($item, $this->{cache}->{$item});
  97.             $this->{dirty}->{$item}=0;
  98.         }
  99.     }
  100.     return $ret;
  101. }
  102.  
  103.  
  104. sub addowner {
  105.     my $this=shift;
  106.     my $item=shift;
  107.     my $owner=shift;
  108.     my $type=shift;
  109.  
  110.     return if $this->{readonly};
  111.     $this->cached($item);
  112.  
  113.     if (! defined $this->{cache}->{$item}) {
  114.         return if ! $this->accept($item, $type);
  115.         debug "db $this->{name}" => "creating in-cache $item";
  116.         $this->{cache}->{$item}={
  117.             owners => {},
  118.             fields => {},
  119.             variables => {},
  120.             flags => {},
  121.         }
  122.     }
  123.  
  124.     if (! exists $this->{cache}->{$item}->{owners}->{$owner}) {
  125.         $this->{cache}->{$item}->{owners}->{$owner}=1;
  126.         $this->{dirty}->{$item}=1;
  127.     }
  128.     return $owner;
  129. }
  130.  
  131.  
  132. sub removeowner {
  133.     my $this=shift;
  134.     my $item=shift;
  135.     my $owner=shift;
  136.  
  137.     return if $this->{readonly};
  138.     return unless $this->cached($item);
  139.  
  140.     if (exists $this->{cache}->{$item}->{owners}->{$owner}) {
  141.         delete $this->{cache}->{$item}->{owners}->{$owner};
  142.         $this->{dirty}->{$item}=1;
  143.     }
  144.     unless (keys %{$this->{cache}->{$item}->{owners}}) {
  145.         $this->{cache}->{$item}=undef;
  146.         $this->{dirty}->{$item}=1;
  147.     }
  148.     return $owner;
  149. }
  150.  
  151.  
  152. sub owners {
  153.     my $this=shift;
  154.     my $item=shift;
  155.  
  156.     return unless $this->cached($item);
  157.     return keys %{$this->{cache}->{$item}->{owners}};
  158. }
  159.  
  160.  
  161. sub getfield {
  162.     my $this=shift;
  163.     my $item=shift;
  164.     my $field=shift;
  165.     
  166.     return unless $this->cached($item);
  167.     return $this->{cache}->{$item}->{fields}->{$field};
  168. }
  169.  
  170.  
  171. sub setfield {
  172.     my $this=shift;
  173.     my $item=shift;
  174.     my $field=shift;
  175.     my $value=shift;
  176.  
  177.     return if $this->{readonly};
  178.     return unless $this->cached($item);
  179.     $this->{dirty}->{$item}=1;
  180.     return $this->{cache}->{$item}->{fields}->{$field} = $value;    
  181. }
  182.  
  183.  
  184. sub removefield {
  185.     my $this=shift;
  186.     my $item=shift;
  187.     my $field=shift;
  188.  
  189.     return if $this->{readonly};
  190.     return unless $this->cached($item);
  191.     $this->{dirty}->{$item}=1;
  192.     return delete $this->{cache}->{$item}->{fields}->{$field};
  193. }
  194.  
  195.  
  196. sub fields {
  197.     my $this=shift;
  198.     my $item=shift;
  199.     
  200.     return unless $this->cached($item);
  201.     return keys %{$this->{cache}->{$item}->{fields}};
  202. }
  203.  
  204.  
  205. sub getflag {
  206.     my $this=shift;
  207.     my $item=shift;
  208.     my $flag=shift;
  209.     
  210.     return unless $this->cached($item);
  211.     return $this->{cache}->{$item}->{flags}->{$flag}
  212.         if exists $this->{cache}->{$item}->{flags}->{$flag};
  213.     return 'false';
  214. }
  215.  
  216.  
  217. sub setflag {
  218.     my $this=shift;
  219.     my $item=shift;
  220.     my $flag=shift;
  221.     my $value=shift;
  222.  
  223.     return if $this->{readonly};
  224.     return unless $this->cached($item);
  225.     $this->{dirty}->{$item}=1;
  226.     return $this->{cache}->{$item}->{flags}->{$flag} = $value;
  227. }
  228.  
  229.  
  230. sub flags {
  231.     my $this=shift;
  232.     my $item=shift;
  233.  
  234.     return unless $this->cached($item);
  235.     return keys %{$this->{cache}->{$item}->{flags}};
  236. }
  237.  
  238.  
  239. sub getvariable {
  240.     my $this=shift;
  241.     my $item=shift;
  242.     my $variable=shift;
  243.  
  244.     return unless $this->cached($item);
  245.     return $this->{cache}->{$item}->{variables}->{$variable};
  246. }
  247.  
  248.  
  249. sub setvariable {
  250.     my $this=shift;
  251.     my $item=shift;
  252.     my $variable=shift;
  253.     my $value=shift;
  254.  
  255.     return if $this->{readonly};
  256.     return unless $this->cached($item);
  257.     $this->{dirty}->{$item}=1;
  258.     return $this->{cache}->{$item}->{variables}->{$variable} = $value;
  259. }
  260.  
  261.  
  262. sub variables {
  263.     my $this=shift;
  264.     my $item=shift;
  265.  
  266.     return unless $this->cached($item);
  267.     return keys %{$this->{cache}->{$item}->{variables}};
  268. }
  269.  
  270.  
  271. 1
  272.