Skip to content

Commit

Permalink
Workaround of litedb-org/LiteDB#1268
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia authored and longfin committed Aug 6, 2019
1 parent 84006f5 commit c42b7e8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ To be released.
registration in some situations. [[#375]]
- Fixed a bug that `TurnClient` had thrown `KeyNotFoundException` and
`IOException` on startup. [[#377], [#378]]
- Fixed a `LiteDBStore` bug that blocks or transactions had got corrupted
sometimes. [[#386], [LiteDB #1268]]

[#319]: https://github.com/planetarium/libplanet/issues/319
[#343]: https://github.com/planetarium/libplanet/pull/343
Expand All @@ -65,6 +67,8 @@ To be released.
[#375]: https://github.com/planetarium/libplanet/pull/375
[#377]: https://github.com/planetarium/libplanet/issues/377
[#378]: https://github.com/planetarium/libplanet/pull/378
[#386]: https://github.com/planetarium/libplanet/pull/366
[LiteDB #1268]: https://github.com/mbdavid/LiteDB/issues/1268


Version 0.4.1
Expand Down
67 changes: 44 additions & 23 deletions Libplanet/Store/LiteDBStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,16 +233,7 @@ public Transaction<T> GetTransaction<T>(TxId txid)
public void PutTransaction<T>(Transaction<T> tx)
where T : IAction, new()
{
using (var stream = new MemoryStream())
{
byte[] encoded = tx.ToBencodex(true);
stream.Write(encoded, 0, encoded.Length);
stream.Seek(0, SeekOrigin.Begin);
_db.FileStorage.Upload(
TxFileId(tx.Id),
tx.Id.ToHex(),
stream);
}
UploadFile(TxFileId(tx.Id), tx.Id.ToHex(), tx.ToBencodex(true));
}

/// <inheritdoc/>
Expand Down Expand Up @@ -312,16 +303,11 @@ public void PutBlock<T>(Block<T> block)
PutTransaction(tx);
}

using (var stream = new MemoryStream())
{
byte[] encoded = block.ToBencodex(true, false);
stream.Write(encoded, 0, encoded.Length);
stream.Seek(0, SeekOrigin.Begin);
_db.FileStorage.Upload(
BlockFileId(block.Hash),
ByteUtil.Hex(block.Hash.ToByteArray()),
stream);
}
UploadFile(
BlockFileId(block.Hash),
ByteUtil.Hex(block.Hash.ToByteArray()),
block.ToBencodex(true, false)
);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -358,11 +344,11 @@ public void SetBlockStates(
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, states);
stream.Seek(0, SeekOrigin.Begin);
_db.FileStorage.Upload(
UploadFile(
BlockStateFileId(blockHash),
ByteUtil.Hex(blockHash.ToByteArray()),
stream);
stream
);
}
}

Expand Down Expand Up @@ -601,6 +587,41 @@ IEnumerable transactions
.Where(tx => tx != null);
}

// As LiteDB's file storage seems unstable, we need to repeat trying to save a file
// until we ensure it's actually saved.
// https://github.com/mbdavid/LiteDB/issues/1268
private void UploadFile(string fileId, string filename, Stream stream)
{
bool IsFiledUploaded()
{
if (_db.FileStorage.FindById(fileId) is LiteFileInfo file && file.Length > 0)
{
using (LiteFileStream f = file.OpenRead())
{
var buffer = new byte[1];
return f.Read(buffer, 0, 1) > 0;
}
}

return false;
}

do
{
stream.Seek(0, SeekOrigin.Begin);
_db.FileStorage.Upload(fileId, filename, stream);
}
while (!IsFiledUploaded());
}

private void UploadFile(string fileId, string filename, byte[] bytes)
{
using (var stream = new MemoryStream(bytes))
{
UploadFile(fileId, filename, stream);
}
}

private LiteCollection<HashDoc> IndexCollection(string @namespace)
{
return _db.GetCollection<HashDoc>($"{IndexColPrefix}{@namespace}");
Expand Down

0 comments on commit c42b7e8

Please sign in to comment.