File: dbschema.c

package info (click to toggle)
whitedb 0.7.2-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,452 kB
  • ctags: 2,681
  • sloc: ansic: 31,714; python: 790; lex: 359; java: 277; makefile: 172; yacc: 138; sh: 87; sed: 36
file content (248 lines) | stat: -rw-r--r-- 6,664 bytes parent folder | download
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* $Id:  $
* $Version: $
*
* Copyright (c) Priit Jrv 2013
*
* This file is part of WhiteDB
*
* WhiteDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* WhiteDB 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WhiteDB.  If not, see <http://www.gnu.org/licenses/>.
*
*/

 /** @file dbschema.c
 * WhiteDB (semi-)structured data representation
 */

/* ====== Includes =============== */

#include <stdio.h>

/* ====== Private headers and defs ======== */

#ifdef __cplusplus
extern "C" {
#endif

#ifdef _WIN32
#include "../config-w32.h"
#else
#include "../config.h"
#endif

#include "dbdata.h"
#include "dbcompare.h"
#include "dbindex.h"
#include "dbschema.h"

/* ======== Data ========================= */

/* ======= Private protos ================ */

#ifdef USE_BACKLINKING
static void *find_document_recursive(void *db, gint *rec, int depth);
#endif
static gint delete_record_recursive(void *db, void *rec, int depth);
static gint show_schema_error(void *db, char *errmsg);

/* ====== Functions ============== */

/*
 * Create a data triple (subj, prop, ob)
 * May also be called to create key-value pairs with (NULL, key, value)
 * if isparam is non-0, the data is not indexed.
 * returns the new record
 * returns NULL on error.
 */
void *wg_create_triple(void *db, gint subj, gint prop, gint ob, gint isparam) {
  void *rec = wg_create_raw_record(db, WG_SCHEMA_TRIPLE_SIZE);
  gint *meta;
  if(rec) {
    meta = ((gint *) rec + RECORD_META_POS);
    if(isparam) {
      *meta |= (RECORD_META_NOTDATA|RECORD_META_MATCH);
    } else if(wg_index_add_rec(db, rec) < -1) {
      return NULL; /* index error */
    }

    if(wg_set_field(db, rec, WG_SCHEMA_TRIPLE_OFFSET, subj))
      return NULL;
    if(wg_set_field(db, rec, WG_SCHEMA_TRIPLE_OFFSET + 1, prop))
      return NULL;
    if(wg_set_field(db, rec, WG_SCHEMA_TRIPLE_OFFSET + 2, ob))
      return NULL;
  }
  return rec;
}

/*
 * Create an empty (JSON) array of given size.
 * if isparam is non-0, the data is not indexed (incl. when updating later)
 * if isdocument is non-0, the record represents a top-level document
 * returns the new record
 * returns NULL on error.
 */
void *wg_create_array(void *db, gint size, gint isdocument, gint isparam) {
  void *rec = wg_create_raw_record(db, size);
  gint *meta;
  if(rec) {
    meta = ((gint *) rec + RECORD_META_POS);
    *meta |= RECORD_META_ARRAY;
    if(isdocument)
      *meta |= RECORD_META_DOC;

    if(isparam) {
      *meta |= (RECORD_META_NOTDATA|RECORD_META_MATCH);
    } else if(wg_index_add_rec(db, rec) < -1) {
      return NULL; /* index error */
    }
  }
  return rec;
}

/*
 * Create an empty (JSON) object of given size.
 * if isparam is non-0, the data is not indexed (incl. when updating later)
 * if isdocument is non-0, the record represents a top-level document
 * returns the new record
 * returns NULL on error.
 */
void *wg_create_object(void *db, gint size, gint isdocument, gint isparam) {
  void *rec = wg_create_raw_record(db, size);
  gint *meta;
  if(rec) {
    meta = ((gint *) rec + RECORD_META_POS);
    *meta |= RECORD_META_OBJECT;
    if(isdocument)
      *meta |= RECORD_META_DOC;

    if(isparam) {
      *meta |= (RECORD_META_NOTDATA|RECORD_META_MATCH);
    } else if(wg_index_add_rec(db, rec) < -1) {
      return NULL; /* index error */
    }
  }
  return rec;
}

/*
 * Find a top-level document that the record belongs to.
 * returns the document pointer on success
 * returns NULL if the document was not found.
 */
void *wg_find_document(void *db, void *rec) {
#ifndef USE_BACKLINKING
  show_schema_error(db, "Backlinks are required to find complete documents");
  return NULL;
#else
  return find_document_recursive(db, (gint *) rec, WG_COMPARE_REC_DEPTH-1);
#endif
}


#ifdef USE_BACKLINKING
/*
 *  Find a document recursively.
 *  iterates through the backlink chain and checks each parent recursively.
 *  Returns the pointer to the (first) found document.
 *  Returns NULL if nothing found.
 *  XXX: if a document links to the contents of another document, it
 *  can "hijack" it in the search results this way. The priority
 *  depends on the position(s) in the backlink chain, as this is a depth-first
 *  search.
 */
static void *find_document_recursive(void *db, gint *rec, int depth) {
  if(is_schema_document(rec))
    return rec;

  if(depth > 0) {
    gint backlink_list = *(rec + RECORD_BACKLINKS_POS);
    if(backlink_list) {
      gcell *next = (gcell *) offsettoptr(db, backlink_list);
      for(;;) {
        void *res = find_document_recursive(db,
          (gint *) offsettoptr(db, next->car),
          depth-1);
        if(res)
          return res; /* Something was found recursively */
        if(!next->cdr)
          break;
        next = (gcell *) offsettoptr(db, next->cdr);
      }
    }
  }

  return NULL; /* Depth exhausted or nothing found. */
}
#endif

/*
 * Delete a top-level document
 * returns 0 on success
 * returns -1 on error
 */
gint wg_delete_document(void *db, void *document) {
#ifdef CHECK
  if(!is_schema_document(document)) {
    return show_schema_error(db, "wg_delete_document: not a document");
  }
#endif
#ifndef USE_BACKLINKING
  return delete_record_recursive(db, document, 99);
#else
  return delete_record_recursive(db, document, WG_COMPARE_REC_DEPTH);
#endif
}

/*
 * Delete a record and all the records it points to.
 * This is safe to call on JSON documents.
 */
static gint delete_record_recursive(void *db, void *rec, int depth) {
  gint i, reclen;
  if(depth <= 0) {
    return show_schema_error(db, "deleting record: recursion too deep");
  }

  reclen = wg_get_record_len(db, rec);
  for(i=0; i<reclen; i++) {
    gint enc = wg_get_field(db, rec, i);
    gint type = wg_get_encoded_type(db, enc);
    if(type == WG_RECORDTYPE) {
      if(wg_set_field(db, rec, i, 0))
        return -1;
      if(delete_record_recursive(db, wg_decode_record(db, enc), depth-1))
        return -1;
    }
  }

  if(wg_delete_record(db, rec))
    return -1;

  return 0;
}

/* ------------ error handling ---------------- */

static gint show_schema_error(void *db, char *errmsg) {
#ifdef WG_NO_ERRPRINT
#else
  fprintf(stderr,"wg schema error: %s.\n", errmsg);
#endif
  return -1;
}

#ifdef __cplusplus
}
#endif