github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

dawsdesign / erl_bdb_sample

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 4
    • 1
  • Source
  • Commits
  • Network (1)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Tree: f6c097b

click here to add a description

click here to add a homepage

  • Branches (1)
    • master
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Erlang -> Berkeley DB Sample — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Fixed makefile. 
Matt Williamson (author)
Tue May 26 07:48:34 -0700 2009
commit  f6c097bf646e5bd3269c12a4c7146f14fea90bba
tree    94a73f7cbd2b01ea6b569733fd958b6a74d93588
parent  4378c320c86a0937ac26107639d72942921d82c7
erl_bdb_sample / priv / bdb_drv.c priv/bdb_drv.c
100644 283 lines (230 sloc) 7.556 kb
edit raw blame history
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "bdb_drv.h"
 
static ErlDrvEntry basic_driver_entry = {
    NULL, /* init */
    start, /* startup */
    stop, /* shutdown */
    NULL, /* output */
    NULL, /* ready_input */
    NULL, /* ready_output */
    "bdb_drv", /* the name of the driver */
    NULL, /* finish */
    NULL, /* handle */
    NULL, /* control */
    NULL, /* timeout */
    outputv, /* outputv */
    NULL, /* ready_async */
    NULL, /* flush */
    NULL, /* call */
    NULL, /* event */
    ERL_DRV_EXTENDED_MARKER, /* ERL_DRV_EXTENDED_MARKER */
    ERL_DRV_EXTENDED_MAJOR_VERSION, /* ERL_DRV_EXTENDED_MAJOR_VERSION */
    ERL_DRV_EXTENDED_MAJOR_VERSION, /* ERL_DRV_EXTENDED_MINOR_VERSION */
    ERL_DRV_FLAG_USE_PORT_LOCKING /* ERL_DRV_FLAGs */
};
 
DRIVER_INIT(basic_driver) {
  return &basic_driver_entry;
}
 
static ErlDrvData start(ErlDrvPort port, char* cmd) {
  bdb_drv_t* retval = (bdb_drv_t*) driver_alloc(sizeof(bdb_drv_t));
  u_int32_t open_flags = DB_CREATE | DB_THREAD;
  DB *db;
  int status;
  
  db_create(&db, NULL, 0);
  status = db->open(db, NULL, DB_PATH, NULL, DB_BTREE, open_flags, 0);
 
  if(status != 0) {
    char *error_reason;
 
    switch(status){
    case DB_OLD_VERSION:
      error_reason = "the file was created with a different version.";
      break;
 
    case EINVAL:
      error_reason = "the file was opened with incorrect flags. Perhaps the system does not support the DB_THREAD flag?";
      break;
 
    case DB_RUNRECOVERY:
      error_reason = "the file needs to be recovered, it may be corrupt.";
      break;
    default:
      error_reason = "of an unkown reason.";
    }
 
    fprintf(stderr, "Unabled to open file: %s because %s\n\n", DB_PATH, error_reason);
  }
 
  retval->port = port;
  retval->db = db;
  
  return (ErlDrvData) retval;
}
 
static void stop(ErlDrvData handle) {
  bdb_drv_t* driver_data = (bdb_drv_t*) handle;
 
  driver_data->db->close(driver_data->db, 0);
  driver_free(driver_data);
}
 
static void outputv(ErlDrvData handle, ErlIOVec *ev) {
  bdb_drv_t* driver_data = (bdb_drv_t*) handle;
  ErlDrvBinary* data = ev->binv[1];
  int command = data->orig_bytes[0];
  
  switch(command) {
  case CMD_PUT:
    put(driver_data, ev);
    break;
 
  case CMD_GET:
    get(driver_data, ev);
    break;
 
  case CMD_DEL:
    del(driver_data, ev);
    break;
 
  default:
    unkown(driver_data, ev);
  }
}
 
static void put(bdb_drv_t *bdb_drv, ErlIOVec *ev) {
  ErlDrvBinary* input = ev->binv[1];
  char *bytes = input->orig_bytes;
  char *key_bytes = bytes+1;
  char *value_bytes = bytes+1+KEY_SIZE;
  int value_size = input->orig_size - 1 - KEY_SIZE;
  
  DB *db = bdb_drv->db;
  DBT key;
  DBT value;
  int status;
 
  bzero(&key, sizeof(DBT));
  bzero(&value, sizeof(DBT));
  
  key.data = key_bytes;
  key.size = KEY_SIZE;
  
  value.data = value_bytes;
  value.size = value_size;
  
  status = db->put(db, NULL, &key, &value, 0);
  db->sync(db, 0);
 
  if(status == 0) {
    ErlDrvTermData spec[] = {ERL_DRV_ATOM, driver_mk_atom("ok")};
 
    driver_output_term(bdb_drv->port, spec, sizeof(spec) / sizeof(spec[0]));
  } else {
    char * error_reason;
 
    switch(status) {
    case DB_LOCK_DEADLOCK:
      error_reason = "deadlock";
      break;
    case EACCES:
      error_reason = "readonly";
      break;
    case EINVAL:
      error_reason = "badflag";
      break;
    case ENOSPC:
      error_reason = "btree_max";
      break;
    case DB_RUNRECOVERY:
      error_reason = "run_recovery";
      break;
    default:
      error_reason = "unkown";
    }
    
    ErlDrvTermData spec[] = {ERL_DRV_ATOM, driver_mk_atom("error"),
ERL_DRV_ATOM, driver_mk_atom(error_reason),
ERL_DRV_TUPLE, 2};
 
    driver_output_term(bdb_drv->port, spec, sizeof(spec) / sizeof(spec[0]));
  }
}
 
static void get(bdb_drv_t *bdb_drv, ErlIOVec *ev) {
  ErlDrvBinary* input = ev->binv[1];
  ErlDrvBinary *output_bytes;
  char *bytes = input->orig_bytes;
  char *key_bytes = bytes+1;
  
  DB *db = bdb_drv->db;
  DBT key;
  DBT value;
  int status;
  
  bzero(&key, sizeof(DBT));
  bzero(&value, sizeof(DBT));
    
  key.data = key_bytes;
  key.size = KEY_SIZE;
  
  value.flags = DB_DBT_MALLOC; // Don't forget to free
  
  status = db->get(db, NULL, &key, &value, 0);
 
  if(status == 0) {
    output_bytes = driver_alloc_binary(value.size);
    output_bytes->orig_size = value.size;
    // TODO:Figure out if we can somehow use this original memory
    //binary->orig_bytes = (char *)&data.data;
    memcpy(output_bytes->orig_bytes, value.data, value.size);
    free(value.data);
    
    ErlDrvTermData spec[] = {ERL_DRV_ATOM, driver_mk_atom("ok"),
ERL_DRV_BINARY, (ErlDrvTermData) output_bytes, output_bytes->orig_size, 0,
ERL_DRV_TUPLE, 2};
    
    driver_output_term(bdb_drv->port, spec, sizeof(spec) / sizeof(spec[0]));
    driver_free_binary(output_bytes);
  } else {
    char *error_reason;
 
    switch(status) {
    case DB_LOCK_DEADLOCK:
      error_reason = "deadlock";
      break;
    case DB_SECONDARY_BAD:
      error_reason = "bad_secondary_index";
      break;
    case ENOMEM:
      error_reason = "insufficient_memory";
      break;
    case EINVAL:
      error_reason = "bad_flag";
      break;
    case DB_RUNRECOVERY:
      error_reason = "run_recovery";
      break;
    default:
      error_reason = "unknown";
    }
    
    ErlDrvTermData spec[] = {ERL_DRV_ATOM, driver_mk_atom("error"),
ERL_DRV_ATOM, driver_mk_atom(error_reason),
ERL_DRV_TUPLE, 2};
    driver_output_term(bdb_drv->port, spec, sizeof(spec) / sizeof(spec[0]));
  }
}
 
static void del(bdb_drv_t *bdb_drv, ErlIOVec *ev) {
  ErlDrvBinary* data = ev->binv[1];
  char *bytes = data->orig_bytes;
  char *key_bytes = bytes+1;
  
  DB *db = bdb_drv->db;
  DBT key;
  int status;
  
  bzero(&key, sizeof(DBT));
    
  key.data = key_bytes;
  key.size = KEY_SIZE;
  
  status = db->del(db, NULL, &key, 0);
  db->sync(db, 0);
  
  if(status == 0) {
    ErlDrvTermData spec[] = {ERL_DRV_ATOM, driver_mk_atom("ok")};
    
    driver_output_term(bdb_drv->port, spec, sizeof(spec) / sizeof(spec[0]));
    
  } else {
    char *error_reason;
 
    switch(status) {
    case DB_NOTFOUND:
      error_reason = "not_found";
      break;
    case DB_LOCK_DEADLOCK:
      error_reason = "deadlock";
      break;
    case DB_SECONDARY_BAD:
      error_reason = "bad_secondary_index";
      break;
    case EINVAL:
      error_reason = "bad_flag";
      break;
    case EACCES:
      error_reason = "readonly";
      break;
    case DB_RUNRECOVERY:
      error_reason = "run_recovery";
      break;
    default:
      error_reason = "unknown";
    }
    
    ErlDrvTermData spec[] = {ERL_DRV_ATOM, driver_mk_atom("error"),
ERL_DRV_ATOM, driver_mk_atom(error_reason),
ERL_DRV_TUPLE, 2};
 
    driver_output_term(bdb_drv->port, spec, sizeof(spec) / sizeof(spec[0]));
  }
}
 
static void unkown(bdb_drv_t *bdb_drv, ErlIOVec *ev) {
  ErlDrvTermData spec[] = {ERL_DRV_ATOM, driver_mk_atom("error"),
ERL_DRV_ATOM, driver_mk_atom("uknown_command"),
ERL_DRV_TUPLE, 2};
  driver_output_term(bdb_drv->port, spec, sizeof(spec) / sizeof(spec[0]));
}
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server