R/map-txt.R

TextMap <- R6::R6Class(
  classname = "TextMap",
  inherit = FileMap,
  portable = TRUE,
  cloneable = TRUE,
  public = list(

    `@set` = function(key, value, signature) {
      # If new key, then no-harm as there is no writing
      # self$`@remove`(key)

      # Generate filename from key
      encoded_key <- safe_urlencode(key)
      # signature is already hashed

      # save value
      fpath <- file.path(private$db_dir, encoded_key)

      tf <- tempfile()
      on.exit({ unlink(tf) })

      yaml::write_yaml(value, file = tf)
      file.copy(from = tf, to = fpath, overwrite = TRUE)

      file.copy(from = private$header_file, to = tf, overwrite = TRUE)

      write.table(
        data.frame(Key = encoded_key, Hash = signature),
        file = tf,
        sep = "|",
        append = TRUE,
        quote = FALSE,
        row.names = FALSE,
        col.names = FALSE
      )
      file.copy(from = tf, to = private$header_file, overwrite = TRUE)

      # write.table(
      #   data.frame(Key = encoded_key, Hash = signature),
      #   file = private$header_file,
      #   sep = "|",
      #   append = TRUE,
      #   quote = FALSE,
      #   row.names = FALSE,
      #   col.names = FALSE
      # )

      return( signature )
    },

    get = function(key, missing_default) {
      ekey <- safe_urlencode(key)
      fpath <- file.path(private$db_dir, ekey)
      if ( file.exists(fpath) ) {
        yaml::read_yaml(fpath)
      } else {
        if (missing(missing_default)) { missing_default <- self$missing_default }
        missing_default
      }
    },

    initialize = function(path) {
      path <- dir_create(path)
      private$root_path <- path
      private$db_dir <- dir_create(file.path(path, "MAP-YAMLDB"))
      header_file <- file.path(path, "MAP-YAMLHEAD")
      if ( !file.exists(header_file) ) {
        header_file <- file_create(header_file)
        writeLines("Key|Hash", con = header_file)
      }
      private$header_file <- header_file
      self$lockfile <- file.path(path, "MAP-YAMLLOCK")
    }
  )
)

Try the dipsaus package in your browser

Any scripts or data that you put into this service are public.

dipsaus documentation built on May 23, 2026, 9:09 a.m.