| 1 | # | ||
| 2 | # The contents of this file are subject to the Mozilla Public | ||
| 3 | # License Version 1.1 (the "License"); you may not use this file | ||
| 4 | # except in compliance with the License. You may obtain a copy of | ||
| 5 | # the License at http://www.mozilla.org/MPL/ | ||
| 6 | # | ||
| 7 | # Software distributed under the License is distributed on an "AS | ||
| 8 | # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | ||
| 9 | # implied. See the License for the specific language governing | ||
| 10 | # rights and limitations under the License. | ||
| 11 | # | ||
| 12 | # The Original Code is the RDF::Core module | ||
| 13 | # | ||
| 14 | # The Initial Developer of the Original Code is Ginger Alliance Ltd. | ||
| 15 | # Portions created by Ginger Alliance are | ||
| 16 | # Copyright (C) 2001 Ginger Alliance Ltd. | ||
| 17 | # All Rights Reserved. | ||
| 18 | # | ||
| 19 | # Contributor(s): | ||
| 20 | # | ||
| 21 | # Alternatively, the contents of this file may be used under the | ||
| 22 | # terms of the GNU General Public License Version 2 or later (the | ||
| 23 | # "GPL"), in which case the provisions of the GPL are applicable | ||
| 24 | # instead of those above. If you wish to allow use of your | ||
| 25 | # version of this file only under the terms of the GPL and not to | ||
| 26 | # allow others to use your version of this file under the MPL, | ||
| 27 | # indicate your decision by deleting the provisions above and | ||
| 28 | # replace them with the notice and other provisions required by | ||
| 29 | # the GPL. If you do not delete the provisions above, a recipient | ||
| 30 | # may use your version of this file under either the MPL or the | ||
| 31 | # GPL. | ||
| 32 | # | ||
| 33 | |||
| 34 | package RDF::Core::Enumerator::DB_File; | ||
| 35 | |||
| 36 | use strict; | ||
| 37 | require Exporter; | ||
| 38 | |||
| 39 | our @ISA = qw(RDF::Core::Enumerator); | ||
| 40 | use Carp; | ||
| 41 | |||
| 42 | #use constants from RDF::Core::Storage::DB_File | ||
| 43 | use constant NAMESPACE => RDF::Core::Storage::DB_File::NAMESPACE; | ||
| 44 | use constant VALUE => RDF::Core::Storage::DB_File::VALUE; | ||
| 45 | use constant LITERAL => RDF::Core::Storage::DB_File::LITERAL; | ||
| 46 | use constant LIT_LANG => RDF::Core::Storage::DB_File::LIT_LANG; | ||
| 47 | use constant LIT_TYPE => RDF::Core::Storage::DB_File::LIT_TYPE; | ||
| 48 | use constant SUBJECT => RDF::Core::Storage::DB_File::SUBJECT; | ||
| 49 | use constant PREDICATE => RDF::Core::Storage::DB_File::PREDICATE; | ||
| 50 | use constant OBJECT_RES => RDF::Core::Storage::DB_File::OBJECT_RES; | ||
| 51 | use constant OBJECT_LIT => RDF::Core::Storage::DB_File::OBJECT_LIT; | ||
| 52 | |||
| 53 | |||
| 54 | sub new { | ||
| 55 | #Gets a hash tied to a data file (see RDF::Core::Storage::DB_File->{_data}) | ||
| 56 | # and an array of statements' indexes in the hash | ||
| 57 | # or undef for all statements | ||
| 58 | my ($pkg,$data,$stmtArray)=@_; | ||
| 59 | $pkg = ref $pkg || $pkg; | ||
| 60 | my $self = {}; | ||
| 61 | $self->{_data} = $data; | ||
| 62 | $self->{_stmtArray} = $stmtArray; | ||
| 63 | $self->{_count} = 0; | ||
| 64 | bless $self,$pkg; | ||
| 65 | } | ||
| 66 | sub getFirst { | ||
| 67 | my $self = shift; | ||
| 68 | #reset counter | ||
| 69 | $self->{_count} = 0; | ||
| 70 | |||
| 71 | return $self->getNext; | ||
| 72 | } | ||
| 73 | sub getNext { | ||
| 74 | my $self = shift; | ||
| 75 | my $subPrefix = SUBJECT; | ||
| 76 | my ($key, $value); | ||
| 77 | if (defined $self->{_stmtArray}) { | ||
| 78 | $key = $self->{_stmtArray}->[$self->{_count}++] | ||
| 79 | } else { | ||
| 80 | #brute force iteration through all possible statements | ||
| 81 | #TODO: find some better way of iteration | ||
| 82 | do { | ||
| 83 | } until (defined $self->{_data}->{+SUBJECT.$self->{_count}++} || | ||
| 84 | $self->{_count} > $self->{_data}->{_statements}); | ||
| 85 | #pretty dirty - {_data}->{_statements} is a private member of RDF::Core::Storage::DB_File | ||
| 86 | #TODO: FIND SOME BETTER WAY OF ITERATION | ||
| 87 | if ($self->{_count} > $self->{_data}->{_statements}) { | ||
| 88 | undef $key; | ||
| 89 | } else { | ||
| 90 | $key = $self->{_count} | ||
| 91 | } | ||
| 92 | } | ||
| 93 | return undef #end of data | ||
| 94 | unless defined $key; | ||
| 95 | #create and return statement | ||
| 96 | my ($subNS,$subLV,$predNS,$predLV, $objNS, $objLV, $objValue, | ||
| 97 | $litLang, $litDatatype, $index); | ||
| 98 | my $isLiteral; | ||
| 99 | $index = $self->{_data}->{+SUBJECT.$key}; | ||
| 100 | $subNS = $self->{_data}->{+NAMESPACE.$index}; | ||
| 101 | $subLV = $self->{_data}->{+VALUE.$index}; | ||
| 102 | $index = $self->{_data}->{+PREDICATE.$key}; | ||
| 103 | $predNS = $self->{_data}->{+NAMESPACE.$index}; | ||
| 104 | $predLV = $self->{_data}->{+VALUE.$index}; | ||
| 105 | if ($isLiteral = exists($self->{_data}->{+OBJECT_LIT.$key})) { | ||
| 106 | $index = $self->{_data}->{+OBJECT_LIT.$key}; | ||
| 107 | $objValue = $self->{_data}->{+LITERAL.$index}; | ||
| 108 | $litDatatype = $self->{_data}->{+LIT_TYPE.$index}; | ||
| 109 | $litLang = $self->{_data}->{+LIT_LANG.$index}; | ||
| 110 | } else { | ||
| 111 | $index = $self->{_data}->{+OBJECT_RES.$key}; | ||
| 112 | $objNS = $self->{_data}->{+NAMESPACE.$index}; | ||
| 113 | $objLV = $self->{_data}->{+VALUE.$index}; | ||
| 114 | $objValue = $objNS.$objLV; | ||
| 115 | } | ||
| 116 | my $newsub = new RDF::Core::Resource($subNS,$subLV); | ||
| 117 | my $newpred = new RDF::Core::Resource($predNS.$predLV); | ||
| 118 | my $newobj; | ||
| 119 | if ($isLiteral) { | ||
| 120 | $newobj = new RDF::Core::Literal($objValue, $litLang, | ||
| 121 | $litDatatype); | ||
| 122 | |||
| 123 | } else { | ||
| 124 | $newobj = new RDF::Core::Resource($objNS,$objLV) | ||
| 125 | } | ||
| 126 | my $statement = new RDF::Core::Statement($newsub,$newpred,$newobj); | ||
| 127 | return $statement; | ||
| 128 | } | ||
| 129 | sub close { | ||
| 130 | $_[0]->{_stmtArray} = undef; | ||
| 131 | } | ||
| 132 | |||
| 133 | 1; | ||
| 134 | |||
| 135 | __END__ |