File: db-utils.R

package info (click to toggle)
r-bioc-annotationhub 3.14.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 592 kB
  • sloc: makefile: 2
file content (157 lines) | stat: -rw-r--r-- 4,649 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
### =========================================================================
### db helpers for Hub objects
### -------------------------------------------------------------------------
###

.db_open <- function(path) {
    tryCatch({
        conn <- dbFileConnect(path)
    }, error=function(err) {
        stop("failed to connect to local data base",
             "\n  database: ", sQuote(path),
             "\n  reason: ", conditionMessage(err),
             call.=FALSE)
    })
    conn
}

.db_close <- function(conn) {
    if (!is.null(conn))
        if (RSQLite::dbIsValid(conn))
            dbDisconnect(conn)
    invisible(conn)
}

.db_query <- function(path, query) {
    if (is.character(path)) {
        path <- .db_open(path)
        on.exit(.db_close(path))
    }
    dbGetQuery(path, query)
}

.db_uid0 <- function(path, .date, localHub){
    tryCatch({
        uid <- .uid0(path, .date, localHub)
        sort(uid)
    }, error=function(err) {
        stop("failed to connect to local data base",
             "\n  database: ", sQuote(path),
             "\n  reason: ", conditionMessage(err),
             call.=FALSE)
    })
}


setMethod("dbconn", "Hub",
    function(x) .db_open(dbfile(x))
)

setMethod("dbfile", "Hub",
    function(x) x@.db_path
)

.db_uid <- function(x) slot(x, ".db_uid")
`.db_uid<-` <- function(x, ..., value)
{
    bad <- value[!value %in% .db_uid(x)]
    if (any(bad))
        stop("invalid subscripts: ",
             paste(sQuote(S4Vectors:::selectSome(bad)), collapse=", "))
    slot(x, ".db_uid") <- value
    x
}

.db_clean_index <- function(x) {

    bfc <- .get_cache(x)
    index_name <- paste0(tolower(as.character(class(x))),
                         ".index.rds")
    bfcremove(bfc, rids=as.data.frame(bfcquery(bfc, index_name))$rid)
}

.db_create_index <- function(x) {

    bfc <- .get_cache(x)
    index_name <- paste0(tolower(as.character(class(x))),
                                ".index.rds")
    res <- bfcquery(bfc, index_name,
                    field="rname", exact=TRUE)
    cnt <- bfccount(res)
    rid <- res %>% collect(Inf) %>% `[[`("rid")

    if (cnt > 1){
        msg <- paste0("Corrupt Cache: index file",
                             "\n  cache: ", bfccache(bfc),
                             "\n  multiple entries for filename: ", index_name,
                             "\n  Attempting to clean cache and regenerate"
                             )
        warning(msg, call.=FALSE)
        .db_clean_index(x)
        res <- bfcquery(bfc, index_name,
                    field="rname", exact=TRUE)
        cnt <- bfccount(res)
        rid <- res %>% collect(Inf) %>% `[[`("rid")
    }
    
    if (cnt > 1){
        stop("Corrupt Cache: index file",
             "\n  See AnnotationHub's TroubleshootingTheHubs vignette section on corrupt cache",
             "\n  cache: ", bfccache(bfc),
             "\n  filename: ", index_name,
             call.=FALSE)
    } else {

        if (cnt == 1){
            index_path <- bfcpath(bfc, rids=rid)
            if (file.exists(index_path)) {
                if (file.mtime(index_path) > file.mtime(dbfile(x)) &&
                    length(x) == length(readRDS(index_path)))
                    return(index_path)
                }
            }
        }

        tryCatch({
            tbl <- .resource_table(x)
            tbl <- setNames(do.call("paste", c(tbl, sep="\r")), rownames(tbl))
            index_path <- ifelse(cnt == 0,
                                 bfcnew(bfc, rname=index_name, ext="_hub_index.rds"),
                                 bfcpath(bfc, rids=rid))
            saveRDS(tbl, unname(index_path))
        }, error=function(err) {
            stop("failed to create index",
                 "\n  hubCache(): ", hubCache(x),
                 "\n  reason: ", conditionMessage(err))
        })
    unname(index_path)
}

.db_index_file <- function(x){
    bfc <- .get_cache(x)
    index_name <- paste0(tolower(as.character(class(x))),
                                ".index.rds")
    res <- bfcquery(bfc, index_name,
                    field="rname", exact=TRUE)
    cnt <- bfccount(res)
    rid <- res %>% collect(Inf) %>% `[[`("rid")
    if (cnt != 1){
        .db_create_index(x)       
    } else {
        unname(bfcpath(bfc, rids=rid))
    }
}

.db_index_load <- function(x)
    readRDS(.db_index_file(x))[names(x)]

.db_index <- function(x) slot(x, ".db_index")
`.db_index<-` <- function(x, ..., value)
{
    if (length(value) > 1L)
        stop("'value' must be length 1")
    if (!is(value, "character"))
        stop("'value' must be a character")
    slot(x, ".db_index") <- value
    x
}