Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: index for large text file

by cafeblue (Novice)
on Mar 28, 2011 at 06:52 UTC ( [id://895849]=note: print w/replies, xml ) Need Help??


in reply to Re: index for large text file
in thread index for large text file

thank you for your advice!!

but the odd lines are not of the same length. maybe your solution would get a wrong result. but you give me a good advice!

I change the pack parameter from "N" to "L", maybe the "L" could be enough to store the line number, while the "N" is not long enough.

thank you!

Replies are listed 'Best First'.
Re^3: index for large text file
by Eliya (Vicar) on Mar 28, 2011 at 10:50 UTC
    I change the pack parameter from "N" to "L", maybe the "L" could be enough to store the line number, while the "N" is not long enough.

    Both "N" and "L" are 32-bit usigned integers, so that's not going to make a (useful) difference.  They only differ with respect to byte order:

    • "N" — 32-bit unsigned integer in big-endian byte order
    • "V" — 32-bit unsigned integer in little-endian byte order
    • "L" — 32-bit unsigned integer in native byte order of the architecture perl is running on

    A 32-bit unsigned int can hold values up to 4294967296 (4G). If you need to store larger values, you could use the "Q" template (64-bit), if your build of perl supports it.  Otherwise - or if you want to save space - you could just "add" another single byte ("C"), so you have 5 bytes / 40-bit in total — which would be able to handle indices of up to around 1 Terabyte:

    my $i = 78187493530; write_index($i); print read_index(); # 78187493530 sub write_index { my $i = shift; open my $f, ">", "myindex" or die $!; my $pi = pack("CN", $i / 2**32, $i % 2**32); print $f $pi; # writes 5 bytes close $f; } sub read_index { open my $f, "<", "myindex" or die $!; read $f, my $pi, 5; my ($C, $N) = unpack "CN", $pi; return $C * 2**32 + $N; }

    This works even with 32-bit-int perls, because then numbers larger than 32-bit are handled as floats internally.

      thank you!

      after I change "N" to "L", indeed there are still some wrong lines! you are right !

      I will try your solution!

      So "Q" is not supported on my architecture and my file is 100GB+ with variable length lines. I'm a little confused with your code. is $i the offset?<\p>

      If I wanted to rebuild the entire index with the following code what would I change.

      sub build_index { my $data_file = shift; my $index_file = shift; my $offset = 0; while (<$data_file>) { print $index_file pack("N", $offset); $offset = tell($data_file); } } sub line_with_index { my $data_file = shift; my $index_file = shift; my $line_number = shift; my $size; my $i_offset; my $entry; my $d_offset; $size = length(pack("N", 0)); $i_offset = $size * ($line_number-1); seek($index_file, $i_offset, 0) or return; read($index_file, $entry, $size); $d_offset = unpack("N", $entry); seek($data_file, $d_offset, 0); return scalar(<$data_file>); }
Re^3: index for large text file
by moritz (Cardinal) on Mar 28, 2011 at 06:55 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://895849]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-19 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found