class FilesystemEnv::GlueEnv

Constants

MoabDataStoreDir

FileSystemMetadataKeys = [PersistLayerKey]

MoabDatastoreName
NamespaceKey
PersistLayerKey
VersionKey

Attributes

_files_mgr_class[RW]

TODO: Rather than using File class directly, should a special class be used?

metadata_keys[RW]

TODO: Rather than using File class directly, should a special class be used?

moab_data[RW]

TODO: Rather than using File class directly, should a special class be used?

moab_datastore_name[RW]

TODO: Rather than using File class directly, should a special class be used?

model_key[RW]

TODO: Rather than using File class directly, should a special class be used?

model_save_params[RW]

TODO: Rather than using File class directly, should a special class be used?

namespace_key[RW]

TODO: Rather than using File class directly, should a special class be used?

node_key[RW]

TODO: Rather than using File class directly, should a special class be used?

persist_layer_key[RW]

TODO: Rather than using File class directly, should a special class be used?

required_instance_keys[RW]

TODO: Rather than using File class directly, should a special class be used?

required_save_keys[RW]

TODO: Rather than using File class directly, should a special class be used?

user_datastore_location[RW]

TODO: Rather than using File class directly, should a special class be used?

user_id[RW]

TODO: Rather than using File class directly, should a special class be used?

version_key[RW]

TODO: Rather than using File class directly, should a special class be used?

views[RW]

TODO: Rather than using File class directly, should a special class be used?

Public Class Methods

new(persist_env, data_model_bindings) click to toggle source
# File lib/glue_envs/filesystem_glue_env.rb, line 167
def initialize(persist_env, data_model_bindings)
  #TODO: determine if class_name is needed to segment cluster data within user data
  
  #via environmental settings
  filesystem_env = persist_env[:env]
  #key_fields = persist_env[:key_fields]
  fs_path = filesystem_env[:path]
  @user_id = filesystem_env[:user_id]
  @cluster_name = persist_env[:name]
  
  #data_model_bindings from NodeElementOperations
  key_fields = data_model_bindings[:key_fields] 
  initial_views_data = data_model_bindings[:views]
  
  @required_instance_keys = key_fields[:required_keys] #DataStructureModels::Tinkit::RequiredInstanceKeys
  @required_save_keys = key_fields[:required_keys] #DataStructureModels::Tinkit::RequiredSaveKeys
  @node_key = key_fields[:primary_key] #DataStructureModels::Tinkit::NodeKey


  @moab_datastore_name = MoabDatastoreName
  @version_key = VersionKey  #
  @model_key = @node_key #ModelKey
  
  #Change model key to persiste layer key ...
  #TODO: See about not making the filesystem dependent upon the node key
  @persist_layer_key = PersistLayerKey
  
  @namespace_key = NamespaceKey
  @metadata_keys = [@persist_layer_key, @version_key, @namespace_key ] #@persist_layer_key, @namespace_key]
  @user_datastore_location = File.join(fs_path, @user_id, MoabDataStoreDir)    
  
  
  
  @model_save_params = {:nodes_save_path => @user_datastore_location, :data_file => @moab_datastore_name, :node_key => @node_key}
  @_files_mgr_class = FilesystemInterface::FilesMgr
  @views = TinkitFileSystemViews
  @moab_data = {:moab_datastore_name => @moab_datastore_name}
  #@views_mgr = ViewsMgr.new({:data_file => @data_file_name})
  
  FileUtils.mkdir_p(fs_path) unless File.exists?(fs_path)
end

Public Instance Methods

convert_node_value_to_file_value(node_key_value) click to toggle source
# File lib/glue_envs/filesystem_glue_env.rb, line 389
def convert_node_value_to_file_value(node_key_value)
  file_base_value = node_key_value.to_s.gsub("::", "_")
  file_value = File.join(@user_datastore_location, file_base_value)
end
destroy_bulk(list_of_native_records) click to toggle source
# File lib/glue_envs/filesystem_glue_env.rb, line 377
def destroy_bulk(list_of_native_records)
  @@log.info {"Bulk Destroy: #{list_of_native_records.inspect}"} if @@log.info?
  return [] unless (list_of_native_records)
  list_of_native_records.each do |recs|
    next unless (recs && recs.size > 0)#raise "recs not hash: #{recs}" unless recs.class == Hash
    rec_id = recs[@persist_layer_key] || generate_model_key(nil, recs[@node_key])
    #rec_id = File.join(@user_datastore_location, rec_id) if File.dirname(rec_id) == "."
    #puts "Removing: #{r.inspect}"
    FileUtils.rm_rf(rec_id)
  end
end
destroy_node(model_metadata) click to toggle source
# File lib/glue_envs/filesystem_glue_env.rb, line 349
def destroy_node(model_metadata)
  #root_dir = @user_datastore_location
  #model_path(model_metadata[@model_key])
  node_id = model_metadata[@persist_layer_key] || generate_model_key(nil, model_metadata[@node_key])
  
  node_dir = node_id #File.join(root_dir, node_id)
  
  FileUtils.rm_rf(node_dir)
  #node = nil
end
find_contains(key, this_value) click to toggle source
# File lib/glue_envs/filesystem_glue_env.rb, line 264
def find_contains(key, this_value) 
  results =[]
  query_all.each do |record|
    test_val = record[key]
    results << record  if find_contains_type_helper(test_val, this_value)
  end
  results 
end
find_contains_type_helper(stored_data, this_value) click to toggle source
# File lib/glue_envs/filesystem_glue_env.rb, line 273
def find_contains_type_helper(stored_data, this_value)
  #p stored_dataj
  resp = nil
  #stored_data = jparse(stored_dataj)
  if stored_data.respond_to?(:"include?")
    resp = (stored_data.include?(this_value))
  else
    resp = (stored_data == this_value)
  end
  return resp
end
find_equals(key, this_value) click to toggle source
# File lib/glue_envs/filesystem_glue_env.rb, line 255
def find_equals(key, this_value) 
  results =[]
  query_all.each do |record|
    test_val = record[key]
    results << record  if test_val == this_value
  end
  results
end
find_nodes_where(key, relation, this_value) click to toggle source

current relations supported:

- :equals (data in the key field matches this_value)
- :contains (this_value is contained in the key field data (same as equals for non-enumerable types )
# File lib/glue_envs/filesystem_glue_env.rb, line 245
def find_nodes_where(key, relation, this_value)
  res = case relation
    when :equals
      find_equals(key, this_value)
    when :contains
      find_contains(key, this_value)
  end #case
  return res    
end
generate_model_key(namespace, node_key_value) click to toggle source

namespace is used to distinguish between unique data sets (i.e., users) within the model

# File lib/glue_envs/filesystem_glue_env.rb, line 362
def generate_model_key(namespace, node_key_value)
  #was in FileSystemEnv mixin
  #fs_generate_model_key(namespace, node_key)
  #TODO: Make sure namespace is portable across model migrations
  file_name = convert_node_value_to_file_value(node_key_value)
  #TODO: Expand bad character set
  #FIXME namespace is redundant so removed it
  #"#{namespace}/#{node_key}"
  file_name
end
get(id) click to toggle source
# File lib/glue_envs/filesystem_glue_env.rb, line 285
def get(id)
 return nil unless id
  #maybe put in some validations to ensure its from the proper collection namespace?
  #id may be the entry or the full path
  id_path = if id.include? (@user_datastore_location)
    id
  else
    convert_node_value_to_file_value(id)
  end
  #id_path = id #convert_node_value_to_file_value(id)
  data_file_path = File.join(id_path, @moab_datastore_name)
  rtn = if File.exists?(data_file_path)
    #data_file_path = File.join(id_path, @moab_datastore_name)
    json_data = File.open(data_file_path, 'r'){|f| f.read}
    node_data = JSON.parse(json_data)
    node_data = HashKeys.str_to_sym(node_data)
  else
    puts "Warning: File path doesn't exist: #{data_file_path.inspect}"
    []
  end
end
model_path(model_key_value) click to toggle source
# File lib/glue_envs/filesystem_glue_env.rb, line 373
def model_path(model_key_value)
  #model_key_value.gsub("::","/")
end
query_all() click to toggle source
# File lib/glue_envs/filesystem_glue_env.rb, line 209
def query_all  #TODO move to ViewsMgr
  unless File.exists?(@user_datastore_location)
    @@log.debug {"Warning: Can't query records. The File System Directory to work from does not exist: #{@user_datastore_location}"} if @@log.debug?
  end
  all_records = []
  my_dir = @user_datastore_location + '/' #TODO: Can this be removed?
  all_entries = Dir.working_entries(my_dir) || []
  @@log.debug "querying directory: #{my_dir.inspect} and got entries: #{all_entries.inspect}"
  all_entries.each do|entry|
    all_records << get(entry) || {}
  end
  @@log.debug {"query_all returning: #{all_records.inspect}" } if @@log.debug?
  return all_records
end
raw_all() click to toggle source

TODO: reconcile raw_all with query_all, this is the only glue env using raw_all

# File lib/glue_envs/filesystem_glue_env.rb, line 225
  def raw_all
    query_all
=begin  
    @@log.debug {"Getting All (Raw) Data using "\
                         "data loc: #{@user_datastore_location.inspect} "\
                         "datastore name: #{@moab_datastore_name.inspect}" } if @@log.debug?
    entries = query_all
    raw_nodes = []
    entries.each do |record|
      data_path = File.join(record[@persist_layer_key], @moab_datastore_name)
      data_json = File.open(data_path, 'r'){|f| f.read}
      data = JSON.parse(data_json)
      raw_nodes << data
    end
    raw_nodes
=end
  end
save(new_data) click to toggle source
# File lib/glue_envs/filesystem_glue_env.rb, line 307
def save(new_data)
  save_data = nil
  if new_data[@persist_layer_key]
    save_data = new_data
  elsif new_data[@node_key]
    persist_key_value = convert_node_value_to_file_value(new_data[@node_key])
    save_data = new_data.merge({@persist_layer_key => persist_key_value})
  else
    raise "Save Data did not include any keys for saving, data: #{new_data.inspect}"
  end
  save_path = @user_datastore_location
  save_location = save_data[@persist_layer_key]
  @@log.debug {"Save Directory: #{save_location.inspect}"} if @@log.debug?
  #was in FileSystemEnv mixin
  #fs_save(@model_save_params, model_data)
  #puts "FSG save udl: #{@user_datastore_location.inspect}"
    #parent_path = @user_datastore_location
    #node_key = @model_save_params[:node_key]
    #node_key = @node_key
    #puts "parent_path: #{parent_path.inspect}"
    #puts "new data node key: #{new_data[node_key].inspect}"
    #node_path = File.join(parent_path, new_data[node_key])
    file_name = File.basename(@moab_datastore_name)
    file_location = File.join(save_location, file_name) 
    
    #error_str = "Filesystem can't handle some characters in/
       #{file_location.inspect}"
    #raise error_str if new_data [node_key] =~ /::/ #FilesystemEnv::BADCHARS
    @@log.info {"File Location: #{file_location.inspect}"} if @@log.info?
    model_data = HashKeys.sym_to_str(save_data)
    
    FileUtils.mkdir_p(save_location) unless File.exist?(save_location)
    #raise "WTF?" unless File.exist?(save_location)
    rev = Time.now.hash #<- I would use File.mtime, but how to get the mod time before saving?
    model_data[@version_key] = rev
    f = File.open(file_location, 'w')
    f.write(model_data.to_json)
    f.close
    model_data['rev'] = model_data[@version_key] #TODO <-Investigate to see if it could be consistent
    return model_data
end