blib/lib/UR/DataSource/File.pm untested

filter: Statementstmt 0.0% 0 / 703 Branchbran 0.0% 0 / 362 Conditioncond 0.0% 0 / 103 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 1222 SCAR 104.1
CC
182
Cov
0%
_comparator_for_operator_and_property
5852.0
_sync_database
1640.0
create_iterator_closure_for_rule
930.0
CRAP 33306.0
1 package UR::DataSource::File;
2  
3 # NOTE! This module is deprecated. Use UR::DataSource::Filesystem instead.
4  
5 # A data source implementation for text files where the fields
6 # are delimited by commas (or anything else really). Usually,
7 # the lines in the file will be sorted by one or more columns,
8 # but it isn't strictly necessary
9 #
10 # For now, it's structured around files where the record is delimited by
11 # newlines, and the fields are delimited by qr(\s*,\s*). Those are
12 # overridable in concrete data sources by specifying record_seperator() and
13 # delimiter().
14 # FIXME - work out a way to support record-oriented data as well as line-oriented data
15  
16 use UR;
17 use strict;
18 use warnings;
19 our $VERSION = "0.46"; # UR $VERSION;
20  
21 use Fcntl qw(:DEFAULT :flock);
22 use Errno qw(EINTR EAGAIN EOPNOTSUPP);
23 use File::Temp;
24 use File::Basename;
25 use IO::File qw();
26  
27 our @CARP_NOT = qw( UR::Context UR::DataSource::FileMux UR::Object::Type );
28  
29 class UR::DataSource::File {
30 is => ['UR::DataSource'],
31 has => [
32 delimiter => { is => 'String', default_value => '\s*,\s*', doc => 'Delimiter between columns on the same line' },
33 record_separator => { is => 'String', default_value => "\n", doc => 'Delimiter between lines in the file' },
34 column_order => { is => 'ARRAY', doc => 'Names of the columns in the file, in order' },
35 skip_first_line => { is => 'Integer', default_value => 0, doc => 'Number of lines at the start of the file to skip' },
36 handle_class => { is => 'String', default_value => 'IO::File', doc => 'Class to use for new file handles' },
37 quick_disconnect => { is => 'Boolean', default_value => 1, doc => 'Do not hold the file handle open between requests' },
38 ],
39 has_optional => [
40 server => { is => 'String', doc => 'pathname to the data file' },
41 file_list => { is => 'ARRAY', doc => 'list of pathnames of equivalent files' },
42 sort_order => { is => 'ARRAY', doc => 'Names of the columns by which the data file is sorted' },
43 constant_values => { is => 'ARRAY', doc => 'Property names which are not in the data file(s), but are part of the objects loaded from the data source' },
44  
45
46 # REMOVE
47 #file_cache_index => { is => 'Integer', doc => 'index into the file cache where the next read will be placed' },
48 _open_query_count => { is => 'Integer', doc => 'number of queries currently using this data source, used internally' },
49
50 ],
51 doc => 'A data source for line-oriented files',
52 };
53  
54  
55 sub can_savepoint { 0;} # Doesn't support savepoints
56
57 sub create_default_handle {
58 my $self = shift;
59  
60 if ($ENV{'UR_DBI_MONITOR_SQL'}) {
61 my $time = time();
62 UR::DBI->sql_fh->printf("\nFILE OPEN AT %d [%s]\n",$time, scalar(localtime($time)));
63 }
64  
65 my $filename = $self->server;
66 unless (-e $filename) {
67 # file doesn't exist
68 $filename = '/dev/null';
69 }
70  
71 my $handle_class = $self->handle_class;
72 my $fh = $handle_class->new($filename);
73 unless($fh) {
74 $self->error_message("Can't open ".$self->server." for reading: $!");
75 return;
76 }
77  
78 if ($ENV{'UR_DBI_MONITOR_SQL'}) {
79 UR::DBI->sql_fh->printf("FILE: opened %s fileno %d\n\n",$self->server, $fh->fileno);
80 }
81  
82 $self->is_connected(1);
83 return $fh;
84 }
85  
86 sub disconnect {
87 my $self = shift;
88  
89 if ($self->has_default_handle) {
90 my $fh = $self->get_default_handle;
91 flock($fh,LOCK_UN);
92 $fh->close();
93 $self->__invalidate_get_default_handle__;
94 $self->is_connected(0);
95 }
96 }
97  
98 sub _file_position {
99 my $self = shift;
100 my $fh = $self->get_default_handle;
101 return $fh ? $fh->tell() : undef;
102 }
103  
104 sub prepare_for_fork {
105 my $self = shift;
106  
107 # make sure this is clear before we fork
108 $self->{'_fh_position'} = undef;
109 if ($self->has_default_handle) {
110 $self->{'_fh_position'} = $self->_file_position();
111 UR::DBI->sql_fh->printf("FILE: preparing to fork; closing file %s and noting position at %s\n",$self->server, $self->{'_fh_position'}) if $ENV{'UR_DBI_MONITOR_SQL'};
112 }
113 $self->disconnect_default_handle;
114 }
115  
116 sub finish_up_after_fork {
117 my $self = shift;
118 if (defined $self->{'_fh_position'}) {
119 UR::DBI->sql_fh->printf("FILE: resetting after fork; reopening file %s and fast-forwarding to %s\n",$self->server, $self->{'_fh_position'}) if $ENV{'UR_DBI_MONITOR_SQL'};
120 my $fh = $self->get_default_handle;
121 $fh->seek($self->{'_fh_position'},0);
122 }
123 }
124  
125 sub _regex {
126 my $self = shift;
127  
128 unless ($self->{'_regex'}) {
129 my $delimiter = $self->delimiter;
130 my $r = eval { qr($delimiter) };
131 if ($@ || !$r) {
132 $self->error_message("Unable to interepret delimiter '".$self->delimiter.": $@");
133 return;
134 }
135 $self->{'_regex'} = $r;
136 }
137 return $self->{'_regex'};
138 }
139  
140 # We're overriding server() so everyone else can have a single way of getting
141 # the file's pathname instead of having to know about both server and file_list
142 sub server {
143 my $self = shift;
144  
145 unless ($self->{'_cached_server'}) {
146 if ($self->__server()) {
147 $self->{'_cached_server'} = $self->__server();
148 } elsif ($self->file_list) {
149 my $files = $self->file_list;
150 my $count = scalar(@$files);
151 my $idx = $$ % $count;
152 $self->{'_cached_server'} = $files->[$idx];
153 } else {
154 die "Data source ",$self->id," didn't specify either server or file_list";
155 }
156 }
157 return $self->{'_cached_server'};
158 }
159  
160  
161 # Should be divisible by 3
162 our $MAX_CACHE_SIZE = 99;
163 # The offset cache is an arrayref containing three pieces of data:
164 # 0: If this cache slot is being used by a loading iterator
165 # 1: concatenated data from the sorted columns for comparison with where you are in the file
166 # 2: the seek position that line came from
167 sub _offset_cache {
168 my $self = shift;
169  
170 unless ($self->{'_offset_cache'}) {
171 $self->{'_offset_cache'} = [];
172 }
173 return $self->{'_offset_cache'};
174 }
175  
176 our %iterator_data_source;
177 our %iterator_cache_slot_refs;
178  
179 sub _allocate_offset_cache_slot {
180 my $self = shift;
181  
182 my $cache = $self->_offset_cache();
183 my $next = scalar(@$cache);
184 #print STDERR "_allocate_offset_cache_slot ".$self->server." current size is $next ";
185 if ($next > $MAX_CACHE_SIZE) {
186 #print STDERR "searching... \n";
187 my $last_offset_cache_slot = $self->{'_last_offset_cache_slot'};
188 if ($last_offset_cache_slot >= $MAX_CACHE_SIZE) {
189 $next = 0;
190 } else {
191 $next = $last_offset_cache_slot + 3;
192 }
193 # Search for an unused slot
194 while ($cache->[$next] and $next != $last_offset_cache_slot) {
195 $next += 3;
196 $next = 0 if ($next > $MAX_CACHE_SIZE);
197 }
198 if ($next > $MAX_CACHE_SIZE or $next eq $last_offset_cache_slot) {
199 #print STDERR scalar(keys(%iterator_data_source))." items in iterator_data_source ".scalar(keys(%iterator_cache_slot))." in iterator_cache_slot\n";
200 Carp::carp("Unable to find an open file offset cache slot because there are too many outstanding loading iterators. Temporarily expanding the cache...");
201 # We'll let it go ahead and expand the list
202 $next = $MAX_CACHE_SIZE;
203 $MAX_CACHE_SIZE += 3;
204 }
205 }
206 $cache->[$next] = 1;
207 $cache->[$next+1] = undef;
208 $cache->[$next+2] = undef;
209  
210 $self->{'_last_offset_cache_slot'} = $next;
211 #print STDERR "using slot $next current size ".scalar(@$cache)."\n";
212 return $next;
213 }
214  
215  
216 sub _free_offset_cache_slot {
217 my($self, $cache_slot) = @_;
218  
219 my $cache = $self->_offset_cache();
220 unless ($cache_slot < scalar(@$cache)) {
221 $self->warning_message("Freeing offset cache slot past the end. Current size ".scalar(@$cache).", requested $cache_slot");
222 return;
223 }
224  
225 unless (defined $cache->[$cache_slot]) {
226 $self->warning_message("Freeing unused offset cache slot $cache_slot");
227 return;
228 }
229  
230 if ($cache->[$cache_slot+1] and scalar(@{$cache->[$cache_slot+1]}) == 0) {
231 # There's no data in here. Must have happened when the reader went all the
232 # way to the end of the file and found nothing. Remove this entry completely
233 # because it's not helpful
234 splice(@$cache, $cache_slot,3);
235  
236 } else {
237 # There is data in here, mark it as a free slot
238 $cache->[$cache_slot] = 0;
239 }
240 return 1;
241 }
242
243  
244  
245 sub _invalidate_cache {
246 my $self = shift;
247  
248 $self->{'_offset_cache'} = [];
249
250 return 1;
251 }
252  
253 sub _generate_loading_templates_arrayref {
254 my($self,$old_sql_cols) = @_;
255  
256 my $columns_in_file = $self->column_order;
257 my %column_to_position_map;
258 for (my $i = 0; $i < @$columns_in_file; $i++) {
259 $column_to_position_map{$columns_in_file->[$i]} = $i;
260 }
261  
262 # strip out columns that don't exist in the file
263 my $sql_cols;
264 foreach my $column_data ( @$old_sql_cols ) {
265 my $propertys_column_name = $column_data->[1]->column_name;
266 next unless ($propertys_column_name and exists($column_to_position_map{$propertys_column_name}));
267  
268 push @$sql_cols, $column_data;
269 }
270  
271 unless ($sql_cols) {
272 $self->error_message("Couldn't determine column information for data source " . $self->id);
273 return;
274 }
275  
276 # reorder the requested columns to be in the same order as the file
277 my @sql_cols_with_column_name =
278 map{ [ $column_to_position_map{ $_->[1]->column_name }, $_ ] }
279 @$sql_cols;
280 my @sorted_sql_cols =
281 map { $_->[1] }
282 sort { $a->[0] <=> $b->[0] }
283 @sql_cols_with_column_name;
284 $sql_cols = \@sorted_sql_cols;
285 my $templates = $self->SUPER::_generate_loading_templates_arrayref($sql_cols);
286  
287 if (my $constant_values = $self->constant_values) {
288 # Find the first unused index in the loading template
289 my $next_template_slot = -1;
290 foreach my $tmpl ( @$templates ) {
291 foreach my $col ( @{$tmpl->{'column_positions'}} ) {
292 if ($col >= $next_template_slot) {
293 $next_template_slot = $col + 1;
294 }
295 }
296 }
297 if ($next_template_slot == -1) {
298 die "Couldn't determine last column in loading template for data source" . $self->id;
299 }
300
301 foreach my $prop ( @$constant_values ) {
302 push @{$templates->[0]->{'column_positions'}}, $next_template_slot++;
303 push @{$templates->[0]->{'property_names'}}, $prop;
304 }
305 }
306
307 return $templates;
308 }
309  
310 sub _things_in_list_are_numeric {
311 my $self = shift;
312  
313 foreach ( @{$_[0]} ) {
314 return 0 if (! Scalar::Util::looks_like_number($_));
315 }
316 return 1;
317 }
318  
319 # Construct a closure to perform a test on the $index-th column of
320 # @$$next_candidate_row. The closures return 0 is the test is successful,
321 # -1 if unsuccessful but the file's value was less than $value, and 1
322 # if unsuccessful and greater. The iterator that churns throug the file
323 # knows that if it's comparing an ID/sorted column, and the comparator
324 # returns 1 then we've gone past the point where we can expect to ever
325 # find another successful match and we should stop looking
326 my $ALWAYS_FALSE = sub { -1 };
327 sub _comparator_for_operator_and_property {
328 my($self,$property,$next_candidate_row, $index, $operator,$value) = @_;
329  
330 no warnings 'uninitialized'; # we're handling ''/undef/null specially below where it matters
331  
332 if ($operator eq 'between') {
333 if ($value->[0] eq '' or $value->[1] eq '') {
334 return $ALWAYS_FALSE;
335 }
336  
337 if ($property->is_numeric and $self->_things_in_list_are_numeric($value)) {
338 if ($value->[0] > $value->[1]) {
339 # Will never be true
340 Carp::carp "'between' comparison will never be true with values ".$value->[0]," and ".$value->[1];
341 return $ALWAYS_FALSE;
342 }
343  
344 # numeric 'between' comparison
345 return sub {
346 return -1 if ($$next_candidate_row->[$index] eq '');
347 if ($$next_candidate_row->[$index] < $value->[0]) {
348 return -1;
349 } elsif ($$next_candidate_row->[$index] > $value->[1]) {
350 return 1;
351 } else {
352 return 0;
353 }
354 };
355 } else {
356 if ($value->[0] gt $value->[1]) {
357 Carp::carp "'between' comparison will never be true with values ".$value->[0]," and ".$value->[1];
358 return $ALWAYS_FALSE;
359 }
360  
361 # A string 'between' comparison
362 return sub {
363 return -1 if ($$next_candidate_row->[$index] eq '');
364 if ($$next_candidate_row->[$index] lt $value->[0]) {
365 return -1;
366 } elsif ($$next_candidate_row->[$index] gt $value->[1]) {
367 return 1;
368 } else {
369 return 0;
370 }
371 };
372 }
373  
374 } elsif ($operator eq 'in') {
375 if (! @$value) {
376 return $ALWAYS_FALSE;
377 }
378  
379 if ($property->is_numeric and $self->_things_in_list_are_numeric($value)) {
380 # Numeric 'in' comparison returns undef if we're within the range of the list
381 # but don't actually match any of the items in the list
382 @$value = sort { $a <=> $b } @$value; # sort the values first
383 return sub {
384 return -1 if ($$next_candidate_row->[$index] eq '');
385 if ($$next_candidate_row->[$index] < $value->[0]) {
386 return -1;
387 } elsif ($$next_candidate_row->[$index] > $value->[-1]) {
388 return 1;
389 } else {
390 foreach ( @$value ) {
391 return 0 if $$next_candidate_row->[$index] == $_;
392 }
393 return -1;
394 }
395 };
396  
397 } else {
398 # A string 'in' comparison
399 @$value = sort { $a cmp $b } @$value;
400 return sub {
401 if ($$next_candidate_row->[$index] lt $value->[0]) {
402 return -1;
403 } elsif ($$next_candidate_row->[$index] gt $value->[-1]) {
404 return 1;
405 } else {
406 foreach ( @$value ) {
407 return 0 if $$next_candidate_row->[$index] eq $_;
408 }
409 return -1;
410 }
411 };
412  
413 }
414  
415 } elsif ($operator eq 'not in') {
416 if (! @$value) {
417 return $ALWAYS_FALSE;
418 }
419  
420 if ($property->is_numeric and $self->_things_in_list_are_numeric($value)) {
421 return sub {
422 return -1 if ($$next_candidate_row->[$index] eq '');
423 foreach ( @$value ) {
424 return -1 if $$next_candidate_row->[$index] == $_;
425 }
426 return 0;
427 }
428  
429 } else {
430 return sub {
431 foreach ( @$value ) {
432 return -1 if $$next_candidate_row->[$index] eq $_;
433 }
434 return 0;
435 }
436 }
437  
438 } elsif ($operator eq 'like') {
439 # 'like' is always a string comparison. In addition, we can't know if we're ahead
440 # or behind in the file's ID columns, so the only two return values are 0 and 1
441
442 return $ALWAYS_FALSE if ($value eq ''); # property like NULL is always false
443  
444 # Convert SQL-type wildcards to Perl-type wildcards
445 # Convert a % to a *, and _ to ., unless they're preceeded by \ to escape them.
446 # Not that this isn't precisely correct, as \\% should really mean a literal \
447 # followed by a wildcard, but we can't be correct in all cases without including
448 # a real parser. This will catch most cases.
449  
450 $value =~ s/(?<!\\)%/.*/g;
451 $value =~ s/(?<!\\)_/./g;
452 my $regex = qr($value);
453 return sub {
454 return -1 if ($$next_candidate_row->[$index] eq '');
455 if ($$next_candidate_row->[$index] =~ $regex) {
456 return 0;
457 } else {
458 return 1;
459 }
460 };
461  
462 } elsif ($operator eq 'not like') {
463 return $ALWAYS_FALSE if ($value eq ''); # property like NULL is always false
464 $value =~ s/(?<!\\)%/.*/;
465 $value =~ s/(?<!\\)_/./;
466 my $regex = qr($value);
467 return sub {
468 return -1 if ($$next_candidate_row->[$index] eq '');
469 if ($$next_candidate_row->[$index] =~ $regex) {
470 return 1;
471 } else {
472 return 0;
473 }
474 };
475  
476  
477 # FIXME - should we only be testing the numericness of the property?
478 } elsif ($property->is_numeric and $self->_things_in_list_are_numeric([$value])) {
479 # Basic numeric comparisons
480 if ($operator eq '=') {
481 return sub {
482 return -1 if ($$next_candidate_row->[$index] eq ''); # null always != a number
483 return $$next_candidate_row->[$index] <=> $value;
484 };
485 } elsif ($operator eq '<') {
486 return sub {
487 return -1 if ($$next_candidate_row->[$index] eq ''); # null always != a number
488 $$next_candidate_row->[$index] < $value ? 0 : 1;
489 };
490 } elsif ($operator eq '<=') {
491 return sub {
492 return -1 if ($$next_candidate_row->[$index] eq ''); # null always != a number
493 $$next_candidate_row->[$index] <= $value ? 0 : 1;
494 };
495 } elsif ($operator eq '>') {
496 return sub {
497 return -1 if ($$next_candidate_row->[$index] eq ''); # null always != a number
498 $$next_candidate_row->[$index] > $value ? 0 : -1;
499 };
500 } elsif ($operator eq '>=') {
501 return sub {
502 return -1 if ($$next_candidate_row->[$index] eq ''); # null always != a number
503 $$next_candidate_row->[$index] >= $value ? 0 : -1;
504 };
505 } elsif ($operator eq 'true') {
506 return sub {
507 $$next_candidate_row->[$index] ? 0 : -1;
508 };
509 } elsif ($operator eq 'false') {
510 return sub {
511 $$next_candidate_row->[$index] ? -1 : 0;
512 };
513 } elsif ($operator eq '!=' or $operator eq 'ne') {
514 return sub {
515 return 0 if ($$next_candidate_row->[$index] eq ''); # null always != a number
516 $$next_candidate_row->[$index] != $value ? 0 : -1;
517 }
518 }
519  
520 } else {
521 # Basic string comparisons
522 if ($operator eq '=') {
523 return sub {
524 return -1 if ($$next_candidate_row->[$index] eq '' xor $value eq '');
525 return $$next_candidate_row->[$index] cmp $value;
526 };
527 } elsif ($operator eq '<') {
528 return sub {
529 $$next_candidate_row->[$index] lt $value ? 0 : 1;
530 };
531 } elsif ($operator eq '<=') {
532 return sub {
533 return -1 if ($$next_candidate_row->[$index] eq '' or $value eq '');
534 $$next_candidate_row->[$index] le $value ? 0 : 1;
535 };
536 } elsif ($operator eq '>') {
537 return sub {
538 $$next_candidate_row->[$index] gt $value ? 0 : -1;
539 };
540 } elsif ($operator eq '>=') {
541 return sub {
542 return -1 if ($$next_candidate_row->[$index] eq '' or $value eq '');
543 $$next_candidate_row->[$index] ge $value ? 0 : -1;
544 };
545 } elsif ($operator eq 'true') {
546 return sub {
547 $$next_candidate_row->[$index] ? 0 : -1;
548 };
549 } elsif ($operator eq 'false') {
550 return sub {
551 $$next_candidate_row->[$index] ? -1 : 0;
552 };
553 } elsif ($operator eq '!=' or $operator eq 'ne') {
554 return sub {
555 $$next_candidate_row->[$index] ne $value ? 0 : -1;
556 }
557 }
558 }
559 }
560
561 my $iterator_serial = 0;
562 sub create_iterator_closure_for_rule {
563 my($self,$rule) = @_;
564  
565 my $class_name = $rule->subject_class_name;
566 my $class_meta = $class_name->__meta__;
567 my $rule_template = $rule->template;
568  
569 my $csv_column_order_names = $self->column_order;
570 my $csv_column_count = scalar @$csv_column_order_names;
571  
572 my $operators_for_properties = $rule_template->operators_for_properties();
573 my $values_for_properties = $rule->legacy_params_hash;
574 foreach ( values %$values_for_properties ) {
575 if (ref eq 'HASH' and exists $_->{'value'}) {
576 $_ = $_->{'value'};
577 }
578 }
579  
580 my $sort_order_names = $self->sort_order;
581 my %sort_column_names = map { $_ => 1 } @$sort_order_names;
582 my @non_sort_column_names = grep { ! exists($sort_column_names{$_}) } @$csv_column_order_names;
583  
584 my %column_name_to_index_map;
585 for (my $i = 0; $i < @$csv_column_order_names; $i++) {
586 $column_name_to_index_map{$csv_column_order_names->[$i]} = $i;
587 }
588  
589 # Index in the split-file-data for each sorted column in order
590 my @sort_order_column_indexes = map { $column_name_to_index_map{$_} } @$sort_order_names;
591  
592 my(%property_meta_for_column_name);
593 foreach my $column_name ( @$csv_column_order_names ) {
594 my $prop = UR::Object::Property->get(class_name => $class_name, column_name => $column_name);
595 our %WARNED_ABOUT_COLUMN;
596 unless ( $prop or $WARNED_ABOUT_COLUMN{$class_name . '::' . $column_name}++) {
597 $self->warning_message("Couldn't find a property in class $class_name that goes with column $column_name");
598 next;
599 }
600 $property_meta_for_column_name{$column_name} = $prop;
601 }
602  
603 my @rule_columns_in_order; # The order we should perform rule matches on - value is the index in @next_file_row to test
604 my @comparison_for_column; # closures to call to perform the match - same order as @rule_columns_in_order
605 my $last_sort_column_in_rule = -1; # Last index in @rule_columns_in_order that applies when trying "the shortcut"
606 my $looking_for_sort_columns = 1;
607  
608 my $next_candidate_row; # This will be filled in by the closure below
609 foreach my $column_name ( @$sort_order_names, @non_sort_column_names ) {
610 my $property_meta = $property_meta_for_column_name{$column_name};
611 unless ($property_meta) {
612 Carp::croak("Class $class_name has no property connected to column named '$column_name' in data source ".$self->id);
613 }
614 my $property_name = $property_meta->property_name;
615 if (! $operators_for_properties->{$property_name}) {
616 $looking_for_sort_columns = 0;
617 next;
618 } elsif ($looking_for_sort_columns && $sort_column_names{$column_name}) {
619 $last_sort_column_in_rule++;
620 } else {
621 # There's been a gap in the ID column list in the rule, stop looking for
622 # further ID columns
623 $looking_for_sort_columns = 0;
624 }
625  
626 push @rule_columns_in_order, $column_name_to_index_map{$column_name};
627
628 my $operator = $operators_for_properties->{$property_name};
629 my $rule_value = $values_for_properties->{$property_name};
630
631 my $comparison_function = $self->_comparator_for_operator_and_property($property_meta,
632 \$next_candidate_row,
633 $column_name_to_index_map{$column_name},
634 $operator,
635 $rule_value);
636 unless ($comparison_function) {
637 Carp::croak("Unknown operator '$operator' in file data source filter");
638 }
639 push @comparison_for_column, $comparison_function;
640 }
641  
642 my $split_regex = $self->_regex();
643  
644 # FIXME - another performance boost might be to do some kind of binary search
645 # against the file to set the initial/next position?
646 my $file_pos = 0;
647  
648 # search in the offset cache for something helpful
649 my $offset_cache = $self->_offset_cache();
650  
651 # If the rule doesn't touch the sorted columns, then we can't use the offset cache for help :(
652 if ($last_sort_column_in_rule >= 0) {
653 # Starting at index 1 because we're interested in the file and seek data, not if it's in use
654 # offset 0 is the in-use flag, offset 1 is a ref to the file data and offset 2 is the file seek pos
655 SEARCH_CACHE:
656 for (my $i = 1; $i < @$offset_cache; $i+=3) {
657 next unless (defined($offset_cache->[$i]) && defined($offset_cache->[$i+1]));
658  
659 $next_candidate_row = $offset_cache->[$i];
660 my $matched = 0;
661 COMPARE_VALUES:
662 for (my $c = 0; $c <= $last_sort_column_in_rule; $c++) {
663 my $comparison = $comparison_for_column[$c]->();
664  
665 next SEARCH_CACHE if $comparison > 0;
666 if ($comparison < 0) {
667 $matched = 1;
668 last COMPARE_VALUES;
669 }
670 }
671 # If we made it this far, then the file data in this slot is earlier in the file
672 # than the data we're looking for. So, if the seek pos data is later than what
673 # we've found yet, use it instead
674 if ($matched and $offset_cache->[$i+1] > $file_pos) {
675 $file_pos = $offset_cache->[$i+1];
676 }
677 }
678 }
679  
680 my($monitor_start_time,$monitor_printed_first_fetch);
681 if ($ENV{'UR_DBI_MONITOR_SQL'}) {
682 $monitor_start_time = Time::HiRes::time();
683 $monitor_printed_first_fetch = 0;
684 my @filters_list;
685 for (my $i = 0; $i < @rule_columns_in_order; $i++) {
686 my $column = $rule_columns_in_order[$i];
687 my $column_name = $csv_column_order_names->[$column];
688 my $is_sorted = $i <= $last_sort_column_in_rule ? ' (sorted)' : '';
689 my $operator = $operators_for_properties->{$column_name} || '=';
690 my $rule_value = $values_for_properties->{$column_name};
691 if (ref $rule_value eq 'ARRAY') {
692 $rule_value = '[' . join(',', @$rule_value) . ']';
693 }
694 my $filter_string = $column_name . " $operator $rule_value" . $is_sorted;
695 push @filters_list, $filter_string;
696 }
697 my $filter_list = join("\n\t", @filters_list);
698 UR::DBI->sql_fh->printf("\nFILE: %s\nFILTERS %s\n\n", $self->server, $filter_list);
699 }
700  
701 $self->{'_last_read_serial'} ||= '';
702  
703 my $record_separator = $self->record_separator;
704 my $cache_slot = $self->_allocate_offset_cache_slot();
705 my $cache_insert_counter = 100; # a "breadcrumb" will be left in the offset cache after this many lines are read
706  
707 my $lines_read = 0;
708 my $printed_first_match = 0;
709 my $lines_matched = 0;
710  
711 my $fh; # File handle we'll be reading from
712 my $this_iterator_serial = $iterator_serial++;
713 my $iterator = sub {
714  
715 unless (ref($fh)) {
716 $fh = $self->get_default_handle();
717 # Lock the file for reading... For more fine-grained locking we could move this to
718 # after READ_LINE_FROM_FILE: but that would slow down read operations a bit. If
719 # there ends up being a problem with lock contention, go ahead and move it before $line = <$fh>;
720 #flock($fh,LOCK_SH);
721 }
722  
723 if ($monitor_start_time && ! $monitor_printed_first_fetch) {
724 UR::DBI->sql_fh->printf("FILE: FIRST FETCH TIME: %.4f s\n", Time::HiRes::time() - $monitor_start_time);
725 $monitor_printed_first_fetch = 1;
726 }
727  
728 if ($self->{'_last_read_serial'} ne $this_iterator_serial) {
729 UR::DBI->sql_fh->printf("FILE: Resetting file position to $file_pos\n") if $ENV{'UR_DBI_MONITOR_SQL'};
730 # The last read was from a different request, reset the position
731 $fh->seek($file_pos,0);
732 if ($file_pos == 0) {
733 my $skip = $self->skip_first_line;
734 while ($skip-- > 0) {
735 scalar(<$fh>);
736 }
737 }
738 $file_pos = $self->_file_position();
739  
740 $self->{'_last_read_serial'} = $this_iterator_serial;
741 }
742  
743 local $/; # Make sure some wise guy hasn't changed this out from under us
744 $/ = $record_separator;
745  
746 my $line;
747 READ_LINE_FROM_FILE:
748 until($line) {
749
750 # Hack for OSX 10.5.
751 # At EOF, the getline below will return undef. Most builds of Perl
752 # will also set $! to 0 at EOF so you can distinguish between the cases
753 # of EOF (which may have actually happened a while ago because of buffering)
754 # and an actual read error. OSX 10.5's Perl does not, and so $!
755 # retains whatever value it had after the last failed syscall, likely
756 # a stat() while looking for a Perl module. This should have no effect
757 # other platforms where you can't trust $! at arbitrary points in time
758 # anyway
759 $! = 0;
760 $line = <$fh>;
761  
762 unless (defined $line) {
763 if ($!) {
764 redo READ_LINE_FROM_FILE if ($! == EAGAIN or $! == EINTR);
765 my $pathname = $self->server();
766 Carp::confess("getline() failed for DataSource $self pathname $pathname boolexpr $rule: $!");
767 }
768  
769 # at EOF. Close up shop and return
770 #flock($fh,LOCK_UN);
771 $fh = undef;
772  
773 if ($monitor_start_time) {
774 UR::DBI->sql_fh->printf("FILE: at EOF\nFILE: $lines_read lines read for this request. $lines_matched matches\nFILE: TOTAL EXECUTE-FETCH TIME: %.4f s\n", Time::HiRes::time() - $monitor_start_time);
775 }
776  
777 return;
778 }
779  
780 $lines_read++;
781 my $last_read_size = length($line);
782 chomp $line;
783 # FIXME - to support record-oriented files, we need some replacement for this...
784 $next_candidate_row = [ split($split_regex, $line, $csv_column_count) ];
785 $#{$a} = $csv_column_count-1;
786  
787 $file_pos = $self->_file_position();
788 my $file_pos_before_read = $file_pos - $last_read_size;
789  
790 # Every so many lines read, leave a breadcrumb about what we've seen
791 unless ($lines_read % $cache_insert_counter) {
792 $offset_cache->[$cache_slot+1] = $next_candidate_row;
793 $offset_cache->[$cache_slot+2] = $file_pos_before_read;
794 $self->_free_offset_cache_slot($cache_slot);
795  
796 # get a new slot
797 $cache_slot = $self->_allocate_offset_cache_slot();
798 $offset_cache->[$cache_slot+1] = $next_candidate_row;
799 $offset_cache->[$cache_slot+2] = $file_pos_before_read;
800  
801 $cache_insert_counter <<= 2; # Double the insert counter
802 }
803  
804 for (my $i = 0; $i < @rule_columns_in_order; $i++) {
805 my $comparison = $comparison_for_column[$i]->();
806  
807 if ($comparison > 0 and $i <= $last_sort_column_in_rule) {
808 # We've gone past the last thing that could possibly match
809  
810 if ($monitor_start_time) {
811 UR::DBI->sql_fh->printf("FILE: $lines_read lines read for this request. $lines_matched matches\nFILE: TOTAL EXECUTE-FETCH TIME: %.4f s\n", Time::HiRes::time() - $monitor_start_time);
812 }
813  
814 #flock($fh,LOCK_UN);
815  
816 # Save the info from the last row we read
817 $offset_cache->[$cache_slot+1] = $next_candidate_row;
818 $offset_cache->[$cache_slot+2] = $file_pos_before_read;
819 return;
820
821 } elsif ($comparison) {
822 # comparison didn't match, read another line from the file
823 redo READ_LINE_FROM_FILE;
824 }
825  
826 # That comparison worked... stay in the for() loop for other comparisons
827 }
828 # All the comparisons return '0', meaning they passed
829  
830 # Now see if the offset cache file data is different than the row we just read
831 COMPARE_TO_CACHE:
832 foreach my $column ( @sort_order_column_indexes) {
833 no warnings 'uninitialized';
834 if ($offset_cache->[$cache_slot+1]->[$column] ne $next_candidate_row->[$column]) {
835 # They're different. Update the offset cache data
836 $offset_cache->[$cache_slot+1] = $next_candidate_row;
837 $offset_cache->[$cache_slot+2] = $file_pos_before_read;
838 last COMPARE_TO_CACHE;
839 }
840 }
841  
842 if (! $printed_first_match and $monitor_start_time) {
843 UR::DBI->sql_fh->printf("FILE: First match after reading $lines_read lines\n");
844 $printed_first_match=1;
845 }
846 $lines_matched++;
847  
848 return $next_candidate_row;
849 }
850 }; # end sub $iterator
851  
852 Sub::Name::subname('UR::DataSource::File::__datasource_iterator(closure)__', $iterator);
853  
854 my $count = $self->_open_query_count() || 0;
855 $self->_open_query_count($count+1);
856 bless $iterator, 'UR::DataSource::File::Tracker';
857 $iterator_data_source{$iterator} = $self;
858 $iterator_cache_slot_refs{$iterator} = \$cache_slot;
859
860 return $iterator;
861 }
862  
863  
864 sub UR::DataSource::File::Tracker::DESTROY {
865 my $iterator = shift;
866 my $ds = delete $iterator_data_source{$iterator};
867 return unless $ds; # The data source may have gone out of scope first during global destruction
868  
869 my $cache_slot_ref = delete $iterator_cache_slot_refs{$iterator};
870 if (defined($cache_slot_ref) and defined($$cache_slot_ref)) {
871 # Mark this slot unused
872 #print STDERR "Freeing cache slot $cache_slot\n";
873 #$ds->_offset_cache->[$$cache_slot_ref] = 0;
874 $ds->_free_offset_cache_slot($$cache_slot_ref);
875 }
876  
877 my $count = $ds->_open_query_count();
878 $ds->_open_query_count(--$count);
879  
880 return unless ($ds->quick_disconnect);
881 if ($count == 0 && $ds->has_default_handle) {
882 # All open queries have supposedly been fulfilled. Close the
883 # file handle and undef it so get_default_handle() will re-open if necessary
884 my $fh = $ds->get_default_handle;
885  
886 UR::DBI->sql_fh->printf("FILE: CLOSING fileno ".fileno($fh)."\n") if ($ENV{'UR_DBI_MONITOR_SQL'});
887 #flock($fh,LOCK_UN);
888 $fh->close();
889 $ds->__invalidate_get_default_handle__;
890 }
891 }
892  
893 # Names of creation params that we should force to be listrefs
894 our %creation_param_is_list = map { $_ => 1 } qw( column_order file_list sort_order constant_values );
895 sub create_from_inline_class_data {
896 my($class, $class_data, $ds_data) = @_;
897  
898 # User didn't specify columns in the file. Assumme every property is a column, and in the same order
899 unless (exists $ds_data->{'column_order'}) {
900 Carp::croak "data_source has no column_order specified";
901 }
902  
903 $ds_data->{'server'} ||= $ds_data->{'path'} || $ds_data->{'file'};
904  
905 my %ds_creation_params;
906 foreach my $param ( qw( delimiter record_separator column_order skip_first_line server file_list sort_order constant_values ) ) {
907 if (exists $ds_data->{$param}) {
908 if ($creation_param_is_list{$param} and ref($ds_data->{$param}) ne 'ARRAY') {
909 $ds_creation_params{$param} = \( $ds_data->{$param} );
910 } else {
911 $ds_creation_params{$param} = $ds_data->{$param};
912 }
913 }
914 }
915
916 my($namespace, $class_name) = ($class_data->{'class_name'} =~ m/^(\w+?)::(.*)/);
917 my $ds_id = "${namespace}::DataSource::${class_name}";
918 my $ds_type = delete $ds_data->{'is'};
919 my $ds = $ds_type->create( %ds_creation_params, id => $ds_id );
920 return $ds;
921 }
922  
923  
924 # The string used to join fields of a row together
925 #
926 # Since the 'delimiter' property is interpreted as a regex in the reading
927 # code, we'll try to be smart about making a real string from that.
928 #
929 # subclasses can override this to provide a different implementation
930 sub join_pattern {
931 my $self = shift;
932  
933 my $join_pattern = $self->delimiter;
934  
935 # make some common substitutions...
936 if ($join_pattern eq '\s*,\s*') {
937 # The default...
938 return ', ';
939 }
940  
941 $join_pattern =~ s/\\s*//g; # Turn 0-or-more whitespaces to nothing
942 $join_pattern =~ s/\\t/\t/; # tab
943 $join_pattern =~ s/\\s/ /; # whitespace
944
945 return $join_pattern;
946 }
947  
948  
949  
950 sub _sync_database {
951 my $self = shift;
952 my %params = @_;
953  
954 unless (ref($self)) {
955 if ($self->isa("UR::Singleton")) {
956 $self = $self->_singleton_object;
957 }
958 else {
959 die "Called as a class-method on a non-singleton datasource!";
960 }
961 }
962  
963 my $read_fh = $self->get_default_handle();
964 unless ($read_fh) {
965 Carp::croak($self->class . ": Can't _sync_database(): Can't open file " . $self->server . " for reading: $!");
966 }
967  
968 my $original_data_file = $self->server;
969 my $original_data_dir = File::Basename::dirname($original_data_file);
970 my $use_quick_rename;
971 unless (-d $original_data_dir){
972 File::Path::mkpath($original_data_dir);
973 }
974 if (-w $original_data_dir) {
975 $use_quick_rename = 1; # We can write to the data dir
976 } elsif (! -w $original_data_file) {
977 $self->error_message("Neither the directory nor the file for $original_data_file are writable - cannot sync_database");
978 return;
979 }
980  
981  
982 my $split_regex = $self->_regex();
983 my $join_pattern = $self->join_pattern;
984 my $record_separator = $self->record_separator;
985 local $/; # Make sure some wise guy hasn't changed this out from under us
986 $/ = $record_separator;
987  
988 my $csv_column_order_names = $self->column_order;
989 my $csv_column_count = scalar(@$csv_column_order_names);
990 my %column_name_to_index_map;
991 for (my $i = 0; $i < @$csv_column_order_names; $i++) {
992 $column_name_to_index_map{$csv_column_order_names->[$i]} = $i;
993 }
994  
995 my $changed_objects = delete $params{changed_objects};
996  
997  
998 # We're going to assumme all the passed-in objects are of the same class *gulp*
999 my $class_name = $changed_objects->[0]->class;
1000 my $class_meta = UR::Object::Type->get(class_name => $class_name);
1001 my %column_name_to_property_meta = map { $_->column_name => $_ }
1002 grep { $_->column_name }
1003 $class_meta->all_property_metas;
1004 my @property_names_in_column_order;
1005 foreach my $column_name ( @$csv_column_order_names ) {
1006 my $prop_meta = $column_name_to_property_meta{$column_name};
1007 unless ($prop_meta) {
1008 die "Data source " . $self->class . " id " . $self->id .
1009 " could not resolve a $class_name property for the data source's column named $column_name";
1010 }
1011  
1012 push @property_names_in_column_order, $prop_meta->property_name;
1013 }
1014  
1015 my $insert = [];
1016 my $update = {};
1017 my $delete = {};
1018 foreach my $obj ( @$changed_objects ) {
1019 if ($obj->isa('UR::Object::Ghost')) {
1020 # This should be removed from the file
1021 my $original = $obj->{'db_committed'};
1022 my $line = join($join_pattern, @{$original}{@property_names_in_column_order}) . $record_separator;
1023 $delete->{$line} = $obj;
1024  
1025 } elsif ($obj->{'db_committed'}) {
1026 # This object is changed since it was read in the file
1027 my $original = $obj->{'db_committed'};
1028 my $original_line = join($join_pattern, @{$original}{@property_names_in_column_order}) . $record_separator;
1029 my $changed_line = join($join_pattern, @{$obj}{@property_names_in_column_order}) . $record_separator;
1030 $update->{$original_line} = $changed_line;
1031
1032 } else {
1033 # This object is new and should be added to the file
1034 push @$insert, [ @{$obj}{@property_names_in_column_order} ];
1035 }
1036 }
1037  
1038 my $sort_order_names = $self->sort_order;
1039 foreach my $sort_column_name ( @$sort_order_names ) {
1040 unless (exists $column_name_to_index_map{$sort_column_name}) {
1041 Carp::croak("Column name '$sort_column_name' appears in the sort_order list, but not in the column_order list for data source ".$self->id);
1042 }
1043 }
1044 my $file_is_sorted = scalar(@$sort_order_names);
1045 my %column_sorts_numerically = map { $_->column_name => $_->is_numeric }
1046 values %column_name_to_property_meta;
1047 my $row_sort_sub = sub ($$) {
1048 my $comparison;
1049  
1050 foreach my $column_name ( @$sort_order_names ) {
1051 my $i = $column_name_to_index_map{$column_name};
1052 if ($column_sorts_numerically{$column_name}) {
1053 $comparison = $_[0]->[$i] <=> $_[1]->[$i];
1054 } else {
1055 $comparison = $_[0]->[$i] cmp $_[1]->[$i];
1056 }
1057 return $comparison if $comparison != 0;
1058 }
1059 return 0;
1060 };
1061 if ($sort_order_names && $file_is_sorted && scalar(@$insert)) {
1062 # the inserted things should be sorted the same way as the file
1063 my @sorted = sort $row_sort_sub @$insert;
1064 $insert = \@sorted;
1065 }
1066  
1067 my $write_fh;
1068 my $temp_file_name;
1069 if ($use_quick_rename) {
1070 $temp_file_name = sprintf("%s/.%d.%d" , $original_data_dir, time(), $$);
1071 $write_fh = IO::File->new($temp_file_name, O_WRONLY|O_CREAT);
1072 } else {
1073 $write_fh = File::Temp->new(UNLINK => 1);
1074 $temp_file_name = $write_fh->filename if ($write_fh);
1075 }
1076 unless ($write_fh) {
1077 Carp::croak "Can't create temporary file for writing: $!";
1078 }
1079  
1080 my $monitor_start_time;
1081 if ($ENV{'UR_DBI_MONITOR_SQL'}) {
1082 $monitor_start_time = Time::HiRes::time();
1083 my $time = time();
1084 UR::DBI->sql_fh->printf("\nFILE: SYNC_DATABASE AT %d [%s]. Started transaction for %s to temp file %s\n",
1085 $time, scalar(localtime($time)), $original_data_file, $temp_file_name);
1086  
1087 }
1088  
1089 unless (flock($read_fh,LOCK_SH)) {
1090 unless ($! == EOPNOTSUPP ) {
1091 Carp::croak($self->class(). ": Can't get exclusive lock for file ".$self->server.": $!");
1092 }
1093 }
1094  
1095 # write headers to the new file
1096 for (my $i = 0; $i < $self->skip_first_line; $i++) {
1097 my $line = <$read_fh>;
1098 $write_fh->print($line);
1099 }
1100  
1101
1102 my $line;
1103 READ_A_LINE:
1104 while(1) {
1105 unless ($line) {
1106 $line = <$read_fh>;
1107 last unless defined $line;
1108 }
1109  
1110 if ($file_is_sorted && scalar(@$insert)) {
1111 # there are sorted things waiting to insert
1112 my $chomped = $line;
1113 chomp $chomped;
1114 my $row = [ split($split_regex, $chomped, $csv_column_count) ];
1115 my $comparison = $row_sort_sub->($row, $insert->[0]);
1116 if ($comparison > 0) {
1117 # write the object's data
1118 no warnings 'uninitialized'; # Some of the object's data may be undef
1119 my $new_row = shift @$insert;
1120 my $new_line = join($join_pattern, @$new_row) . $record_separator;
1121  
1122 if ($ENV{'UR_DBI_MONITOR_SQL'}) {
1123 UR::DBI->sql_fh->print("INSERT >>$new_line<<\n");
1124 }
1125  
1126 $write_fh->print($new_line);
1127 # Don't undef the last line read, meaning it could still be written to the output...
1128 next READ_A_LINE;
1129 }
1130 }
1131  
1132 if (my $obj = delete $delete->{$line}) {
1133 if ($ENV{'UR_DBI_MONITOR_SQL'}) {
1134 UR::DBI->sql_fh->print("DELETE >>$line<<\n");
1135 }
1136 $line = undef;
1137 next;
1138
1139 } elsif (my $changed = delete $update->{$line}) {
1140 if ($ENV{'UR_DBI_MONITOR_SQL'}) {
1141 UR::DBI->sql_fh->print("UPDATE replace >>$line<< with >>$changed<<\n");
1142 }
1143 $write_fh->print($changed);
1144 $line = undef;
1145 next;
1146
1147 } else {
1148 # This line from the file was unchanged in the app
1149 $write_fh->print($line);
1150 $line = undef;
1151 }
1152 }
1153  
1154 if (keys %$delete) {
1155 $self->warning_message("There were ",scalar(keys %$delete)," deleted $class_name objects that did not match data in the file");
1156 }
1157 if (keys %$update) {
1158 $self->warning_message("There were ",scalar(keys %$update)," updated $class_name objects that did not match data in the file");
1159 }
1160  
1161 # finish out by writing the rest of the new data
1162 foreach my $new_row ( @$insert ) {
1163 no warnings 'uninitialized'; # Some of the object's data may be undef
1164 my $new_line = join($join_pattern, @$new_row) . $record_separator;
1165 if ($ENV{'UR_DBI_MONITOR_SQL'}) {
1166 UR::DBI->sql_fh->print("INSERT >>$new_line<<\n");
1167 }
1168 $write_fh->print($new_line);
1169 }
1170 $write_fh->close();
1171
1172 if ($use_quick_rename) {
1173 if ($ENV{'UR_DBI_MONITOR_SQL'}) {
1174 UR::DBI->sql_fh->print("FILE: COMMIT rename $temp_file_name over $original_data_file\n");
1175 }
1176  
1177 unless(rename($temp_file_name, $original_data_file)) {
1178 $self->error_message("Can't rename the temp file over the original file: $!");
1179 return;
1180 }
1181 } else {
1182 # We have to copy the data from the temp file to the original file
1183  
1184 if ($ENV{'UR_DBI_MONITOR_SQL'}) {
1185 UR::DBI->sql_fh->print("FILE: COMMIT write over $original_data_file in place\n");
1186 }
1187 my $new_write_fh = IO::File->new($original_data_file, O_WRONLY|O_TRUNC);
1188 unless ($new_write_fh) {
1189 $self->error_message("Can't open $original_data_file for writing: $!");
1190 return;
1191 }
1192  
1193 my $temp_file_fh = IO::File->new($temp_file_name);
1194 unless ($temp_file_fh) {
1195 $self->error_message("Can't open $temp_file_name for reading: $!");
1196 return;
1197 }
1198
1199 while(<$temp_file_fh>) {
1200 $new_write_fh->print($_);
1201 }
1202
1203 $new_write_fh->close();
1204 }
1205  
1206 # Because of the rename/copy process during syncing, the previously opened filehandle may
1207 # not be valid anymore. get_default_handle will reopen the file next time it's needed
1208 $self->_invalidate_cache();
1209 $self->__invalidate_get_default_handle__;
1210  
1211 if ($ENV{'UR_DBI_MONITOR_SQL'}) {
1212 UR::DBI->sql_fh->printf("FILE: TOTAL COMMIT TIME: %.4f s\n", Time::HiRes::time() - $monitor_start_time);
1213 }
1214  
1215 flock($read_fh, LOCK_UN);
1216 $read_fh->close();
1217  
1218 # FIXME - this is ugly... With RDBMS-type data sources, they will call $dbh->commit() which
1219 # gets to UR::DBI->commit(), which calls _set_object_saved_committed for them. Since we're
1220 # not using DBI we have to do this 2-part thing ourselves. In the future, we might break
1221 # out things so the saving to the temp file goes in _sync_database(), and moving the temp
1222 # file over the original goes in commit()
1223 unless ($self->_set_specified_objects_saved_uncommitted($changed_objects)) {
1224 Carp::croak("Error setting objects to a saved state after sync_database. Exiting.");
1225 return;
1226 }
1227  
1228 $self->_set_specified_objects_saved_committed($changed_objects);
1229 return 1;
1230 }
1231  
1232
1233  
1234 sub initializer_should_create_column_name_for_class_properties {
1235 1;
1236 }
1237  
1238  
1239 1;
1240  
1241 =pod
1242  
1243 =head1 NAME
1244  
1245 UR::DataSource::File - Parent class for file-based data sources
1246  
1247 =head1 DEPRECATED
1248  
1249 This module is deprecated. Use UR::DataSource::Filesystem instead.
1250  
1251 =head1 SYNOPSIS
1252
1253 package MyNamespace::DataSource::MyFile;
1254 class MyNamespace::DataSource::MyFile {
1255 is => ['UR::DataSource::File', 'UR::Singleton'],
1256 };
1257 sub server { '/path/to/file' }
1258 sub delimiter { "\t" }
1259 sub column_order { ['thing_id', 'thing_name', 'thing_color' ] }
1260 sub sort_order { ['thing_id'] }
1261  
1262 package main;
1263 class MyNamespace::Thing {
1264 id_by => 'thing_id',
1265 has => [ 'thing_id', 'thing_name', 'thing_color' ],
1266 data_source => 'MyNamespace::DataSource::MyFile',
1267 }
1268 my @objs = MyNamespace::Thing->get(thing_name => 'Bob');
1269  
1270 =head1 DESCRIPTION
1271  
1272 Classes which wish to retrieve their data from a regular file can use a UR::DataSource::File-based
1273 data source. The modules implementing these data sources live under the DataSource subdirectory
1274 of the application's Namespace, by convention. Besides defining a class for your data source
1275 inheriting from UR::DataSource::File, it should have the following methods, either as properties
1276 or functions in the package.
1277  
1278 =head2 Configuration
1279  
1280 These methods determine the configuration for your data source.
1281  
1282 =over 4
1283  
1284 =item server()
1285  
1286 server() should return a string representing the pathname of the file where the data is stored.
1287  
1288 =item file_list()
1289  
1290 The file_list() method should return a listref of pathnames to one or more identical files
1291 where data is stored. Use file_list() instead of server() when you want to load-balance several NFS
1292 servers, for example.
1293  
1294 You must have either server() or file_list() in your module, but not both. The existence of server()
1295 takes precedence over file_list().
1296  
1297 =item delimiter()
1298  
1299 delimiter() should return a string representing how the fields in each record are split into
1300 columns. This string is interpreted as a regex internally. The default delimiter is "\s*,\s*"
1301 meaning that the file is separated by commas.
1302  
1303 =item record_separator()
1304  
1305 record_separator() should return a string that gets stored in $/ before getline() is called on the
1306 file's filehandle. The default record_separator() is "\n" meaning that the file's records are
1307 separated by newlines.
1308  
1309 =item skip_first_line()
1310  
1311 skip_first_line() should return a boolean value. If true, the first line of the file is ignored, for
1312 example if the first line defines the columns in the file.
1313  
1314 =item column_order()
1315  
1316 column_order() should return a listref of column names in the file. column_order is required; there
1317 is no default.
1318  
1319 =item sort_order()
1320  
1321 If the data file is sorted in some way, sort_order() should return a listref of column names (which must
1322 exist in column_order()) by which the file is sorted. This gives the system a hint about how the file
1323 is structured, and is able to make shortcuts when reading the file to speed up data access. The default
1324 is to assumme the file is not sorted.
1325  
1326 =back
1327  
1328 =head1 INHERITANCE
1329  
1330 UR::DataSource
1331  
1332 =head1 SEE ALSO
1333  
1334 UR, UR::DataSource
1335  
1336 =cut