Ignore:
Timestamp:
08/03/11 08:46:00 (14 years ago)
Author:
lgiessmann
Message:

trunk: rest-interface: changed the usage of reader- and writrer-locks in the rest-handlers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/src/rest_interface/set-up-json-interface.lisp

    r644 r704  
    178178   topictype-constraint (if it exists) and the can't be abstract."
    179179  (declare (ignorable param))
    180   (handler-case (let ((topic-types
    181                          (with-reader-lock
    182                            (map 'list #'(lambda (oid)
    183                                           (elephant::controller-recreate-instance
    184                                            elephant::*store-controller* oid))
    185                                 *type-table*))))
    186                   (setf (hunchentoot:content-type*) "application/json") ;RFC 4627
    187                   (json:encode-json-to-string
    188                    (map 'list #'(lambda(y)
    189                                   (map 'list #'uri y))
    190                         (map 'list #'psis topic-types))))
     180  (handler-case (with-reader-lock
     181                  (let ((topic-types
     182                         (map 'list #'(lambda (oid)
     183                                        (elephant::controller-recreate-instance
     184                                         elephant::*store-controller* oid))
     185                              *type-table*)))
     186                    (setf (hunchentoot:content-type*) "application/json") ;RFC 4627
     187                    (json:encode-json-to-string
     188                     (map 'list #'(lambda(y)
     189                                    (map 'list #'uri y))
     190                          (map 'list #'psis topic-types)))))
    191191    (condition (err) (progn
    192192                       (setf (hunchentoot:return-code*) hunchentoot:+http-internal-server-error+)
     
    200200   type-instance or supertype-subtype."
    201201  (declare (ignorable param))
    202   (handler-case (let ((topic-instances
    203                          (with-reader-lock
    204                            (map 'list #'(lambda (oid)
    205                                           (elephant::controller-recreate-instance
    206                                            elephant::*store-controller* oid))
    207                                 *instance-table*))))
    208                   (setf (hunchentoot:content-type*) "application/json") ;RFC 4627
    209                   (json:encode-json-to-string
    210                    (map 'list #'(lambda(y)
    211                                   (map 'list #'uri y))
    212                         (map 'list #'psis topic-instances))))
     202  (handler-case (with-reader-lock
     203                  (let ((topic-instances
     204                         (map 'list #'(lambda (oid)
     205                                        (elephant::controller-recreate-instance
     206                                         elephant::*store-controller* oid))
     207                              *instance-table*)))
     208                    (setf (hunchentoot:content-type*) "application/json") ;RFC 4627
     209                    (json:encode-json-to-string
     210                     (map 'list #'(lambda(y)
     211                                    (map 'list #'uri y))
     212                          (map 'list #'psis topic-instances)))))
    213213    (condition (err) (progn
    214214                       (setf (hunchentoot:return-code*) hunchentoot:+http-internal-server-error+)
     
    221221   passed psi as a topic-stub-construct."
    222222  (assert psi)
    223   (let ((topic (d:get-item-by-psi psi)))
    224     (if topic
    225         (let ((topic-json
    226                (handler-case
    227                    (with-reader-lock
     223  (with-reader-lock
     224    (let ((topic (d:get-item-by-psi psi)))
     225      (if topic
     226          (handler-case
     227              (progn (setf (hunchentoot:content-type*) "application/json") ;RFC 4627
    228228                     (json-exporter::to-json-topicStub-string topic :revision 0))
    229                  (condition (err) (progn
    230                                     (setf (hunchentoot:return-code*) hunchentoot:+http-internal-server-error+)
    231                                     (setf (hunchentoot:content-type*) "text")
    232                                     (format nil "Condition: \"~a\"" err))))))
    233           (setf (hunchentoot:content-type*) "application/json") ;RFC 4627
    234           topic-json)
    235         (progn
    236           (setf (hunchentoot:return-code*) hunchentoot:+http-not-found+)
    237           (setf (hunchentoot:content-type*) "text")
    238           (format nil "Condition: Topic \"~a\" not found" psi)))))
     229            (condition (err)
     230              (progn
     231                (setf (hunchentoot:return-code*)
     232                      hunchentoot:+http-internal-server-error+)
     233                (setf (hunchentoot:content-type*) "text")
     234                (format nil "Condition: \"~a\"" err))))
     235          (progn
     236            (setf (hunchentoot:return-code*) hunchentoot:+http-not-found+)
     237            (setf (hunchentoot:content-type*) "text")
     238            (format nil "Condition: Topic \"~a\" not found" psi))))))
    239239
    240240
     
    251251                                            :force-text t)))
    252252            (handler-case
    253                 (let ((psis
    254                        (json:decode-json-from-string json-data)))
    255                   (let ((tmcl
    256                          (with-reader-lock
    257                            (json-tmcl:get-constraints-of-fragment
    258                             psis :treat-as treat-as :revision 0))))
    259                     (if tmcl
    260                         (progn
    261                           (setf (hunchentoot:content-type*)
    262                                 "application/json") ;RFC 4627
    263                           tmcl)
    264                         (progn
    265                           (setf (hunchentoot:return-code*)
    266                                 hunchentoot:+http-not-found+)
    267                           (setf (hunchentoot:content-type*) "text")
    268                           (format nil "Topic \"~a\" not found." psis)))))
     253                (with-reader-lock
     254                  (let ((psis (json:decode-json-from-string json-data)))
     255                    (let ((tmcl (json-tmcl:get-constraints-of-fragment
     256                                 psis :treat-as treat-as :revision 0)))
     257                      (if tmcl
     258                          (progn
     259                            (setf (hunchentoot:content-type*)
     260                                  "application/json") ;RFC 4627
     261                            tmcl)
     262                          (progn
     263                            (setf (hunchentoot:return-code*)
     264                                  hunchentoot:+http-not-found+)
     265                            (setf (hunchentoot:content-type*) "text")
     266                            (format nil "Topic \"~a\" not found." psis))))))
    269267              (condition ()
    270268                (setf (hunchentoot:content-type*) "application/json") ;RFC 4627
     
    281279        (progn
    282280          (setf (hunchentoot:content-type*) "application/json") ;RFC 4627
    283           (handler-case (with-reader-lock
    284                           (get-all-topic-psis :revision 0))
     281          (handler-case
     282              (with-reader-lock
     283                (get-all-topic-psis :revision 0))
    285284            (condition (err) (progn
    286                                (setf (hunchentoot:return-code*) hunchentoot:+http-internal-server-error+)
     285                               (setf (hunchentoot:return-code*)
     286                                     hunchentoot:+http-internal-server-error+)
    287287                               (setf (hunchentoot:content-type*) "text")
    288288                               (format nil "Condition: \"~a\"" err)))))
     
    299299        (let ((identifier (string-replace psi "%23" "#")))
    300300          (setf (hunchentoot:content-type*) "application/json") ;RFC 4627
    301           (let ((fragment
    302                  (with-reader-lock
    303                    (get-latest-fragment-of-topic identifier))))
    304             (if (and fragment
    305                      (find-item-by-revision (topic fragment) 0))
    306                 (handler-case (with-reader-lock
    307                                 (export-construct-as-isidorus-json-string
    308                                  fragment :revision 0))
    309                   (condition (err)
    310                     (progn
    311                       (setf (hunchentoot:return-code*) hunchentoot:+http-internal-server-error+)
    312                       (setf (hunchentoot:content-type*) "text")
    313                       (format nil "Condition: \"~a\"" err))))
    314                 (progn
    315                   (setf (hunchentoot:return-code*) hunchentoot:+http-not-found+)
    316                   (setf (hunchentoot:content-type*) "text")
    317                   (format nil "Topic \"~a\" not found" psi)))))
     301          (with-reader-lock
     302            (let ((fragment (get-latest-fragment-of-topic identifier)))
     303              (if (and fragment (find-item-by-revision (topic fragment) 0))
     304                  (handler-case
     305                      (export-construct-as-isidorus-json-string
     306                       fragment :revision 0)
     307                    (condition (err)
     308                      (progn
     309                        (setf (hunchentoot:return-code*)
     310                              hunchentoot:+http-internal-server-error+)
     311                        (setf (hunchentoot:content-type*) "text")
     312                        (format nil "Condition: \"~a\"" err))))
     313                  (progn
     314                    (setf (hunchentoot:return-code*) hunchentoot:+http-not-found+)
     315                    (setf (hunchentoot:content-type*) "text")
     316                    (format nil "Topic \"~a\" not found" psi))))))
    318317        (setf (hunchentoot:return-code*) hunchentoot:+http-bad-request+))))
    319318
     
    326325        (let ((identifier (string-replace psi "%23" "#")))
    327326          (setf (hunchentoot:content-type*) "application/json") ;RFC 4627
    328           (let ((fragment
    329                  (with-reader-lock
    330                    (get-latest-fragment-of-topic identifier))))
    331             (if (and fragment
    332                      (find-item-by-revision (topic fragment) 0))
    333                 (handler-case (with-reader-lock
    334                                 (rdf-exporter:to-rdf-string fragment))
    335                   (condition (err)
    336                     (progn
    337                       (setf (hunchentoot:return-code*) hunchentoot:+http-internal-server-error+)
    338                       (setf (hunchentoot:content-type*) "text")
    339                       (format nil "Condition: \"~a\"" err))))
    340                 (progn
    341                   (setf (hunchentoot:return-code*) hunchentoot:+http-not-found+)
    342                   (setf (hunchentoot:content-type*) "text")
    343                   (format nil "Topic \"~a\" not found" psi)))))
     327          (with-reader-lock
     328            (let ((fragment (get-latest-fragment-of-topic identifier)))
     329              (if (and fragment (find-item-by-revision (topic fragment) 0))
     330                  (handler-case
     331                      (rdf-exporter:to-rdf-string fragment)
     332                    (condition (err)
     333                      (progn
     334                        (setf (hunchentoot:return-code*)
     335                              hunchentoot:+http-internal-server-error+)
     336                        (setf (hunchentoot:content-type*) "text")
     337                        (format nil "Condition: \"~a\"" err))))
     338                  (progn
     339                    (setf (hunchentoot:return-code*) hunchentoot:+http-not-found+)
     340                    (setf (hunchentoot:content-type*) "text")
     341                    (format nil "Topic \"~a\" not found" psi))))))
    344342        (setf (hunchentoot:return-code*) hunchentoot:+http-bad-request+))))
    345343
     
    352350    (if (or (eq http-method :PUT)
    353351            (eq http-method :POST))
    354         (let ((external-format (flexi-streams:make-external-format :UTF-8 :eol-style :LF)))
    355           (let ((json-data (hunchentoot:raw-post-data :external-format external-format :force-text t)))
    356             (handler-case
    357                 (with-writer-lock
     352        (let ((external-format
     353               (flexi-streams:make-external-format :UTF-8 :eol-style :LF)))
     354          (let ((json-data
     355                 (hunchentoot:raw-post-data :external-format external-format
     356                                            :force-text t)))
     357            (with-writer-lock
     358              (handler-case
    358359                  (let ((frag (json-importer:import-from-isidorus-json json-data)))
    359360                    (when frag
    360                       (push-to-cache (d:topic frag)))))
    361               (condition (err)
    362                 (progn
    363                   (setf (hunchentoot:return-code*) hunchentoot:+http-internal-server-error+)
    364                   (setf (hunchentoot:content-type*) "text")
    365                   (format nil "Condition: \"~a\"" err))))))
     361                      (push-to-cache (d:topic frag))))
     362                (condition (err)
     363                  (progn
     364                    (setf (hunchentoot:return-code*)
     365                          hunchentoot:+http-internal-server-error+)
     366                    (setf (hunchentoot:content-type*) "text")
     367                    (format nil "Condition: \"~a\"" err)))))))
    366368        (setf (hunchentoot:return-code*) hunchentoot:+http-bad-request+))))
    367369
     
    376378         (handler-case (parse-integer (hunchentoot:get-parameter "end"))
    377379           (condition () nil))))
    378     (handler-case (with-reader-lock
    379                     (let ((topics
    380                            (remove-null
    381                             (map 'list
    382                                  #'(lambda(top)
    383                                      (when (find-item-by-revision top 0)
    384                                        top))
    385                                  (elephant:get-instances-by-class 'd:TopicC)))))
    386                       (let ((end
    387                              (cond
    388                                ((not end-idx)
    389                                 (length topics))
    390                                ((> end-idx (length topics))
    391                                 (length topics))
    392                                ((< end-idx 0)
    393                                 0)
    394                                (t
    395                                 end-idx))))
    396                         (let ((start
    397                                (cond
    398                                  ((> start-idx (length topics))
    399                                   end)
    400                                  ((< start-idx 0)
    401                                   0)
    402                                  (t
    403                                   start-idx))))
    404                           (let ((topics-in-range
    405                                  (if (<= start end)
    406                                      (subseq topics start end)
    407                                      (reverse (subseq topics end start)))))
    408                             (setf (hunchentoot:content-type*) "application/json") ;RFC 4627
    409                             (json-exporter:make-topic-summary topics-in-range))))))
    410       (condition (err) (progn
    411                          (setf (hunchentoot:return-code*) hunchentoot:+http-internal-server-error+)
    412                          (setf (hunchentoot:content-type*) "text")
    413                          (format nil "Condition: \"~a\"" err))))))
     380    (with-reader-lock
     381      (handler-case
     382          (let ((topics
     383                 (remove-null
     384                  (map 'list
     385                       #'(lambda(top)
     386                           (when (find-item-by-revision top 0)
     387                             top))
     388                       (elephant:get-instances-by-class 'd:TopicC)))))
     389            (let ((end
     390                   (cond
     391                     ((not end-idx)
     392                      (length topics))
     393                     ((> end-idx (length topics))
     394                      (length topics))
     395                     ((< end-idx 0)
     396                      0)
     397                     (t
     398                      end-idx))))
     399              (let ((start
     400                     (cond
     401                       ((> start-idx (length topics))
     402                        end)
     403                       ((< start-idx 0)
     404                        0)
     405                       (t
     406                        start-idx))))
     407                (let ((topics-in-range
     408                       (if (<= start end)
     409                           (subseq topics start end)
     410                           (reverse (subseq topics end start)))))
     411                  (setf (hunchentoot:content-type*) "application/json") ;RFC 4627
     412                  (json-exporter:make-topic-summary topics-in-range)))))
     413        (condition (err) (progn
     414                           (setf (hunchentoot:return-code*)
     415                                 hunchentoot:+http-internal-server-error+)
     416                           (setf (hunchentoot:content-type*) "text")
     417                           (format nil "Condition: \"~a\"" err)))))))
    414418
    415419
     
    437441    (if (or (eq http-method :DELETE)
    438442            (eq http-method :POST)) ;not nice - but the current ui-library can't send http-delete messages
    439         (let ((external-format (flexi-streams:make-external-format :UTF-8 :eol-style :LF)))
    440           (let ((json-data (hunchentoot:raw-post-data :external-format external-format :force-text t)))
    441             (handler-case
    442                 (with-writer-lock
     443        (let ((external-format
     444               (flexi-streams:make-external-format :UTF-8 :eol-style :LF)))
     445          (let ((json-data
     446                 (hunchentoot:raw-post-data :external-format external-format
     447                                            :force-text t)))
     448            (with-writer-lock
     449              (handler-case
    443450                  (let ((result (json-delete-interface:mark-as-deleted-from-json
    444451                                 json-data :revision (d:get-revision))))
     
    453460                        (progn
    454461                          (setf (hunchentoot:return-code*) hunchentoot:+http-not-found+)
    455                           (format nil "object not found")))))
    456               (condition (err)
    457                 (progn
    458                   (setf (hunchentoot:return-code*) hunchentoot:+http-internal-server-error+)
    459                   (setf (hunchentoot:content-type*) "text")
    460                   (format nil "Condition: \"~a\"" err))))))
     462                          (format nil "object not found"))))
     463                (condition (err)
     464                  (progn
     465                    (setf (hunchentoot:return-code*)
     466                          hunchentoot:+http-internal-server-error+)
     467                    (setf (hunchentoot:content-type*) "text")
     468                    (format nil "Condition: \"~a\"" err)))))))
    461469        (setf (hunchentoot:return-code*) hunchentoot:+http-bad-request+))))
    462470
Note: See TracChangeset for help on using the changeset viewer.