-
Notifications
You must be signed in to change notification settings - Fork 0
/
montezuma-store.lisp
81 lines (73 loc) · 2.68 KB
/
montezuma-store.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
(defpackage :slacker.montezuma-store
(:use :cl :alexandria :serapeum :fw.lu)
(:export
#:montezuma-store
#:search-index
#:retire-open-indices))
(in-package :slacker.montezuma-store)
(defclass montezuma-store ()
((%indexes :reader indexes
:initform (make-hash-table :test #'equal :synchronized t))
(%montezuma-index-path :reader index-path
:initarg :index-path))
(:default-initargs :index-path nil))
(defun retire-open-indices (client)
(sb-ext:with-locked-hash-table ((indexes client))
(let ((old-indexes (indexes client)))
(setf (slot-value client '%indexes)
(make-hash-table :test #'equal :synchronized t))
(alexandria:maphash-values (lambda (v) (montezuma:close v))
old-indexes))))
(defun search-index (store index text)
(let* ((index (ensure-index-for-type store index)))
(values
(montezuma:search index
(format nil "!text:\";arc\" text:~a" text)
:num-docs 3)
index)))
(defun ensure-index-for-type (store type)
(sb-ext:with-locked-hash-table ((indexes store))
(ensure-gethash type (indexes store)
(if (index-path store)
(make-instance 'montezuma:index
:path (ensure-directories-exist
(format nil "~a/~a/"
(index-path store)
type))
:create-p nil
:create-if-missing-p t)
(make-instance 'montezuma:index)))))
(defgeneric combine-child (parent key value)
(:method (parent k child)
(setf (gethash k parent) child))
(:method (parent k (child string))
(setf (gethash k parent) child))
(:method (parent k (children list))
(combine-child parent k (coerce children 'vector)))
(:method (parent k (children vector))
(map nil
(lambda (idx child)
(combine-child parent
(concat k "::" (princ-to-string idx))
child))
(iota (length children))
children))
(:method ((parent hash-table) k (child hash-table))
(do-hash-table (sk sv child)
(combine-child parent (concat k "::" sk) sv))))
(defun flatten-hash-table (hash-table)
(let ((new (fw.lu:empty-hash-table-like hash-table)))
(do-hash-table (k v hash-table new)
(combine-child new k v))))
(defgeneric store-message (store message)
(:method ((store montezuma-store) message)
(let* ((type (gethash "type" message))
(index (ensure-index-for-type store type)))
(montezuma:add-document-to-index index
(flatten-hash-table message))
(montezuma:flush index))))
(defmethod slacker:handle-message :before
(type (event-pump montezuma-store) ts channel message)
(declare (ignore type ts channel))
(store-message event-pump message)
(values))