hamsterdb Embedded Database  2.1.1
hamsterdb.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2005-2013 Christoph Rupp (chris@crupp.de).
00003  *
00004  * This program is free software; you can redistribute it and/or modify it
00005  * under the terms of the GNU General Public License as published by the
00006  * Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * See files COPYING.* for License information.
00010  */
00011 
00102 #ifndef HAM_HAMSTERDB_H__
00103 #define HAM_HAMSTERDB_H__
00104 
00105 #include <ham/types.h>
00106 
00107 #ifdef __cplusplus
00108 extern "C" {
00109 #endif
00110 
00117 #define HAM_API_REVISION        2
00118 
00125 struct ham_db_t;
00126 typedef struct ham_db_t ham_db_t;
00127 
00134 struct ham_env_t;
00135 typedef struct ham_env_t ham_env_t;
00136 
00146 struct ham_cursor_t;
00147 typedef struct ham_cursor_t ham_cursor_t;
00148 
00172 typedef struct {
00174   ham_u32_t size;
00175 
00177   void *data;
00178 
00180   ham_u32_t flags;
00181 
00183   ham_u32_t partial_offset;
00184 
00186   ham_u32_t partial_size;
00187 
00189   ham_u32_t _intflags;
00190 
00192   ham_u64_t _rid;
00193 
00194 } ham_record_t;
00195 
00199 #define HAM_RECORD_USER_ALLOC   1
00200 
00227 typedef struct {
00229   ham_u16_t size;
00230 
00232   void *data;
00233 
00235   ham_u32_t flags;
00236 
00238   ham_u32_t _flags;
00239 
00240 } ham_key_t;
00241 
00245 #define HAM_KEY_USER_ALLOC    1
00246 
00264 typedef struct {
00266   ham_u32_t name;
00267 
00269   ham_u64_t value;
00270 
00271 } ham_parameter_t;
00272 
00273 
00280 #define HAM_SUCCESS                     (  0)
00281 
00282 #define HAM_INV_KEYSIZE                 ( -3)
00283 
00284 #define HAM_INV_PAGESIZE                ( -4)
00285 
00286 #define HAM_OUT_OF_MEMORY               ( -6)
00287 
00288 #define HAM_INV_PARAMETER               ( -8)
00289 
00290 #define HAM_INV_FILE_HEADER             ( -9)
00291 
00292 #define HAM_INV_FILE_VERSION            (-10)
00293 
00294 #define HAM_KEY_NOT_FOUND               (-11)
00295 
00296 #define HAM_DUPLICATE_KEY               (-12)
00297 
00298 #define HAM_INTEGRITY_VIOLATED          (-13)
00299 
00300 #define HAM_INTERNAL_ERROR              (-14)
00301 
00302 #define HAM_WRITE_PROTECTED             (-15)
00303 
00304 #define HAM_BLOB_NOT_FOUND              (-16)
00305 
00306 #define HAM_PREFIX_REQUEST_FULLKEY      (-17)
00307 
00308 #define HAM_IO_ERROR                    (-18)
00309 
00310 #define HAM_CACHE_FULL                  (-19)
00311 
00312 #define HAM_NOT_IMPLEMENTED             (-20)
00313 
00314 #define HAM_FILE_NOT_FOUND              (-21)
00315 
00316 #define HAM_WOULD_BLOCK                 (-22)
00317 
00318 #define HAM_NOT_READY                   (-23)
00319 
00320 #define HAM_LIMITS_REACHED              (-24)
00321 
00322 #define HAM_ALREADY_INITIALIZED         (-27)
00323 
00324 #define HAM_NEED_RECOVERY               (-28)
00325 
00326 #define HAM_CURSOR_STILL_OPEN           (-29)
00327 
00328 #define HAM_FILTER_NOT_FOUND            (-30)
00329 
00330 #define HAM_TXN_CONFLICT                (-31)
00331 /* internal use: key was erased in a Transaction */
00332 #define HAM_KEY_ERASED_IN_TXN           (-32)
00333 
00334 #define HAM_TXN_STILL_OPEN              (-33)
00335 
00336 #define HAM_CURSOR_IS_NIL               (-100)
00337 
00338 #define HAM_DATABASE_NOT_FOUND          (-200)
00339 
00340 #define HAM_DATABASE_ALREADY_EXISTS     (-201)
00341 
00342 #define HAM_DATABASE_ALREADY_OPEN       (-202)
00343 
00344 #define HAM_ENVIRONMENT_ALREADY_OPEN    (-203)
00345 
00346 #define HAM_LOG_INV_FILE_HEADER         (-300)
00347 
00348 #define HAM_NETWORK_ERROR               (-400)
00349 
00377 typedef void HAM_CALLCONV (*ham_errhandler_fun)(int level, const char *message);
00378 
00380 #define HAM_DEBUG_LEVEL_DEBUG     0
00381 
00383 #define HAM_DEBUG_LEVEL_NORMAL    1
00384 
00386 #define HAM_DEBUG_LEVEL_FATAL     3
00387 
00403 HAM_EXPORT void HAM_CALLCONV
00404 ham_set_errhandler(ham_errhandler_fun f);
00405 
00413 HAM_EXPORT const char * HAM_CALLCONV
00414 ham_strerror(ham_status_t status);
00415 
00423 HAM_EXPORT void HAM_CALLCONV
00424 ham_get_version(ham_u32_t *major, ham_u32_t *minor,
00425             ham_u32_t *revision);
00426 
00434 HAM_EXPORT void HAM_CALLCONV
00435 ham_get_license(const char **licensee, const char **product);
00436 
00539 HAM_EXPORT ham_status_t HAM_CALLCONV
00540 ham_env_create(ham_env_t **env, const char *filename,
00541             ham_u32_t flags, ham_u32_t mode, const ham_parameter_t *param);
00542 
00624 HAM_EXPORT ham_status_t HAM_CALLCONV
00625 ham_env_open(ham_env_t **env, const char *filename,
00626             ham_u32_t flags, const ham_parameter_t *param);
00627 
00657 HAM_EXPORT ham_status_t HAM_CALLCONV
00658 ham_env_get_parameters(ham_env_t *env, ham_parameter_t *param);
00659 
00719 HAM_EXPORT ham_status_t HAM_CALLCONV
00720 ham_env_create_db(ham_env_t *env, ham_db_t **db,
00721             ham_u16_t name, ham_u32_t flags, const ham_parameter_t *params);
00722 
00763 HAM_EXPORT ham_status_t HAM_CALLCONV
00764 ham_env_open_db(ham_env_t *env, ham_db_t **db,
00765             ham_u16_t name, ham_u32_t flags, const ham_parameter_t *params);
00766 
00791 HAM_EXPORT ham_status_t HAM_CALLCONV
00792 ham_env_rename_db(ham_env_t *env, ham_u16_t oldname,
00793             ham_u16_t newname, ham_u32_t flags);
00794 
00813 HAM_EXPORT ham_status_t HAM_CALLCONV
00814 ham_env_erase_db(ham_env_t *env, ham_u16_t name, ham_u32_t flags);
00815 
00831 HAM_EXPORT ham_status_t HAM_CALLCONV
00832 ham_env_flush(ham_env_t *env, ham_u32_t flags);
00833 
00834 /* internal use only - don't lock mutex */
00835 #define HAM_DONT_LOCK        0xf0000000
00836 
00859 HAM_EXPORT ham_status_t HAM_CALLCONV
00860 ham_env_get_database_names(ham_env_t *env, ham_u16_t *names,
00861             ham_size_t *count);
00862 
00895 HAM_EXPORT ham_status_t HAM_CALLCONV
00896 ham_env_close(ham_env_t *env, ham_u32_t flags);
00897 
00914 struct ham_txn_t;
00915 typedef struct ham_txn_t ham_txn_t;
00916 
00945 HAM_EXPORT ham_status_t
00946 ham_txn_begin(ham_txn_t **txn, ham_env_t *env, const char *name,
00947             void *reserved, ham_u32_t flags);
00948 
00950 #define HAM_TXN_READ_ONLY                     1
00951 
00952 /* Internal flag for @ref ham_txn_begin */
00953 #define HAM_TXN_TEMPORARY                     2
00954 
00960 HAM_EXPORT const char *
00961 ham_txn_get_name(ham_txn_t *txn);
00962 
00981 HAM_EXPORT ham_status_t
00982 ham_txn_commit(ham_txn_t *txn, ham_u32_t flags);
00983 
01002 HAM_EXPORT ham_status_t
01003 ham_txn_abort(ham_txn_t *txn, ham_u32_t flags);
01004 
01005 /* note: ham_txn_abort flag 0x0001 is reserved for internal use:
01006  * DO_NOT_NUKE_PAGE_STATS */
01007 
01020 #define HAM_ENABLE_FSYNC                            0x00000001
01021 
01022 /* unused                                           0x00000002 */
01023 
01026 #define HAM_READ_ONLY                               0x00000004
01027 
01028 /* unused                                           0x00000008 */
01029 
01030 /* unused                                           0x00000010 */
01031 
01032 /* reserved                                         0x00000020 */
01033 
01036 #define HAM_DISABLE_VAR_KEYLEN                      0x00000040
01037 
01040 #define HAM_IN_MEMORY                               0x00000080
01041 
01042 /* reserved: DB_USE_MMAP (not persistent)           0x00000100 */
01043 
01046 #define HAM_DISABLE_MMAP                            0x00000200
01047 
01050 #define HAM_CACHE_STRICT                            0x00000400
01051 
01054 #define HAM_RECORD_NUMBER                           0x00002000
01055 
01058 #define HAM_ENABLE_DUPLICATES                       0x00004000
01059 
01062 #define HAM_ENABLE_RECOVERY                         0x00008000
01063 
01066 #define HAM_AUTO_RECOVERY                           0x00010000
01067 
01070 #define HAM_ENABLE_TRANSACTIONS                     0x00020000
01071 
01074 #define HAM_CACHE_UNLIMITED                         0x00040000
01075 
01078 #define HAM_ENABLE_EXTENDED_KEYS                    0x00080000
01079 
01080 /* reserved: DB_IS_REMOTE   (not persistent)        0x00200000 */
01081 
01082 /* reserved: DB_DISABLE_AUTO_FLUSH (not persistent) 0x00400000 */
01083 
01093 HAM_EXPORT ham_status_t HAM_CALLCONV
01094 ham_db_get_error(ham_db_t *db);
01095 
01108 typedef int HAM_CALLCONV (*ham_prefix_compare_func_t)
01109                  (ham_db_t *db,
01110                   const ham_u8_t *lhs, ham_size_t lhs_length,
01111                   ham_size_t lhs_real_length,
01112                   const ham_u8_t *rhs, ham_size_t rhs_length,
01113                   ham_size_t rhs_real_length);
01114 
01128 HAM_EXPORT ham_status_t HAM_CALLCONV
01129 ham_db_set_prefix_compare_func(ham_db_t *db, ham_prefix_compare_func_t foo);
01130 
01139 typedef int HAM_CALLCONV (*ham_compare_func_t)(ham_db_t *db,
01140                   const ham_u8_t *lhs, ham_size_t lhs_length,
01141                   const ham_u8_t *rhs, ham_size_t rhs_length);
01142 
01162 HAM_EXPORT ham_status_t HAM_CALLCONV
01163 ham_db_set_compare_func(ham_db_t *db, ham_compare_func_t foo);
01164 
01289 HAM_EXPORT ham_status_t HAM_CALLCONV
01290 ham_db_find(ham_db_t *db, ham_txn_t *txn, ham_key_t *key,
01291             ham_record_t *record, ham_u32_t flags);
01292 
01374 HAM_EXPORT ham_status_t HAM_CALLCONV
01375 ham_db_insert(ham_db_t *db, ham_txn_t *txn, ham_key_t *key,
01376             ham_record_t *record, ham_u32_t flags);
01377 
01389 #define HAM_OVERWRITE                   0x0001
01390 
01392 #define HAM_DUPLICATE                   0x0002
01393 
01395 #define HAM_DUPLICATE_INSERT_BEFORE     0x0004
01396 
01398 #define HAM_DUPLICATE_INSERT_AFTER      0x0008
01399 
01401 #define HAM_DUPLICATE_INSERT_FIRST      0x0010
01402 
01404 #define HAM_DUPLICATE_INSERT_LAST       0x0020
01405 
01407 #define HAM_DIRECT_ACCESS               0x0040
01408 
01411 #define HAM_PARTIAL                     0x0080
01412 
01425 #define HAM_HINT_APPEND                 0x00080000
01426 
01439 #define HAM_HINT_PREPEND                0x00100000
01440 
01445 #define HAM_HINTS_MASK                  0x001F0000
01446 
01470 HAM_EXPORT ham_status_t HAM_CALLCONV
01471 ham_db_erase(ham_db_t *db, ham_txn_t *txn, ham_key_t *key, ham_u32_t flags);
01472 
01473 /* internal flag for ham_db_erase() - do not use */
01474 #define HAM_ERASE_ALL_DUPLICATES                1
01475 
01497 HAM_EXPORT ham_status_t HAM_CALLCONV
01498 ham_db_get_key_count(ham_db_t *db, ham_txn_t *txn, ham_u32_t flags,
01499             ham_u64_t *keycount);
01500 
01523 HAM_EXPORT ham_status_t HAM_CALLCONV
01524 ham_db_get_parameters(ham_db_t *db, ham_parameter_t *param);
01525 
01528 #define HAM_PARAM_CACHESIZE             0x00000100
01529 
01531 #define HAM_PARAM_PAGESIZE              0x00000101
01532 
01534 #define HAM_PARAM_KEYSIZE               0x00000102
01535 
01538 #define HAM_PARAM_MAX_DATABASES         0x00000103
01539 
01542 #define HAM_PARAM_LOG_DIRECTORY         0x00000105
01543 
01548 #define HAM_PARAM_FLAGS                 0x00000200
01549 
01554 #define HAM_PARAM_FILEMODE              0x00000201
01555 
01563 #define HAM_PARAM_FILENAME              0x00000202
01564 
01572 #define HAM_PARAM_DATABASE_NAME         0x00000203
01573 
01582 #define HAM_PARAM_MAX_KEYS_PER_PAGE     0x00000204
01583 
01591 HAM_EXPORT ham_env_t *HAM_CALLCONV
01592 ham_db_get_env(ham_db_t *db);
01593 
01617 HAM_EXPORT int HAM_CALLCONV
01618 ham_key_get_approximate_match_type(ham_key_t *key);
01619 
01654 HAM_EXPORT ham_status_t HAM_CALLCONV
01655 ham_db_close(ham_db_t *db, ham_u32_t flags);
01656 
01658 #define HAM_AUTO_CLEANUP                1
01659 
01661 #define HAM_DONT_CLEAR_LOG              2
01662 
01664 #define HAM_TXN_AUTO_ABORT              4
01665 
01667 #define HAM_TXN_AUTO_COMMIT             8
01668 
01706 HAM_EXPORT ham_status_t HAM_CALLCONV
01707 ham_cursor_create(ham_cursor_t **cursor, ham_db_t *db, ham_txn_t *txn,
01708             ham_u32_t flags);
01709 
01728 HAM_EXPORT ham_status_t HAM_CALLCONV
01729 ham_cursor_clone(ham_cursor_t *src, ham_cursor_t **dest);
01730 
01847 HAM_EXPORT ham_status_t HAM_CALLCONV
01848 ham_cursor_move(ham_cursor_t *cursor, ham_key_t *key,
01849             ham_record_t *record, ham_u32_t flags);
01850 
01852 #define HAM_CURSOR_FIRST                0x0001
01853 
01855 #define HAM_CURSOR_LAST                 0x0002
01856 
01858 #define HAM_CURSOR_NEXT                 0x0004
01859 
01861 #define HAM_CURSOR_PREVIOUS             0x0008
01862 
01864 #define HAM_SKIP_DUPLICATES             0x0010
01865 
01867 #define HAM_ONLY_DUPLICATES             0x0020
01868 
01887 HAM_EXPORT ham_status_t HAM_CALLCONV
01888 ham_cursor_overwrite(ham_cursor_t *cursor, ham_record_t *record,
01889             ham_u32_t flags);
01890 
02046 HAM_EXPORT ham_status_t HAM_CALLCONV
02047 ham_cursor_find(ham_cursor_t *cursor, ham_key_t *key,
02048             ham_record_t *record, ham_u32_t flags);
02049 
02058 #define HAM_FIND_EXACT_MATCH            0x4000
02059 
02066 #define HAM_FIND_LT_MATCH               0x1000
02067 
02074 #define HAM_FIND_GT_MATCH               0x2000
02075 
02085 #define HAM_FIND_LEQ_MATCH      (HAM_FIND_LT_MATCH | HAM_FIND_EXACT_MATCH)
02086 
02096 #define HAM_FIND_GEQ_MATCH      (HAM_FIND_GT_MATCH | HAM_FIND_EXACT_MATCH)
02097 
02113 #define HAM_FIND_NEAR_MATCH     (HAM_FIND_LT_MATCH | HAM_FIND_GT_MATCH  \
02114                                   | HAM_FIND_EXACT_MATCH)
02115 
02234 HAM_EXPORT ham_status_t HAM_CALLCONV
02235 ham_cursor_insert(ham_cursor_t *cursor, ham_key_t *key,
02236             ham_record_t *record, ham_u32_t flags);
02237 
02259 HAM_EXPORT ham_status_t HAM_CALLCONV
02260 ham_cursor_erase(ham_cursor_t *cursor, ham_u32_t flags);
02261 
02279 HAM_EXPORT ham_status_t HAM_CALLCONV
02280 ham_cursor_get_duplicate_count(ham_cursor_t *cursor,
02281             ham_size_t *count, ham_u32_t flags);
02282 
02295 HAM_EXPORT ham_status_t HAM_CALLCONV
02296 ham_cursor_get_record_size(ham_cursor_t *cursor, ham_u64_t *size);
02297 
02312 HAM_EXPORT ham_status_t HAM_CALLCONV
02313 ham_cursor_close(ham_cursor_t *cursor);
02314 
02319 #ifdef __cplusplus
02320 } // extern "C"
02321 #endif
02322 
02323 #endif /* HAM_HAMSTERDB_H__ */