Created
December 21, 2011 06:09
-
-
Save mrb/1504840 to your computer and use it in GitHub Desktop.
Probably naive, non-idiomatic beginning of a clojure Redis .rdb file parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Obviously
parse-body
will start to do something useful, and leverage the ability to store a function in a record.