Skip to content
This repository was archived by the owner on Sep 30, 2023. It is now read-only.

Commit f252b3a

Browse files
committed
feat: adjustable in-memory entry storage
1 parent 8f9e922 commit f252b3a

24 files changed

+601
-380
lines changed

API.md

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const Log = require('ipfs-log')
1010

1111
### Constructor
1212

13-
#### new Log(ipfs, identity, [{ logId, access, entries, heads, clock, sortFn }])
13+
#### new Log(ipfs, identity, [{ logId, access, entries, heads, clock, sortFn, hashIndex }])
1414

1515
Create a log. Each log gets a unique ID, which can be passed in the `options` as `logId`. Returns a `Log` instance.
1616

@@ -32,15 +32,6 @@ console.log(log.id)
3232

3333
Returns the ID of the log.
3434

35-
#### values
36-
37-
Returns an `Array` of [entries](https://github.com/orbitdb/ipfs-log/blob/master/src/entry.js) in the log. The values are in linearized order according to their [Lamport clocks](https://en.wikipedia.org/wiki/Lamport_timestamps).
38-
39-
```javascript
40-
const values = log.values
41-
// TODO: output example
42-
```
43-
4435
#### length
4536

4637
Returns the number of entries in the log.
@@ -58,16 +49,25 @@ const heads = log.heads
5849
// TODO: output example
5950
```
6051

61-
#### tails
52+
### Methods
53+
54+
#### values
6255

63-
Return the tails of the log. Tails are the entries that reference other entries that are not in the log.
56+
Returns a *Promise* that resolves to an `Array` of [entries](https://github.com/orbitdb/ipfs-log/blob/master/src/entry.js) in the log. The values are in linearized order according to their [Lamport clocks](https://en.wikipedia.org/wiki/Lamport_timestamps).
6457

6558
```javascript
66-
const tails = log.tails
59+
const values = await log.values()
6760
// TODO: output example
6861
```
6962

70-
### Methods
63+
#### tails
64+
65+
Returns a *Promise* that resolves to the tails of the log. Tails are the entries that reference other entries that are not in the log.
66+
67+
```javascript
68+
const tails = await log.tails()
69+
// TODO: output example
70+
```
7171

7272
#### append(data)
7373

@@ -115,6 +115,18 @@ console.log(log.values)
115115
// ]
116116
```
117117

118+
#### get(hash)
119+
120+
Returns a *Promise* that resolves to an entry if it exists, otherwise undefined.
121+
122+
```javascript
123+
const entry = await get(hash)
124+
```
125+
126+
#### has(hash)
127+
128+
Returns true if the hash exists in the log, otherwise false.
129+
118130
#### join(log, [length])
119131

120132
Join the log with another log. Returns a Promise that resolves to a `Log` instance. The size of the joined log can be specified by giving `length` argument.
@@ -158,7 +170,7 @@ const buffer = log1.toBuffer()
158170

159171
### toString
160172

161-
Returns the log values as a nicely formatted string.
173+
Returns a *Promise* that resolves to the log values as a nicely formatted string.
162174

163175
```javascript
164176
console.log(log.toString())

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,14 @@ See [API Documentation](https://github.com/orbitdb/ipfs-log/tree/master/API.md)
137137
- [new Log(ipfs, identity, [{ logId, access, entries, heads, clock, sortFn }])](https://github.com/orbitdb/ipfs-log/tree/master/API.md##new-log-ipfs-id)
138138
- [Properties](https://github.com/orbitdb/ipfs-log/tree/master/API.md##properties)
139139
- [id](https://github.com/orbitdb/ipfs-log/tree/master/API.md##id)
140-
- [values](https://github.com/orbitdb/ipfs-log/tree/master/API.md##values)
141140
- [length](https://github.com/orbitdb/ipfs-log/tree/master/API.md##length)
142141
- [clock](https://github.com/orbitdb/ipfs-log/tree/master/API.md##length)
143142
- [heads](https://github.com/orbitdb/ipfs-log/tree/master/API.md##heads)
144-
- [tails](https://github.com/orbitdb/ipfs-log/tree/master/API.md##tails)
145143
- [Methods](https://github.com/orbitdb/ipfs-log/tree/master/API.md##methods)
144+
- [values](https://github.com/orbitdb/ipfs-log/tree/master/API.md##values)
145+
- [tails](https://github.com/orbitdb/ipfs-log/tree/master/API.md##tails)
146+
- [get(hash)](https://github.com/orbitdb/ipfs-log/tree/master/API.md##get)
147+
- [has(hash)](https://github.com/orbitdb/ipfs-log/tree/master/API.md##has)
146148
- [append(data)](https://github.com/orbitdb/ipfs-log/tree/master/API.md##appenddata)
147149
- [join(log)](https://github.com/orbitdb/ipfs-log/tree/master/API.md##joinlog)
148150
- [toMultihash()](https://github.com/orbitdb/ipfs-log/tree/master/API.md##tomultihash)

benchmarks/tail-hashes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const base = {
1515
return { log, ipfsd }
1616
},
1717
cycle: async function ({ log }) {
18-
return log.tailHashes
18+
return log.tailHashes()
1919
},
2020
teardown: async function ({ ipfsd }) {
2121
await stopIpfs(ipfsd)

benchmarks/tails.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const base = {
1515
return { log, ipfsd }
1616
},
1717
cycle: async function ({ log }) {
18-
return log.tails
18+
return log.tails()
1919
},
2020
teardown: async function ({ ipfsd }) {
2121
await stopIpfs(ipfsd)

benchmarks/utils/create-log.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const createLog = async (ipfs, logId) => {
1717
const access = new AccessController()
1818
const keystore = new Keystore(store)
1919
const identity = await IdentityProvider.createIdentity({ id: 'userA', keystore })
20-
const log = new Log(ipfs, identity, { logId: 'A', access })
20+
const log = new Log(ipfs, identity, { logId: 'A', access, cacheSize: 5 })
2121
return { log, access, identity }
2222
}
2323

benchmarks/values.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const base = {
1515
return { log, ipfsd }
1616
},
1717
cycle: async function ({ log }) {
18-
return log.values
18+
return log.values()
1919
},
2020
teardown: async function ({ ipfsd }) {
2121
await stopIpfs(ipfsd)

lib/es5/entry-index.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,45 @@ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/cl
66

77
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
88

9+
var LRU = require('lru-cache');
10+
911
var EntryIndex = /*#__PURE__*/function () {
1012
function EntryIndex() {
1113
var entries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
14+
var cacheSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Infinity;
1215
(0, _classCallCheck2["default"])(this, EntryIndex);
13-
this._cache = entries;
16+
this._cache = new LRU({
17+
max: cacheSize
18+
});
19+
this.add(entries);
1420
}
1521

1622
(0, _createClass2["default"])(EntryIndex, [{
1723
key: "set",
1824
value: function set(k, v) {
19-
this._cache[k] = v;
25+
this._cache.set(k, v);
2026
}
2127
}, {
2228
key: "get",
2329
value: function get(k) {
24-
return this._cache[k];
30+
return this._cache.get(k);
2531
}
2632
}, {
2733
key: "delete",
2834
value: function _delete(k) {
29-
return delete this._cache[k];
35+
this._cache.del(k);
3036
}
3137
}, {
3238
key: "add",
33-
value: function add(newItems) {
34-
this._cache = Object.assign(this._cache, newItems);
39+
value: function add(items) {
40+
for (var k in items) {
41+
this._cache.set(k, items[k]);
42+
}
3543
}
3644
}, {
3745
key: "length",
3846
get: function get() {
39-
return Object.values(this._cache).length;
47+
return this._cache.length;
4048
}
4149
}]);
4250
return EntryIndex;

lib/es5/log-io.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ var LogIO = /*#__PURE__*/function () {
5353
var _toMultihash = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(ipfs, log) {
5454
var _ref,
5555
format,
56+
values,
5657
_args = arguments;
5758

5859
return _regenerator["default"].wrap(function _callee$(_context) {
@@ -78,20 +79,25 @@ var LogIO = /*#__PURE__*/function () {
7879

7980
case 5:
8081
if (!isDefined(format)) format = 'dag-cbor';
82+
_context.next = 8;
83+
return log.values();
8184

82-
if (!(log.values.length < 1)) {
83-
_context.next = 8;
85+
case 8:
86+
values = _context.sent;
87+
88+
if (!(values.length < 1)) {
89+
_context.next = 11;
8490
break;
8591
}
8692

8793
throw new Error('Can\'t serialize an empty log');
8894

89-
case 8:
95+
case 11:
9096
return _context.abrupt("return", io.write(ipfs, format, log.toJSON(), {
9197
links: IPLD_LINKS
9298
}));
9399

94-
case 9:
100+
case 12:
95101
case "end":
96102
return _context.stop();
97103
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
],
2121
"dependencies": {
2222
"json-stringify-deterministic": "^1.0.1",
23+
"lru-cache": "^6.0.0",
2324
"multihashing-async": "^2.0.1",
2425
"orbit-db-identity-provider": "^0.3.1",
2526
"orbit-db-io": "^1.0.1",

src/entry-index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
'use strict'
22

3+
const LRU = require('lru-cache')
4+
35
class EntryIndex {
4-
constructor (entries = {}) {
5-
this._cache = entries
6+
constructor (entries = {}, cacheSize = Infinity) {
7+
this._cache = new LRU({ max: cacheSize })
8+
this.add(entries)
69
}
710

811
set (k, v) {
9-
this._cache[k] = v
12+
this._cache.set(k, v)
1013
}
1114

1215
get (k) {
13-
return this._cache[k]
16+
return this._cache.get(k)
1417
}
1518

1619
delete (k) {
17-
return delete this._cache[k]
20+
this._cache.del(k)
1821
}
1922

20-
add (newItems) {
21-
this._cache = Object.assign(this._cache, newItems)
23+
add (items) {
24+
for (const k in items) {
25+
this._cache.set(k, items[k])
26+
}
2227
}
2328

2429
get length () {
25-
return Object.values(this._cache).length
30+
return this._cache.length
2631
}
2732
}
2833

0 commit comments

Comments
 (0)