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

Commit

Permalink
feat: adjustable in-memory entry storage
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakia committed Apr 10, 2021
1 parent 4a44cca commit ad11296
Show file tree
Hide file tree
Showing 23 changed files with 1,331 additions and 718 deletions.
42 changes: 27 additions & 15 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Log = require('ipfs-log')

### Constructor

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

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

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

Returns the ID of the log.

#### values

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).

```javascript
const values = log.values
// TODO: output example
```

#### length

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

#### tails
### Methods

#### values

Return the tails of the log. Tails are the entries that reference other entries that are not in the log.
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).

```javascript
const tails = log.tails
const values = await log.values()
// TODO: output example
```

### Methods
#### tails

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.

```javascript
const tails = await log.tails()
// TODO: output example
```

#### append(data)

Expand Down Expand Up @@ -115,6 +115,18 @@ console.log(log.values)
// ]
```

#### get(hash)

Returns a *Promise* that resolves to an entry if it exists, otherwise undefined.

```javascript
const entry = await get(hash)
```

#### has(hash)

Returns true if the hash exists in the log, otherwise false.

#### join(log, [length])

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.
Expand Down Expand Up @@ -158,7 +170,7 @@ const buffer = log1.toBuffer()

### toString

Returns the log values as a nicely formatted string.
Returns a *Promise* that resolves to the log values as a nicely formatted string.

```javascript
console.log(log.toString())
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,14 @@ See [API Documentation](https://github.com/orbitdb/ipfs-log/tree/master/API.md)
- [new Log(ipfs, identity, [{ logId, access, entries, heads, clock, sortFn }])](https://github.com/orbitdb/ipfs-log/tree/master/API.md##new-log-ipfs-id)
- [Properties](https://github.com/orbitdb/ipfs-log/tree/master/API.md##properties)
- [id](https://github.com/orbitdb/ipfs-log/tree/master/API.md##id)
- [values](https://github.com/orbitdb/ipfs-log/tree/master/API.md##values)
- [length](https://github.com/orbitdb/ipfs-log/tree/master/API.md##length)
- [clock](https://github.com/orbitdb/ipfs-log/tree/master/API.md##length)
- [heads](https://github.com/orbitdb/ipfs-log/tree/master/API.md##heads)
- [tails](https://github.com/orbitdb/ipfs-log/tree/master/API.md##tails)
- [Methods](https://github.com/orbitdb/ipfs-log/tree/master/API.md##methods)
- [values](https://github.com/orbitdb/ipfs-log/tree/master/API.md##values)
- [tails](https://github.com/orbitdb/ipfs-log/tree/master/API.md##tails)
- [get(hash)](https://github.com/orbitdb/ipfs-log/tree/master/API.md##get)
- [has(hash)](https://github.com/orbitdb/ipfs-log/tree/master/API.md##has)
- [append(data)](https://github.com/orbitdb/ipfs-log/tree/master/API.md##appenddata)
- [join(log)](https://github.com/orbitdb/ipfs-log/tree/master/API.md##joinlog)
- [toMultihash()](https://github.com/orbitdb/ipfs-log/tree/master/API.md##tomultihash)
Expand Down
10 changes: 5 additions & 5 deletions dist/ipfslog.min.js

Large diffs are not rendered by default.

22 changes: 15 additions & 7 deletions lib/es5/entry-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,45 @@ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/cl

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

var LRU = require('lru-cache');

var EntryIndex = /*#__PURE__*/function () {
function EntryIndex() {
var entries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var cacheSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Infinity;
(0, _classCallCheck2["default"])(this, EntryIndex);
this._cache = entries;
this._cache = new LRU({
max: cacheSize
});
this.add(entries);
}

(0, _createClass2["default"])(EntryIndex, [{
key: "set",
value: function set(k, v) {
this._cache[k] = v;
this._cache.set(k, v);
}
}, {
key: "get",
value: function get(k) {
return this._cache[k];
return this._cache.get(k);
}
}, {
key: "delete",
value: function _delete(k) {
return delete this._cache[k];
this._cache.del(k);
}
}, {
key: "add",
value: function add(newItems) {
this._cache = Object.assign(this._cache, newItems);
value: function add(items) {
for (var k in items) {
this._cache.set(k, items[k]);
}
}
}, {
key: "length",
get: function get() {
return Object.values(this._cache).length;
return this._cache.length;
}
}]);
return EntryIndex;
Expand Down
14 changes: 10 additions & 4 deletions lib/es5/log-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var LogIO = /*#__PURE__*/function () {
var _toMultihash = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(ipfs, log) {
var _ref,
format,
values,
_args = arguments;

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

case 5:
if (!isDefined(format)) format = 'dag-cbor';
_context.next = 8;
return log.values();

if (!(log.values.length < 1)) {
_context.next = 8;
case 8:
values = _context.sent;

if (!(values.length < 1)) {
_context.next = 11;
break;
}

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

case 8:
case 11:
return _context.abrupt("return", io.write(ipfs, format, log.toJSON(), {
links: IPLD_LINKS
}));

case 9:
case 12:
case "end":
return _context.stop();
}
Expand Down
Loading

0 comments on commit ad11296

Please sign in to comment.