Skip to content

Commit

Permalink
Add blob value usage example + update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
harold committed Apr 23, 2021
1 parent 7f098c1 commit 3f3c013
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
22 changes: 20 additions & 2 deletions doc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ Here are the supported consistency and serial-consistency levels:
(alia/execute session bst {:consistency :all})
```
You can also set the consistency globaly at the cluster level via
You can also set the consistency globally at the cluster level via
`qbits.alia/cluster` options.
#### Routing key
Expand Down Expand Up @@ -503,7 +503,7 @@ and this can be applied on streaming queries just fine as a result.

Row generators allow you to control how Row values are accumulated
into a single unit. By default alia will create one (transient) map
per row, but you might like vectors intead, or you could want to apply
per row, but you might like vectors instead, or you could want to apply
some computation at this level, this is just what they enable.

The default Generator is defined as follows:
Expand Down Expand Up @@ -565,3 +565,21 @@ You can have control over the query caching using
can set its value to `qbits.hayt/->raw` if you prefer not to use query caching.
The default uses `clojure.core.memoize` with a LU cache with a `:threshold`
of 100.

## `blob` data: `byte-array`s

Cassandra `blob` columns can store Java `byte-array` data.

Ex:

```clojure
(defn- update-byte-array!
[session id ^bytes ba]
(let [statement (alia/prepare session "INSERT into t (id, ba) VALUES (?, ?);")]
(alia/execute session statement {:values [id ba]})))
```

Notably, Alia returns the column data as a `java.nio.ByteBuffer`, but conversion back to a `byte-array` can be accomplished by calling `.array`:
- https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html#array--

For a working example, see `blob-test` in: [../test/qbits/alia_test.clj](../test/qbits/alia_test.clj).
19 changes: 18 additions & 1 deletion test/qbits/alia_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
[manifold.stream :as stream])
(:import
[java.time Instant]
[java.util UUID]
[java.nio ByteBuffer]
[com.datastax.oss.driver.api.core.cql ExecutionInfo]
[com.datastax.oss.driver.api.core.data UdtValue TupleValue]))

Expand Down Expand Up @@ -112,7 +114,9 @@
id int,
text varchar,
PRIMARY KEY (id)
);"))
);")

(alia/execute session "CREATE TABLE IF NOT EXISTS blobs (id uuid, data blob, PRIMARY KEY (id));"))

(defn teardown-test-keyspace
[session]
Expand Down Expand Up @@ -571,3 +575,16 @@
(-> (try (alia/tuple-encoder *session* :invalid-col :invalid-type) (catch Exception e e))
ex-data
:type)))))

(deftest blob-test
(testing "basic blob interactions"
(let [id (UUID/randomUUID)
bytes (byte-array (shuffle (range 256)))
insert-statement (alia/prepare *session* "INSERT INTO blobs (id, data) VALUES (?, ?);")]
(alia/execute *session* insert-statement {:values [id bytes]})
(let [row (->> (alia/execute *session* "SELECT * FROM blobs WHERE id = :id;"
{:values {:id id}})
(first))
^ByteBuffer data (:data row)
row-bytes (.array data)]
(is (= (vec bytes) (vec row-bytes)))))))

0 comments on commit 3f3c013

Please sign in to comment.