GithubHelp home page GithubHelp logo

lu4 / leveldb-swift Goto Github PK

View Code? Open in Web Editor NEW

This project forked from emilwojtaszek/leveldb-swift

0.0 2.0 0.0 169 KB

Swift LevelDB Wrapper

License: MIT License

Swift 97.86% Objective-C 0.94% Ruby 1.20%

leveldb-swift's Introduction

#LevelDB - Swift

##A Swift wrapper for LevelDB*

This is a pure Swift wrapper around the LevelDB C API, configured to build OS X & iOS dynamic frameworks. It's a minimal, low-level wrapper without a lot of the usual convenience methods (subscripts, string keys, queries etc). The goal is to eventually expose all of the LevelDB functionality with a reasonably idiomatic Swift API, so that more sophisticated mobile database frameworks can be built on top.

The current version should be considered an ALPHA release and the API will probably change from here.

Basic usage:

let db = try Database.createDatabase(directory, options: Options(createIfMissing: true))
try db.put(key, value: value)
try db.get(key)

WriteBatch support:

let batch = WriteBatch()
batch.put(key1, value1)
batch.delete(key2)
try db.write(batch)

The C++ style LevelDB iterator has been wrapped with Swift SequenceTypes:

for key: String in db.keys()
	print(key)
}

for (key, value) in db.values(from: startKey, to: endKey, descending: true)
	print("\(key): \(value)")
}

Custom Swift Comparator class. This required some C glue code - I'm not the world's best C programmer, so there's a good chance I've stuffed up something really basic.

class MyCustomComparator: Comparator {
	var name: String {
		get {
			return "MyCustomComparator"
		}
	}
	
    func compare(a: LevelDB.Slice, _ b: LevelDB.Slice) -> NSComparisonResult {
        let string1 = String(bytes: a.bytes, length: a.length)
        let string2 = String(bytes: b.bytes, length: b.length)
        return string1.compare(string2)
    }
}

Note that all values are NSData instances, as this is the closest Foundation equivalent to LevelDB's Slice, but this will probably change to a Swift type eventually. Keys must conform to KeyType - there are built in implementations for String and NSData to allow the use of binary keys (and keep Andy happy).

The LevelDB source is included as a submodule (run git submodule init && git submodule update if you've cloned the source). It's compiled into the framework targets directly by Xcode rather than using an external build target because I rage-quit the LevelDB Makefile.

The TODO list:

  • Implement filter policy support
  • More code comments & better doco
  • Enable Snappy compression?
  • Snapshot might be better as a trailing closure method
  • DB properties, compact & cache support
  • Use UnsafeBufferPointer<Int8> rather than the LevelDB.Slice
  • Check Carthage support
  • SwiftCheck tests

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.