Skip to content

Instantly share code, notes, and snippets.

@mrb
Created December 21, 2011 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrb/1504840 to your computer and use it in GitHub Desktop.
Save mrb/1504840 to your computer and use it in GitHub Desktop.
Probably naive, non-idiomatic beginning of a clojure Redis .rdb file parser
; Import the Java libs necessary for byte seq'ing
(ns pianist.core
(:import (java.io FileOutputStream FileInputStream FileReader File)
(java.nio Buffer ByteBuffer CharBuffer)))
; Define the record structure
(defrecord DataTest [numbytes predicate])
; Make some datatests
(def magic
(DataTest. 5, #(= % '(82 69 68 73 83))))
(def dumpversion
(DataTest. 4, #(= % '(48 48 48 49))))
; Function to hold structure of records, probably not idiomatic
(defn parsemap []
[magic dumpversion])
; Inside a with-open, assembles a byte-array,
; reads from a stream, and returns a seq
(defn grab-bytes [stream bytes]
(let [ary (byte-array bytes)]
(.read stream ary)
(seq ary)))
; Does the magic number and dump version number match
; the map?
(defn is-redis-file? [stream dataseq]
(= [true true]
(into []
(map
#(let [sbytes (:numbytes %) predicate (:predicate %)]
(predicate
(grab-bytes stream sbytes))) dataseq))))
; Parse the body of the data file
(defn parse-body [stream dataseq]
(let [ary (byte-array (.available stream))]
(.read stream ary)
(seq ary)))
; The main interface function
(defn parse-rdb-file [path dataseq]
(with-open [#^FileInputStream stream (FileInputStream. path)]
(cond
(is-redis-file? stream dataseq) (parse-body stream dataseq)
:else "Not a valid Redis .rdb file")))
; Calling the function with a path and parsemap
(parse-rdb-file "/Users/mrb/dump.rdb" (parsemap))
@mrb
Copy link
Author

mrb commented Dec 21, 2011

Obviously parse-body will start to do something useful, and leverage the ability to store a function in a record.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment