forked from google/android-fhir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.kt
149 lines (128 loc) · 5.33 KB
/
Database.kt
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
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.fhir.db
import com.google.android.fhir.db.impl.dao.IndexedIdAndResource
import com.google.android.fhir.LocalChange
import com.google.android.fhir.db.impl.dao.LocalChangeToken
import com.google.android.fhir.db.impl.entities.LocalChangeEntity
import com.google.android.fhir.db.impl.entities.ResourceEntity
import com.google.android.fhir.search.SearchQuery
import java.time.Instant
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType
/** The interface for the FHIR resource database. */
internal interface Database {
/**
* Inserts a list of local `resources` into the FHIR resource database. If any of the resources
* already exists, it will be overwritten.
*
* @param <R> The resource type
* @return the logical IDs of the newly created resources.
*/
suspend fun <R : Resource> insert(vararg resource: R): List<String>
/**
* Inserts a list of remote `resources` into the FHIR resource database. If any of the resources
* already exists, it will be overwritten.
*
* @param <R> The resource type
*/
suspend fun <R : Resource> insertRemote(vararg resource: R)
/**
* Updates the `resource` in the FHIR resource database. If the resource does not already exist,
* then it will not be created.
*
* @param <R> The resource type
*/
suspend fun update(vararg resources: Resource)
/** Updates the `resource` meta in the FHIR resource database. */
suspend fun updateVersionIdAndLastUpdated(
resourceId: String,
resourceType: ResourceType,
versionId: String,
lastUpdated: Instant
)
/**
* Selects the FHIR resource of type `clazz` with `id`.
*
* @param <R> The resource type
* @throws ResourceNotFoundException if the resource is not found in the database
*/
@Throws(ResourceNotFoundException::class)
suspend fun select(type: ResourceType, id: String): Resource
/**
* Selects the saved `ResourceEntity` of type `clazz` with `id`.
*
* @param <R> The resource type
* @throws ResourceNotFoundException if the resource is not found in the database
*/
@Throws(ResourceNotFoundException::class)
suspend fun selectEntity(type: ResourceType, id: String): ResourceEntity
/**
* Insert resources that were synchronised.
*
* @param syncedResources The synced resource
*/
suspend fun insertSyncedResources(resources: List<Resource>)
/**
* Deletes the FHIR resource of type `clazz` with `id`.
*
* @param <R> The resource type
*/
suspend fun delete(type: ResourceType, id: String)
suspend fun <R : Resource> search(query: SearchQuery): List<R>
suspend fun searchReferencedResources(query: SearchQuery): List<IndexedIdAndResource>
suspend fun count(query: SearchQuery): Long
/**
* Retrieves all [LocalChange]s for all [Resource]s, which can be used to update the remote FHIR
* server.
*/
suspend fun getAllLocalChanges(): List<LocalChange>
/** Remove the [LocalChangeEntity] s with given ids. Call this after a successful sync. */
suspend fun deleteUpdates(token: LocalChangeToken)
/** Remove the [LocalChangeEntity] s with matching resource ids. */
suspend fun deleteUpdates(resources: List<Resource>)
/** Runs the block as a database transaction. */
suspend fun withTransaction(block: suspend () -> Unit)
/** Closes the database connection. */
fun close()
/**
* Clears all database tables without resetting the auto-increment value generated by
* PrimaryKey.autoGenerate. WARNING: This will clear the database and it's not recoverable.
*/
suspend fun clearDatabase()
/**
* Retrieve a list of [LocalChange] for [Resource] with given type and id, which can be used to
* purge resource from database. If there is no local change for given [resourceType] and
* [Resource.id], return an empty list.
* @param type The [ResourceType]
* @param id The resource id [Resource.id]
* @return [List]<[LocalChange]> A list of local changes for given [resourceType] and
* [Resource.id] . If there is no local change for given [resourceType] and [Resource.id], return
* empty list.
*/
suspend fun getLocalChanges(type: ResourceType, id: String): List<LocalChange>
/**
* Purge resource from database based on resource type and id without any deletion of data from
* the server.
* @param type The [ResourceType]
* @param id The resource id [Resource.id]
* @param isLocalPurge default value is false here resource will not be deleted from
* LocalChangeEntity table but it will throw IllegalStateException("Resource has local changes
* either sync with server or FORCE_PURGE required") if local change exists. If true this API will
* delete resource entry from LocalChangeEntity table.
*/
suspend fun purge(type: ResourceType, id: String, forcePurge: Boolean = false)
}