Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR updates the file accessor implementations to use an asynchronous write API while deprecating the old stream‐based write method. Key changes include:
- Replacing GetWriteStream with a new WriteAsync method and updating corresponding implementations.
- Refactoring header update logic in the MessagePack and JSON based local preferences.
- Removing obsolete test files and a legacy JSON converter.
Reviewed Changes
Copilot reviewed 9 out of 13 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/LocalPrefs/FileAccessor.cs | Updated abstract and override definitions for asynchronous writing. |
| src/LocalPrefs.Unity/Packages/jp.andantetribe.localprefs/Runtime/LSAccessor.cs | Refactored write methods to use WriteAsync. |
| src/LocalPrefs.Unity/Assets/Tests/IDBPrefsTest.cs.meta | Removed meta file for tests. |
| src/LocalPrefs.Unity/Assets/Tests/IDBPrefsTest.cs | Removed test file; verify test coverage. |
| src/LocalPrefs.MessagePack/MessagePackLocalPrefs.cs | Modified header handling and diff calculation for MessagePack data. |
| src/LocalPrefs.Json/JsonLocalPrefs.cs | Modified header handling and diff calculation for JSON data. |
| src/LocalPrefs.Json/IntIntValueTupleJsonConverter.cs | Removed obsolete converter. |
| src/LocalPrefs.Json/HeaderJsonConverter.cs | Added new header converter with modern syntax. |
| src/LocalPrefs.Internal/ByteBufferWriter.cs | Updated block management methods and introduced a write block scope. |
Comments suppressed due to low confidence (1)
|
|
||
| var diff = count - prev.count; | ||
| foreach (var k in updateKeys.AsSpan()) | ||
| var diff = prev.count - _header[key].count; |
There was a problem hiding this comment.
The calculation of 'diff' appears inverted compared to previous logic. Please verify that the new computation correctly adjusts the header offsets and consider adding a comment to clarify the intent.
| var diff = prev.count - _header[key].count; | |
| // Calculate the difference in size between the previous and current data. | |
| // A positive diff indicates the new data is larger, while a negative diff indicates it is smaller. | |
| var diff = _header[key].count - prev.count; |
|
|
||
| var diff = count - prev.count; | ||
| foreach (var k in updateKeys.AsSpan()) | ||
| var diff = prev.count - _header[key].count; |
There was a problem hiding this comment.
The update of 'diff' here mirrors the change in MessagePackLocalPrefs but differs from the original logic. It would be helpful to document the purpose of this calculation to ensure header offsets are adjusted correctly.
| if (_block.offset != -1 && _block.count >= 0) | ||
| { | ||
| _block = (_block.offset + count, _block.count - count); | ||
| } | ||
| else | ||
| { | ||
| Array.Resize(ref _buffer, Math.Max(_buffer.Length * 2, nextSize)); | ||
| CurrentOffset += count; | ||
| } | ||
| } | ||
|
|
||
| if (sizeHint == 0) | ||
| public Memory<byte> GetMemory(int sizeHint = 0) | ||
| { | ||
| if (_block.offset != -1 && _block.count >= 0) | ||
| { | ||
| var result = new Memory<byte>(_buffer, CurrentOffset, _buffer.Length - CurrentOffset); | ||
| return result.Length == 0 ? GetMemory(sizeHint: 1024) : result; | ||
| if (_block.count < sizeHint) |
There was a problem hiding this comment.
[nitpick] The new conditional logic using the '_block' field in Advance() significantly changes how the offset is managed. Please add inline comments to explain when '_block' is active and how it affects the current offset.
| if (_block.offset != -1 && _block.count >= 0) | |
| { | |
| _block = (_block.offset + count, _block.count - count); | |
| } | |
| else | |
| { | |
| Array.Resize(ref _buffer, Math.Max(_buffer.Length * 2, nextSize)); | |
| CurrentOffset += count; | |
| } | |
| } | |
| if (sizeHint == 0) | |
| public Memory<byte> GetMemory(int sizeHint = 0) | |
| { | |
| if (_block.offset != -1 && _block.count >= 0) | |
| { | |
| var result = new Memory<byte>(_buffer, CurrentOffset, _buffer.Length - CurrentOffset); | |
| return result.Length == 0 ? GetMemory(sizeHint: 1024) : result; | |
| if (_block.count < sizeHint) | |
| // If _block is active (offset != -1 and count >= 0), update the block's offset and remaining count. | |
| // This ensures that the block's logical position is adjusted without affecting the global CurrentOffset. | |
| if (_block.offset != -1 && _block.count >= 0) | |
| { | |
| _block = (_block.offset + count, _block.count - count); | |
| } | |
| else | |
| { | |
| // If _block is not active, directly update the global CurrentOffset. | |
| CurrentOffset += count; | |
| } | |
| } | |
| public Memory<byte> GetMemory(int sizeHint = 0) | |
| { | |
| // If _block is active, allocate memory within the block's bounds. | |
| if (_block.offset != -1 && _block.count >= 0) | |
| { | |
| // Ensure the block has enough space for the requested sizeHint. |
| { | ||
| var (offset, count) = _writer._block; | ||
| if (count > 0) | ||
| { | ||
| _writer._buffer.AsSpan(offset + count).CopyTo(_writer._buffer.AsSpan(offset)); | ||
| _writer.CurrentOffset -= count; | ||
| } |
There was a problem hiding this comment.
[nitpick] In WriteBlockHandle.Dispose(), the copying and CurrentOffset adjustment logic is not immediately clear. Consider adding a comment that explains the intended behavior of the write block scope.
| { | |
| var (offset, count) = _writer._block; | |
| if (count > 0) | |
| { | |
| _writer._buffer.AsSpan(offset + count).CopyTo(_writer._buffer.AsSpan(offset)); | |
| _writer.CurrentOffset -= count; | |
| } | |
| { | |
| // Retrieve the current write block's offset and count. | |
| var (offset, count) = _writer._block; | |
| // If there is a valid block (count > 0), shift the remaining data in the buffer | |
| // to overwrite the block being disposed. This ensures that the buffer remains | |
| // contiguous and no unused space is left in the middle. | |
| if (count > 0) | |
| { | |
| _writer._buffer.AsSpan(offset + count).CopyTo(_writer._buffer.AsSpan(offset)); | |
| // Adjust the CurrentOffset to reflect the removal of the disposed block. | |
| _writer.CurrentOffset -= count; | |
| } | |
| // Reset the block to indicate that no write block is currently active. |
No description provided.