1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/* $Id: atom.c,v 1.3 2002/02/01 16:49:11 jan Exp $
Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: jan@swi.psy.uva.nl
WWW: http://www.swi-prolog.org
Copyright (C): 1985-2002, University of Amsterdam
This library 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 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <SWI-Stream.h>
#include "db4pl.h"
#include <sys/types.h>
#include <stdlib.h>
#include <assert.h>
#include "error.h"
#define DEBUG(g) (void)0
/*******************************
* TMP HACKS *
*******************************/
extern int unboundStringHashValue(const char *t, unsigned int len);
typedef unsigned long plhash_t;
unsigned long
PL_atom_hash(atom_t a)
{ const char *s;
unsigned int len;
s = PL_atom_nchars(a, &len);
return unboundStringHashValue(s, len);
}
/*******************************
* ATOM MANAGEMENT *
*******************************/
static int maxdupl;
static int
equal_dbt(DBT *a, DBT *b)
{ if ( a->size == b->size )
{ if ( a->data == b->data )
return TRUE;
if ( memcmp(a->data, b->data, a->size) == 0 )
return TRUE;
}
return FALSE;
}
int
db_atom_id(dbh *db, atom_t a, atomid_t *id, int flags)
{ unsigned long hash = PL_atom_hash(a) & 0xffffL;
unsigned long idx = 0;
int rval;
DBC *cursor;
DBT k, v;
DBT av;
if ( (rval = db->db->cursor(db->db, NULL, &cursor, 0)) != 0 )
return db_status(rval);
memset(&av, 0, sizeof(av));
memset(&v, 0, sizeof(v));
memset(&k, 0, sizeof(k));
k.data = &hash;
k.size = sizeof(hash);
av.data = (void *)PL_atom_nchars(a, &av.size);
if ( (rval=cursor->c_get(cursor, &k, &v, DB_SET)) == 0 )
{ DBT k2;
if ( equal_dbt(&v, &av) )
{ cursor->c_close(cursor);
*id = (idx<<16) | hash;
return TRUE;
}
memset(&k2, 0, sizeof(k2));
for(;;)
{ if ( (rval=cursor->c_get(cursor, &k2, &v, DB_NEXT)) == 0 &&
k2.size == sizeof(hash) &&
*((plhash_t *)k2.data) == hash )
{ idx++;
if ( equal_dbt(&v, &av) )
{ cursor->c_close(cursor);
*id = (idx<<16) | hash;
if ( idx > maxdupl )
{ Sdprintf("\n%% %s: max-duplicates: %d\n", av.data, idx);
maxdupl = idx;
}
return TRUE;
}
continue;
}
if ( rval > 0 ) /* some error */
{ cursor->c_close(cursor);
return db_status(rval);
}
cursor->c_close(cursor);
idx++;
if ( idx >= 1<<16 )
return pl_error(ERR_LIMIT, "atoms_per_key", 1<<16);
DEBUG(Sdprintf("Added '%s' at %ld\n", av.data, (idx<<16)|hash));
goto add;
}
} else if ( rval == DB_NOTFOUND )
{ cursor->c_close(cursor);
if ( (flags & DB4PL_ATOM_CREATE) )
{
add:
rval = db_status(db->db->put(db->db, NULL, &k, &av, 0));
if ( rval )
*id = (idx<<16)|hash;
return rval;
} else
return FALSE;
} else
{ cursor->c_close(cursor);
return db_status(rval);
}
}
int
pl_atom_from_db(dbh *db, atomid_t id, atom_t *a)
{ unsigned long idx = (id>>16) & 0xffff;
unsigned long kv = id & 0xffff;
int rval;
DBT k, v;
memset(&v, 0, sizeof(v));
memset(&k, 0, sizeof(k));
k.size = sizeof(kv);
k.data = &kv;
if ( idx == 0 ) /* first, common case */
{ if ( (rval=db->db->get(db->db, NULL, &k, &v, 0)) == 0 )
{ *a = PL_new_atom_nchars(v.size, v.data);
return TRUE;
}
} else
{ DBC *cursor;
unsigned long i = 0;
if ( (rval = db->db->cursor(db->db, NULL, &cursor, 0)) != 0 )
return db_status(rval);
if ( (rval=cursor->c_get(cursor, &k, &v, DB_SET)) == 0 )
{ DBT k2;
memset(&k2, 0, sizeof(k2));
while(i<idx)
{ if ( (rval=cursor->c_get(cursor, &k2, &v, DB_NEXT)) == 0 )
i++;
else
{ cursor->c_close(cursor);
return db_status(rval);
}
}
*a = PL_new_atom_nchars(v.size, v.data);
cursor->c_close(cursor);
return TRUE;
}
}
assert(0);
return FALSE; /* exception */
}
|