Skip to content

Commit

Permalink
interim commit while adding fjoin operator
Browse files Browse the repository at this point in the history
  • Loading branch information
hoeck committed Jun 11, 2009
1 parent 64bf0b8 commit 94c55dc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 190 deletions.
149 changes: 0 additions & 149 deletions README

This file was deleted.

60 changes: 20 additions & 40 deletions README.textile
Expand Up @@ -25,15 +25,12 @@ Include the generated rel.jar in your classpath
h2. Some examples using the reflection relations

* Load the hoeck.rel library
<pre>
<code>
<pre><code>
user> (use 'hoeck.rel.reflection)
</code>
</pre>
</code></pre>

* Print all accessible relations in a map rel-name -> fields
<pre>
<code>
<pre><code>
user> (with-relations (pprint (into {} (field-map (relations) :relation :field))))
{:publics (:ns :name :varname),
:namespace (:name),
Expand All @@ -48,12 +45,10 @@ user> (with-relations (pprint (into {} (field-map (relations) :relation :field))
:classes (:super :class :type :modifiers),
:refers (:ns :name :varname),
:aliases (:ns :name :alias)}
</code>
</pre>
</code></pre>

* Private definitions in the hoeck.rel.reflection namespace
<pre>
<code>
<pre><code>
user> (with-relations (project (select (difference :interns :publics)
(= ~ns 'hoeck.rel.reflection))
:name))
Expand All @@ -68,12 +63,10 @@ user> (with-relations (project (select (difference :interns :publics)
{:name class-tuple }
{:name find-classnames }
{:name classnames-from-jars }}
</code>
</pre>
</code></pre>

* Relation of all files on the classpath, ordered by size
<pre>
<code>
<pre><code>
user> (with-relations
(order-by
(project :files [(/ ~size 1000.0) :size-in-kb] :name :path)
Expand All @@ -82,57 +75,45 @@ user> (with-relations
#{{:size-in-kb 43800.36, :name "rt.jar", :path "C:\\Programme\\Java\\jre6\\lib"}
{:size-in-kb 6592.915, :name "charsets.jar", :path "C:\\Programme\\Java\\jre6\\lib"}
...}
</code>
</pre>
</code></pre>

* Relations are first-class citizens
<pre>
<code>
<pre><code>
user> (with-relations (def java-lang-classes (select :classes (rlike "^java\\.lang\\.[A-Z].*" ~class))))
#'hoeck.rel.reflection/java-lang-classes

user> (defn without-inner-classes [classes-rel]
(select classes-rel (rlike "[^\\$]*" ~class)))
#'hoeck.rel.reflection/without-inner-classes
</code>
</pre>
</code></pre>

* The number of classes in the java.lang package
<pre>
<code>
<pre><code>
user> (count (without-inner-classes java-lang-classes))
108
</code>
</pre>
</code></pre>

* All inner classes in the java.lang package
(takes some time to compute, calculates all indexes over the :classes relation due to my poor difference implementation)
<pre>
<code>
<pre><code>
user> (count (difference java-lang-classes (without-inner-classes java-lang-classes)))
48
</code>
</pre>
</code></pre>

* Number of implemented interfaces
<pre>
<code>
<pre><code>
user> (with-relations (count (project :interfaces :interface)))
2035
</code>
</pre>
</code></pre>

* Total number of interfaces
<pre>
<code>
<pre><code>
user> (with-relations (count (select :classes (= ~type :interface))))
2580
</code>
</pre>
</code></pre>

* Classes (directly) implementing clojure.lang.IFn
<pre>
<code>
<pre><code>
user> (with-relations (-> (select :interfaces
(= 'clojure.lang.IFn ~interface))
(field-seq :class)
Expand All @@ -143,8 +124,7 @@ user> (with-relations (-> (select :interfaces
clojure.lang.Keyword
clojure.lang.Ref
clojure.lang.Var)
</code>
</pre>
</code></pre>

h2. TODO:

Expand Down
1 change: 1 addition & 0 deletions hoeck/rel/operators.clj
Expand Up @@ -56,6 +56,7 @@
(defmulti join join-dispatch) ;; natural-join
(defmulti outer-join join-dispatch) ;; right-outer-join
(defmulti full-outer-join join-dispatch)
(defmulti fjoin op-dispatch-fn) ;; m

(defmulti union two-op-dispatch-fn)
(defmulti difference two-op-dispatch-fn)
Expand Down
11 changes: 10 additions & 1 deletion hoeck/rel/structmaps.clj
Expand Up @@ -52,7 +52,6 @@
(next keys)))
ret)))


;; index tools

(defn- index-lookup
Expand Down Expand Up @@ -355,6 +354,8 @@
(magic-map a (fn ([] (keys b))
([k] (get b k)))))

;;; joins

(defmethod join :clojure [R r S s]
(let [index-Ss (find (index S) s)
join-tuple (fn [r-tup] (let [friends (get (val index-Ss) (r-tup r))]
Expand All @@ -375,6 +376,14 @@
(is (= (set (fields j)) (set (concat (fields R) (filter #(not= :id %) (fields S))))) "joined fields")
(is (= (clean-index (index j)) (make-index j (fields j))) "index"))))

(defmethod fjoin [R f] ;; much like project with an expression, but allows 1..n joins instead of 1..1 only
;; f must return a seq of tuples or nil
(let []
(Relation (merge ^R {:fields (fields R) ()}) {}
{'seq _
'get _
'count _})))

(defmethod xproduct :clojure [R S]
(let [cross-tuple (fn [r-tup] (map #(merge r-tup %) S))]
(Relation. (merge ^S ^R {:fields (concat (fields R) (fields S))
Expand Down

0 comments on commit 94c55dc

Please sign in to comment.