DT.streams.anon     <- copy(DT.spot)
DT.tracks.anon   <- copy(DT.tracks)
DT.releases.anon <- copy(DT.releases)


setnames(DT.releases.anon, "label_is_dthree",   "label_property_d")
setnames(DT.releases.anon, "label_is_ioda",     "label_property_i")
setnames(DT.releases.anon, "label_is_o_and_o",  "label_property_o")

isrc <- sunique(c(DT.streams.anon$isrc, DT.tracks.anon$isrc))
upc <- sort(as.character(unique(c(DT.streams.anon$upc, DT.tracks.anon$upc, DT.releases.anon$upc))))
labelid <- sunique(c(DT.tracks.anon$labelid, DT.releases.anon$labelid))
playlist_uri <- sunique(DT.streams.anon$playlist_uri)
playlist_owner <- sunique(DT.streams.anon$playlist_owner)


types <- c("isrc", "upc", "labelid", "playlist_uri", "playlist_owner")

e <- environment()
ll_DTs <- emptylist(types)
for (type in types) {
  .t <- get(type, envir=e, inherits=FALSE)
  ll_DTs[[type]] <- setnames(data.table(
        x = .t
      , anon_value = sprintf("%s_%05i", type, seq_along(.t))
    ), "x", type)
}

replace_anon_val_ <- function(DT, type, from, to) {
  if (type %ni% names(DT))
    return(DT)

  stopifnot(length(from) == length(to))

  DT[, (type) := {.t <- get(type); stopifnot(length(.t) == .N); toFactorWithExpectedLevels(.t, levels=from, labels=to) }]
  return(DT)
}

for (type in types) {
  catn("processing DT.releases.anon");  replace_anon_val_(DT.releases.anon, type, from=ll_DTs[[type]][[type]], to=ll_DTs[[type]][["anon_value"]])
  catn("processing DT.tracks.anon");    replace_anon_val_(DT.tracks.anon,   type, from=ll_DTs[[type]][[type]], to=ll_DTs[[type]][["anon_value"]])
  catn("processing DT.streams.anon");   replace_anon_val_(DT.streams.anon,     type, from=ll_DTs[[type]][[type]], to=ll_DTs[[type]][["anon_value"]])
}

{
  writeDT(DT.streams.anon) %>% s3_upload_and_share("rick", subject="New Hire Data (1/4): DT.streams.anon")
  writeDT(DT.tracks.anon,    to=getRS(), subject="New Hire Data (2/4): DT.tracks.anon")
  writeDT(DT.releases.anon,  to=getRS(), subject="New Hire Data (3/4): DT.releases.anon")
  writeDT(DT.territories,    to=getRS(), subject="New Hire Data (4/4): DT.territories")
}


data available here
http://we.tl/MaImZWBFDU

