| 1 | package Judy; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Judy::_obj -impl; | ||
| 7 | |||
| 8 | use constant TYPE => 1; | ||
| 9 | use constant ARGS => 2; | ||
| 10 | |||
| 11 | sub TIEHASH { | ||
| 12 | # Allow spaces and such in the spec but remove them prior to use. | ||
| 13 | # Normal specs look like: 'Judy::1' or 'Judy::L -> Judy::1' | ||
| 14 | # | ||
| 15 | my $fulltype = $_[TYPE]; | ||
| 16 | $fulltype =~ s/[(\s)]+//g; | ||
| 17 | |||
| 18 | # Detect whether this is a simple object or not. | ||
| 19 | # | ||
| 20 | if ( -1 == index $fulltype, '->' ) { | ||
| 21 | |||
| 22 | # Allow '1' instead of 'Judy::1' | ||
| 23 | # | ||
| 24 | if ( -1 == index $fulltype, ':' ) { | ||
| 25 | $fulltype = "Judy::$fulltype"; | ||
| 26 | } | ||
| 27 | |||
| 28 | # Return a non-Judy object. No need to get extra re-dispatching | ||
| 29 | # | ||
| 30 | if ( $Devel::Trace::TRACE ) { | ||
| 31 | printf "%s:%d %s->TIEHASH( $_[TYPE], $_[ARGS] )\n", __FILE__, __LINE__, $_[0], $_[TYPE], $_[ARGS]; | ||
| 32 | } | ||
| 33 | return $fulltype->TIEHASH( $_[ARGS] ? $_[ARGS] : () ); | ||
| 34 | } | ||
| 35 | else { | ||
| 36 | # Tie a simple, non-Judy array internally. | ||
| 37 | # | ||
| 38 | my ( $type ) = $fulltype =~ /^([\w:']+)/ | ||
| 39 | or die; | ||
| 40 | |||
| 41 | # Allow '1' instead of 'Judy::1' | ||
| 42 | # | ||
| 43 | if ( -1 == index $type, ':' ) { | ||
| 44 | $type = "Judy::$type"; | ||
| 45 | } | ||
| 46 | |||
| 47 | my @self; | ||
| 48 | $self[_innerobj] = $type->TIEHASH( $_[ARGS] ? $_[ARGS] : () ); | ||
| 49 | |||
| 50 | # Store the complete type: Judy::SL->Judy::SL->... | ||
| 51 | # | ||
| 52 | $self[_fulltype] = $fulltype; | ||
| 53 | $self[_type] = $type; | ||
| 54 | |||
| 55 | return bless \ @self, 'Judy'; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | use constant SELF => 0; | ||
| 60 | use constant KEY => 1; | ||
| 61 | use constant VALUE => 2; | ||
| 62 | |||
| 63 | sub FETCH { | ||
| 64 | for ( $_[SELF][_innerobj] ) { | ||
| 65 | |||
| 66 | # Fetch the next hash. | ||
| 67 | # | ||
| 68 | my ( $pval, $val) = $_->get( $_[KEY] ); | ||
| 69 | |||
| 70 | # Autovivify the current hash. This is more eager than normal perl. | ||
| 71 | # | ||
| 72 | if ( ! $pval ) { | ||
| 73 | my $optr = $_->ptr; | ||
| 74 | $pval = $_->set( $_[KEY], NULL ); | ||
| 75 | |||
| 76 | my $ptr = $_->ptr; | ||
| 77 | |||
| 78 | if ( $optr != $ptr ) { | ||
| 79 | $_->setptr( $ptr ); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | my $innertype = $_[SELF][_fulltype]; | ||
| 84 | $innertype =~ s/^[\w:']+->//; | ||
| 85 | |||
| 86 | # printf "%s:%d innertype=%s\n", __FILE__, __LINE__, $innertype; | ||
| 87 | |||
| 88 | # Return the desired thing. Let Judy decide whether it wants to delegate or not. | ||
| 89 | # | ||
| 90 | tie my(%h), Judy => $innertype, { ptrpath => [ $_[SELF], $_[KEY]] }; | ||
| 91 | return \ %h; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | sub STORE { | ||
| 96 | for ( $_[SELF][_innerobj] ) { | ||
| 97 | |||
| 98 | # Fetch the current hash to detect orphans. | ||
| 99 | # | ||
| 100 | my ( $pval, $val ) = $_->get( $_[KEY] ); | ||
| 101 | if ( $val ) { | ||
| 102 | warn( sprintf "Orphaning %s(0x%x)", $_[SELF][_type], $val ); | ||
| 103 | } | ||
| 104 | |||
| 105 | # Store the new pointer. | ||
| 106 | # | ||
| 107 | my $storeobj = tied %{ $_[VALUE] }; | ||
| 108 | my $storeptr = $storeobj->ptr; | ||
| 109 | if ( $pval ) { | ||
| 110 | Judy::Mem::Poke( $pval, $storeptr ); | ||
| 111 | } | ||
| 112 | else { | ||
| 113 | my $optr = $_->ptr; | ||
| 114 | $pval = $_->set( $_[KEY], $storeptr ); | ||
| 115 | my $ptr = $_->ptr; | ||
| 116 | if ( $ptr != $optr ) { | ||
| 117 | $_->setptr($ptr); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | # Return the stored thingy. | ||
| 122 | # | ||
| 123 | if ( defined wantarray ) { | ||
| 124 | tie my(%h), Judy => $_[SELF][_type], { ptrpath => [ $_[SELF], $_[KEY]] }; | ||
| 125 | return \%h; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | sub EXISTS { | ||
| 131 | my ( $pval ) = $_[SELF][_innerobj]->get( $_[KEY] ); | ||
| 132 | return defined $pval; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub FIRSTKEY { | ||
| 136 | my ( undef, undef, $key ) = $_[SELF][_innerobj]->first; | ||
| 137 | return $key; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub NEXTKEY { | ||
| 141 | my ( undef, undef, $key ) = $_[SELF][_innerobj]->next( $_[KEY] ); | ||
| 142 | return $key; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub DELETE { | ||
| 146 | my ( undef, $val ) = $_[SELF][_innerobj]->delete( $_[KEY] ); | ||
| 147 | return defined $val ? $val : (); | ||
| 148 | } | ||
| 149 | |||
| 150 | sub CLEAR { | ||
| 151 | for ( $_[SELF][_innerobj] ) { | ||
| 152 | $_->free; | ||
| 153 | my $ptr = $_->ptr; | ||
| 154 | $_->setptr( $ptr ); | ||
| 155 | |||
| 156 | return; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | sub SCALAR { | ||
| 161 | Carp::confess('not implemented'); | ||
| 162 | } | ||
| 163 | |||
| 164 | sub UNTIE { | ||
| 165 | } | ||
| 166 | |||
| 167 | sub DESTROY { | ||
| 168 | } | ||
| 169 | |||
| 170 | 1; |