#!/usr/bin/perl # #******************************************************************************* # #Copyright (c) 2009, Perforce Software, Inc. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #******************************************************************************* # #Author: Stephen Moon # #Date: May 25, 2010 #Program: Truncates a large size journal or text file # #******************************************************************************* use warnings; use strict; use POSIX qw/floor/; use Benchmark; sub main() { my $argc = @ARGV; if ($argc !=2) { die "Usage: truncJnl.pl \n"; } my $jnl = $ARGV[0]; my $splits = int($ARGV[1]); my $start = new Benchmark; open(IN,"$jnl") or die "Unable to open $jnl: $!\n"; my $count = 0; while() { $count++; } close(IN); print "Total line count of $jnl: " . $count . "\n"; print "The line count of each splited $jnl is " . floor($count/int($splits)) . "\n\n"; my $chunk = floor($count/int($splits)); open(IN,"$jnl") or die "Unable to open $jnl: $!\n"; my ($jnl_ct,$chunk_bk) = 0; my $total_size = 0; my $line_ct = 0; while() { if($line_ct == 0 || $chunk_bk == 0) { if($line_ct == 0) { &findJournalCtr($_); } ($line_ct,$jnl,$jnl_ct,$total_size,$chunk_bk,$chunk) = &createFile($line_ct,$jnl,$jnl_ct,$total_size,$chunk_bk,$chunk); } elsif($line_ct == 1 && $_ =~ /^\@ex\@\s\d+\s(\d+).*$/) { print "Journal created on " . &unix2local($1) . "\n"; } print OUT $_; $total_size = $total_size + length($_); $line_ct++; $chunk_bk--; } close(IN); my $end = new Benchmark; my $diff = timediff($end, $start); print "Time taken was ", timestr($diff, 'nop'), "\n"; } #find out the journal counter value of the input journal sub findJournalCtr() { my $firstLine = shift; #print "test: " . $_ . "\n"; if($firstLine =~ /^\@vv\@ \d+ \@db.counters\@ \@journal\@ \@(\d+)\@.*$/) { print "Journal # is " . $1 . "\n"; } #convert unix time to local time } sub unix2local() { my $unixTime = shift; my $time_local = localtime $unixTime; return $time_local; } #helper function to create a new file sub createFile() { my $line_ct = shift; my $jnl = shift; my $jnl_ct = shift; my $total_size = shift; my $chunk_bk = shift; my $chunk = shift; if($line_ct == 0) { $total_size = 0; } else { printf("Done with %s.%d\tFile Size: %.0f Kbytes\n", $jnl,$jnl_ct,$total_size/1024); close(OUT); $total_size = 0; } $chunk_bk = $chunk; $jnl_ct++; open(OUT,"> $jnl.$jnl_ct") or die "Unable to open $jnl.$jnl_ct\n"; return ($line_ct,$jnl,$jnl_ct,$total_size,$chunk_bk,$chunk); } #run main here # &main()