;;;;
;;;; FILE
;;;;	rules/linf/rmgr.l
;;;;
;;;; DESCRIPTION
;;;;	Routines to process rules affecting a query.
;;;;
(defvar *RCS-rmgr*
  "$Header: rmgr.l,v 1.2 89/02/21 01:24:01 hirohama Exp $")
;;;;
;;;; EXPORTS
;;;;	myRelationGetRelationId
;;;;	rule-manager
;;;;	debug-rule-manager
;;;;	debug2-rule-manager

;;;
;;; global variables -- XXX note the unconventional names
;;;
(defvar first_time t)
(defvar depthOfRecursion 0)
(defvar timesCalled 0)
(defvar DebugLevel 0)
(defvar DebugFileName "/a/postgres/spyros/postgres/src/tcop/DebugSpec.l")


;;;
;;; ?
;;;
(defun myRelationGetRelationId (a)
  (RelationGetRelationId a))

;;;
;;; rule-manager
;;;
(defun rule-manager (command
		     user
		     relDesc
		     relId
		     oldTuple
		     oldBufferPage
		     newTuple
		     newBufferPage
		     attrDesc
		     targetList)

  (setq depthOfRecursion (+ depthOfRecursion 1))
  (setq timesCalled (+ 1 timesCalled))
  (if (and (not (equal command 'start)) (not (equal command 'end)))
      (setq relId (myRelationGetRelationId relDesc)))

  (cond (first_time
	 (print "RULE MANAGER: Initializing Debug Info....") (terpri)
	 (setq first_time nil)
	 (cond ((probef DebugFileName)
	        (load DebugFileName)
	        (print "DEBUG LEVEL = ") (print DebugLevel) (terpri)))))
    
  (prog (result thisTimeCalled thisDepthOfRecursion)
	;;
	;; NOTE: The buffer of the tuple returned will always be
	;; InvalidBuffer. This should change later on however...
	;;
	(setq thisTimeCalled timesCalled)
	(setq thisDepthOfRecursion depthOfRecursion)
	(if (>= DebugLevel 1)
	    (debug-rule-manager
	     command
	     user
	     relDesc
	     relId
	     oldTuple
	     oldBufferPage
	     newTuple
	     newBufferPage
	     attrDesc
	     targetList
	     thisDepthOfRecursion
	     thisTimeCalled))
	    
	(if (and (not (null oldTuple)) (not (eq 0 oldTuple)))
	    (progn
	      (setq oldTuple (palloctup oldTuple
					(vrefi-long oldBufferPage 0)
					relDesc))
	      (setq oldBufferPage (ReturnInvalidBuffer)))) ;why?

	(if (and (not (null newTuple))
		 (not (eq 0 newTuple))
		 (eq command 'replace))
	    (progn
	      (setq newBufferPage (ReturnInvalidBuffer)) ;why?
	      (setq newTuple (palloctup newTuple
					newBufferPage ;(vrefi-long) if fix (RIB)
					relDesc))))

	(setq result
	      (caseq command
		     ('retrieve
		      (DoRetrieve oldTuple
				  targetList
				  relDesc
				  relId
				  attrDesc
				  oldBufferPage))
		     ('delete
		      (DoDelete oldTuple
				targetList
				relDesc
				relId
				attrDesc
				oldBufferPage))
		     ('append
		      (DoAppend oldTuple
				targetList
				relDesc
				relId
				attrDesc
				oldBufferPage))
		     ('replace
		      (DoAppend newTuple
				targetList
				relDesc
				relId
				attrDesc
				newBufferPage))
		     (t
		      (list 'OK oldTuple oldBufferPage))))

;;; XXX Check the result and how it is used.
;;; XXX The buffer page returned may be of the incorrect format!

	(if (>= DebugLevel 1)
	    (debug2-rule-manager result
				 attrDesc
				 thisDepthOfRecursion
				 thisTimeCalled))
	(setq depthOfRecursion (- depthOfRecursion 1))
	(return result)))

(defun debug-rule-manager (command
			   user
			   relDesc
			   relId
			   oldTuple
			   oldBufferPage
			   newTuple
			   newBufferPage
			   attrDesc
			   targetList
			   thisDepthOfRecursion
			   thisTimeCalled)
  (print '------------------ ) (terpri)
  (print 'RULE_MANAGER_CALLED) (terpri)
  (print 'depthOfRecursion=) (print thisDepthOfRecursion) (terpri)
  (print 'timesCalled=) (print thisTimeCalled) (terpri)
  (print 'command=) (print command) (terpri)
  (print 'targetList=) (print targetList) (terpri)
  (print 'oldTuple=)
  (if (or (null oldTuple) (eq 0 oldTuple))
      (progn
	(print 'NIL) (terpri))
    (progn
      (debugtup oldTuple attrDesc)
      ;; Print RuleLocks
      (print "----LOCKS (old Tuple):") (terpri)
      (PrintTupleLocks oldTuple oldBufferPage relDesc)))

  (print 'newTuple= )
  (if (or (null newTuple) (eq 0 newTuple))
      (progn
	(print 'NIL) (terpri))
    (progn
      (debugtup newTuple attrDesc)
      ;; Print RuleLocks
      (print "----LOCKS (new Tuple):") (terpri)
      (PrintTupleLocks newTuple newBufferPage relDesc)))

  (terpri))

(defun debug2-rule-manager (result
			    attrDesc
			    thisDepthOfRecursion
			    thisTimeCalled)
  (print 'RULE_MANAGER_FINISHED) (terpri)
  (print 'depthOfRecursion=) (print thisDepthOfRecursion) (terpri)
  (print 'timesCalled=) (print thisTimeCalled) (terpri)
  (print 'returnedTuple=)
  (if (null (cadr result))
      (print 'NIL)
    (debugtup (cadr result) attrDesc))
  (terpri)
  (print '------------------) (terpri))
