Skip to content

Commit

Permalink
Allow io.Data for the storage lib. (#2372)
Browse files Browse the repository at this point in the history
Also deprecate `write --from` in favor of `write --at`.
  • Loading branch information
floitsch authored May 31, 2024
1 parent 9f2da5a commit 5c6b119
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
24 changes: 21 additions & 3 deletions lib/system/storage.toit
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import system.storage
main:
region := storage.Region.open --flash "my-region" --capacity=128
region.write --from=0 #[0x12, 0x34]
region.write --at=0 #[0x12, 0x34]
```
*/

Expand Down Expand Up @@ -372,10 +372,25 @@ class Region extends ServiceResourceProxy:
--to=to
--max-size=max-size

/**
Deprecated. Use $(write --at bytes) instead.
*/
write --from/int bytes/ByteArray -> none:
if not resource_: throw "ALREADY_CLOSED"
flash-region-write_ resource_ from bytes

/**
Writes the given $data into the region at the given offset $at.
If the region has already data, the new data might be combined
with the existing data. See $erase-value, $write-can-clear-bits,
and $write-can-set-bits. Use $erase to reset areas to the $erase-value
after which this method will write the data as given.
*/
write --at/int data/io.Data -> none:
if not resource_: throw "ALREADY_CLOSED"
flash-region-write_ resource_ at data

is-erased --from/int=0 --to/int=size -> bool:
if not resource_: throw "ALREADY_CLOSED"
return flash-region-is-erased_ resource_ from (to - from)
Expand Down Expand Up @@ -435,8 +450,11 @@ flash-region-close_ resource:
flash-region-read_ resource from bytes:
#primitive.flash.region-read

flash-region-write_ resource from bytes:
#primitive.flash.region-write
flash-region-write_ resource at data:
#primitive.flash.region-write: | error |
return io.primitive-redo-io-data_ error data: | bytes |
flash-region-write_ resource at bytes


flash-region-is-erased_ resource from size:
#primitive.flash.region-is-erased
Expand Down
5 changes: 3 additions & 2 deletions tests/envelope/boot-flash-source.toit
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ main:
region := storage.Region.open --flash "toitlang.org/envelope-test-region" --capacity=100
bucket := storage.Bucket.open --flash "toitlang.org/envelope-test-bucket"

hello-bytes := "hello world".to-byte-array
hello := "hello world"
hello-bytes := hello.to-byte-array
existing-region := region.read --from=0 --to=hello-bytes.size
existing-bucket := bucket.get "hello"
if existing-region != hello-bytes or existing-bucket != "world":
// We just assume that they haven't been written yet.
region.write --from=0 hello-bytes
region.write --at=0 hello
bucket["hello"] = "world"
region.close
bucket.close
Expand Down
35 changes: 25 additions & 10 deletions tests/storage-test.toit
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import system.storage
import encoding.tison
import expect show *
import .io-data show FakeData

main:
test-bucket-ram
Expand All @@ -19,6 +20,7 @@ main:
test-region-flash-erase
test-region-flash-is-erased
test-region-flash-write-all
test-region-flash-write-io-data
test-region-flash-ignore-set
test-region-flash-out-of-space
test-region-flash-stream
Expand Down Expand Up @@ -210,19 +212,19 @@ test-region-flash-is-erased:
expect (region.is-erased --from=1 --to=28)
expect (region.is-erased --from=4 --to=28)

region.write --from=3 #[1]
region.write --at=3 #[1]
expect-not (region.is-erased --from=1 --to=31)
expect (region.is-erased --from=4 --to=31)
expect-not (region.is-erased --from=1 --to=28)
expect (region.is-erased --from=4 --to=28)

region.write --from=29 #[2]
region.write --at=29 #[2]
expect-not (region.is-erased --from=1 --to=31)
expect-not (region.is-erased --from=4 --to=31)
expect-not (region.is-erased --from=1 --to=28)
expect (region.is-erased --from=4 --to=28)

region.write --from=17 #[2]
region.write --at=17 #[2]
expect-not (region.is-erased --from=1 --to=31)
expect-not (region.is-erased --from=4 --to=31)
expect-not (region.is-erased --from=1 --to=28)
Expand All @@ -244,7 +246,7 @@ test-region-flash-write-all:
while written < region.size:
snippet-size := min ((random 128) + 1) (region.size - written)
snippets.add (ByteArray snippet-size: random 0x100)
region.write --from=written snippets.last
region.write --at=written snippets.last
written += snippet-size

read := 0
Expand All @@ -253,12 +255,25 @@ test-region-flash-write-all:
read += snippet.size
region.close

test-region-flash-write-io-data:
region := storage.Region.open --flash "region-1" --capacity=1000

region.erase
region.write --at=0 "foo"
expect-equals #['f', 'o', 'o'] (region.read --from=0 --to=3)

region.erase
region.write --at=0 (FakeData "bar")
expect-equals #['b', 'a', 'r'] (region.read --from=0 --to=3)

region.close

test-region-flash-ignore-set:
region := storage.Region.open --flash "region-2" --capacity=1000
region.erase
region.write --from=0 #[0b1010_1010]
region.write --at=0 #[0b1010_1010]
expect-bytes-equal #[0b1010_1010] (region.read --from=0 --to=1)
region.write --from=0 #[0b1111_0000]
region.write --at=0 #[0b1111_0000]
expect-bytes-equal #[0b1010_0000] (region.read --from=0 --to=1)
region.close

Expand Down Expand Up @@ -289,7 +304,7 @@ test-region-flash-stream:
test-region-flash-stream region/storage.Region max-size/int?:
region.erase
bytes-written := ByteArray region.size: random 0x100
region.write --from=0 bytes-written
region.write --at=0 bytes-written

if max-size and max-size < 16:
expect-throw "Bad Argument": region.stream --max-size=max-size
Expand Down Expand Up @@ -319,7 +334,7 @@ test-region-flash-stream region/storage.Region max-size/int?:
test-region-flash-no-writable:
region := storage.Region.open --flash "region-1" --capacity=1000 --no-writable
expect-throw "PERMISSION_DENIED": region.erase
expect-throw "PERMISSION_DENIED": region.write --from=0 #[0b1010_1010]
expect-throw "PERMISSION_DENIED": region.write --at=0 #[0b1010_1010]
region.read --from=0 --to=1
region.close

Expand All @@ -328,7 +343,7 @@ test-region-flash-delete:
expect-throw "ALREADY_IN_USE": storage.Region.delete --flash "region-3"
region.close
expect-throw "ALREADY_CLOSED": region.read --from=0 --to=4
expect-throw "ALREADY_CLOSED": region.write --from=0 #[1]
expect-throw "ALREADY_CLOSED": region.write --at=0 #[1]
expect-throw "ALREADY_CLOSED": region.is-erased --from=0 --to=4
expect-throw "ALREADY_CLOSED": region.erase --from=0 --to=4096
storage.Region.delete --flash "region-3"
Expand All @@ -349,6 +364,6 @@ test-region-partition:
test-region-partition-no-writable:
region := storage.Region.open --partition "partition-0" --no-writable
expect-throw "PERMISSION_DENIED": region.erase
expect-throw "PERMISSION_DENIED": region.write --from=0 #[0b1010_1010]
expect-throw "PERMISSION_DENIED": region.write --at=0 #[0b1010_1010]
region.read --from=0 --to=1
region.close

0 comments on commit 5c6b119

Please sign in to comment.