-
-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathdatabase.js
More file actions
253 lines (230 loc) · 7.37 KB
/
Copy pathdatabase.js
File metadata and controls
253 lines (230 loc) · 7.37 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* @module Database
* @description
* Database is the base class for OrbitDB data stores and handles all lower
* level add operations and database sync-ing using IPFS.
*/
import { EventEmitter } from 'events'
import PQueue from 'p-queue'
import Sync from './sync.js'
import { Log } from './oplog/index.js'
import { ComposedStorage, LRUStorage, IPFSBlockStorage, LevelStorage } from './storage/index.js'
import pathJoin from './utils/path-join.js'
const defaultReferencesCount = 16
const defaultCacheSize = 1000
/**
* Creates an instance of Database.
* @function
* @param {Object} params One or more parameters for configuring Database.
* @param {IPFS} params.ipfs An IPFS instance.
* @param {Identity} [params.identity] An Identity instance.
* @param {string} [params.address] The address of the database.
* @param {string} [params.name] The name of the database.
* @param {module:AccessControllers} [params.access] An AccessController
* instance.
* @param {string} [params.directory] A location for storing Database-related
* data. Defaults to ./orbitdb/[params.address].
* @param {*} [params.meta={}] The database's metadata.
* @param {module:Storage} [params.headsStorage] A compatible storage
* instance for storing log heads. Defaults to ComposedStorage.
* @param {module:Storage} [params.entryStorage] A compatible storage instance
* for storing log entries. Defaults to ComposedStorage.
* @param {module:Storage} [params.indexStorage] A compatible storage
* instance for storing an index of log entries. Defaults to ComposedStorage.
* @param {number} [params.referencesCount=16] The maximum distance between
* references to other entries.
* @param {boolean} [params.syncAutomatically=false] If true, sync databases
* automatically. Otherwise, false.
* @param {function} [params.onUpdate] A function callback. Fired when an
* entry is added to the oplog.
* @param {Function} options.encryptFn An encryption function.
* @param {Function} options.decryptFn A decryption function.
* @return {module:Databases~Database} An instance of Database.
* @instance
*/
const Database = async ({ ipfs, identity, address, name, access, directory, meta, headsStorage, entryStorage, indexStorage, referencesCount, syncAutomatically, onUpdate, encryption }) => {
/**
* @namespace module:Databases~Database
* @description The instance returned by {@link module:Database~Database}.
*/
/**
* Event fired when an update occurs.
* @event module:Databases~Database#update
* @param {module:Entry} entry An entry.
* @example
* database.events.on('update', (entry) => ...)
*/
/**
* Event fired when a close occurs.
* @event module:Databases~Database#close
* @example
* database.events.on('close', () => ...)
*/
/**
* Event fired when a drop occurs.
* @event module:Databases~Database#drop
* @example
* database.events.on('drop', () => ...)
*/
/** Events inherited from Sync */
/**
* Event fired when when a peer has connected to the database.
* @event module:Databases~Database#join
* @param {PeerID} peerId PeerID of the peer who connected
* @param {Entry[]} heads An array of Log entries
* @example
* database.events.on('join', (peerID, heads) => ...)
*/
/**
* Event fired when a peer has disconnected from the database.
* @event module:Databases~Database#leave
* @param {PeerID} peerId PeerID of the peer who disconnected
* @example
* database.events.on('leave', (peerID) => ...)
*/
directory = pathJoin(directory || './orbitdb', `./${address}/`)
meta = meta || {}
referencesCount = Number(referencesCount) > -1 ? referencesCount : defaultReferencesCount
entryStorage = entryStorage || await ComposedStorage(
await LRUStorage({ size: defaultCacheSize }),
await IPFSBlockStorage({ ipfs, pin: true })
)
headsStorage = headsStorage || await ComposedStorage(
await LRUStorage({ size: defaultCacheSize }),
await LevelStorage({ path: pathJoin(directory, '/log/_heads/') })
)
indexStorage = indexStorage || await ComposedStorage(
await LRUStorage({ size: defaultCacheSize }),
await LevelStorage({ path: pathJoin(directory, '/log/_index/') })
)
encryption = encryption || {}
const log = await Log(identity, { logId: address, access, entryStorage, headsStorage, indexStorage, encryption })
const events = new EventEmitter()
const queue = new PQueue({ concurrency: 1 })
/**
* Adds an operation to the oplog.
* @function addOperation
* @param {*} op Some operation to add to the oplog.
* @return {string} The hash of the operation.
* @memberof module:Databases~Database
* @instance
* @async
*/
const addOperation = async (op) => {
const task = async () => {
const entry = await log.append(op, { referencesCount })
await sync.add(entry)
if (onUpdate) {
await onUpdate(log, entry)
}
events.emit('update', entry)
return entry.hash
}
const hash = await queue.add(task)
return hash
}
const applyOperation = async (entry) => {
const task = async () => {
try {
if (entry) {
const updated = await log.joinEntry(entry)
if (updated) {
if (onUpdate) {
await onUpdate(log, entry)
}
events.emit('update', entry)
}
}
} catch (e) {
console.error(e)
}
}
await queue.add(task)
}
/**
* Closes the database, stopping sync and closing the oplog.
* @memberof module:Databases~Database
* @instance
* @async
*/
const close = async () => {
await sync.stop()
await queue.onIdle()
await log.close()
if (access && access.close) {
await access.close()
}
events.emit('close')
}
/**
* Drops the database, clearing the oplog.
* @memberof module:Databases~Database
* @instance
* @async
*/
const drop = async () => {
await queue.clear()
await log.clear()
if (access && access.drop) {
await access.drop()
}
events.emit('drop')
}
const sync = await Sync({ ipfs, log, events, onSynced: applyOperation, start: syncAutomatically })
return {
/**
* The address of the database.
* @†ype string
* @memberof module:Databases~Database
* @instance
*/
address,
/**
* The name of the database.
* @†ype string
* @memberof module:Databases~Database
* @instance
*/
name,
identity,
meta,
close,
drop,
addOperation,
/**
* The underlying [operations log]{@link module:Log~Log} of the database.
* @†ype {module:Log~Log}
* @memberof module:Databases~Database
* @instance
*/
log,
/**
* A [sync]{@link module:Sync~Sync} instance of the database.
* @†ype {module:Sync~Sync}
* @memberof module:Databases~Database
* @instance
*/
sync,
/**
* Set of currently connected peers for this Database instance.
* @†ype Set
* @memberof module:Databases~Database
* @instance
*/
peers: sync.peers,
/**
* Event emitter that emits Database changes. See Events section for details.
* @†ype EventEmitter
* @memberof module:Databases~Database
* @instance
*/
events,
/**
* The [access controller]{@link module:AccessControllers} instance of the database.
* @memberof module:Databases~Database
* @instance
*/
access
}
}
export default Database