Back to index

lightning-sunbird  0.9+nobinonly
remstore.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include "rdf-int.h"
00038 #include "gs.h"
00039 
00040 int
00041 asEqual(RDFT r, Assertion as, RDF_Resource u, RDF_Resource s, void* v, 
00042               RDF_ValueType type)
00043 {
00044   return (((r == NULL) || (as->db == r)) && 
00045          (as->u == u) && 
00046          (as->s == s) && 
00047          (as->type == type) && 
00048          ((as->value == v) || 
00049           ((type == RDF_STRING_TYPE) && (strcmp((char*)v, (char*)as->value) == 0))));
00050 }
00051 
00052 
00053 
00054 Assertion
00055 makeNewAssertion (RDFT r, RDF_Resource u, RDF_Resource s, void* v, 
00056                          RDF_ValueType type, int tv)
00057 {
00058   Assertion newAs = (Assertion) fgetMem(sizeof(RDF_AssertionStruct));
00059   newAs->u = u;
00060   newAs->s = s;
00061   newAs->value = v;
00062   newAs->type = type;
00063   newAs->db = r;
00064   return newAs;
00065 }
00066 
00067 
00068 void
00069 addToAssertionList (RDFT f, Assertion as)
00070 {
00071   if (f->assertionListCount >= f->assertionListSize) {
00072     f->assertionList = (Assertion*) realloc(f->assertionList, 
00073                             (sizeof(Assertion*) *
00074                                 (f->assertionListSize = 
00075                                  f->assertionListSize + GROW_LIST_INCR)));
00076   }
00077   *(f->assertionList + f->assertionListCount++) = as;
00078 }
00079 
00080 void
00081 freeAssertion (Assertion as)
00082 {
00083   if (as->type == RDF_STRING_TYPE) {
00084     freeMem(as->value);
00085   } 
00086   freeMem(as);
00087 }
00088 
00089 
00090 Assertion
00091 remoteStoreAdd (RDFT mcf, RDF_Resource u, RDF_Resource s, void* v, 
00092                        RDF_ValueType type, int tv)
00093 {
00094   Assertion  newAs = makeNewAssertion(mcf, u, s, v, type, tv);
00095   newAs->next = u->rarg1;
00096   u->rarg1 = newAs;
00097 
00098   if (type == RDF_RESOURCE_TYPE) {
00099     RDF_Resource iu = (RDF_Resource)v;
00100     newAs->invNext  = iu->rarg2;
00101     iu->rarg2       = newAs;
00102   }
00103   /*  if (type == RDF_STRING_TYPE)   RDFGS_AddSearchIndex(mcf, (char*) v, s, u); */
00104   
00105   return newAs;
00106 }
00107 
00108 
00109 Assertion
00110 remoteStoreRemove (RDFT mcf, RDF_Resource u, RDF_Resource s,
00111                           void* v, RDF_ValueType type)
00112 {
00113   Assertion nextAs, prevAs, ans;
00114   int found = false;
00115   nextAs = prevAs = u->rarg1;
00116   while (nextAs != null) {
00117     if (asEqual(mcf, nextAs, u, s, v, type)) {
00118       if (prevAs == nextAs) {
00119        u->rarg1 = nextAs->next;
00120       } else {
00121        prevAs->next = nextAs->next;
00122       }
00123       found = true;
00124       ans = nextAs;
00125       break;
00126     }
00127     prevAs = nextAs;
00128     nextAs = nextAs->next; 
00129   }
00130   if (found == false) return null;
00131   if (type == RDF_RESOURCE_TYPE) {
00132     nextAs = prevAs = ((RDF_Resource)v)->rarg2;
00133     while (nextAs != null) {
00134       if (nextAs == ans) {
00135        if (prevAs == nextAs) {
00136          ((RDF_Resource)v)->rarg2 = nextAs->invNext;
00137        } else {
00138          prevAs->invNext = nextAs->invNext;
00139        }
00140       }
00141       prevAs = nextAs;
00142       nextAs = nextAs->invNext;
00143     }
00144   }
00145   return ans;
00146 }
00147 
00148 int
00149 remoteStoreHasAssertion (RDFT mcf, RDF_Resource u, RDF_Resource s, void* v, RDF_ValueType type, int tv)
00150 {
00151   Assertion nextAs;
00152   
00153   nextAs = u->rarg1;
00154   while (nextAs != null) {
00155     if (asEqual(mcf, nextAs, u, s, v, type)) return true;
00156     nextAs = nextAs->next;
00157   }
00158   return false;
00159 }
00160 
00161 
00162 
00163 void *
00164 getSlotValue (RDFT mcf, RDF_Resource u, RDF_Resource s, RDF_ValueType type, int inversep,  int tv)
00165 {
00166   Assertion nextAs;
00167 
00168   nextAs = (inversep ? u->rarg2 : u->rarg1);
00169   while (nextAs != null) {
00170     if (((nextAs->db == mcf) || (!mcf)) && (nextAs->s == s) 
00171         && (nextAs->type == type)) {
00172       return (inversep ? nextAs->u : nextAs->value);
00173     }
00174     nextAs = (inversep ? nextAs->invNext : nextAs->next);
00175   }
00176   return null;
00177 }
00178 
00179 RDF_Cursor
00180 getSlotValues (RDFT mcf, RDF_Resource u, RDF_Resource s, RDF_ValueType type,  int inversep, int tv)
00181 {
00182   Assertion as = (inversep ? u->rarg2 : u->rarg1);
00183   RDF_Cursor c;
00184   if (!as) return NULL;
00185   c = (RDF_Cursor)getMem(sizeof(RDF_CursorStruct));
00186   c->u = u;
00187   c->s = s;
00188   c->type = type;
00189   c->inversep = inversep;
00190   c->count = 0;
00191   c->db = mcf;
00192   c->pdata = as;
00193   c->queryType = GET_SLOT_VALUES; 
00194   return c;
00195 }
00196 
00197 void *
00198 nextValue (RDF_Cursor c) {
00199   if (!c) return null;
00200   while (c->pdata != null) {
00201     Assertion as = (Assertion) c->pdata;
00202     if (((as->db == c->db) || (!c->db)) && (as->s == c->s) &&  (c->type == as->type)) {
00203       c->value = (c->inversep ? as->u : as->value);
00204       c->pdata = (c->inversep ? as->invNext : as->next);
00205       return c->value;
00206     }
00207     c->pdata = (c->inversep ? as->invNext : as->next);
00208   }
00209   return null;
00210 }
00211 
00212 void
00213 disposeCursor (RDF_Cursor c)
00214 {
00215   if (c) freeMem(c);
00216 }
00217 
00218 
00219 void
00220 unloadRDFT (RDFT f)
00221 {
00222   int n = 0;
00223   while (n < f->assertionListCount) {
00224     Assertion as = *(f->assertionList + n);
00225     remoteStoreRemove(f, as->u, as->s, as->value, as->type);
00226     freeAssertion(as);
00227     *(f->assertionList + n) = NULL;
00228     n++;
00229   }
00230   f->assertionListCount = 0;
00231   f->assertionListSize = 0;
00232   freeMem(f->assertionList);
00233   f->assertionList = NULL;
00234 }
00235