dwww Home | Show directory contents | Find package

R version 4.1.2 Patched (2022-01-20 r81529) -- "Bird Hippie"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin21.2.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> suppressMessages(library(filehash))
> 
> ######################################################################
> ## Test 'filehashRDS' class
> 
> dbCreate("mydbRDS", "RDS")
[1] TRUE
> db <- dbInit("mydbRDS", "RDS")
> show(db)
'filehashRDS' database 'mydbRDS'
> 
> ## Put some data into it
> set.seed(1000)
> dbInsert(db, "a", 1:10)
> dbInsert(db, "b", rnorm(100))
> dbInsert(db, "c", 100:1)
> dbInsert(db, "d", runif(1000))
> dbInsert(db, "other", "hello")
> 
> dbList(db)
[1] "a"     "b"     "c"     "d"     "other"
> 
> dbExists(db, "e")
[1] FALSE
> dbExists(db, "a")
[1] TRUE
> 
> env <- db2env(db)
> ls(env)
[1] "a"     "b"     "c"     "d"     "other"
> 
> env$a
 [1]  1  2  3  4  5  6  7  8  9 10
> env$b
  [1] -0.44577826 -1.20585657  0.04112631  0.63938841 -0.78655436 -0.38548930
  [7] -0.47586788  0.71975069 -0.01850562 -1.37311776 -0.98242783 -0.55448870
 [13]  0.12138119 -0.12087232 -1.33604105  0.17005748  0.15507872  0.02493187
 [19] -2.04658541  0.21315411  2.67007166 -1.22701601  0.83424733  0.53257175
 [25] -0.64682496  0.60316126 -1.78384414  0.33494217  0.56097572  1.22093565
 [31] -0.21145359  0.69942953 -0.70643668 -0.46515095 -1.76619861  0.18928860
 [37] -0.36618068  1.05760118 -0.74162146 -1.34835905 -0.51730643  1.41173570
 [43]  0.18546503 -0.04369144 -0.21591338  1.46377535  0.22966664  0.10762363
 [49] -1.37810256 -0.96818288  0.25171138 -1.09469370  0.39764284 -0.99630200
 [55]  0.10057801  0.95368028 -1.79032293  0.31170122  2.55398801 -0.86083776
 [61]  0.54392844 -0.39233804  1.23544190  1.19608644 -0.49574690 -0.29434122
 [67] -0.57349748  1.61920873 -0.95692767  0.04123712 -1.49831044  0.66095916
 [73]  0.28545762  1.38886629 -0.15934361 -0.46091890  0.16843807  1.39549302
 [79]  0.72842626  0.33508995  1.16927649  0.24796682 -0.35814947  1.38349332
 [85]  0.41206917 -0.12300786 -0.06622931 -2.32249088 -1.04565650  2.05787502
 [91]  1.97153237 -1.92099520  0.46212607 -0.16072406 -0.10421153  0.46783940
 [97]  0.44392082  0.82855281 -0.38705012  2.01893816
> env$c
  [1] 100  99  98  97  96  95  94  93  92  91  90  89  88  87  86  85  84  83
 [19]  82  81  80  79  78  77  76  75  74  73  72  71  70  69  68  67  66  65
 [37]  64  63  62  61  60  59  58  57  56  55  54  53  52  51  50  49  48  47
 [55]  46  45  44  43  42  41  40  39  38  37  36  35  34  33  32  31  30  29
 [73]  28  27  26  25  24  23  22  21  20  19  18  17  16  15  14  13  12  11
 [91]  10   9   8   7   6   5   4   3   2   1
> str(env$d)
 num [1:1000] 0.0854 0.3317 0.5647 0.4989 0.4549 ...
> env$other
[1] "hello"
> 
> env$b <- rnorm(100)
> mean(env$b)
[1] -0.02208835
> 
> env$a[1:5] <- 5:1
> print(env$a)
 [1]  5  4  3  2  1  6  7  8  9 10
> 
> dbDelete(db, "c")
> 
> tryCatch(print(env$c), error = function(e) cat(as.character(e)))
Error in dbFetch(db, key): unable to obtain value for key 'c'
> tryCatch(dbFetch(db, "c"), error = function(e) cat(as.character(e)))
Error in dbFetch(db, "c"): unable to obtain value for key 'c'
> 
> ## Check trailing '/' problem
> dbCreate("testRDSdb", "RDS")
[1] TRUE
> db <- dbInit("testRDSdb/", "RDS")
> print(db)
'filehashRDS' database 'testRDSdb'
> 
> ######################################################################
> ## test filehashDB1 class
> 
> dbCreate("mydb", "DB1")
[1] TRUE
> db <- dbInit("mydb", "DB1")
> 
> ## Put some data into it
> set.seed(1000)
> dbInsert(db, "a", 1:10)
> dbInsert(db, "b", rnorm(100))
> dbInsert(db, "c", 100:1)
> dbInsert(db, "d", runif(1000))
> dbInsert(db, "other", "hello")
> 
> dbList(db)
[1] "a"     "b"     "other" "c"     "d"    
> 
> env <- db2env(db)
> ls(env)
[1] "a"     "b"     "c"     "d"     "other"
> 
> env$a
 [1]  1  2  3  4  5  6  7  8  9 10
> env$b
  [1] -0.44577826 -1.20585657  0.04112631  0.63938841 -0.78655436 -0.38548930
  [7] -0.47586788  0.71975069 -0.01850562 -1.37311776 -0.98242783 -0.55448870
 [13]  0.12138119 -0.12087232 -1.33604105  0.17005748  0.15507872  0.02493187
 [19] -2.04658541  0.21315411  2.67007166 -1.22701601  0.83424733  0.53257175
 [25] -0.64682496  0.60316126 -1.78384414  0.33494217  0.56097572  1.22093565
 [31] -0.21145359  0.69942953 -0.70643668 -0.46515095 -1.76619861  0.18928860
 [37] -0.36618068  1.05760118 -0.74162146 -1.34835905 -0.51730643  1.41173570
 [43]  0.18546503 -0.04369144 -0.21591338  1.46377535  0.22966664  0.10762363
 [49] -1.37810256 -0.96818288  0.25171138 -1.09469370  0.39764284 -0.99630200
 [55]  0.10057801  0.95368028 -1.79032293  0.31170122  2.55398801 -0.86083776
 [61]  0.54392844 -0.39233804  1.23544190  1.19608644 -0.49574690 -0.29434122
 [67] -0.57349748  1.61920873 -0.95692767  0.04123712 -1.49831044  0.66095916
 [73]  0.28545762  1.38886629 -0.15934361 -0.46091890  0.16843807  1.39549302
 [79]  0.72842626  0.33508995  1.16927649  0.24796682 -0.35814947  1.38349332
 [85]  0.41206917 -0.12300786 -0.06622931 -2.32249088 -1.04565650  2.05787502
 [91]  1.97153237 -1.92099520  0.46212607 -0.16072406 -0.10421153  0.46783940
 [97]  0.44392082  0.82855281 -0.38705012  2.01893816
> env$c
  [1] 100  99  98  97  96  95  94  93  92  91  90  89  88  87  86  85  84  83
 [19]  82  81  80  79  78  77  76  75  74  73  72  71  70  69  68  67  66  65
 [37]  64  63  62  61  60  59  58  57  56  55  54  53  52  51  50  49  48  47
 [55]  46  45  44  43  42  41  40  39  38  37  36  35  34  33  32  31  30  29
 [73]  28  27  26  25  24  23  22  21  20  19  18  17  16  15  14  13  12  11
 [91]  10   9   8   7   6   5   4   3   2   1
> str(env$d)
 num [1:1000] 0.0854 0.3317 0.5647 0.4989 0.4549 ...
> env$other
[1] "hello"
> 
> env$b <- rnorm(100)
> mean(env$b)
[1] -0.02208835
> 
> env$a[1:5] <- 5:1
> print(env$a)
 [1]  5  4  3  2  1  6  7  8  9 10
> 
> dbDelete(db, "c")
> 
> tryCatch(print(env$c), error = function(e) cat(as.character(e)))
Error in readSingleKey(con, map, key): unable to obtain value for key 'c'
> tryCatch(dbFetch(db, "c"), error = function(e) cat(as.character(e)))
Error in readSingleKey(con, map, key): unable to obtain value for key 'c'
> 
> numbers <- rnorm(100)
> dbInsert(db, "numbers", numbers)
> b <- dbFetch(db, "numbers")
> stopifnot(all.equal(numbers, b))
> stopifnot(identical(numbers, b))
> 
> ################################################################################
> ## Other tests
> 
> rm(list = ls())
> 
> 
> dbCreate("testLoadingDB", "DB1")
[1] TRUE
> db <- dbInit("testLoadingDB", "DB1")
> 
> set.seed(234)
> 
> db$a <- rnorm(100)
> db$b <- runif(1000)
> 
> dbLoad(db)  ## 'a', 'b'
> summary(a, digits = 4)
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
-3.036000 -0.642100  0.172000  0.004131  0.614100  2.107000 
> summary(b, digits = 4)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.004583 0.229900 0.478600 0.482200 0.729200 0.999800 
> 
> rm(list = ls())
> db <- dbInit("testLoadingDB", "DB1")
> 
> dbLazyLoad(db)
> 
> summary(a, digits = 4)
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
-3.036000 -0.642100  0.172000  0.004131  0.614100  2.107000 
> summary(b, digits = 4)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.004583 0.229900 0.478600 0.482200 0.729200 0.999800 
> 
> 
> 
> ################################################################################
> ## Check dbReorganize
> 
> dbCreate("test_reorg", "DB1")
[1] TRUE
> db <- dbInit("test_reorg", "DB1")
> 
> set.seed(1000)
> dbInsert(db, "a", 1)
> dbInsert(db, "a", 1)
> dbInsert(db, "a", 1)
> dbInsert(db, "a", 1)
> dbInsert(db, "b", rnorm(1000))
> dbInsert(db, "b", rnorm(1000))
> dbInsert(db, "b", rnorm(1000))
> dbInsert(db, "b", rnorm(1000))
> dbInsert(db, "c", runif(1000))
> dbInsert(db, "c", runif(1000))
> dbInsert(db, "c", runif(1000))
> dbInsert(db, "c", runif(1000))
> 
> summary(db$b, digits = 4)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
-2.76800 -0.65520 -0.06100 -0.01269  0.65240  3.73900 
> summary(db$c, digits = 4)
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
0.0002346 0.2416000 0.4813000 0.4938000 0.7492000 0.9992000 
> 
> print(file.info(db@datafile)$size)
> 
> dbReorganize(db)
Reorganizing database: 
Finished; reload database with 'dbInit'
[1] TRUE
> 
> db <- dbInit("test_reorg", "DB1")
> 
> print(file.info(db@datafile)$size)
> 
> summary(db$b, digits = 4)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
-2.76800 -0.65520 -0.06100 -0.01269  0.65240  3.73900 
> summary(db$c, digits = 4)
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
0.0002346 0.2416000 0.4813000 0.4938000 0.7492000 0.9992000 
> 
> 
> ################################################################################
> ## Taken from the vignette
> 
> file.remove("mydb")
[1] TRUE
> 
> dbCreate("mydb")
[1] TRUE
> db <- dbInit("mydb")
> 
> set.seed(100)
> 
> dbInsert(db, "a", rnorm(100))
> value <- dbFetch(db, "a")
> mean(value)
[1] 0.002912563
> 
> dbInsert(db, "b", 123)
> dbDelete(db, "a")
> dbList(db)
[1] "b"
> dbExists(db, "a")
[1] FALSE
> 
> file.remove("mydb")
[1] TRUE
> 
> ################################################################################
> ## Check queue
> 
> db <- createQ("testq")
> push(db, 1)
> push(db, 2)
> top(db)
[1] 1
> 
> pop(db)
[1] 1
> top(db)
[1] 2
> 
> proc.time()
   user  system elapsed 
  0.215   0.064   0.269 

Generated by dwww version 1.15 on Sun Jun 30 10:53:23 CEST 2024.