Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
begin binary protocol and client. move away from Montezuma for full t…
…ext indexing (not thread safe).
- Loading branch information
Showing
16 changed files
with
574 additions
and
181 deletions.
There are no files selected for viewing
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
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,33 @@ | |||
(in-package #:vivace-graph-client) | |||
|
|||
(defparameter +max-bytes+ 40960) | |||
(defparameter +magic-byte+ #x2A) | |||
(defparameter +version+ #x01) | |||
(defparameter +ack+ #x01) | |||
(defparameter +success+ #x00) | |||
(defparameter +success-no-results+ #x01) | |||
(defparameter +error+ #x02) | |||
(defparameter +retry+ #x03) | |||
(defparameter +authentication-error+ #x04) | |||
(defparameter +success+ #x05) | |||
(defparameter +hello+ #x06) | |||
(defparameter +quit+ #xFF) | |||
|
|||
(defparameter +select+ #x00) | |||
(defparameter +select-flat+ #x01) | |||
(defparameter +<-+ #x02) | |||
(defparameter +insert+ #x03) | |||
(defparameter +do-query+ #x04) | |||
|
|||
(define-condition client-error (error) | |||
((reason :initarg :reason)) | |||
(:report (lambda (error stream) | |||
(with-slots (reason) error | |||
(format stream "Vivace-graph client error: ~A." reason))))) | |||
|
|||
(define-condition authentication-error (error) | |||
((reason :initarg :reason)) | |||
(:report (lambda (error stream) | |||
(with-slots (reason) error | |||
(format stream "Vivace-graph client authentication error: ~A." reason))))) | |||
|
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,68 @@ | |||
(in-package #:vivace-graph) | |||
|
|||
(defstruct (session | |||
(:predicate session?)) | |||
socket | |||
stream | |||
vars | |||
query | |||
pointer | |||
version) | |||
|
|||
(defun authenticate (username password graph) | |||
(declare (ignore username password graph)) | |||
t) | |||
|
|||
(defmethod get-byte ((socket usocket:socket)) | |||
(read-byte (usocket:socket-stream socket) nil :eof)) | |||
|
|||
(defmethod get-byte ((session session)) | |||
(read-byte (session-stream session) nil :eof)) | |||
|
|||
(defun connect-to-server (ip port) | |||
(handler-case | |||
(let ((socket (usocket:socket-connect | |||
ip port :protocol :tcp :element-type '(unsigned-byte 8) :timeout 600))) | |||
(if socket | |||
(progn | |||
(write-byte +magic-byte+ (socket-stream socket)) | |||
(write-byte +version+ (socket-stream socket)) | |||
(force-output (socket-stream socket)) | |||
(let ((response (get-byte socket))) | |||
(cond ((eq response +ack+) t) | |||
((eq response :eof) (error "Server disconnected!")) | |||
(t (error "Invalid ACK from server: ~A" response)))) | |||
(let ((magic-byte (get-byte socket)) (version (get-byte socket))) | |||
(if (and (eq +magic-byte+ magic-byte) (eq +version+ version)) | |||
(make-session :socket socket :version +version+ :stream (socket-stream socket)) | |||
(error "Unknown greeting from server: ~A ~A" magic-byte version)))) | |||
(error "Unable to connect to ~A:~A" ip port))) | |||
(error (condition) | |||
(error 'client-error :reason condition)))) | |||
|
|||
(defmethod disconnect-from-server ((session session)) | |||
(handler-case | |||
(progn | |||
(write-byte +magic-byte+ (session-stream session)) | |||
(write-byte +quit+ (session-stream session)) | |||
(force-output (session-stream session)) | |||
(let ((response (get-byte session))) | |||
(cond ((or (eq response +ack+) (eq response :eof)) | |||
(ignore-errors (close (session-stream session))) | |||
(setf (session-socket session) nil | |||
(session-stream session) nil)) | |||
(t (error "Invalid ACK from server on request to quit: ~A" response))))) | |||
(error (condition) | |||
(ignore-errors (close (session-stream session))) | |||
(error 'client-error :reason condition)))) | |||
|
|||
(defmethod set-vars ((session session) vars) | |||
(setf (session-vars session) (make-hash-table)) | |||
(loop for i from 0 to (list-length vars) do | |||
(setf (gethash i (session-vars session)) (nth i vars) | |||
(gethash (nth i vars) (session-vars session)) i))) | |||
|
|||
(defmethod select ((session session) vars &rest goals) | |||
(set-vars session vars) | |||
(let ((goals (substitute-vars session vars goals))) | |||
|
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,116 @@ | |||
(in-package #:vivace-graph) | |||
|
|||
(defun tokenize-string (seq) | |||
(remove-if #'(lambda (token) | |||
(< (length token) 3)) | |||
(tokenize seq | |||
:escapes #\\ | |||
:multi-escapes "\"|" | |||
:delimiters (format nil "_,:~A" #\Tab) | |||
:terminators "" | |||
:punctuation "[]()!.?~`;'<>/+=-*&^%$#@" | |||
:whitespace :whitespace | |||
:defaults (let ((i 0)) | |||
(lambda () (incf i)))))) | |||
|
|||
(defmethod index-text ((triple triple)) | |||
(with-transaction ((full-text-idx *graph*)) | |||
(when (stringp (triple-subject triple)) | |||
(dolist (token (remove-duplicates (tokenize-string (triple-subject triple)) :test 'equalp)) | |||
(set-btree (full-text-idx *graph*) | |||
(make-slot-key "s" (string-downcase token)) | |||
(triple-uuid triple) :mode :concat))) | |||
(when (stringp (triple-object triple)) | |||
(dolist (token (remove-duplicates (tokenize-string (triple-object triple)) :test 'equalp)) | |||
(set-btree (full-text-idx *graph*) | |||
(make-slot-key "o" (string-downcase token)) | |||
(triple-uuid triple) :mode :concat))))) | |||
|
|||
(defmethod deindex-text ((triple triple)) | |||
(with-transaction ((full-text-idx *graph*)) | |||
(when (stringp (triple-subject triple)) | |||
(dolist (token (remove-duplicates (tokenize-string (triple-subject triple)) :test 'equalp)) | |||
(rem-btree (full-text-idx *graph*) | |||
(make-slot-key "s" (string-downcase token)) | |||
(triple-uuid triple)))) | |||
(when (stringp (triple-object triple)) | |||
(dolist (token (remove-duplicates (tokenize-string (triple-object triple)) :test 'equalp)) | |||
(rem-btree (full-text-idx *graph*) | |||
(make-slot-key "o" (string-downcase token)) | |||
(triple-uuid triple)))))) | |||
|
|||
(defun full-text-search (string &key subject? object?) | |||
(let ((result nil)) | |||
(dolist (token (remove-duplicates (tokenize-string string) :test 'equalp)) | |||
(when subject? | |||
(let ((klist (get-btree (full-text-idx *graph*) | |||
(make-slot-key "s" (string-downcase token)) :mode :klist))) | |||
(when (klist? klist) | |||
(unwind-protect | |||
(map-klist #'(lambda (id) | |||
(format t "GOT ~A~%" (lookup-triple-by-id id)) | |||
(pushnew (lookup-triple-by-id id) result)) klist) | |||
(klist-free klist))))) | |||
(when object? | |||
(let ((klist (get-btree (full-text-idx *graph*) | |||
(make-slot-key "o" (string-downcase token)) :mode :klist))) | |||
(when (klist? klist) | |||
(unwind-protect | |||
(map-klist #'(lambda (id) | |||
(format t "GOT ~A~%" (lookup-triple-by-id id)) | |||
(pushnew (lookup-triple-by-id id) result)) klist) | |||
(klist-free klist)))))) | |||
result)) | |||
|
|||
|
|||
|
|||
|
|||
|
|||
#| | |||
(defmethod index-text ((triple triple)) | |||
(when (or (stringp (triple-subject triple)) | |||
(stringp (triple-object triple))) | |||
(let ((doc (make-instance 'montezuma:document))) | |||
(montezuma:add-field | |||
doc (montezuma:make-field "uuid" | |||
(format nil "~A" (triple-uuid triple)) | |||
:stored t :index :untokenized)) | |||
(when (stringp (triple-subject triple)) | |||
(montezuma:add-field | |||
doc (montezuma:make-field "subject" | |||
(triple-subject triple) | |||
:stored t :index :tokenized))) | |||
(when (stringp (triple-object triple)) | |||
(montezuma:add-field | |||
doc (montezuma:make-field "object" | |||
(triple-object triple) | |||
:stored t :index :tokenized))) | |||
(with-recursive-lock-held ((full-text-lock *graph*)) | |||
(montezuma:add-document-to-index (full-text-idx *graph*) doc))))) | |||
(defmethod deindex-text ((triple triple)) | |||
(let ((docs nil)) | |||
(with-recursive-lock-held ((full-text-lock *graph*)) | |||
(montezuma:search-each | |||
(full-text-idx *graph*) | |||
(uuid:print-bytes nil (triple-uuid triple)) | |||
#'(lambda (doc score) (declare (ignore score)) (push doc docs))) | |||
(dolist (doc docs) | |||
(montezuma:delete-document (full-text-idx *graph*) doc))))) | |||
(defun map-text-search (string fn &key collect) | |||
(let ((result nil)) | |||
(with-recursive-lock-held ((full-text-lock *graph*)) | |||
(montezuma:search-each | |||
(full-text-idx *graph*) | |||
string | |||
#'(lambda (doc score) | |||
(declare (ignore score)) | |||
(let ((r | |||
(funcall fn | |||
(lookup-triple-by-id | |||
(montezuma:document-value | |||
(montezuma:get-document (full-text-idx *graph*) doc) "uuid"))))) | |||
(if collect (push r result))))) | |||
(nreverse result)))) | |||
|# |
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
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
Oops, something went wrong.