Browse Source

ts_generate

master
boB Rudis 4 years ago
parent
commit
1426aa98cb
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 3
      NAMESPACE
  2. 18
      R/RcppExports.R
  3. 7
      R/gen.R
  4. 13
      README.Rmd
  5. 73
      README.md
  6. 11
      man/ULIDgenerate.Rd
  7. 18
      man/ts_generate.Rd
  8. 3
      man/unmarshal.Rd
  9. 12
      src/RcppExports.cpp
  10. 30
      src/ulid-main.cpp

3
NAMESPACE

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

18
R/RcppExports.R

@ -1,9 +1,24 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
#' Generate ULIDs from timestamps
#'
#' This function generates a new [Universally Unique Lexicographically
#' Sortable Identifier](https://github.com/ulid/spec) from a vector of
#' `POSIXct` timestamps.
#'
#' @md
#' @param tsv vector of `POSIXct` values
#' @export
#' @examples
#' ts_generate(as.POSIXct("2017-11-01 15:00:00", origin="1970-01-01"))
ts_generate <- function(tsv) {
.Call(`_ulid_ts_generate`, tsv)
}
#' Generate ULID
#'
#' [ULIDgenerate()] generates a new []Universally Unique Lexicographically
#' [ULIDgenerate()] generates a new [Universally Unique Lexicographically
#' Sortable Identifier](https://github.com/ulid/spec).
#'
#' @md
@ -20,6 +35,7 @@ ULIDgenerate <- function(n = 1L) {
#' @md
#' @param ulids character ULIDs (e.g. created with [ULIDgenerate()])
#' @export
#' @return data frame (tibble)
#' @examples
#' unmarshal(ULIDgenerate())
unmarshal <- function(ulids) {

7
R/gen.R

@ -0,0 +1,7 @@
#' @rdname ULIDgenerate
#' @export
generate <- ULIDgenerate
#' @rdname ULIDgenerate
#' @export
ulid_generate <- ULIDgenerate

13
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` / `ulid_generate`: Generate a time-based ULID
- `ts_generate`: Generate ULID from timestamps
- `unmarshal`: Unmarshal a ULID into a data frame with timestamp and random bitstring columns
## Installation
@ -94,8 +95,16 @@ ulid::ULIDgenerate()
(u <- ulid::ULIDgenerate(20))
```
### Unmarshall
### Unmarshal
```{r}
unmarshal(u)
```
### Use defined timestamps
```{r}
(ut <- ts_generate(as.POSIXct("2017-11-01 15:00:00", origin="1970-01-01")))
unmarshal(ut)
```

73
README.md

@ -64,7 +64,9 @@ must be used. Within the same millisecond, sort order is not guaranteed
The following functions are implemented:
- `ULIDgenerate`: Generate a time-based ULID
- `ULIDgenerate` / `generate` / `ulid_generate`: Generate a time-based
ULID
- `ts_generate`: Generate ULID from timestamps
- `unmarshal`: Unmarshal a ULID into a data frame with timestamp and
random bitstring columns
@ -91,7 +93,7 @@ packageVersion("ulid")
ulid::ULIDgenerate()
```
## [1] "0001E2CWNS3ERSFZQ31W4WXVSM"
## [1] "0001E2D1EVWTN5G7C2244Q8SJ8"
### Many
@ -99,36 +101,51 @@ ulid::ULIDgenerate()
(u <- ulid::ULIDgenerate(20))
```
## [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"
## [1] "0001E2D1EV54P4DCS24WSYX2R1" "0001E2D1EV0D75WQ55XQ9TQ3RS" "0001E2D1EVPZVD2W626E49GHT6" "0001E2D1EVZCCD9DNH3SSTQN3K"
## [5] "0001E2D1EV6B8SYC81KSMVMM80" "0001E2D1EV5CT6Q9JB5H1SA5RF" "0001E2D1EVZEBD1J5RMGJ9B23S" "0001E2D1EVTX25KJGVGDJ61X5J"
## [9] "0001E2D1EVAT8T7V54G6ZQCX0F" "0001E2D1EVKRPHHANAZ5A070HN" "0001E2D1EVPZJ1NDV7M27TQBH0" "0001E2D1EVVZ0PAJVS3WY08QA4"
## [13] "0001E2D1EVZRQGQV0MEK65DKRA" "0001E2D1EVV1KP7XEKSEQDV8XQ" "0001E2D1EVZH19KNXG6GVEKKHP" "0001E2D1EVB7D31MKZQTRQP0S1"
## [17] "0001E2D1EVTPGNFEYW4MKGHZA1" "0001E2D1EV8SG2BCSFS3G7SXFV" "0001E2D1EV22JE3JZYF8P33PGS" "0001E2D1EVC6X98C3TT0N9B75C"
### Unmarshall
### Unmarshal
``` 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
## 1 2018-12-28 15:21:47 54P4DCS24WSYX2R1
## 2 2018-12-28 15:21:47 0D75WQ55XQ9TQ3RS
## 3 2018-12-28 15:21:47 PZVD2W626E49GHT6
## 4 2018-12-28 15:21:47 ZCCD9DNH3SSTQN3K
## 5 2018-12-28 15:21:47 6B8SYC81KSMVMM80
## 6 2018-12-28 15:21:47 5CT6Q9JB5H1SA5RF
## 7 2018-12-28 15:21:47 ZEBD1J5RMGJ9B23S
## 8 2018-12-28 15:21:47 TX25KJGVGDJ61X5J
## 9 2018-12-28 15:21:47 AT8T7V54G6ZQCX0F
## 10 2018-12-28 15:21:47 KRPHHANAZ5A070HN
## 11 2018-12-28 15:21:47 PZJ1NDV7M27TQBH0
## 12 2018-12-28 15:21:47 VZ0PAJVS3WY08QA4
## 13 2018-12-28 15:21:47 ZRQGQV0MEK65DKRA
## 14 2018-12-28 15:21:47 V1KP7XEKSEQDV8XQ
## 15 2018-12-28 15:21:47 ZH19KNXG6GVEKKHP
## 16 2018-12-28 15:21:47 B7D31MKZQTRQP0S1
## 17 2018-12-28 15:21:47 TPGNFEYW4MKGHZA1
## 18 2018-12-28 15:21:47 8SG2BCSFS3G7SXFV
## 19 2018-12-28 15:21:47 22JE3JZYF8P33PGS
## 20 2018-12-28 15:21:47 C6X98C3TT0N9B75C
### Use defined timestamps
``` r
(ut <- ts_generate(as.POSIXct("2017-11-01 15:00:00", origin="1970-01-01")))
```
## [1] "0001CZM6DG91GGPEZMYZ1TNKH3"
``` r
unmarshal(ut)
```
## ts rnd
## 1 2017-11-01 15:00:00 91GGPEZMYZ1TNKH3

11
man/ULIDgenerate.Rd

@ -1,17 +1,22 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/RcppExports.R
% Please edit documentation in R/RcppExports.R, R/gen.R
\name{ULIDgenerate}
\alias{ULIDgenerate}
\alias{generate}
\alias{ulid_generate}
\title{Generate ULID}
\usage{
ULIDgenerate(n = 1L)
generate(n = 1L)
ulid_generate(n = 1L)
}
\arguments{
\item{n}{number of id's to generate (default = \code{1})}
}
\description{
\code{\link[=ULIDgenerate]{ULIDgenerate()}} generates a new []Universally Unique Lexicographically
Sortable Identifier](https://github.com/ulid/spec).
\code{\link[=ULIDgenerate]{ULIDgenerate()}} generates a new \href{https://github.com/ulid/spec}{Universally Unique Lexicographically Sortable Identifier}.
}
\examples{
ULIDgenerate()

18
man/ts_generate.Rd

@ -0,0 +1,18 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/RcppExports.R
\name{ts_generate}
\alias{ts_generate}
\title{Generate ULIDs from timestamps}
\usage{
ts_generate(tsv)
}
\arguments{
\item{tsv}{vector of \code{POSIXct} values}
}
\description{
This function generates a new \href{https://github.com/ulid/spec}{Universally Unique Lexicographically Sortable Identifier} from a vector of
\code{POSIXct} timestamps.
}
\examples{
ts_generate(as.POSIXct("2017-11-01 15:00:00", origin="1970-01-01"))
}

3
man/unmarshal.Rd

@ -9,6 +9,9 @@ unmarshal(ulids)
\arguments{
\item{ulids}{character ULIDs (e.g. created with \code{\link[=ULIDgenerate]{ULIDgenerate()}})}
}
\value{
data frame (tibble)
}
\description{
Unmarshal a ULID into a data frame with timestamp and random bitstring columns
}

12
src/RcppExports.cpp

@ -5,6 +5,17 @@
using namespace Rcpp;
// ts_generate
CharacterVector ts_generate(Rcpp::DatetimeVector tsv);
RcppExport SEXP _ulid_ts_generate(SEXP tsvSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::DatetimeVector >::type tsv(tsvSEXP);
rcpp_result_gen = Rcpp::wrap(ts_generate(tsv));
return rcpp_result_gen;
END_RCPP
}
// ULIDgenerate
CharacterVector ULIDgenerate(long n);
RcppExport SEXP _ulid_ULIDgenerate(SEXP nSEXP) {
@ -29,6 +40,7 @@ END_RCPP
}
static const R_CallMethodDef CallEntries[] = {
{"_ulid_ts_generate", (DL_FUNC) &_ulid_ts_generate, 1},
{"_ulid_ULIDgenerate", (DL_FUNC) &_ulid_ULIDgenerate, 1},
{"_ulid_unmarshal", (DL_FUNC) &_ulid_unmarshal, 1},
{NULL, NULL, 0}

30
src/ulid-main.cpp

@ -4,9 +4,34 @@
using namespace Rcpp;
//' Generate ULIDs from timestamps
//'
//' This function generates a new [Universally Unique Lexicographically
//' Sortable Identifier](https://github.com/ulid/spec) from a vector of
//' `POSIXct` timestamps.
//'
//' @md
//' @param tsv vector of `POSIXct` values
//' @export
//' @examples
//' ts_generate(as.POSIXct("2017-11-01 15:00:00", origin="1970-01-01"))
// [[Rcpp::export]]
CharacterVector ts_generate(Rcpp::DatetimeVector tsv) {
CharacterVector c(tsv.size());
for (long i=0; i<tsv.size(); i++) {
ulid::ULID u = 0;
time_t t = static_cast<time_t>(tsv[i]);
ulid::EncodeTime(t, u);
ulid::EncodeEntropyRand(u);
c[i] = ulid::Marshal(u);
}
return(c);
}
//' Generate ULID
//'
//' [ULIDgenerate()] generates a new []Universally Unique Lexicographically
//' [ULIDgenerate()] generates a new [Universally Unique Lexicographically
//' Sortable Identifier](https://github.com/ulid/spec).
//'
//' @md
@ -29,13 +54,14 @@ CharacterVector ULIDgenerate(long n=1) {
//' @md
//' @param ulids character ULIDs (e.g. created with [ULIDgenerate()])
//' @export
//' @return data frame (tibble)
//' @examples
//' unmarshal(ULIDgenerate())
// [[Rcpp::export]]
DataFrame unmarshal(std::vector<std::string> ulids) {
unsigned long sz = ulids.size();
std::vector<Rcpp::Datetime> dt(sz);
Rcpp::DatetimeVector dt(sz);
std::vector<std::string> cv(sz);
for (unsigned long i=0; i<sz; i++) {

Loading…
Cancel
Save