Skip to content

Commit

Permalink
Drop byte-streams dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
greglook committed Dec 16, 2020
1 parent 9cfe786 commit c962d44
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 13 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

...
### Changed
- Drop `byte-streams` dependency to avoid reflection issues which interfere
with Graal native-image compilation.


## [2.0.4] - 2020-04-20
Expand Down
1 change: 0 additions & 1 deletion dev/user.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
[blocks.store.memory :refer [memory-block-store]]
[blocks.store.replica :refer [replica-block-store]]
[blocks.store.tests :as tests]
[byte-streams :as bytes :refer [bytes=]]
[clojure.java.io :as io]
[clojure.repl :refer :all]
[clojure.stacktrace :refer [print-cause-trace]]
Expand Down
3 changes: 1 addition & 2 deletions src/blocks/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
[blocks.meter :as meter]
[blocks.store :as store]
[blocks.summary :as sum]
[byte-streams :as bytes]
[clojure.java.io :as io]
[clojure.string :as str]
[manifold.deferred :as d]
Expand Down Expand Up @@ -140,7 +139,7 @@
"Write a block's content to an output stream."
[block out]
(with-open [stream (open block)]
(bytes/transfer stream out)))
(io/copy stream out)))


(defn load!
Expand Down
19 changes: 16 additions & 3 deletions src/blocks/data.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
demand. A block with in-memory content is considered a _loaded block_, while
blocks with readers are _lazy blocks_."
(:require
[byte-streams :as bytes]
[clojure.java.io :as io]
[multiformats.hash :as multihash])
(:import
blocks.data.PersistentBytes
java.io.InputStream
(java.io
ByteArrayOutputStream
InputStream)
java.time.Instant
multiformats.hash.Multihash
(org.apache.commons.io.input
Expand Down Expand Up @@ -200,6 +202,17 @@
(Instant/now))


(defn- to-byte-array
"Coerce the given source into a byte array."
^bytes
[source]
(if (bytes? source)
source
(let [baos (ByteArrayOutputStream.)]
(io/copy source baos)
(.toByteArray baos))))


(defn hasher
"Return the hashing function for an algorithm keyword, or throw an exception
if no supported function is available."
Expand Down Expand Up @@ -236,7 +249,7 @@
"Create a block by reading the source into memory and hashing it."
[algorithm source]
(let [hash-fn (hasher algorithm)
content (PersistentBytes/wrap (bytes/to-byte-array source))
content (PersistentBytes/wrap (to-byte-array source))
size (count content)]
(when (pos? size)
(create-block (hash-fn (read-all content)) size (now) content))))
Expand Down
3 changes: 1 addition & 2 deletions test/blocks/bytes_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(ns blocks.bytes-test
(:require
[byte-streams :refer [bytes=]]
[clojure.test :refer [deftest testing is]])
(:import
blocks.data.PersistentBytes))
Expand Down Expand Up @@ -44,7 +43,7 @@
(is (not= (hash pb1) (hash pb3)))
(is (= (.hasheq pb1) (.hasheq pb2)))
(is (not= (.hasheq pb1) (.hasheq pb3)))
(is (bytes= (.open pb1) "foo"))))
(is (= "foo" (slurp (.open pb1))))))


(deftest persistent-bytes-coll
Expand Down
7 changes: 3 additions & 4 deletions test/blocks/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
[blocks.data :as data]
[blocks.store :as store]
[blocks.test-utils :refer [quiet-exception]]
[byte-streams :refer [bytes=]]
[clojure.java.io :as io]
[clojure.test :refer [deftest testing is]]
[manifold.deferred :as d]
Expand Down Expand Up @@ -44,7 +43,7 @@
(is (thrown? IllegalStateException
(dosync (block/write! block baos))))
(block/write! block baos)
(is (bytes= "frobblenitz" (.toByteArray baos)))))
(is (= "frobblenitz" (String. (.toByteArray baos))))))
(testing "loading"
(let [lazy-readme (block/from-file "README.md")
loaded-readme (block/load! lazy-readme)]
Expand All @@ -54,8 +53,8 @@
"load returns loaded block for lazy block")
(is (identical? loaded-readme (block/load! loaded-readme))
"load returns loaded block unchanged")
(is (bytes= (block/open loaded-readme)
(block/open lazy-readme))
(is (= (slurp (block/open loaded-readme))
(slurp (block/open lazy-readme)))
"loaded block content should match lazy block"))))


Expand Down

0 comments on commit c962d44

Please sign in to comment.