R/package-schema.R

Defines functions init_package_schema

#' @export
init_package_schema = function(package_name, yaml_path=NULL){
  if(is.null(yaml_path)){
    yaml_path = system.file(package = package_name, "extdata", "SCHEMA.yaml")
  }
  schema_yaml = yaml::read_yaml(yaml_path)
  schema_yaml$package$package_name = package_name
  
  entity_names = names(schema_yaml$array)
  schema_yaml$array = lapply(entity_names,
                             function(x){
                               tmp = schema_yaml$array[[x]]
                               tmp$entity_name = x
                               if(is.null(tmp$predefined_array_class)){
                                 return(RevealcoreArraySchema$new(tmp))
                               } else{
                                 return(eval(parse(text=paste0(tmp$predefined_array_class, "$new(tmp)"))))
                               }
                             })
  names(schema_yaml$array) = entity_names
  new_entities = lapply(entity_names,
                        function(x){
                          tmp = schema_yaml$array[[x]]
                          if(is.null(tmp$implied_entities)){
                            NULL
                          } else {
                            sapply(tmp$implied_entities, 
                                   function(y){
                                     return(eval(parse(text=paste0(y, "$new(tmp)"))))
                                   })
                          }
                        })
  new_entities = unlist(new_entities)
  if(!is.null(new_entities)){
    names(new_entities) = sapply(new_entities, function(x) x$entity_name)
    schema_yaml$array = c(schema_yaml$array, new_entities)
  }
  schema_yaml
}

##### RevealcoreArraySchema #####
#' Revealcore array schema
RevealcoreArraySchema = R6::R6Class(classname = 'RevealcoreArraySchema',
  public = list(
    #' @field entity_name The name of the entity, which must be unique within the package schema.  The full array name of the entity will be `{namespace}.{entity_name}`
    entity_name=NULL,
    #' @field entity_id A (package-unique) id for the entity, or -1 if the entity does not have one
    entity_id=NULL,
    data_class=NULL,
    data_subclass=NULL,
    #' @field namespace The namespace where the entity can be found.  The full array name of the entity will be `{namespace}.{entity_name}`
    namespace=NULL,
    #' @field attributes A named vector of array attributes, with the attribute names as the list names and the scidb type of the attribute as the value
    attributes=NULL,
    #' @field unique_fields A list of attributes and dimension names that correspond to a unique entry in the array
    unique_fields=NULL,
    #' @field unique_fields_dim A single array dimension.  If set, a single combination of unique_fields should correspond to a unique unique_fields_dim value
    unique_fields_dim=NULL,
    #' @field dims A list of vectors.  The first level should contain dimension names, and the second values for `start`, `end`, `chunk_interval`, `overlap`.  All values should be integers, though `start` can be `-Inf` and end can be `Inf`
    dims=NULL,
    hash_dims=NULL,
    search_by_entity=NULL,
    delete_by_entity=NULL,
    #' @field info_array Does the entity have an info array?  This will automatically cause entities named `{entity_name}_INFO`, `{entity_name}_METADATA_VALUE`, and `{entity_name}_METADATA_ATTRKEY` to be created if `TRUE`
    info_array=FALSE,
    #' @field info_array_attributes Names of any attributes of the entity that should also appear in the info array
    info_array_attributes=NULL,
    #' @field compression_on Should the entity be compressed?
    compression_on=FALSE,
    #' @field cached Should the entity be cached? (Not supported yet)
    cached=FALSE,
    distribution=NULL,
    default_value=NULL,
    implied_entities=NULL,
    #' @field keep_versions The number of array versions to keep, including the latest (default only the latest).  Set to NULL to keep all versions
    keep_versions=1,
    #' @field yaml The original yaml entry, which may contain package-specific values
    yaml=NULL,
    initialize = function(yaml){
      self$entity_name = yaml$entity_name
      self$entity_id=yaml$entity_id
      self$data_class=yaml$data_class
      self$data_subclass=yaml$data_subclass
      self$namespace=yaml$namespace
      self$attributes=yaml$attributes
      self$unique_fields=yaml$unique_fields
      self$dims=yaml$dims
      self$hash_dims=yaml$hash_dims
      self$search_by_entity=yaml$search_by_entity
      self$delete_by_entity=yaml$delete_by_entity
      if("info_array" %in% names(yaml) && yaml$info_array){
        self$info_array=yaml$info_array
        self$implied_entities = c(self$implied_entities, 
                                  "RevealcoreArrayImpliedArraySchemaInfoArray",
                                  "RevealcoreArrayImpliedArraySchemaMetadataValueArray",
                                  "RevealcoreArrayImpliedArraySchemaMetadataAttrkeyArray")
      }
      if("compression_on" %in% names(yaml))
        self$compression_on=yaml$compression_on
      if("cached" %in% names(yaml))
        self$cached=yaml$cached
      if("keep_versions" %in% names(yaml))
        self$keep_versions=yaml$keep_versions
      self$unique_fields_dim = yaml$unique_fields_dim
      self$info_array_attributes = yaml$info_array_attributes
      self$yaml = yaml
    }
  )
)

##### RevealcoreArraySchemaPermissions #####
RevealcoreArraySchemaPermissions = R6::R6Class(classname = 'RevealcoreArraySchemaPermissions',
  inherit = RevealcoreArraySchema,
  public = list(
    initialize = function(yaml){
      super$initialize(yaml)
      stopifnot(all(c("entity_id", "namespace", "secure_dimension") %in% names(yaml)))
      self$data_class = "permissionsdata"
      self$attributes = list(access="bool")
      self$dims = list(user_id=list(start=-1, end="Inf",overlap=0,chunk_interval=1),
                       tmp1=list(start=0, end="Inf",overlap=0,chunk_interval=256))
      names(self$dims) = c("user_id", yaml$secure_dimension)
      self$distribution = "replicated"
      self$keep_versions = 1
    }
  )
)

##### RevealcoreArraySchemaConfig #####
RevealcoreArraySchemaConfig = R6::R6Class(classname = 'RevealcoreArraySchemaConfig',
  inherit = RevealcoreArraySchema,
  public = list(
    initialize = function(yaml){
      super$initialize(yaml)
      stopifnot(all(c("entity_id", "namespace") %in% names(yaml)))
      self$data_class = "configuration"
      self$attributes = list(config_key="string", config_value="string")
      self$dims = list(config_key_id=list(start=0, end="Inf",overlap=0,chunk_interval=256),
                       config_value_id=list(start=0, end="Inf",overlap=0,chunk_interval=1024))
      self$unique_fields = list()
      self$implied_entities = c(self$implied_entities, "RevealcoreArrayImpliedArraySchemaConfigKey")
      self$keep_versions = 10
    }
  )
)

##### RevealcoreArrayImpliedArraySchemaInfoArray #####
RevealcoreArrayImpliedArraySchemaInfoArray = R6::R6Class(classname = "RevealcoreArrayImpliedArraySchemaInfoArray",
  inherit = RevealcoreArraySchema,
  public = list(
    initialize = function(array_schema){
      self$entity_id = -1
      self$entity_name = paste0(array_schema$entity_name, "_INFO")
      self$namespace=array_schema$namespace
      self$dims = array_schema$dims
      self$dims[["metadata_attrkey_id"]] = list(start=0, end="Inf", chunk_interval=4096, overlap=0)
      self$dims[["metadata_value_id"]] = list(start=-1, end="Inf", chunk_interval=256, overlap=0)
      self$attributes = list(metadata_value="string", metadata_attrkey="string")
      self$unique_fields = c(names(array_schema$dims), "metadata_value", "metadata_attrkey")
      self$info_array = FALSE
      self$keep_versions = array_schema$keep_versions
    }
  )
)

##### RevealcoreArrayImpliedArraySchemaMetadataValueArray #####
RevealcoreArrayImpliedArraySchemaMetadataValueArray = R6::R6Class(classname = "RevealcoreArrayImpliedArraySchemaMetadataValueArray",
  inherit = RevealcoreArraySchema,
  public = list(
    initialize = function(array_schema){
      self$entity_id = -1
      self$entity_name = paste0(array_schema$entity_name, "_METADATA_VALUE")
      self$namespace=array_schema$namespace
      self$data_class = "metadata"
      self$data_subclass = "metadata_curated_values"
      self$attributes = list(metadata_value="string")
      self$dims = list(metadata_value_id=list(start=0, end="Inf",overlap=0,chunk_interval=256))
      self$unique_fields = c("metadata_value")
      self$unique_fields_dim = "metadata_value_id"
      self$info_array = FALSE
      self$delete_by_entity = self$entity_name
      self$keep_versions = 1
    }
  )
)

##### RevealcoreArrayImpliedArraySchemaMetadataAttrkeyArray #####
RevealcoreArrayImpliedArraySchemaMetadataAttrkeyArray = R6::R6Class(classname = "RevealcoreArrayImpliedArraySchemaMetadataAttrkeyArray",
  inherit = RevealcoreArraySchema,
  public = list(
    initialize = function(array_schema){
      self$entity_id = -1
      self$entity_name = paste0(array_schema$entity_name, "_METADATA_ATTRKEY")
      self$namespace=array_schema$namespace
      self$data_class = "metadata"
      self$data_subclass = "metadata_curated_values"
      self$attributes = list(metadata_attrkey="string")
      self$dims = list(metadata_attrkey_id=list(start=0, end="Inf",overlap=0,chunk_interval=4096))
      self$unique_fields = c("metadata_attrkey")
      self$unique_fields_dim = "metadata_attrkey_id"
      self$info_array = FALSE
      self$delete_by_entity = self$entity_name
      self$keep_versions = 1
    }
  )
)

##### RevealcoreArrayImpliedArraySchemaConfigKey #####
RevealcoreArrayImpliedArraySchemaConfigKey = R6::R6Class(classname = "RevealcoreArrayImpliedArraySchemaConfigKey",
  inherit = RevealcoreArraySchema,
  public = list(
    initialize = function(array_schema){
      self$entity_id = -1
      self$entity_name = paste0(array_schema$entity_name, "_KEY")
      self$data_class = "configuration"
      self$namespace=array_schema$namespace
      self$attributes = list(config_key="string")
      self$dims = list(config_key_id=list(start=0, end="Inf",overlap=0,chunk_interval=256))
      self$unique_fields = c("config_key")
      self$info_array = FALSE
      self$cached = TRUE
      self$keep_versions = array_schema$keep_versions
    }
  )
)
Paradigm4/revealcore documentation built on May 21, 2023, 9:57 a.m.