autobee 2.5.1 → 2.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +15 -7
  2. package/lib/system.js +10 -5
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -1165,17 +1165,25 @@ module.exports = class Autobee extends ReadyResource {
1165
1165
 
1166
1166
  const { writer: w, batch } = next
1167
1167
 
1168
- if (w.isAdded || (w.isRemoved && w.hasReferrals())) {
1169
- await this._processBatch(batch)
1170
- w.notify(batch)
1171
- return true
1172
- }
1168
+ // an optimistic batch from a writer that is not (or no longer) added gets a
1169
+ // shot at apply and is rolled back unless apply adds or acks the writer
1170
+ if (this.optimistic && batch[0].optimistic && (!w.isAdded || w.isRemoved)) {
1171
+ if (await this._optimisticBatch(batch)) {
1172
+ w.notify(batch)
1173
+ return true
1174
+ }
1173
1175
 
1174
- if (this.optimistic && !w.isRemoved && batch[0].optimistic) {
1175
- if (!(await this._optimisticBatch(batch))) {
1176
+ if (!(w.isRemoved && w.hasReferrals())) {
1176
1177
  w.removePending()
1177
1178
  return true
1178
1179
  }
1180
+
1181
+ // declined, but other writers link to it: fall through and process it
1182
+ // like any other node from a removed writer
1183
+ }
1184
+
1185
+ if (w.isAdded || (w.isRemoved && w.hasReferrals())) {
1186
+ await this._processBatch(batch)
1179
1187
  w.notify(batch)
1180
1188
  return true
1181
1189
  }
package/lib/system.js CHANGED
@@ -90,10 +90,12 @@ module.exports = class Systembee {
90
90
  if (length === 0) {
91
91
  length = info ? info.length : 0
92
92
  }
93
- // acked but never granted: keep the op (record its length) but record the
94
- // writer as removed so nothing else from it is applied
95
- const isRemoved = !info || info.isAdded === false
96
- this.update(key, length, -1, -1, false, false, isRemoved, -1, null)
93
+ // acked but never granted (unknown, ack-only or removed): keep the op
94
+ // (record its length) but record the writer as removed - it is not writable
95
+ // and only its optimistic ops reach apply, each rolled back unless acked again
96
+ const w = this.writers.get(b4a.toString(key, 'hex'))
97
+ const granted = w ? w.added : info !== null && info.isAdded !== false && !info.isRemoved
98
+ this.update(key, length, -1, -1, false, false, !granted, -1, null)
97
99
  }
98
100
 
99
101
  async removeWriter(key, { length = 0 } = {}) {
@@ -233,11 +235,14 @@ module.exports = class Systembee {
233
235
  }
234
236
 
235
237
  async canApply(key, optimistic) {
238
+ // an optimistic node always reaches apply, whether its writer is unknown,
239
+ // acked or removed - the caller rolls it back unless apply accepts it
240
+ if (optimistic) return true
236
241
  const id = b4a.toString(key, 'hex')
237
242
  const w = this.writers.get(id)
238
243
  if (w) return w.added
239
244
  const info = await this.get(key)
240
- return info ? !info.isRemoved : !!optimistic
245
+ return info ? !info.isRemoved : false
241
246
  }
242
247
 
243
248
  snapshot(head = null) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autobee",
3
- "version": "2.5.1",
3
+ "version": "2.5.2",
4
4
  "description": "Bee based autobase",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Holepunch Inc.",