Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs-storage: Port BaseStorage abstraction from Kotlin #23

Merged
merged 11 commits into from
Apr 25, 2024
Merged

Conversation

Pushkarm029
Copy link
Collaborator

Resolves #18

Signed-off-by: Pushkar Mishra <[email protected]>
@Pushkarm029 Pushkarm029 requested a review from twitu April 8, 2024 06:16
Signed-off-by: Pushkar Mishra <[email protected]>
@Pushkarm029 Pushkarm029 added the enhancement New feature or request label Apr 8, 2024
Copy link

github-actions bot commented Apr 8, 2024

Benchmark for 6ecc575

Click to view benchmark
Test Base PR %
../test-assets/lena.jpg/compute_bytes 13.5±0.07µs 13.3±0.11µs -1.48%
../test-assets/test.pdf/compute_bytes 110.9±0.35µs 110.4±0.68µs -0.45%
compute_bytes_large/compute_bytes 139.0±0.78µs 140.3±3.21µs +0.94%
compute_bytes_medium/compute_bytes 26.8±0.30µs 27.5±0.27µs +2.61%
compute_bytes_small/compute_bytes 127.4±0.93ns 127.7±1.83ns +0.24%
index_build/index_build/../test-assets/ 156.2±4.08µs 158.1±1.50µs +1.22%

Copy link

github-actions bot commented Apr 8, 2024

Benchmark for 3fa6db1

Click to view benchmark
Test Base PR %
../test-assets/lena.jpg/compute_bytes 13.9±0.13µs 14.6±0.19µs +5.04%
../test-assets/test.pdf/compute_bytes 111.8±0.90µs 112.1±1.15µs +0.27%
compute_bytes_large/compute_bytes 142.4±1.26µs 142.7±2.19µs +0.21%
compute_bytes_medium/compute_bytes 27.9±0.33µs 27.2±0.26µs -2.51%
compute_bytes_small/compute_bytes 132.2±2.60ns 132.5±4.54ns +0.23%
index_build/index_build/../test-assets/ 157.5±1.01µs 160.1±0.57µs +1.65%

Signed-off-by: Pushkar Mishra <[email protected]>
Copy link

github-actions bot commented Apr 8, 2024

Benchmark for 2966539

Click to view benchmark
Test Base PR %
../test-assets/lena.jpg/compute_bytes 13.5±0.99µs 13.3±0.10µs -1.48%
../test-assets/test.pdf/compute_bytes 107.0±2.99µs 140.2±0.46µs +31.03%
compute_bytes_large/compute_bytes 136.0±1.21µs 455.0±11.28µs +234.56%
compute_bytes_medium/compute_bytes 27.6±0.45µs 27.7±1.00µs +0.36%
compute_bytes_small/compute_bytes 127.7±2.34ns 132.5±21.38ns +3.76%
index_build/index_build/../test-assets/ 160.3±2.21µs 160.3±5.47µs 0.00%

@kirillt kirillt requested a review from tareknaser April 8, 2024 14:11
@kirillt
Copy link
Member

kirillt commented Apr 8, 2024

Good start, let @tareknaser know please, when the todos are solved and ready for review.

Signed-off-by: Pushkar Mishra <[email protected]>
@Pushkarm029 Pushkarm029 marked this pull request as ready for review April 10, 2024 12:49
@Pushkarm029 Pushkarm029 requested review from tareknaser and removed request for tareknaser April 10, 2024 12:49
Copy link
Collaborator

@twitu twitu left a comment

Choose a reason for hiding this comment

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

The trait implementation looks solid!

fs-storage/src/base_storage.rs Outdated Show resolved Hide resolved
fs-storage/src/file_storage.rs Outdated Show resolved Hide resolved
Signed-off-by: Pushkar Mishra <[email protected]>
@Pushkarm029 Pushkarm029 requested a review from twitu April 12, 2024 16:34
Copy link

Benchmark for b50e471

Click to view benchmark
Test Base PR %
../test-assets/lena.jpg/compute_bytes 13.3±0.10µs 13.4±0.24µs +0.75%
../test-assets/test.pdf/compute_bytes 135.2±0.94µs 109.9±1.33µs -18.71%
compute_bytes_large/compute_bytes 191.9±1.94µs 139.8±4.19µs -27.15%
compute_bytes_medium/compute_bytes 28.1±0.50µs 28.0±0.84µs -0.36%
compute_bytes_small/compute_bytes 127.7±1.97ns 127.8±1.41ns +0.08%
index_build/index_build/../test-assets/ 161.9±2.03µs 158.9±2.05µs -1.85%

Copy link
Collaborator

@tareknaser tareknaser left a comment

Choose a reason for hiding this comment

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

Thanks for putting this together

fs-storage/src/base_storage.rs Outdated Show resolved Hide resolved
fs-storage/src/file_storage.rs Show resolved Hide resolved
fs-storage/src/base_storage.rs Outdated Show resolved Hide resolved
fs-storage/src/file_storage.rs Outdated Show resolved Hide resolved
fs-storage/src/file_storage.rs Outdated Show resolved Hide resolved
fs-storage/src/file_storage.rs Outdated Show resolved Hide resolved
fs-storage/src/file_storage.rs Outdated Show resolved Hide resolved
fs-storage/src/base_storage.rs Outdated Show resolved Hide resolved
fs-storage/src/base_storage.rs Outdated Show resolved Hide resolved
fs-storage/src/file_storage.rs Outdated Show resolved Hide resolved
@tareknaser
Copy link
Collaborator

I noticed that the current output from FileStorage isn't output a valid JSON file. The output is:

version 2
{"a":"1","b":"2","c":"3"}

It's also worth noting that the file doesn't have .json extension.
It probably should be

{
  "version": 2,
  "data": {
    "a": "1",
    "b": "2",
    "c": "3"
  }
}

This is not relevant to changes in this PR but we should fix it later

@kirillt
Copy link
Member

kirillt commented Apr 14, 2024

Good note about JSON, the version header certainly came from our legacy format. We should convert it into JSON field.

And let's bump the version number to 3.

Signed-off-by: Pushkar Mishra <[email protected]>
Copy link

Benchmark for 9e73735

Click to view benchmark
Test Base PR %
../test-assets/lena.jpg/compute_bytes 13.3±0.05µs 13.3±0.19µs 0.00%
../test-assets/test.pdf/compute_bytes 107.2±0.61µs 165.8±3.93µs +54.66%
compute_bytes_large/compute_bytes 137.2±5.82µs 240.7±3.87µs +75.44%
compute_bytes_medium/compute_bytes 27.8±0.35µs 26.9±0.48µs -3.24%
compute_bytes_small/compute_bytes 127.7±1.11ns 128.4±3.81ns +0.55%
index_build/index_build/../test-assets/ 157.8±2.76µs 157.9±1.26µs +0.06%

fs-storage/src/file_storage.rs Show resolved Hide resolved
fs-storage/src/base_storage.rs Outdated Show resolved Hide resolved
Signed-off-by: Pushkar Mishra <[email protected]>
Copy link

Benchmark for c1eb286

Click to view benchmark
Test Base PR %
../test-assets/lena.jpg/compute_bytes 13.3±0.06µs 13.3±0.38µs 0.00%
../test-assets/test.pdf/compute_bytes 141.2±1.62µs 113.8±2.82µs -19.41%
compute_bytes_large/compute_bytes 491.0±1.57µs 149.1±2.03µs -69.63%
compute_bytes_medium/compute_bytes 27.5±0.11µs 27.6±0.43µs +0.36%
compute_bytes_small/compute_bytes 128.6±9.27ns 127.4±1.08ns -0.93%
index_build/index_build/../test-assets/ 159.3±2.80µs 157.9±1.69µs -0.88%

@Pushkarm029 Pushkarm029 requested a review from twitu April 16, 2024 18:19
Copy link
Collaborator

@twitu twitu left a comment

Choose a reason for hiding this comment

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

Minor changes. Rest looks good 💯

This will make it easy to add in the conflict resolution too!

fs-storage/src/base_storage.rs Outdated Show resolved Hide resolved
fs-storage/src/file_storage.rs Outdated Show resolved Hide resolved
Signed-off-by: Pushkar Mishra <[email protected]>
Copy link

Benchmark for cf85a86

Click to view benchmark
Test Base PR %
../test-assets/lena.jpg/compute_bytes 14.7±0.13µs 13.3±0.08µs -9.52%
../test-assets/test.pdf/compute_bytes 331.9±1.10µs 109.4±0.81µs -67.04%
compute_bytes_large/compute_bytes 248.7±5.83µs 138.6±1.86µs -44.27%
compute_bytes_medium/compute_bytes 26.9±0.33µs 26.9±0.44µs 0.00%
compute_bytes_small/compute_bytes 128.0±1.80ns 127.8±1.45ns -0.16%
index_build/index_build/../test-assets/ 160.8±8.51µs 161.0±10.49µs +0.12%

Signed-off-by: Pushkar Mishra <[email protected]>
@Pushkarm029 Pushkarm029 requested a review from twitu April 16, 2024 22:01
Copy link

Benchmark for fd6373a

Click to view benchmark
Test Base PR %
../test-assets/lena.jpg/compute_bytes 13.3±0.23µs 13.5±0.29µs +1.50%
../test-assets/test.pdf/compute_bytes 108.3±0.80µs 108.3±1.01µs 0.00%
compute_bytes_large/compute_bytes 137.7±2.90µs 328.6±2.16µs +138.63%
compute_bytes_medium/compute_bytes 27.6±0.66µs 27.6±0.27µs 0.00%
compute_bytes_small/compute_bytes 127.7±1.28ns 127.9±2.14ns +0.16%
index_build/index_build/../test-assets/ 158.8±1.82µs 157.6±0.71µs -0.76%

Copy link
Collaborator

@tareknaser tareknaser left a comment

Choose a reason for hiding this comment

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

Thank you for putting this together. I think we are ready to merge.
I noticed some functions are not tested.

fs-storage/src/file_storage.rs Show resolved Hide resolved
fs-storage/src/file_storage.rs Show resolved Hide resolved
@Pushkarm029 Pushkarm029 requested a review from tareknaser April 19, 2024 06:46
Copy link

Benchmark for b61245f

Click to view benchmark
Test Base PR %
../test-assets/lena.jpg/compute_bytes 13.3±0.11µs 13.3±0.07µs 0.00%
../test-assets/test.pdf/compute_bytes 111.2±0.63µs 110.4±1.12µs -0.72%
compute_bytes_large/compute_bytes 139.2±2.10µs 139.5±2.25µs +0.22%
compute_bytes_medium/compute_bytes 26.8±0.43µs 28.2±0.33µs +5.22%
compute_bytes_small/compute_bytes 127.2±1.29ns 127.3±2.62ns +0.08%
index_build/index_build/../test-assets/ 160.8±2.01µs 160.0±0.68µs -0.50%

file_storage.data = match file_storage.read_fs() {
Ok(data) => data,
Err(err) => {
log::error!("Error reading storage file: {}", err);
Copy link
Collaborator

Choose a reason for hiding this comment

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

If it's the first time creating a FileStorage instance in the specified path, we currently log this as an error.
This is actually a valid scenario and shouldn't be treated as an error.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Indeed, the first time a FileStorage is created the file doesn't exist. Perhaps that can be checked here. If the file doesn't exist when creating the FileStorage it's being created for the first time. So it can start with an empty BTreeMap.

@@ -219,4 +226,28 @@ mod tests {
}
assert_eq!(storage_path.exists(), false);
}

#[test]
fn test_file_storage_is_storage_updated() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This test fails on my machine at least.
Output:

failures:

---- file_storage::tests::test_file_storage_is_storage_updated stdout ----
thread 'file_storage::tests::test_file_storage_is_storage_updated' panicked at fs-storage/src/file_storage.rs:240:9:
assertion `left == right` failed
  left: true
 right: false
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    file_storage::tests::test_file_storage_is_storage_updated

Copy link
Collaborator

Choose a reason for hiding this comment

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

Tracing the error back. seems like the it is because the nanoseconds don't match. SystemTime stores the time in nanosecond precision

running 1 test
Self timestamp: SystemTime { tv_sec: 1713553086, tv_nsec: 320082935 }
File timestamp: SystemTime { tv_sec: 1713553086, tv_nsec: 320194727 }
thread 'file_storage::tests::test_file_storage_is_storage_updated' panicked at fs-storage/src/file_storage.rs:243:9:
assertion `left == right` failed
  left: true
 right: false

I don't think we need nanosecond precision. Maybe we can just compare seconds

fn is_storage_updated(&self) -> Result<bool> {
	let file_timestamp = fs::metadata(&self.path)?.modified()?;
	let file_time_secs = file_timestamp
		.duration_since(SystemTime::UNIX_EPOCH)
		.unwrap() // SAFETY: We can safely unwrap because the timestamp is always after the epoch
		.as_secs();
	let self_time_secs = self
		.timestamp
		.duration_since(SystemTime::UNIX_EPOCH)
		.unwrap()
		.as_secs();
	Ok(file_time_secs > self_time_secs)
}

But you might have a better idea.

fs-storage/src/file_storage.rs Outdated Show resolved Hide resolved
fs-storage/src/base_storage.rs Show resolved Hide resolved
Signed-off-by: Pushkar Mishra <[email protected]>
Copy link

Benchmark for f4f7bd8

Click to view benchmark
Test Base PR %
../test-assets/lena.jpg/compute_bytes 13.3±0.10µs 13.3±0.08µs 0.00%
../test-assets/test.pdf/compute_bytes 108.2±0.69µs 108.0±0.92µs -0.18%
compute_bytes_large/compute_bytes 139.1±1.34µs 134.1±2.25µs -3.59%
compute_bytes_medium/compute_bytes 27.6±0.59µs 27.2±0.49µs -1.45%
compute_bytes_small/compute_bytes 127.3±2.08ns 127.2±2.54ns -0.08%
index_build/index_build/../test-assets/ 157.6±0.81µs 159.8±5.33µs +1.40%

Copy link
Collaborator

@tareknaser tareknaser left a comment

Choose a reason for hiding this comment

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

Thanks for putting this together

@Pushkarm029 Pushkarm029 merged commit 3b03e9a into main Apr 25, 2024
2 checks passed
kirillt pushed a commit that referenced this pull request Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fs-storage: Port BaseStorage abstraction from Kotlin
4 participants