Permalink
Browse files

initial version

  • Loading branch information...
wmorgan committed Jun 17, 2011
0 parents commit 63e6edbb7a49ca415410a8a9108e93b3000203c2
Showing with 19,858 additions and 0 deletions.
  1. +9 −0 .gitignore
  2. +17 −0 README
  3. +29 −0 Rakefile
  4. +10 −0 ext/leveldb/extconf.rb
  5. +107 −0 ext/leveldb/leveldb.cc
  6. +8 −0 leveldb/AUTHORS
  7. +27 −0 leveldb/LICENSE
  8. +172 −0 leveldb/Makefile
  9. +17 −0 leveldb/NEWS
  10. +51 −0 leveldb/README
  11. +14 −0 leveldb/TODO
  12. +90 −0 leveldb/db/builder.cc
  13. +36 −0 leveldb/db/builder.h
  14. +354 −0 leveldb/db/corruption_test.cc
  15. +677 −0 leveldb/db/db_bench.cc
  16. +1,236 −0 leveldb/db/db_impl.cc
  17. +180 −0 leveldb/db/db_impl.h
  18. +298 −0 leveldb/db/db_iter.cc
  19. +26 −0 leveldb/db/db_iter.h
  20. +1,192 −0 leveldb/db/db_test.cc
  21. +87 −0 leveldb/db/dbformat.cc
  22. +165 −0 leveldb/db/dbformat.h
  23. +112 −0 leveldb/db/dbformat_test.cc
  24. +135 −0 leveldb/db/filename.cc
  25. +80 −0 leveldb/db/filename.h
  26. +122 −0 leveldb/db/filename_test.cc
  27. +35 −0 leveldb/db/log_format.h
  28. +254 −0 leveldb/db/log_reader.cc
  29. +108 −0 leveldb/db/log_reader.h
  30. +500 −0 leveldb/db/log_test.cc
  31. +103 −0 leveldb/db/log_writer.cc
  32. +48 −0 leveldb/db/log_writer.h
  33. +108 −0 leveldb/db/memtable.cc
  34. +85 −0 leveldb/db/memtable.h
  35. +384 −0 leveldb/db/repair.cc
  36. +378 −0 leveldb/db/skiplist.h
  37. +378 −0 leveldb/db/skiplist_test.cc
  38. +66 −0 leveldb/db/snapshot.h
  39. +95 −0 leveldb/db/table_cache.cc
  40. +50 −0 leveldb/db/table_cache.h
  41. +268 −0 leveldb/db/version_edit.cc
  42. +106 −0 leveldb/db/version_edit.h
  43. +46 −0 leveldb/db/version_edit_test.cc
  44. +1,060 −0 leveldb/db/version_set.cc
  45. +306 −0 leveldb/db/version_set.h
  46. +138 −0 leveldb/db/write_batch.cc
  47. +45 −0 leveldb/db/write_batch_internal.h
  48. +89 −0 leveldb/db/write_batch_test.cc
  49. +89 −0 leveldb/doc/doc.css
  50. +213 −0 leveldb/doc/impl.html
  51. +506 −0 leveldb/doc/index.html
  52. +75 −0 leveldb/doc/log_format.txt
  53. +61 −0 leveldb/doc/table_format.txt
  54. +99 −0 leveldb/include/leveldb/cache.h
  55. +63 −0 leveldb/include/leveldb/comparator.h
  56. +148 −0 leveldb/include/leveldb/db.h
  57. +302 −0 leveldb/include/leveldb/env.h
  58. +100 −0 leveldb/include/leveldb/iterator.h
  59. +198 −0 leveldb/include/leveldb/options.h
  60. +109 −0 leveldb/include/leveldb/slice.h
  61. +100 −0 leveldb/include/leveldb/status.h
  62. +70 −0 leveldb/include/leveldb/table.h
  63. +91 −0 leveldb/include/leveldb/table_builder.h
  64. +64 −0 leveldb/include/leveldb/write_batch.h
  65. +10 −0 leveldb/port/README
  66. +23 −0 leveldb/port/port.h
  67. +64 −0 leveldb/port/port_android.cc
  68. +150 −0 leveldb/port/port_android.h
  69. +80 −0 leveldb/port/port_chromium.cc
  70. +97 −0 leveldb/port/port_chromium.h
  71. +115 −0 leveldb/port/port_example.h
  72. +50 −0 leveldb/port/port_osx.cc
  73. +125 −0 leveldb/port/port_osx.h
  74. +50 −0 leveldb/port/port_posix.cc
  75. +94 −0 leveldb/port/port_posix.h
  76. +298 −0 leveldb/port/sha1_portable.cc
  77. +25 −0 leveldb/port/sha1_portable.h
  78. +39 −0 leveldb/port/sha1_test.cc
  79. +24 −0 leveldb/port/win/stdint.h
  80. +263 −0 leveldb/table/block.cc
  81. +43 −0 leveldb/table/block.h
  82. +109 −0 leveldb/table/block_builder.cc
  83. +57 −0 leveldb/table/block_builder.h
  84. +131 −0 leveldb/table/format.cc
  85. +103 −0 leveldb/table/format.h
  86. +67 −0 leveldb/table/iterator.cc
  87. +63 −0 leveldb/table/iterator_wrapper.h
  88. +197 −0 leveldb/table/merger.cc
  89. +26 −0 leveldb/table/merger.h
  90. +175 −0 leveldb/table/table.cc
  91. +227 −0 leveldb/table/table_builder.cc
  92. +845 −0 leveldb/table/table_test.cc
  93. +182 −0 leveldb/table/two_level_iterator.cc
  94. +34 −0 leveldb/table/two_level_iterator.h
  95. +68 −0 leveldb/util/arena.cc
  96. +68 −0 leveldb/util/arena.h
  97. +68 −0 leveldb/util/arena_test.cc
  98. +255 −0 leveldb/util/cache.cc
  99. +169 −0 leveldb/util/cache_test.cc
  100. +194 −0 leveldb/util/coding.cc
  101. +104 −0 leveldb/util/coding.h
  102. +173 −0 leveldb/util/coding_test.cc
  103. +72 −0 leveldb/util/comparator.cc
  104. +332 −0 leveldb/util/crc32c.cc
  105. +45 −0 leveldb/util/crc32c.h
  106. +72 −0 leveldb/util/crc32c_test.cc
  107. +77 −0 leveldb/util/env.cc
  108. +612 −0 leveldb/util/env_chromium.cc
  109. +606 −0 leveldb/util/env_posix.cc
  110. +102 −0 leveldb/util/env_test.cc
  111. +45 −0 leveldb/util/hash.cc
  112. +19 −0 leveldb/util/hash.h
  113. +128 −0 leveldb/util/histogram.cc
  114. +41 −0 leveldb/util/histogram.h
  115. +81 −0 leveldb/util/logging.cc
  116. +47 −0 leveldb/util/logging.h
  117. +39 −0 leveldb/util/mutexlock.h
  118. +28 −0 leveldb/util/options.cc
  119. +59 −0 leveldb/util/random.h
  120. +75 −0 leveldb/util/status.cc
  121. +65 −0 leveldb/util/testharness.cc
  122. +129 −0 leveldb/util/testharness.h
  123. +51 −0 leveldb/util/testutil.cc
  124. +53 −0 leveldb/util/testutil.h
  125. +26 −0 lib/leveldb.rb
@@ -0,0 +1,9 @@
+.*.swp
+*.o
+ext/leveldb/Makefile
+ext/leveldb/extconf.h
+ext/leveldb/leveldb.so
+ext/leveldb/libleveldb.a
+leveldb/libleveldb.a
+leveldb/leveldb.gyp
+pkg
17 README
@@ -0,0 +1,17 @@
+LevelDB Ruby bindings.
+
+Do this:
+
+ gem install leveldb-ruby
+
+
+And then run this:
+
+ require 'rubygems' # on ruby < 1.9
+ require 'leveldb'
+
+ db = LevelDB.new "/tmp/asdf"
+ db.put "it", "works"
+ db.get "it"
+
+Only a very few operations are supported right now:
@@ -0,0 +1,29 @@
+require 'rubygems'
+require 'rubygems/package_task'
+require 'find'
+
+spec = Gem::Specification.new do |s|
+ s.name = "leveldb-ruby"
+ s.version = "0.1"
+ s.date = Time.now.to_s
+ s.email = "wmorgan-leveldb-ruby-gemspec@masanjin.net"
+ s.authors = ["William Morgan"]
+ s.summary = "a Ruby binding to LevelDB"
+ s.homepage = "http://github.com/wmorgan/leveldb-ruby"
+ s.files = %w(README ext/leveldb/extconf.rb lib/leveldb.rb ext/leveldb/leveldb.cc)
+ Find.find("leveldb") { |x| s.files << x }
+ s.extensions = %w(ext/leveldb/extconf.rb)
+ s.executables = []
+ s.extra_rdoc_files = %w(README ext/leveldb/leveldb.cc)
+ s.rdoc_options = %w(-c utf8 --main README --title LevelDB)
+ s.description = "LevelDB-Ruby is a Ruby binding to LevelDB."
+end
+
+task :rdoc do |t|
+ sh "rm -rf doc; rdoc #{spec.rdoc_options.join(' ')} #{spec.extra_rdoc_files.join(' ')} lib/leveldb.rb"
+end
+
+Gem::PackageTask.new spec do
+end
+
+# vim: syntax=ruby
@@ -0,0 +1,10 @@
+require 'mkmf'
+require 'fileutils'
+
+Dir.chdir "../../leveldb"
+system "make" or abort
+Dir.chdir "../ext/leveldb"
+
+$CFLAGS << " -I../../leveldb/include"
+$LIBS << " -L../../leveldb -Wl,--whole-archive -lleveldb -Wl,--no-whole-archive"
+create_makefile "leveldb/leveldb"
@@ -0,0 +1,107 @@
+#include <ruby.h>
+
+#include "leveldb/db.h"
+#include "leveldb/write_batch.h"
+
+static VALUE m_leveldb;
+static VALUE c_db;
+static VALUE c_error;
+
+// support 1.9 and 1.8
+#ifndef RSTRING_PTR
+#define RSTRING_PTR(v) RSTRING(v)->ptr
+#endif
+
+// convert status errors into exceptions
+#define RAISE_ON_ERROR(status) do { \
+ if(!status.ok()) { \
+ VALUE exc = rb_exc_new2(c_error, status.ToString().c_str()); \
+ rb_exc_raise(exc); \
+ } \
+} while(0)
+
+typedef struct bound_db {
+ leveldb::DB* db;
+} bound_db;
+
+typedef struct bound_batch {
+ bound_db* db;
+ leveldb::WriteBatch* batch;
+} bound_batch;
+
+static void db_free(bound_db* db) {
+ delete db->db;
+}
+
+static VALUE db_make(VALUE klass, VALUE v_pathname, VALUE v_create_if_necessary, VALUE v_break_if_exists) {
+ Check_Type(v_pathname, T_STRING);
+
+ bound_db* db = new bound_db;
+ char* pathname_c = RSTRING_PTR(v_pathname);
+ std::string pathname = std::string((char*)RSTRING_PTR(v_pathname));
+
+ leveldb::Options options;
+ if(RTEST(v_create_if_necessary)) options.create_if_missing = true;
+ if(RTEST(v_break_if_exists)) options.error_if_exists = true;
+ leveldb::Status status = leveldb::DB::Open(options, pathname, &db->db);
+ RAISE_ON_ERROR(status);
+
+ VALUE o_db = Data_Wrap_Struct(klass, NULL, db_free, db);
+ VALUE argv[1] = { v_pathname };
+ rb_obj_call_init(o_db, 1, argv);
+
+ return o_db;
+}
+
+static VALUE db_get(VALUE self, VALUE v_key) {
+ Check_Type(v_key, T_STRING);
+
+ bound_db* db;
+ Data_Get_Struct(self, bound_db, db);
+
+ std::string key = std::string((char*)RSTRING_PTR(v_key));
+ std::string value;
+ leveldb::Status status = db->db->Get(leveldb::ReadOptions(), key, &value);
+ RAISE_ON_ERROR(status);
+
+ return rb_str_new2(value.c_str());
+}
+
+static VALUE db_put(VALUE self, VALUE v_key, VALUE v_value) {
+ Check_Type(v_key, T_STRING);
+ Check_Type(v_value, T_STRING);
+
+ bound_db* db;
+ Data_Get_Struct(self, bound_db, db);
+
+ std::string key = std::string((char*)RSTRING_PTR(v_key));
+ std::string value = std::string((char*)RSTRING_PTR(v_value));
+
+ leveldb::Status status = db->db->Put(leveldb::WriteOptions(), key, value);
+ RAISE_ON_ERROR(status);
+
+ return v_value;
+}
+
+static VALUE db_init(VALUE self, VALUE v_pathname) {
+ rb_iv_set(self, "@pathname", v_pathname);
+ return self;
+}
+
+extern "C" {
+void Init_leveldb() {
+ VALUE m_leveldb;
+
+ m_leveldb = rb_define_module("LevelDB");
+
+ c_db = rb_define_class_under(m_leveldb, "DB", rb_cObject);
+ rb_define_singleton_method(c_db, "make", (VALUE (*)(...))db_make, 3);
+ rb_define_method(c_db, "initialize", (VALUE (*)(...))db_init, 1);
+ rb_define_method(c_db, "get", (VALUE (*)(...))db_get, 1);
+ rb_define_method(c_db, "put", (VALUE (*)(...))db_put, 2);
+ //rb_define_singleton_method(c_index, "create", db_create, 1);
+ //rb_define_singleton_method(c_index, "load", db_load, 1);
+
+ c_error = rb_define_class_under(m_leveldb, "Error", rb_eStandardError);
+}
+}
@@ -0,0 +1,8 @@
+# Names should be added to this file like so:
+# Name or Organization <email address>
+
+Google Inc.
+
+# Initial version authors:
+Jeffrey Dean <jeff@google.com>
+Sanjay Ghemawat <sanjay@google.com>
@@ -0,0 +1,27 @@
+Copyright (c) 2011 The LevelDB Authors. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+ * Redistributions of source code must retain the above copyright
+notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+copyright notice, this list of conditions and the following disclaimer
+in the documentation and/or other materials provided with the
+distribution.
+ * Neither the name of Google Inc. nor the names of its
+contributors may be used to endorse or promote products derived from
+this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,172 @@
+# 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.
+
+CC = g++
+
+#-----------------------------------------------
+# Uncomment exactly one of the lines labelled (A), (B), and (C) below
+# to switch between compilation modes.
+
+OPT = -O2 -DNDEBUG # (A) Production use (optimized mode)
+# OPT = -g2 # (B) Debug mode, w/ full line-level debugging symbols
+# OPT = -O2 -g2 -DNDEBUG # (C) Profiling mode: opt, but w/debugging symbols
+#-----------------------------------------------
+
+
+UNAME := $(shell uname)
+
+ifeq ($(UNAME), Darwin)
+ # To build for iOS, set PLATFORM=IOS.
+ ifndef PLATFORM
+ PLATFORM=OSX
+ endif # PLATFORM
+ PLATFORM_CFLAGS = -DLEVELDB_PLATFORM_OSX
+ PORT_MODULE = port_osx.o
+else # UNAME
+ PLATFORM_CFLAGS = -DLEVELDB_PLATFORM_POSIX -std=c++0x
+ PORT_MODULE = port_posix.o
+endif # UNAME
+
+CFLAGS = -c -I. -I./include $(PLATFORM_CFLAGS) $(OPT) -DUSE_SNAPPY=1
+
+LDFLAGS=-lpthread
+
+LIBOBJECTS = \
+ ./db/builder.o \
+ ./db/db_impl.o \
+ ./db/db_iter.o \
+ ./db/filename.o \
+ ./db/dbformat.o \
+ ./db/log_reader.o \
+ ./db/log_writer.o \
+ ./db/memtable.o \
+ ./db/repair.o \
+ ./db/table_cache.o \
+ ./db/version_edit.o \
+ ./db/version_set.o \
+ ./db/write_batch.o \
+ ./port/$(PORT_MODULE) \
+ ./table/block.o \
+ ./table/block_builder.o \
+ ./table/format.o \
+ ./table/iterator.o \
+ ./table/merger.o \
+ ./table/table.o \
+ ./table/table_builder.o \
+ ./table/two_level_iterator.o \
+ ./util/arena.o \
+ ./util/cache.o \
+ ./util/coding.o \
+ ./util/comparator.o \
+ ./util/crc32c.o \
+ ./util/env.o \
+ ./util/env_posix.o \
+ ./util/hash.o \
+ ./util/histogram.o \
+ ./util/logging.o \
+ ./util/options.o \
+ ./util/status.o
+
+TESTUTIL = ./util/testutil.o
+TESTHARNESS = ./util/testharness.o $(TESTUTIL)
+
+TESTS = \
+ arena_test \
+ cache_test \
+ coding_test \
+ corruption_test \
+ crc32c_test \
+ db_test \
+ dbformat_test \
+ env_test \
+ filename_test \
+ log_test \
+ skiplist_test \
+ table_test \
+ version_edit_test \
+ write_batch_test
+
+PROGRAMS = db_bench $(TESTS)
+
+LIBRARY = libleveldb.a
+
+ifeq ($(PLATFORM), IOS)
+# Only XCode can build executable applications for iOS.
+all: $(LIBRARY)
+else
+all: $(PROGRAMS) $(LIBRARY)
+endif
+
+check: $(TESTS)
+ for t in $(TESTS); do echo "***** Running $$t"; ./$$t || exit 1; done
+
+clean:
+ -rm -f $(PROGRAMS) $(LIBRARY) */*.o ios-x86/*/*.o ios-arm/*/*.o
+ -rmdir -p ios-x86/* ios-arm/*
+
+$(LIBRARY): $(LIBOBJECTS)
+ rm -f $@
+ $(AR) -rs $@ $(LIBOBJECTS)
+
+db_bench: db/db_bench.o $(LIBOBJECTS) $(TESTUTIL)
+ $(CC) $(LDFLAGS) db/db_bench.o $(LIBOBJECTS) $(TESTUTIL) -o $@
+
+arena_test: util/arena_test.o $(LIBOBJECTS) $(TESTHARNESS)
+ $(CC) $(LDFLAGS) util/arena_test.o $(LIBOBJECTS) $(TESTHARNESS) -o $@
+
+cache_test: util/cache_test.o $(LIBOBJECTS) $(TESTHARNESS)
+ $(CC) $(LDFLAGS) util/cache_test.o $(LIBOBJECTS) $(TESTHARNESS) -o $@
+
+coding_test: util/coding_test.o $(LIBOBJECTS) $(TESTHARNESS)
+ $(CC) $(LDFLAGS) util/coding_test.o $(LIBOBJECTS) $(TESTHARNESS) -o $@
+
+corruption_test: db/corruption_test.o $(LIBOBJECTS) $(TESTHARNESS)
+ $(CC) $(LDFLAGS) db/corruption_test.o $(LIBOBJECTS) $(TESTHARNESS) -o $@
+
+crc32c_test: util/crc32c_test.o $(LIBOBJECTS) $(TESTHARNESS)
+ $(CC) $(LDFLAGS) util/crc32c_test.o $(LIBOBJECTS) $(TESTHARNESS) -o $@
+
+db_test: db/db_test.o $(LIBOBJECTS) $(TESTHARNESS)
+ $(CC) $(LDFLAGS) db/db_test.o $(LIBOBJECTS) $(TESTHARNESS) -o $@
+
+dbformat_test: db/dbformat_test.o $(LIBOBJECTS) $(TESTHARNESS)
+ $(CC) $(LDFLAGS) db/dbformat_test.o $(LIBOBJECTS) $(TESTHARNESS) -o $@
+
+env_test: util/env_test.o $(LIBOBJECTS) $(TESTHARNESS)
+ $(CC) $(LDFLAGS) util/env_test.o $(LIBOBJECTS) $(TESTHARNESS) -o $@
+
+filename_test: db/filename_test.o $(LIBOBJECTS) $(TESTHARNESS)
+ $(CC) $(LDFLAGS) db/filename_test.o $(LIBOBJECTS) $(TESTHARNESS) -o $@
+
+log_test: db/log_test.o $(LIBOBJECTS) $(TESTHARNESS)
+ $(CC) $(LDFLAGS) db/log_test.o $(LIBOBJECTS) $(TESTHARNESS) -o $@
+
+table_test: table/table_test.o $(LIBOBJECTS) $(TESTHARNESS)
+ $(CC) $(LDFLAGS) table/table_test.o $(LIBOBJECTS) $(TESTHARNESS) -o $@
+
+skiplist_test: db/skiplist_test.o $(LIBOBJECTS) $(TESTHARNESS)
+ $(CC) $(LDFLAGS) db/skiplist_test.o $(LIBOBJECTS) $(TESTHARNESS) -o $@
+
+version_edit_test: db/version_edit_test.o $(LIBOBJECTS) $(TESTHARNESS)
+ $(CC) $(LDFLAGS) db/version_edit_test.o $(LIBOBJECTS) $(TESTHARNESS) -o $@
+
+write_batch_test: db/write_batch_test.o $(LIBOBJECTS) $(TESTHARNESS)
+ $(CC) $(LDFLAGS) db/write_batch_test.o $(LIBOBJECTS) $(TESTHARNESS) -o $@
+
+ifeq ($(PLATFORM), IOS)
+# For iOS, create universal object files to be used on both the simulator and
+# a device.
+.cc.o:
+ mkdir -p ios-x86/$(dir $@)
+ $(CC) $(CFLAGS) -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk -arch i686 $< -o ios-x86/$@
+ mkdir -p ios-arm/$(dir $@)
+ $(CC) $(CFLAGS) -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk -arch armv6 -arch armv7 $< -o ios-arm/$@
+ lipo ios-x86/$@ ios-arm/$@ -create -output $@
+else
+.cc.o:
+ $(CC) $(CFLAGS) $< -o $@
+endif
+
+# TODO(gabor): dependencies for .o files
+# TODO(gabor): Build library
Oops, something went wrong.

0 comments on commit 63e6edb

Please sign in to comment.