Browse Source

unmarshalling

master
boB Rudis 5 years ago
parent
commit
e543b642fc
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 1
      NAMESPACE
  2. 11
      R/RcppExports.R
  3. 10
      README.Rmd
  4. 44
      README.md
  5. 17
      man/unmarshal.Rd
  6. 12
      src/RcppExports.cpp
  7. 38
      src/ulid-main.cpp

1
NAMESPACE

@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand
export(ULIDgenerate)
export(unmarshal)
importFrom(Rcpp,sourceCpp)
useDynLib(ulid, .registration = TRUE)

11
R/RcppExports.R

@ -15,3 +15,14 @@ ULIDgenerate <- function(n = 1L) {
.Call(`_ulid_ULIDgenerate`, n)
}
#' Unmarshal a ULID into a data frame with timestamp and random bitstring columns
#'
#' @md
#' @param ulids character ULIDs (e.g. created with [ULIDgenerate()])
#' @export
#' @examples
#' unmarshal(ULIDgenerate())
unmarshal <- function(ulids) {
.Call(`_ulid_unmarshal`, ulids)
}

10
README.Rmd

@ -59,7 +59,8 @@ The left-most character must be sorted first, and the right-most character sorte
The following functions are implemented:
- `ULIDgenerate`: Generate a time-based ULID
- `ULIDgenerate`: Generate a time-based ULID
- `unmarshal`: Unmarshal a ULID into a data frame with timestamp and random bitstring columns
## Installation
@ -90,6 +91,11 @@ ulid::ULIDgenerate()
### Many
```{r}
ulid::ULIDgenerate(20)
(u <- ulid::ULIDgenerate(20))
```
### Unmarshall
```{r}
unmarshal(u)
```

44
README.md

@ -65,6 +65,8 @@ must be used. Within the same millisecond, sort order is not guaranteed
The following functions are implemented:
- `ULIDgenerate`: Generate a time-based ULID
- `unmarshal`: Unmarshal a ULID into a data frame with timestamp and
random bitstring columns
## Installation
@ -89,16 +91,44 @@ packageVersion("ulid")
ulid::ULIDgenerate()
```
## [1] "0001E2CP1V7SS4P8R3JXKS654H"
## [1] "0001E2CWNS3ERSFZQ31W4WXVSM"
### Many
``` r
ulid::ULIDgenerate(20)
(u <- ulid::ULIDgenerate(20))
```
## [1] "0001E2CP1V9W5X9W7EXKPERJZT" "0001E2CP1V8GJ9FD9XJB2HEZBK" "0001E2CP1VDEM79FEDZJTK57DR" "0001E2CP1V8X07N2597NBGZQD1"
## [5] "0001E2CP1VP0B03JYW2P27NEGY" "0001E2CP1VDZ9F06K04RJR68NB" "0001E2CP1V8MS3FVT3JYS7S642" "0001E2CP1VJC64WK60QB9N7V2S"
## [9] "0001E2CP1VC26CCDS4BH730NFP" "0001E2CP1VFB04MCPB6CJDV2Z5" "0001E2CP1V24NC0SCDEDVS4AWZ" "0001E2CP1V08JV5664P962S6BY"
## [13] "0001E2CP1V0Y5Y8PXHMKYRS4G2" "0001E2CP1VHSNXR7GSBFE817G4" "0001E2CP1V09C00K3EZKEK6FD7" "0001E2CP1V19CKBAXNBN2CPJQ8"
## [17] "0001E2CP1VK35KF7PB0JQY567H" "0001E2CP1VT8WQ5W0K452JR4XG" "0001E2CP1VJTB1NB6344MH7QW5" "0001E2CP1VJ8QB3C3M7VVPS0Q6"
## [1] "0001E2CWNS0ZTZT8N2HBARCQ8F" "0001E2CWNSMQYKTA2DC9M5Q5SG" "0001E2CWNS4SZYS03Z1FXY5THC" "0001E2CWNSS5SJRQESE956SASQ"
## [5] "0001E2CWNS77P6X1MH1E93S9W8" "0001E2CWNSRYBVT63QPFVCHNJ3" "0001E2CWNS7KTHMJ6JPYDR7P66" "0001E2CWNSZRT8DDV952XAC2HH"
## [9] "0001E2CWNS7ZF28CQDHHF84TQA" "0001E2CWNSQT8MDESQ6QGF0FBH" "0001E2CWNSRH50VAGBHAVTZ4C5" "0001E2CWNSQMX29P05JA2NRZ1N"
## [13] "0001E2CWNS0873HP4QQ1VP4MC2" "0001E2CWNS1V29YAKW2GY39E7R" "0001E2CWNSA40H808JCB9ZTY6B" "0001E2CWNSQTZ3Q41V7D6V32XW"
## [17] "0001E2CWNS9XPF4EG450HH8R9H" "0001E2CWNSHZ15YKJR0VA6TQ8S" "0001E2CWNSRAPRP614BM7VW3BP" "0001E2CWNSTGPN4PF6EKPW5H2A"
### Unmarshall
``` r
unmarshal(u)
```
## ts rnd
## 1 2018-12-28 14:00:09 0ZTZT8N2HBARCQ8F
## 2 2018-12-28 14:00:09 MQYKTA2DC9M5Q5SG
## 3 2018-12-28 14:00:09 4SZYS03Z1FXY5THC
## 4 2018-12-28 14:00:09 S5SJRQESE956SASQ
## 5 2018-12-28 14:00:09 77P6X1MH1E93S9W8
## 6 2018-12-28 14:00:09 RYBVT63QPFVCHNJ3
## 7 2018-12-28 14:00:09 7KTHMJ6JPYDR7P66
## 8 2018-12-28 14:00:09 ZRT8DDV952XAC2HH
## 9 2018-12-28 14:00:09 7ZF28CQDHHF84TQA
## 10 2018-12-28 14:00:09 QT8MDESQ6QGF0FBH
## 11 2018-12-28 14:00:09 RH50VAGBHAVTZ4C5
## 12 2018-12-28 14:00:09 QMX29P05JA2NRZ1N
## 13 2018-12-28 14:00:09 0873HP4QQ1VP4MC2
## 14 2018-12-28 14:00:09 1V29YAKW2GY39E7R
## 15 2018-12-28 14:00:09 A40H808JCB9ZTY6B
## 16 2018-12-28 14:00:09 QTZ3Q41V7D6V32XW
## 17 2018-12-28 14:00:09 9XPF4EG450HH8R9H
## 18 2018-12-28 14:00:09 HZ15YKJR0VA6TQ8S
## 19 2018-12-28 14:00:09 RAPRP614BM7VW3BP
## 20 2018-12-28 14:00:09 TGPN4PF6EKPW5H2A

17
man/unmarshal.Rd

@ -0,0 +1,17 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/RcppExports.R
\name{unmarshal}
\alias{unmarshal}
\title{Unmarshal a ULID into a data frame with timestamp and random bitstring columns}
\usage{
unmarshal(ulids)
}
\arguments{
\item{ulids}{character ULIDs (e.g. created with \code{\link[=ULIDgenerate]{ULIDgenerate()}})}
}
\description{
Unmarshal a ULID into a data frame with timestamp and random bitstring columns
}
\examples{
unmarshal(ULIDgenerate())
}

12
src/RcppExports.cpp

@ -16,9 +16,21 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// unmarshal
DataFrame unmarshal(std::vector<std::string> ulids);
RcppExport SEXP _ulid_unmarshal(SEXP ulidsSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::vector<std::string> >::type ulids(ulidsSEXP);
rcpp_result_gen = Rcpp::wrap(unmarshal(ulids));
return rcpp_result_gen;
END_RCPP
}
static const R_CallMethodDef CallEntries[] = {
{"_ulid_ULIDgenerate", (DL_FUNC) &_ulid_ULIDgenerate, 1},
{"_ulid_unmarshal", (DL_FUNC) &_ulid_unmarshal, 1},
{NULL, NULL, 0}
};

38
src/ulid-main.cpp

@ -22,3 +22,41 @@ CharacterVector ULIDgenerate(long n=1) {
}
return(c);
}
//' Unmarshal a ULID into a data frame with timestamp and random bitstring columns
//'
//' @md
//' @param ulids character ULIDs (e.g. created with [ULIDgenerate()])
//' @export
//' @examples
//' unmarshal(ULIDgenerate())
// [[Rcpp::export]]
DataFrame unmarshal(std::vector<std::string> ulids) {
unsigned long sz = ulids.size();
std::vector<Rcpp::Datetime> dt(sz);
std::vector<std::string> cv(sz);
for (unsigned long i=0; i<sz; i++) {
ulid::ULID u = ulid::Unmarshal(ulids[i]);
dt[i] = Rcpp::Datetime(ulid::Time(u));
cv[i] = ulids[i].substr(10);
}
DataFrame out =
DataFrame::create(
_["ts"] = dt,
_["rnd"] = cv,
_["stringsAsFactors"] = false
);
out.attr("class") = CharacterVector::create("tbl_df", "tbl", "data.frame");
return(out);
}

Loading…
Cancel
Save