/// <inheritdoc /> public Result <StorageNode> Store(string path, string type, Stream data) { var findExisting = Exists(path); StorageNode next; if (findExisting.IsFailure || findExisting.ResultData == null) { next = new StorageNode { CurrentVersion = 1, Id = path, IsDeleted = false }; } else { next = findExisting.ResultData; next.CurrentVersion++; next.IsDeleted = false; } _db.WriteDocument($"file/{path}/{type}/{next.CurrentVersion}", data); UpdateNode(path, next); if (next.CurrentVersion > 2) // clean up versions more than one old { for (int i = next.CurrentVersion - 2; i > 0; i--) { var id = $"file/{path}/{type}/{i}"; _db.Delete(id); } } _db.Flush(); return(Result <StorageNode> .Success(next)); }
/// <inheritdoc /> public Result <Nothing> UpdateNode(string path, StorageNode node) { if (_disposed) { return(Result <Nothing> .Failure(NoDb)); } lock (_storageLock) { try { _writeLock = true; var nodes = _db.GetCollection <StorageNode>("map"); if (nodes == null) { return(Result <Nothing> .Failure(NoDb)); } nodes.Upsert(path, node); return(Result <Nothing> .Success(Nothing.Instance)); } finally { _writeLock = false; } } }
/// <inheritdoc /> public Result <Nothing> UpdateNode(string path, StorageNode node) { if (node == null) { throw new Exception("Tried to update a tile node with null data"); } _db.WriteDocument("map/" + path, node.ToStream()); _db.Flush(); return(Result <Nothing> .Success(Nothing.Instance)); }
/// <inheritdoc /> public Result <StorageNode> Exists(string path) { var ok = _db.Get("map/" + path, out var rawStream); if (!ok || rawStream == null) { return(Result <StorageNode> .Failure(NotFound)); } try { var node = StorageNode.FromStream(rawStream); if (node.IsDeleted) { return(Result <StorageNode> .Failure(NotFound)); } return(Result <StorageNode> .Success(node)); } catch (Exception ex) { return(Result.Failure <StorageNode>(ex)); } }
/// <inheritdoc /> public Result <StorageNode> Store(string path, string type, Stream data) { if (_disposed) { return(Result <StorageNode> .Failure(NoDb)); } lock (_storageLock) try { _writeLock = true; var nodes = _db.GetCollection <StorageNode>("map"); if (nodes == null) { return(Result <StorageNode> .Failure(NoDb)); } var node = nodes.FindById(path); if (node == null) { node = new StorageNode { CurrentVersion = 1, Id = path, IsDeleted = false }; } else { node.CurrentVersion++; node.IsDeleted = false; } if (node.Id == null) { throw new Exception("Loading storage node failed!"); } if (_db.FileStorage == null) { return(Result <StorageNode> .Failure(NoDb)); } var id = $"{path}/{type}/{node.CurrentVersion}"; try { _db.FileStorage.Delete(id); // make sure it's unique _db.FileStorage.Upload(id, type, data); nodes.Upsert(path, node); } catch (Exception ex) { return(Result.Failure <StorageNode>(ex)); } // if version is greater than 2, scan back to delete old versions if (node.CurrentVersion > 2) // clean up versions more than one old { for (int i = node.CurrentVersion - 2; i > 0; i--) { id = $"{path}/{type}/{i}"; if (!_db.FileStorage.Exists(id)) { break; } try { _db.FileStorage.Delete(id); } catch { // Error internal to LiteDB break; } } } return(Result <StorageNode> .Success(node)); } finally { _writeLock = false; } }