Add mdb as a git submodule

Will figure out how to deal with it as a standalone library later.
This commit is contained in:
Howard Chu 2011-08-17 03:54:46 -07:00
parent 7c0f0ad014
commit 227e6976db
6 changed files with 8 additions and 3674 deletions

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "servers/slapd/back-mdb/libmdb"]
path = servers/slapd/back-mdb/libmdb
url = https://git.gitorious.org/mdb/mdb.git

@ -0,0 +1 @@
Subproject commit 319a976a50058efc6232eb240c929850ed583671

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1 @@
libmdb/mdb.c

View file

@ -1,151 +0,0 @@
/* mdb.h - memory-mapped database library header file */
/*
* Copyright 2011 Howard Chu, Symas Corp.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*
* This code is derived from btree.c written by Martin Hedenfalk.
*
* Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef _MDB_H_
#define _MDB_H_
#include <sys/types.h>
#define MDB_VERSION_MAJOR 0
#define MDB_VERSION_MINOR 8
#define MDB_VERSION_PATCH 0
#define MDB_VERINT(a,b,c) ((a << 24) | (b << 16) | c)
#define MDB_VERSION_FULL \
MDB_VERINT(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH)
#define MDB_VERSION_DATE "August 11, 2011"
#define MDB_VERSTR(a,b,c,d) "MDB " #a "." #b "." #c "(" #d ")"
#define MDB_VERSION_STRING \
MDB_VERSTR(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE)
struct MDB_cursor;
struct MDB_txn;
struct MDB_env;
typedef struct MDB_cursor MDB_cursor;
typedef struct MDB_txn MDB_txn;
typedef struct MDB_env MDB_env;
typedef unsigned int MDB_dbi;
typedef struct MDB_val {
size_t mv_size;
void *mv_data;
} MDB_val;
typedef int (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
typedef void (MDB_rel_func)(void *ptr, void *oldptr);
#define MDB_NOOVERWRITE 0x10
#define MDB_NODUPDATA 0x20
#define MDB_DEL_DUP 0x40
typedef enum MDB_cursor_op { /* cursor operations */
MDB_FIRST,
MDB_GET_BOTH, /* position at key/data */
MDB_GET_BOTH_RANGE, /* position at key, nearest data */
MDB_LAST,
MDB_NEXT,
MDB_NEXT_DUP,
MDB_NEXT_NODUP,
MDB_PREV,
MDB_PREV_DUP,
MDB_PREV_NODUP,
MDB_SET, /* position at key, or fail */
MDB_SET_RANGE /* position at given key */
} MDB_cursor_op;
/* return codes */
#define MDB_SUCCESS 0
#define MDB_FAIL -1
#define MDB_KEYEXIST -2
#define MDB_NOTFOUND -3
#define MDB_VERSION_MISMATCH -4
/* DB flags */
#define MDB_REVERSEKEY 0x02 /* use reverse string keys */
#define MDB_DUPSORT 0x04 /* use sorted duplicates */
#define MDB_INTEGERKEY 0x08 /* numeric keys in native byte order */
/* environment flags */
#define MDB_FIXEDMAP 0x01 /* mmap at a fixed address */
#define MDB_NOSYNC 0x10000 /* don't fsync after commit */
#define MDB_RDONLY 0x20000 /* read only */
/* DB or env flags */
#define MDB_CREATE 0x40000 /* create if not present */
typedef struct MDB_stat {
unsigned int ms_psize;
unsigned int ms_depth;
unsigned long ms_branch_pages;
unsigned long ms_leaf_pages;
unsigned long ms_overflow_pages;
unsigned long ms_entries;
} MDB_stat;
char *mdb_version(int *major, int *minor, int *patch);
int mdb_env_create(MDB_env **env);
int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode);
int mdb_env_stat(MDB_env *env, MDB_stat *stat);
int mdb_env_sync(MDB_env *env, int force);
void mdb_env_close(MDB_env *env);
int mdb_env_get_flags(MDB_env *env, unsigned int *flags);
int mdb_env_get_path(MDB_env *env, const char **path);
int mdb_env_set_mapsize(MDB_env *env, size_t size);
int mdb_env_set_maxreaders(MDB_env *env, int readers);
int mdb_env_get_maxreaders(MDB_env *env, int *readers);
int mdb_env_set_maxdbs(MDB_env *env, int dbs);
int mdb_txn_begin(MDB_env *env, int rdonly, MDB_txn **txn);
int mdb_txn_commit(MDB_txn *txn);
void mdb_txn_abort(MDB_txn *txn);
int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
void mdb_close(MDB_txn *txn, MDB_dbi dbi);
int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
unsigned int flags);
int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
unsigned int flags);
int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
void mdb_cursor_close(MDB_cursor *cursor);
int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
MDB_cursor_op op);
int mdb_cursor_count(MDB_cursor *cursor, unsigned long *countp);
int mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
#endif /* _MDB_H_ */

View file

@ -0,0 +1 @@
libmdb/mdb.h

View file

@ -1,181 +0,0 @@
/* idl.c - ldap bdb back-end ID list functions */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 2000-2011 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#include <string.h>
#include <sys/types.h>
#include <assert.h>
#include "midl.h"
typedef unsigned long pgno_t;
/* Sort the IDLs from highest to lowest */
#define IDL_CMP(x,y) ( x > y ? -1 : ( x < y ? 1 : 0 ) )
/* Sort the IDL2s from lowest to highest */
#define IDL2_CMP(x,y) ( x < y ? -1 : ( x > y ? 1 : 0 ) )
unsigned mdb_midl_search( ID *ids, ID id )
{
/*
* binary search of id in ids
* if found, returns position of id
* if not found, returns first position greater than id
*/
unsigned base = 0;
unsigned cursor = 0;
int val = 0;
unsigned n = ids[0];
while( 0 < n ) {
int pivot = n >> 1;
cursor = base + pivot;
val = IDL_CMP( id, ids[cursor + 1] );
if( val < 0 ) {
n = pivot;
} else if ( val > 0 ) {
base = cursor + 1;
n -= pivot + 1;
} else {
return cursor + 1;
}
}
if( val > 0 ) {
return cursor + 2;
} else {
return cursor + 1;
}
}
int mdb_midl_insert( ID *ids, ID id )
{
unsigned x, i;
if (MDB_IDL_IS_RANGE( ids )) {
/* if already in range, treat as a dup */
if (id >= MDB_IDL_FIRST(ids) && id <= MDB_IDL_LAST(ids))
return -1;
if (id < MDB_IDL_FIRST(ids))
ids[1] = id;
else if (id > MDB_IDL_LAST(ids))
ids[2] = id;
return 0;
}
x = mdb_midl_search( ids, id );
assert( x > 0 );
if( x < 1 ) {
/* internal error */
return -2;
}
if ( x <= ids[0] && ids[x] == id ) {
/* duplicate */
return -1;
}
if ( ++ids[0] >= MDB_IDL_DB_MAX ) {
if( id < ids[1] ) {
ids[1] = id;
ids[2] = ids[ids[0]-1];
} else if ( ids[ids[0]-1] < id ) {
ids[2] = id;
} else {
ids[2] = ids[ids[0]-1];
}
ids[0] = NOID;
} else {
/* insert id */
for (i=ids[0]; i>x; i--)
ids[i] = ids[i-1];
ids[x] = id;
}
return 0;
}
unsigned mdb_midl2_search( MIDL2 *ids, MIDL2 *id )
{
/*
* binary search of id in ids
* if found, returns position of id
* if not found, returns first position greater than id
*/
unsigned base = 0;
unsigned cursor = 0;
int val = 0;
unsigned n = ids[0].mid;
while( 0 < n ) {
int pivot = n >> 1;
cursor = base + pivot;
val = IDL2_CMP( id->mid, ids[cursor + 1].mid );
if( val < 0 ) {
n = pivot;
} else if ( val > 0 ) {
base = cursor + 1;
n -= pivot + 1;
} else {
return cursor + 1;
}
}
if( val > 0 ) {
return cursor + 2;
} else {
return cursor + 1;
}
}
int mdb_midl2_insert( MIDL2 *ids, MIDL2 *id )
{
unsigned x, i;
x = mdb_midl2_search( ids, id );
assert( x > 0 );
if( x < 1 ) {
/* internal error */
return -2;
}
if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
/* duplicate */
return -1;
}
if ( ids[0].mid >= MDB_IDL_DB_MAX ) {
/* too big */
return -2;
} else {
/* insert id */
ids[0].mid++;
for (i=ids[0].mid; i>x; i--)
ids[i] = ids[i-1];
ids[x] = *id;
}
return 0;
}

View file

@ -0,0 +1 @@
libmdb/midl.c

View file

@ -1,84 +0,0 @@
/* idl.h - ldap bdb back-end ID list header file */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 2000-2011 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#ifndef _MDB_MIDL_H_
#define _MDB_MIDL_H_
#define ID unsigned long
#define NOID ((ID)~0)
/* IDL sizes - likely should be even bigger
* limiting factors: sizeof(ID), thread stack size
*/
#define MDB_IDL_LOGN 16 /* DB_SIZE is 2^16, UM_SIZE is 2^17 */
#define MDB_IDL_DB_SIZE (1<<MDB_IDL_LOGN)
#define MDB_IDL_UM_SIZE (1<<(MDB_IDL_LOGN+1))
#define MDB_IDL_UM_SIZEOF (MDB_IDL_UM_SIZE * sizeof(ID))
#define MDB_IDL_DB_MAX (MDB_IDL_DB_SIZE-1)
#define MDB_IDL_UM_MAX (MDB_IDL_UM_SIZE-1)
#define MDB_IDL_IS_RANGE(ids) ((ids)[0] == NOID)
#define MDB_IDL_RANGE_SIZE (3)
#define MDB_IDL_RANGE_SIZEOF (MDB_IDL_RANGE_SIZE * sizeof(ID))
#define MDB_IDL_SIZEOF(ids) ((MDB_IDL_IS_RANGE(ids) \
? MDB_IDL_RANGE_SIZE : ((ids)[0]+1)) * sizeof(ID))
#define MDB_IDL_RANGE_FIRST(ids) ((ids)[1])
#define MDB_IDL_RANGE_LAST(ids) ((ids)[2])
#define MDB_IDL_RANGE( ids, f, l ) \
do { \
(ids)[0] = NOID; \
(ids)[1] = (f); \
(ids)[2] = (l); \
} while(0)
#define MDB_IDL_ZERO(ids) \
do { \
(ids)[0] = 0; \
(ids)[1] = 0; \
(ids)[2] = 0; \
} while(0)
#define MDB_IDL_IS_ZERO(ids) ( (ids)[0] == 0 )
#define MDB_IDL_IS_ALL( range, ids ) ( (ids)[0] == NOID \
&& (ids)[1] <= (range)[1] && (range)[2] <= (ids)[2] )
#define MDB_IDL_CPY( dst, src ) (memcpy( dst, src, MDB_IDL_SIZEOF( src ) ))
#define MDB_IDL_ID( bdb, ids, id ) MDB_IDL_RANGE( ids, id, ((bdb)->bi_lastid) )
#define MDB_IDL_ALL( bdb, ids ) MDB_IDL_RANGE( ids, 1, ((bdb)->bi_lastid) )
#define MDB_IDL_FIRST( ids ) ( ids[1] )
#define MDB_IDL_LAST( ids ) ( MDB_IDL_IS_RANGE(ids) \
? ids[2] : ids[ids[0]] )
#define MDB_IDL_N( ids ) ( MDB_IDL_IS_RANGE(ids) \
? (ids[2]-ids[1])+1 : ids[0] )
int mdb_midl_insert( ID *ids, ID id );
typedef struct MIDL2 {
ID mid;
void *mptr;
} MIDL2;
unsigned mdb_midl2_search( MIDL2 *ids, MIDL2 *id );
int mdb_midl2_insert( MIDL2 *ids, MIDL2 *id );
#endif /* _MDB_MIDL_H_ */

View file

@ -0,0 +1 @@
libmdb/midl.h