Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit 73a0224

Browse files
committed
Track IPs & aliases with an object.
1 parent ee2062d commit 73a0224

File tree

2 files changed

+66
-11
lines changed

2 files changed

+66
-11
lines changed

hosts.janet

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,28 @@
7373
[]
7474
(:init (table/setproto @{} Hosts)))
7575

76+
(def Tracker
77+
"Model to track objects seen, create with `(tracker)`."
78+
79+
@{:init (fn [self hash-fn]
80+
(-> self
81+
(put :hash-fn hash-fn)
82+
(put :seen @{})))
83+
84+
:see (fn [self obj]
85+
(put (self :seen) ((self :hash-fn) obj) true))
86+
87+
:seen? (fn [self obj]
88+
(if (get (self :seen) ((self :hash-fn) obj))
89+
true
90+
false))})
91+
92+
(defn tracker
93+
"Create an instance of `Tracker` to track seen objects."
94+
[&opt hash-fn]
95+
(default hash-fn identity)
96+
(:init (table/setproto @{} Tracker) hash-fn))
97+
7698
(defn parse-and-merge
7799
"Parse lines in combined hosts file, merging entries as appropriate."
78100
[lines &opt delimiter]
@@ -96,9 +118,7 @@
96118

97119
(def processed @[])
98120

99-
(def ip-visit @{})
100-
(defn ip/visit [ip] (put ip-visit ip true))
101-
(defn ip/visited? [ip] (get ip-visit ip))
121+
(def ip-tracker (tracker))
102122

103123
# Update parsed IP field, merge aliases, skip duplicate lines/records.
104124
(loop [i :range [0 (length lines)]]
@@ -112,17 +132,15 @@
112132

113133
(array/remove record 0) (array/insert record 0 ip)
114134

115-
(when (not (ip/visited? ip))
116-
(def host-visit @{})
117-
(defn host/visit [host] (put host-visit host true))
118-
(defn host/visited? [host] (get host-visit host))
119-
(map (fn [host] (host/visit host)) hosts)
135+
(when (not (:seen? ip-tracker ip))
136+
(def host-tracker (tracker))
137+
(map (fn [host] (:see host-tracker host)) hosts)
120138
(loop [host :in aliases]
121-
(unless (host/visited? host)
139+
(unless (:seen? host-tracker host)
122140
(host/add-host record host delimiter)
123-
(host/visit host)))
141+
(:see host-tracker host)))
124142
(array/push processed (string/join record))
125-
(ip/visit ip)))
143+
(:see ip-tracker ip)))
126144

127145
(array/push processed line))))
128146

test/unit/test-hosts.janet

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,40 @@
4040

4141
(assert (= (:hosts ipv4 "127.0.0.1") nil)
4242
"Hosts lookup by original IP, after updating via alias."))
43+
44+
(let [int-tracker (tracker)]
45+
(assert (= (:seen? int-tracker 7) false)
46+
"Checking a not-yet-seen value.")
47+
48+
(:see int-tracker 7)
49+
50+
(assert (= (:seen? int-tracker 7) true)
51+
"Checking a seen value.")
52+
53+
(assert (= (:seen? int-tracker 42) false)
54+
"Checking an unrelated value."))
55+
56+
(let [table-tracker (tracker hash)
57+
a-table @{7 "seven"}]
58+
(assert (= (:seen? table-tracker a-table) false)
59+
"Checking a not-yet-seen value.")
60+
61+
(:see table-tracker a-table)
62+
63+
(assert (= (:seen? table-tracker a-table) true)
64+
"Checking a seen value.")
65+
66+
(assert (= (:seen? table-tracker @{42 "forty-two" 7 "seven"}) false)
67+
"Checking an unrelated value."))
68+
69+
(let [custom-tracker (tracker (fn [xs] (string/join xs "|")))]
70+
(assert (= (:seen? custom-tracker @["hello" "world"]) false)
71+
"Checking a not-yet-seen value.")
72+
73+
(:see custom-tracker @["hello" "world"])
74+
75+
(assert (= (:seen? custom-tracker @["hello" "world"]) true)
76+
"Checking a seen value.")
77+
78+
(assert (= (:seen? custom-tracker @["hello"]) false)
79+
"Checking an unrelated value."))

0 commit comments

Comments
 (0)