#  
#  Sample from these three: 
#                   Name      Size       Type     Rows Columns       Key
# 1:              DB.raw  24.45 GB data.table 57270943      63       upc
# 2:    DB.all.countmeta    5.4 GB data.table 44570488      16       upc
# 5: DB.SpotifyMeta.Long 338.94 KB data.table      771      40 upc, Week
# 7:      DB.SpotifyMeta 204.27 KB data.table      257      42       upc
#  


set.seed(1)
# Select a set of genres
Genres.Using <- c("Latin Music","Country","Children's","Punk","Pop","Hip-hop/Rap","Metal","Electronic","Rock")
# Sample at least 3 upcs per genre
UPCS.using <- DB.SpotifyMeta[genre %in% Genres.Using, .SD[sample(.N, min(.N, 3)), list(upc)], by=genre][, upc]


## (1)  Sample DB.raw
	# How many rows we ultimately want to end up with 
	nr.goal <- 5e5 # 600K
	setkeyIfNot(DB.raw, "upc", verbose=FALSE)
	perc <- nr.goal / nrow(DB.raw[UPCS.using])
	s.t(SAMP.DB.raw <- DB.raw[UPCS.using, .SD[sort(sample(.N, max(1, .N * perc)))]], "to create the sample")
	# if the sample came out too large, pare it down
	if (nrow(SAMP.DB.raw) > 1.1 * nr.goal) 
		SAMP.DB.raw <- SAMP.DB.raw[sort(sample(nrow(SAMP.DB.raw), nr.goal))]
	# re-order the columns
	setcolorder(SAMP.DB.raw, names(DB.raw))

## (2) Sample DB.all.countmeta
	s.t(SAMP.DB.all.countmeta <- DB.all.countmeta[genre %in% Genres.Using], title="Create AllCountMeta Sample")
	dim(SAMP.DB.all.countmeta)

## (3) Sample DB.SpotifyMeta.Long
	SAMP.DB.SpotifyMeta.Long <- DB.SpotifyMeta.Long[.(UPCS.using)]

## (4) Sample DB.SpotifyMeta.Long
	SAMP.DB.SpotifyMeta <- DB.SpotifyMeta[.(UPCS.using)]

## SET THE KEYS FOR ALL THE TABLES
	setkeyv(SAMP.DB.SpotifyMeta, 	  key(DB.SpotifyMeta))
	setkeyv(SAMP.DB.SpotifyMeta.Long, key(DB.SpotifyMeta.Long))
	setkeyv(SAMP.DB.all.countmeta,    key(DB.all.countmeta))
	setkeyv(SAMP.DB.raw,              key(DB.raw))


files.SAMP <- jesusForData(SAMP.DB.raw, SAMP.DB.all.countmeta
							, SAMP.DB.SpotifyMeta.Long, SAMP.DB.SpotifyMeta
							, sub="SAMP", summary=FALSE)

reproduce(files.SAMP)

lsos(DT, order="Columns")

# LOCAL ONLY # ::   # then on the local machine:                # :: # LOCAL ONLY # ::
# LOCAL ONLY # ::   l <- bringme(files.SAMP)                    # :: # LOCAL ONLY # ::
# LOCAL ONLY # ::   for (l.file in l) load(l.file)              # :: # LOCAL ONLY # ::
# LOCAL ONLY # ::   DB.raw <- SAMP.DB.raw                       # :: # LOCAL ONLY # ::
# LOCAL ONLY # ::   DB.all.countmeta <- SAMP.DB.all.countmeta   # :: # LOCAL ONLY # ::
# LOCAL ONLY # ::   DB.SpotifyMeta <- SAMP.DB.SpotifyMeta       # :: # LOCAL ONLY # ::
# LOCAL ONLY # ::   DB.SpotifyMeta.Long <- SAMP.DB.SpotifyMeta.Long       # :: # LOCAL ONLY # ::
# LOCAL ONLY # ::   rm(list=ls(pat="^SAMP\\.D"))                # :: # LOCAL ONLY # ::

