Skip to content

Instantly share code, notes, and snippets.

Failing tests for a driver

Tests are failing relating to a feature added in PR 46089 for drivers that inherit from a driver that can limit json length.

Explanation

Selecting huge json objects comes with obvious memory pressure. We want to limit that. To do this, we have a multimethod called json-field-length that knows how to get the length of json fields. We want to use this to select only those values which aren't "too big"™.

--- sdw_500.java 2023-04-14 17:55:38
+++ sdw_523.java 2023-04-14 17:56:20
@@ -32,17 +32,18 @@
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
(let [addr (java.net.UnixDomainSocketAddress/of "/tmp/222")
sc (doto (java.nio.channels.ServerSocketChannel/open java.net.StandardProtocolFamily/UNIX)
(.bind addr))]
(while true
(let [c (.accept sc)]
(future
(let [in (java.nio.channels.Channels/newInputStream c)
out (java.nio.channels.Channels/newOutputStream c)]
(binding [*in* (clojure.lang.LineNumberingPushbackReader.
(clojure.java.io/reader in))
(ns dev.nocommit.pprint-middleware
(:require [honeysql.core :as hsql]
[sql-honey.core :as sqlhoney]
[metabase.models.database :refer [Database]]
[clojure.string :as str])
(:import org.apache.openjpa.lib.jdbc.SQLFormatter))
(let [formatter (SQLFormatter.)]
(println
(.prettyPrint formatter "select * from orders")))
(require '[clojure.string :as str])
(require '[clojure.core.server :as server])
#_(peek (str/split (str *ns*) #"\.")) ; foo.bar.quux> is just displayed as quux>
(clojure.main/repl
;; keep prompts shorter
:prompt (fn [] (printf "%s=> " (peek (str/split (str *ns*) #"\."))))
;; allow easily ending this sub repl watching for :repl/quit
:read server/repl-read)
;; fantastic reveal
;; use a heap to keep only the max N (here 10) items in memory at a time. imagine reducing over a
;; quite large result set. don't want to realize it all, sort, and then take N. Just keep discarding
;; compared to our heap's min item.
(let [threshold 10
cmp (reify java.util.Comparator
(compare [_ x y]
;; score is second so want to compare [score item]
(compare (vec (reverse x)) (vec (reverse y)))))
xf (comp (map (fn [{:keys [name]}] [name (count name)]))
;; shadow-cljs.edn: {:dependencies [[org.clojure/test.check "1.1.0"]]}
;; npm i shadow-cljs
;; cider-jack-in-cljs
;; shadow
;; node-repl
foo> (require '[clojure.spec.test.alpha :as spec])
nil
foo> (spec/check)
;; ‘C-x r s <register-key>’ save to register
;; 'C-c C-j x <register-key' to send to repl
(defun cider-insert-register-contents (register)
(interactive (list (register-read-with-preview "From register")))
(let ((form (get-register register)))
;; could put form into a buffer and check if its parens are
;; balanced
(if form
(cider-insert-in-repl form (not cider-invert-insert-eval-p))
(user-error "No saved form in register"))))
(let (defcustoms)
(dolist (file (directory-files "/Users/dan/projects/dev/cider" t "\.el"))
(unless (string-match-p "dir-locals" file)
(with-current-buffer (find-file file)
(goto-char (point-min))
(while (re-search-forward "defcustom" nil t)
(let ((custom (substring-no-properties (progn (paredit-forward 1)
(thing-at-point 'sexp))))
(docstring (substring-no-properties (progn (paredit-forward 2)
(or (thing-at-point 'sexp)
;; thanks ghadi from #clojure.beginners
(defn into-str
"reduce coll into a String, given a transducer"
[xf coll]
(transduce (comp xf (map str))
(fn
([] (StringBuilder.))
([^StringBuilder sb] (.toString sb))
([^StringBuilder sb s] (.append sb ^String s)))