;; elephant-site.lisp --- ;; Copyright (C) 2008 Clinton Ebadi ;; Author: Clinton Ebadi ;; This program is free software: you can redistribute it and/or modify ;; it under the terms of the GNU Lesser General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU Lesser General Public License ;; along with this program. If not, see . (in-package :org.unknownlamer.golgonooza.elephant) (defclass elephant-store-site-mixin () ((store-spec :accessor site-store-spec :initarg :store-spec :documentation "Either an elephant database spec or a pathname. If the store spec is a list then it is passed directly to elephant:open-store. If the store spec is a pathname it is assumed to be a BDB backed db in the DATA-ROOT of the site.") (store :reader site-store-controller :initform nil)) (:documentation "Automatically open and close an Elephant data store when starting and stopping an application")) (defmethod start-site :before ((site elephant-store-site-mixin)) (with-slots (store) site (unless store (setf store (open-store (let ((db-spec (site-store-spec site))) (if (listp db-spec) db-spec (list :bdb (data-root-path site db-spec)))) :recover t :deadlock-detect t))))) (defmethod stop-site :after ((site elephant-store-site-mixin)) (with-slots (store) site (when store (close-store store) (if (eq *store-controller* store) (setq *store-controller* nil)) (setf store nil)))) (defclass elephant-store-application-mixin () ((store-spec :accessor app-store-spec :initarg :store-spec :initform nil :documentation "An elephant database spec to be passed directly to elephant:open-store.") (store :accessor app-store-controller :initform nil)) (:documentation "Open and close an elephant database at application startup and shutdown and bind elephant:*store-controller* around actions.")) (defmethod ucw-core:startup-application :before ((app elephant-store-application-mixin)) (unless (app-store-controller app) (setf (app-store-controller app) (apply #'open-store (app-store-spec app))))) (defmethod ucw-core:shutdown-application :after ((app elephant-store-application-mixin)) (when (app-store-controller app) (close-store (app-store-controller app)))) (defmethod ucw-core:handle-action :around (action (app elephant-store-application-mixin) session frame) (let ((*store-controller* (app-store-controller app))) (call-next-method))) (defmethod start-site-application :before ((site elephant-store-site-mixin) (app elephant-store-application-mixin)) (unless (app-store-spec app) (setf (app-store-controller app) (site-store-controller site)))) (defmethod stop-site-application :before ((site elephant-store-site-mixin) (app elephant-store-application-mixin)) (unless (app-store-spec app) (setf (app-store-controller app) nil)))