Created
July 12, 2012 05:55
-
-
Save bizenn/3096144 to your computer and use it in GitHub Desktop.
Now this is not a memory eater
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
#!/usr/bin/env gosh | |
;;; -*- mode: scheme; coding: utf-8 -*- | |
(use srfi-13) | |
(use srfi-14) | |
(use parser.peg) | |
(use gauche.generator) | |
(use gauche.sequence) | |
;; Fetch a record each time | |
(define (fetch-record emitter) | |
(define (%escaped-char emitter) | |
(and-let* ((c (emitter)) | |
((not (eof-object? c)))) | |
(write-char #\\) | |
(write-char c))) | |
(define (%record emitter) | |
(let loop ((c (emitter))) | |
(cond ((eof-object? c) #f) | |
((char=? c #\newline) | |
(write-char #\newline)) | |
((char=? c #\\) | |
(%escaped-char emitter) | |
(loop (emitter))) | |
(else | |
(write-char c) | |
(loop (emitter)))))) | |
(let1 s (with-output-to-string (cut %record emitter)) | |
(if (string-null? s) | |
(eof-object) | |
s))) | |
(define %escape ($char #\\)) | |
(define %eor ($char #\newline)) | |
(define %fs ($char #\tab)) | |
(define %field | |
(let* ((%special-char | |
($do %escape | |
($or %eor %fs))) | |
(%unescaped ($none-of #[\t\n])) | |
(%body-char ($or %special-char %unescaped))) | |
($->rope ($many %body-char)))) | |
(define %record | |
($do [v ($sep-by %field ($char #\tab))] | |
%eor | |
($return v))) | |
(define outfile-parser | |
($or eof %record)) | |
(define (string-hexify str) | |
(with-output-to-string | |
(lambda () | |
(dotimes (i (string-size str)) | |
(format #t "~2,'0x" (string-byte-ref str i)))))) | |
(define (print-record rec out) | |
(write (string-hexify (car rec)) out) | |
(display ": [[\"kv_value\"," out) | |
(format out "~s,~d" (cadr rec) (* 1000 (sys-time))) | |
(display "]]" out)) | |
(define (outfile->cassandra-json file :optional (out (current-output-port))) | |
(call-with-input-file file | |
(lambda (in) | |
(let ((emitter (cut read-char in)) | |
(print-comma (cut display ",\n" <>))) | |
(display "{\n" out) | |
(generator-fold (lambda (rec print-delim) | |
(print-delim out) | |
(print-record (peg-parse-string outfile-parser rec) out) | |
print-comma) | |
(^ (_) #f) | |
(cut fetch-record emitter)) | |
(display "\n}\n" out))))) | |
(define (main args) | |
(outfile->cassandra-json (cadr args))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment