http://gmd20.blog.163.com/blog/static/1684392320131733835919/
// Copyright (c) 2011 The LevelDB Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. #ifndef STORAGE_LEVELDB_PORT_PORT_H_ #define STORAGE_LEVELDB_PORT_PORT_H_ #include <string.h> // Include the appropriate platform specific file below. If you are // porting to a new platform, see "port_example.h" for documentation // of what the new port_<platform>.h file must provide. #if defined(LEVELDB_PLATFORM_POSIX) # include "port/port_posix.h" #elif defined(LEVELDB_PLATFORM_CHROMIUM) # include "port/port_chromium.h" #elif defined(LEVELDB_PLATFORM_WINDOWS) # include "port/port_win.h" #endif #endif // STORAGE_LEVELDB_PORT_PORT_H_
// Thread-safe initialization. // Used as follows: // static port::OnceType init_control = LEVELDB_ONCE_INIT; // static void Initializer() { ... do something ...; } // ... // port::InitOnce(&init_control, &Initializer); typedef long OnceType; #define LEVELDB_ONCE_INIT 0 extern void InitOnce(port::OnceType*, void (*initializer)());
//----------------cpp-----------------------------------
void InitOnce(OnceType* once, void (*initializer)()) { if (InterlockedCompareExchange (once, 1,0) == 0 ) { initializer(); } }
#ifdef WIN32 #include <io.h> #include <process.h> #else #include <unistd.h> #endif
LIBRARY libleveldb EXPORTS ; db operations leveldb_open leveldb_close leveldb_put leveldb_delete leveldb_write leveldb_get leveldb_create_iterator leveldb_create_snapshot leveldb_release_snapshot leveldb_property_value leveldb_approximate_sizes ; management leveldb_destroy_db leveldb_repair_db ; iterator leveldb_iter_destroy leveldb_iter_valid leveldb_iter_seek_to_first leveldb_iter_seek_to_last leveldb_iter_seek leveldb_iter_next leveldb_iter_prev leveldb_iter_key leveldb_iter_value leveldb_iter_get_error ; write batch leveldb_writebatch_create leveldb_writebatch_destroy leveldb_writebatch_clear leveldb_writebatch_put leveldb_writebatch_delete leveldb_writebatch_iterate ; options leveldb_options_create leveldb_options_destroy leveldb_options_set_comparator leveldb_options_set_create_if_missing leveldb_options_set_error_if_exists leveldb_options_set_paranoid_checks leveldb_options_set_env leveldb_options_set_info_log leveldb_options_set_write_buffer_size leveldb_options_set_max_open_files leveldb_options_set_cache leveldb_options_set_block_size leveldb_options_set_block_restart_interval ; compression leveldb_options_set_compression ; comparator leveldb_comparator_create leveldb_comparator_destroy ; read options leveldb_readoptions_create leveldb_readoptions_destroy leveldb_readoptions_set_verify_checksums leveldb_readoptions_set_fill_cache leveldb_readoptions_set_snapshot ; write options leveldb_writeoptions_create leveldb_writeoptions_destroy leveldb_writeoptions_set_sync ; cache leveldb_cache_create_lru leveldb_cache_destroy ; env leveldb_create_default_env leveldb_env_destroy ; misc leveldb_free
#include "leveldb/c.h"
leveldb_t* db; leveldb_comparator_t* cmp; leveldb_cache_t* cache; leveldb_env_t* env; leveldb_options_t* options; leveldb_readoptions_t* roptions; leveldb_writeoptions_t* woptions; char* err = NULL; env = leveldb_create_default_env(); cache = leveldb_cache_create_lru(100000); options = leveldb_options_create(); // leveldb_options_set_comparator(options, cmp); //leveldb_options_set_error_if_exists(options, 1); //leveldb_options_set_cache(options, cache); //leveldb_options_set_env(options, env); //leveldb_options_set_info_log(options, NULL); //leveldb_options_set_write_buffer_size(options, 100000); //leveldb_options_set_paranoid_checks(options, 1); //leveldb_options_set_max_open_files(options, 10); //leveldb_options_set_block_size(options, 1024); //leveldb_options_set_block_restart_interval(options, 8); leveldb_options_set_compression(options, leveldb_no_compression); //roptions = leveldb_readoptions_create(); //leveldb_readoptions_set_verify_checksums(roptions, 1); //leveldb_readoptions_set_fill_cache(roptions, 0); woptions = leveldb_writeoptions_create(); //leveldb_writeoptions_set_sync(woptions, 1); //同步写非常慢 leveldb_writeoptions_set_sync(woptions, 0); leveldb_destroy_db(options, "D:/Trace/test.db" , &err); leveldb_options_set_create_if_missing(options, 1); db = leveldb_open(options, "D:/Trace/test.db", &err); leveldb_put(db, woptions, "foo", 3, "hello", 5, &err); //leveldb_writebatch_t* wb = leveldb_writebatch_create(); //leveldb_writebatch_put(wb, "foo", 3, "a", 1); //leveldb_writebatch_clear(wb); //leveldb_writebatch_put(wb, "bar", 3, "b", 1); //leveldb_writebatch_put(wb, "box", 3, "c", 1); //leveldb_writebatch_delete(wb, "bar", 3); //leveldb_write(db, woptions, wb, &err); char data [200]; char key[64]; QueryPerformanceCounter(&t0); unsigned long last_time = GetTickCount(); unsigned long max_time =0; int i, j; for (i=0; i< 5000000; i++) { //UpdateTimeString(date,64); itoa(i, key,10); leveldb_put(db, woptions,key,strlen(key) , data, 200, &err); unsigned long time = GetTickCount(); unsigned long max = time - last_time; last_time = time; if (max > max_time) { max_time = max; } } QueryPerformanceCounter(&t1); std::cout << "最大耗时 " << max_time << " 毫秒" << endl; unsigned long time = (((t1.QuadPart-t0.QuadPart)*1000000)/freq.QuadPart); std::cout << "执行 " << i <<" 次, 耗时 " << time << " 微秒" << std::endl;

