Skip to content
Common Lisp library for Avro
Common Lisp
Branch: master
Clone or download
Latest commit e27dbec Aug 14, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Add schema parser Aug 14, 2019
LICENSE initial commit Aug 12, 2019
README.md Update README Aug 14, 2019
cl-avro.asd Add schema parser Aug 14, 2019
version.lisp initial commit Aug 12, 2019

README.md

cl-avro

license

A Common Lisp implementation of the Apache Avro data serialization system.

This was written against the Avro 1.9.0 spec and currently adheres to the happy-path so there are a few TODOs:

Examples

(ql:quickload '(:cl-avro :trivial-gray-streams))

(use-package :trivial-gray-streams)

;; create in-memory stream classes:

(defclass input-stream (fundamental-binary-input-stream)
  ((bytes
    :initform (error "Must supply :bytes vector")
    :initarg :bytes)
   (position
    :initform 0)))

(defmethod stream-read-byte ((stream input-stream))
  (with-slots (bytes position) stream
    (if (= position (length bytes))
        :eof
        (prog1 (elt bytes position)
          (incf position)))))

(defclass output-stream (fundamental-binary-output-stream)
  ((bytes
    :initform (make-array 0
                          :element-type '(unsigned-byte 8)
                          :adjustable t
                          :fill-pointer 0)
    :reader get-bytes)))

(defmethod stream-write-byte ((stream output-stream) (byte integer))
  (check-type byte (unsigned-byte 8))
  (with-slots (bytes) stream
    (vector-push-extend byte bytes))
  byte)


(defparameter *schema*
  (avro:parse-schema
   "{\"type\": \"record\",
      \"name\": \"LongList\",
      \"doc\": \"A linked-list of 64-bit values\",
      \"aliases\": [\"LinkedLongs\"],
      \"fields\": [
        {\"name\": \"value\", \"type\": \"long\"},
        {\"name\": \"next\", \"type\": [\"null\", \"LongList\"]},
      ]
     }"))

(avro:validp *schema* '(2 (4 (6 (8 (10 nil))))))
;; => t

(avro:validp *schema* '(2 (4 (6 (#.(1- (expt 2 63)) (10 nil))))))
;; => t

(avro:validp *schema* '(2 (4 (6 (#.(expt 2 63) (10 nil))))))
;; => nil

(let* ((output-stream (make-instance 'output-stream))
       (input-stream (make-instance 'input-stream
                                    :bytes (get-bytes output-stream))))
  (avro:stream-serialize output-stream *schema* '(2 (4 (6 (8 (10 nil))))))
  (avro:stream-deserialize input-stream *schema*))
;; => #(2 #(4 #(6 #(8 #(10 NIL)))))
You can’t perform that action at this time.