Skip to content

Feature/fix fileaccessor#17

Merged
kochounoyume merged 6 commits intomainfrom
feature/fix-fileaccessor
Jun 27, 2025
Merged

Feature/fix fileaccessor#17
kochounoyume merged 6 commits intomainfrom
feature/fix-fileaccessor

Conversation

@kochounoyume
Copy link
Member

No description provided.

@kochounoyume kochounoyume self-assigned this Jun 27, 2025
Copilot AI review requested due to automatic review settings June 27, 2025 19:11
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link

Copilot AI Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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;

Copilot uses AI. Check for mistakes.
Comment on lines 108 to +109

var diff = count - prev.count;
foreach (var k in updateKeys.AsSpan())
var diff = prev.count - _header[key].count;
Copy link

Copilot AI Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +16 to +30
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)
Copy link

Copilot AI Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Suggested change
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.

Copilot uses AI. Check for mistakes.
Comment on lines +94 to +100
{
var (offset, count) = _writer._block;
if (count > 0)
{
_writer._buffer.AsSpan(offset + count).CopyTo(_writer._buffer.AsSpan(offset));
_writer.CurrentOffset -= count;
}
Copy link

Copilot AI Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Suggested change
{
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.

Copilot uses AI. Check for mistakes.
@kochounoyume kochounoyume merged commit ff5e6fc into main Jun 27, 2025
4 checks passed
@kochounoyume kochounoyume deleted the feature/fix-fileaccessor branch June 27, 2025 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants