6

I am working with documents compiled by Rakudo Perl and the documents can get updated.
I store the documents in a CompUnit::PrecompilationStore::File

How do I change an older version for a newer one?

The following program produces the same output, as if the newer version is not stored in CompUnit. What am I doing wrong?

use v6.c;
use nqp;
'cache'.IO.unlink if 'cache'.IO ~~ e;
my $precomp-store = CompUnit::PrecompilationStore::File.new(prefix=>'cache'.IO);
my $precomp = CompUnit::PrecompilationRepository::Default.new(store=> $precomp-store );
my $key = nqp::sha1('test.pod6');

'test.pod6'.IO.spurt(q:to/--END--/);
    =begin pod
    =TITLE More and more

    Some text

    =end pod
    --END--
$precomp.precompile('test.pod6'.IO, $key, :force);
my $handle = $precomp.load( $key )[0];
my $resurrected = nqp::atkey($handle.unit,'$=pod')[0];
say $resurrected.contents[1].contents[0];


'test.pod6'.IO.spurt(q:to/--END--/);
    =begin pod
    =TITLE More and more

    Some more text added

    =end pod
    --END--
# $precomp-store.unlock;
# fails with:
# Attempt to unlock mutex by thread not holding it
#  in block <unit> at comp-test.p6 line 30

$precomp.precompile('test.pod6'.IO, $key, :force);
my $new-handle = $precomp.load($key)[0];
my $new-resurrected = nqp::atkey($new-handle.unit,'$=pod')[0];
say $new-resurrected.contents[1].contents[0];

The output is always:

Some text
Some text

Update: My original question had '$handle' not '$new-handle' where '$new-resurrected' is defined. There is no change to the output.

Richard Hainsworth
  • 1,585
  • 7
  • 9
  • Hi Richard, I just wrote a comment addressed to you on a different SO, asking you to review your question and existing answers and maybe accept one of them. The same applies to all questions really. If one of the answers seems like it's on the right track then maybe a comment on that answer will help us collectively get to closure. And if none are anywhere near what the questioner needs, then maybe adding a comment to the question or editing it will get us there. Please consider reviewing all your questions to see if you can progress them in this fashion. TIA. – raiph May 27 '19 at 08:34

2 Answers2

4

I think the answer might be in the answer to other, similar question of yours here In general, CompUnits are intended to be immutable. If the object changes, the target needs to change too. As @ugexe says there,

$key is intended to represent an immutable name, such that it will always point at the same content.

So you might be actually be looking for a precomp-like behavior, but you might not want to use the actual CompUnits to do that.

jjmerelo
  • 22,578
  • 8
  • 40
  • 86
  • 2
    I re-read @ugexe answer again. I had not considered the extent of the immutability. There seem to be ways of removing things from cache (methods called delete and remove-from-cache). What then is the purpose of :force, if nothing can be forced? – Richard Hainsworth Dec 12 '18 at 07:06
  • 3
    You are forcing precompilation but seem to be expecting it to also force loading to break the cache. These are two different things. Restart your program after precompiling so the load happens on an empty cache and you will see the updates. – ugexe Dec 12 '18 at 13:27
2

As mentioned previously load methods cache, not the method call to precomp. You are expecting the parameter :force to method precompile to affect a later call to method load -- this is incorrect. We can easily prove that :force is working as expected for precompiling by skipping the first call to load and seeing if the final call to load shows the updated results:

use v6.c;
use nqp;
'cache'.IO.unlink if 'cache'.IO ~~ e;
my $precomp-store = CompUnit::PrecompilationStore::File.new(prefix=>'cache'.IO);
my $precomp = CompUnit::PrecompilationRepository::Default.new(store=> $precomp-store );
my $key = nqp::sha1('test.pod6');

'test.pod6'.IO.spurt(q:to/--END--/);
    =begin pod
    =TITLE More and more

    Some text

    =end pod
    --END--
$precomp.precompile('test.pod6'.IO, $key, :force);


'test.pod6'.IO.spurt(q:to/--END--/);
    =begin pod
    =TITLE More and more

    Some more text added

    =end pod
    --END--
# $precomp-store.unlock;
# fails with:
# Attempt to unlock mutex by thread not holding it
#  in block <unit> at comp-test.p6 line 30

$precomp.precompile('test.pod6'.IO, $key, :force);
my $new-handle = $precomp.load($key)[0];
my $new-resurrected = nqp::atkey($new-handle.unit,'$=pod')[0];
say $new-resurrected.contents[1].contents[0];

which gives: Some more text added

ugexe
  • 5,297
  • 1
  • 28
  • 49