* rdfdb.el (rdfdb-remove-triple): Check whether the triple is already
authorueno <ueno>
Tue, 3 Feb 2004 09:57:07 +0000 (09:57 +0000)
committerueno <ueno>
Tue, 3 Feb 2004 09:57:07 +0000 (09:57 +0000)
indexed in database.

rdfdb.el

index 0da9aed..250b36d 100644 (file)
--- a/rdfdb.el
+++ b/rdfdb.el
@@ -351,58 +351,75 @@ MATCHER is a triple whose slots can be nil (wildcard)."
 
 (defun rdfdb-add-triple (database triple)
   "Add TRIPLE in DATABASE."
-  (let (key)
-    ;; Index by subject and property.
-    (rdfdb-puthash (setq key (rdfdb-get-key database
-                                           (rdfdb-triple-subject triple)))
-                  (cons triple
-                        (rdfdb-gethash key
-                                       (rdfdb-database-subject-index
-                                        database)))
-                  (rdfdb-database-subject-index database))
-    (rdfdb-puthash (setq key (rdfdb-get-key database
-                                           (rdfdb-triple-subject triple)
-                                           (rdfdb-triple-property triple)))
-                  (cons triple
-                        (rdfdb-gethash key
-                                       (rdfdb-database-subject-index
-                                        database)))
-                  (rdfdb-database-subject-index database))
-    ;; Index by property and object.
-    (rdfdb-puthash (setq key (rdfdb-get-key database
+  (let* ((subject-property-key
+         (rdfdb-get-key database
+                        (rdfdb-triple-subject triple)
+                        (rdfdb-triple-property triple)))
+        (property-object-key
+         (rdfdb-get-key database
+                        (rdfdb-triple-property triple)
+                        (rdfdb-triple-object triple)))
+        (object-subject-key
+         (rdfdb-get-key database
+                        (rdfdb-triple-object triple)
+                        (rdfdb-triple-subject triple)))
+        (subject-property-triples
+         (rdfdb-gethash subject-property-key
+                        (rdfdb-database-subject-index
+                         database)))
+        (property-object-triples
+         (rdfdb-gethash property-object-key
+                        (rdfdb-database-property-index
+                         database)))
+        (object-subject-triples
+         (rdfdb-gethash object-subject-key
+                        (rdfdb-database-object-index
+                         database)))
+        (minimum-length-triples subject-property-triples))
+    ;; Check whether triple is already in database.
+    (if (> minimum-length-triples
+          (length property-object-triples))
+       (setq minimum-length-triples property-object-triples))
+    (if (> minimum-length-triples
+          (length object-subject-triples))
+       (setq minimum-length-triples object-subject-triples))
+    (unless (member triple minimum-length-triples)
+      ;; Index by subject and property.
+      (rdfdb-puthash subject-property-key
+                    (cons triple subject-property-triples)
+                    (rdfdb-database-subject-index database))
+      (rdfdb-puthash (setq key (rdfdb-get-key database
+                                             (rdfdb-triple-subject triple)))
+                    (cons triple
+                          (rdfdb-gethash key
+                                         (rdfdb-database-subject-index
+                                          database)))
+                    (rdfdb-database-subject-index database))
+      ;; Index by property and object.
+      (rdfdb-puthash property-object-key
+                    (cons triple property-object-triples)
+                    (rdfdb-database-property-index database))
+      (rdfdb-puthash (setq key (rdfdb-get-key database
                                            (rdfdb-triple-property triple)))
-                  (cons triple
-                        (rdfdb-gethash key
-                                       (rdfdb-database-property-index
-                                        database)))
-                  (rdfdb-database-property-index database))
-    (rdfdb-puthash (setq key (rdfdb-get-key database
-                                           (rdfdb-triple-property triple)
-                                           (rdfdb-triple-object triple)))
-                  (cons triple
-                        (rdfdb-gethash key
-                                       (rdfdb-database-property-index
-                                        database)))
-                  (rdfdb-database-property-index database))
-    ;; Index by object and subject.
-    (rdfdb-puthash (setq key (rdfdb-get-key database
-                                           (rdfdb-triple-object triple)))
-                  (cons triple
-                        (rdfdb-gethash key
-                                       (rdfdb-database-object-index
-                                        database)))
-                  (rdfdb-database-object-index database))
-    (rdfdb-puthash (setq key (rdfdb-get-key database
-                                           (rdfdb-triple-object triple)
-                                           (rdfdb-triple-subject triple)))
-                  (cons triple
-                        (rdfdb-gethash key
-                                       (rdfdb-database-object-index
-                                        database)))
-                  (rdfdb-database-object-index database))
-    (rdfdb-database-set-triple-list
-     database
-     (cons triple (rdfdb-database-triple-list database)))))
+                    (cons triple
+                          (rdfdb-gethash key
+                                         (rdfdb-database-property-index
+                                          database)))
+                    (rdfdb-database-property-index database))
+      ;; Index by object and subject.
+      (rdfdb-puthash object-subject-key
+                    (cons triple object-subject-triples)
+                    (rdfdb-database-object-index database))
+      (rdfdb-puthash (setq key (rdfdb-get-key database
+                                             (rdfdb-triple-object triple)))
+                    (cons triple
+                          (rdfdb-gethash key
+                                         (rdfdb-database-object-index
+                                          database)))
+                    (rdfdb-database-object-index database))
+      (rdfdb-database-set-triple-list
+       database
+       (cons triple (rdfdb-database-triple-list database))))))
 
 (defun rdfdb-remove-triple (database triple)
   "Remove TRIPLE from DATABASE."