Skip to content

Commit 1f3972e

Browse files
committed
Fix nippy README, and add storage README
1 parent a532a94 commit 1f3972e

File tree

2 files changed

+67
-66
lines changed

2 files changed

+67
-66
lines changed

nippy/README.md

+21-66
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,40 @@
1-
# converge/transit
1+
# converge/nippy
22

3-
transit-(clj|cljs) handlers for Converge
3+
Nippy serialization support for Converge in Clojure.
44

55
## Usage
66

7-
### Clojure + transit-clj
8-
9-
This is the transit-clj Usage example, modified to include a
10-
ConvergentRef and the read/write handlers provided by this library.
7+
Nippy provides a global extension mechanism for supporting custom
8+
types. Simply requiring the `converge.nippy` namespace adds support
9+
for Converge types:
1110

1211
``` clojure
13-
(require '[cognitect.transit :as transit]
14-
'[converge.transit :as converge-transit]
12+
(require '[taoensso.nippy :as nippy]
13+
'converge.nippy
1514
'[converge.api :as convergent])
16-
(import [java.io ByteArrayInputStream ByteArrayOutputStream])
17-
18-
;; Write data to a stream
19-
(def out (ByteArrayOutputStream. 4096))
20-
(def writer
21-
(transit/writer out :json
22-
{:handlers (merge transit/default-write-handlers
23-
converge-transit/write-handlers)}))
24-
(def cref (convergent/ref {:foo :bar
25-
:a [1 2]}))
26-
(transit/write writer cref)
27-
28-
cref
29-
30-
;; Take a peek at the JSON
31-
(.toString out)
32-
;; => "[\"~#opset/ref\", <snip a bunch of operations ...>]"
33-
34-
;; Read data from a stream
35-
(def in (ByteArrayInputStream. (.toByteArray out)))
36-
(def reader
37-
(transit/reader in :json
38-
{:handlers (merge transit/default-read-handlers
39-
converge-transit/read-handlers)}))
40-
(def read-ref (transit/read reader))
41-
(prn read-ref)
42-
;; => #converge.opset.ref.OpsetConvergentRef[{:status :ready, :val {:a [1 2], :foo :bar}} 0x3bd98fc8]
43-
(prn @read-ref)
44-
;; => {:a [1 2], :foo :bar}
45-
```
46-
47-
### ClojureScript + transit-cljs
48-
49-
This is the transit-cljs Usage example, modified to include a
50-
ConvergentRef and the read/write handlers provided by this library.
51-
52-
5315

54-
## Development
16+
(def r (convergent/ref {:foo [:bar #{:baz}]}))
5517

56-
Invoke a library API function from the command-line:
18+
(def nippy-bytes (nippy/freeze r))
5719

58-
$ clojure -X converge.transit/foo :a 1 :b '"two"'
59-
{:a 1, :b "two"} "Hello, World!"
20+
(def roundtripped (nippy/thaw nippy-bytes))
6021

61-
Run the project's tests (they'll fail until you edit them):
22+
@roundtripped
23+
;=> {:foo [:bar #{:baz}]}
6224

63-
$ clojure -T:build test
25+
(def r2 (convergent/ref-from-ops (convergent/ref-log r)))
6426

65-
Run the project's CI pipeline and build a JAR (this will fail until you edit the tests to pass):
27+
(swap! r assoc :baz :quux)
6628

67-
$ clojure -T:build ci
29+
(def patch (convergent/patch-from-clock r (convergent/clock r2)))
6830

69-
This will produce an updated `pom.xml` file with synchronized dependencies inside the `META-INF`
70-
directory inside `target/classes` and the JAR in `target`. You can update the version (and SCM tag)
71-
information in generated `pom.xml` by updating `build.clj`.
31+
(nippy/thaw (nippy/freeze patch))
32+
;=> <the patch>
7233

73-
Install it locally (requires the `ci` task be run first):
74-
75-
$ clojure -T:build install
76-
77-
Deploy it to Clojars -- needs `CLOJARS_USERNAME` and `CLOJARS_PASSWORD` environment
78-
variables (requires the `ci` task be run first):
79-
80-
$ clojure -T:build deploy
81-
82-
Your library will be deployed to net.clojars.converge/transit on clojars.org by default.
34+
(def clock (convergent/clock r1))
35+
(nippy/thaw (nippy/freeze clock))
36+
;=> <the clock>
37+
```
8338

8439
## License
8540

storage/README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# converge/storage
2+
3+
An API for persisting convergent refs to the filesystem in a Git-friendly way.
4+
5+
## Usage
6+
7+
Convergent refs are stored on disk as a directory of Nippy serialized
8+
patch files, which together comprise the OpSet of the ref.
9+
10+
``` clojure
11+
(def dirname "a-ref-dir")
12+
13+
(def r (convergent/ref {:foo :bar :baz :quux}))
14+
15+
(sync-directory r dirname)
16+
;=> returns the name of the patch file added to the directory to bring the
17+
; on-disk ref to (at least) the same clock point as the in-memory ref, or
18+
; nil if no file was needed/written. If directory doesn't contain a ref,
19+
; creates the root file and returns its name. Patch filenames are SHA256-3
20+
; hashes of their contents, so writing patches is idempotent.
21+
22+
(def r (from-directory dirname))
23+
;=> returns a ConvergentRef having the same clock state as on-disk
24+
25+
@r
26+
27+
(swap! r assoc :quux [1 2 :lalala #{:a 'nice "set"} {:of [2 'things]}])
28+
29+
(sync-directory r dirname)
30+
```
31+
32+
## License
33+
34+
Copyright 2020 Evident Systems LLC
35+
36+
Licensed under the Apache License, Version 2.0 (the "License");
37+
you may not use this file except in compliance with the License.
38+
You may obtain a copy of the License at
39+
40+
http://www.apache.org/licenses/LICENSE-2.0
41+
42+
Unless required by applicable law or agreed to in writing, software
43+
distributed under the License is distributed on an "AS IS" BASIS,
44+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45+
See the License for the specific language governing permissions and
46+
limitations under the License.

0 commit comments

Comments
 (0)