Skip to content

Moneta#new doesn't support thread-safe YAML::Store via positional arguments to the backend #231

@todd-a-jacobs

Description

@todd-a-jacobs

Given the following initialization with the documented :threadsafe keyword set to true doesn't yield a YAML::Store backend where @thread_safe=true. If I were calling YAML::Store#new directly, I would invoke:

require "yaml/store"

YAML::Store.new "store.yml", true

In Moneta, I can't seem to do the equivalent with either Moneta#new or Moneta::Adapters::YAML#new. With Moneta#new, I get the following, where the backend shows @thread_safe=false. Perhaps Moneta::Lock is handling the thread-safety on behalf of YAML::Store, but this is an assumption rather than something I was able to find documented. Consider the following:

m = Moneta.new :YAML, file: "store.yml", threadsafe: true, value_serializer: :yaml
m
#=> 
#<Moneta::Transformer::MarshalPrefixKeyYamlValue:0x00000001100d3520                           
 @adapter=                     
  #<Moneta::Lock:0x00000001100d8430
   @adapter=                   
    #<Moneta::Adapters::YAML:0x00000001100d9010
     @backend=
      #<Psych::Store:0x00000001100d8a48
       @abort=false,
       @filename="store.yml",
       @lock=#<Thread::Mutex:0x00000001100d87f0>,
       @opt={},
       @thread_safe=false,
       @ultra_safe=false>,
     @config=nil,
     @id="Moneta::Adapters::PStore(312140)">,
   @config=nil,
   @lock=#<Thread::Mutex:0x00000001100d3f98>>,
 @config=nil,
 @prefix="">

This seems to be fixable on the fly with:

m.adapter.adapter.backend.instance_variable_set :@thread_safe, true
m
#=> 
#<Moneta::Transformer::MarshalPrefixKeyYamlValue:0x00000001100d3520                                                   
 @adapter=                                                                                                            
  #<Moneta::Lock:0x00000001100d8430                                                                                   
   @adapter=                                                                                                          
    #<Moneta::Adapters::YAML:0x00000001100d9010                                                                       
     @backend=                                                                                                        
      #<Psych::Store:0x00000001100d8a48                                                                               
       @abort=false,                                                                                                  
       @filename="store.yml",                                                                                         
       @lock=#<Thread::Mutex:0x00000001100d87f0>,                                                                     
       @opt={},                                                                                                       
       @thread_safe=true,                                                                                             
       @ultra_safe=false>,                                                                                   
     @config=nil,                                                                                            
     @id="Moneta::Adapters::PStore(312140)">,                                                                
   @config=nil,                                                                                              
   @lock=#<Thread::Mutex:0x00000001100d3f98>>,                                                               
 @config=nil,
 @prefix="">

However, unless Moneta::Lock is handling the thread-safety on behalf of YAML::Store (which doesn't seen to be explicit in the documentation for Moneta or the YAML adapter) it seems as if the standard constructor doesn't have a way to pass other positional arguments to the :YAML backend. This may simply be a documentation issue, or possibly a bug. Either way, it should probably be more obvious about what arguments a given adapter can accept, or how (or even if) YAML::Store and other adapters can receive positional arguments during initialization.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions