You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.3 KiB

  1. This is an implementation of the [LevelDB key/value database](http:code.google.com/p/leveldb) in the [Go programming language](http:golang.org).
  2. [![Build Status](https://travis-ci.org/syndtr/goleveldb.png?branch=master)](https://travis-ci.org/syndtr/goleveldb)
  3. Installation
  4. -----------
  5. go get github.com/syndtr/goleveldb/leveldb
  6. Requirements
  7. -----------
  8. * Need at least `go1.5` or newer.
  9. Usage
  10. -----------
  11. Create or open a database:
  12. ```go
  13. // The returned DB instance is safe for concurrent use. Which mean that all
  14. // DB's methods may be called concurrently from multiple goroutine.
  15. db, err := leveldb.OpenFile("path/to/db", nil)
  16. ...
  17. defer db.Close()
  18. ...
  19. ```
  20. Read or modify the database content:
  21. ```go
  22. // Remember that the contents of the returned slice should not be modified.
  23. data, err := db.Get([]byte("key"), nil)
  24. ...
  25. err = db.Put([]byte("key"), []byte("value"), nil)
  26. ...
  27. err = db.Delete([]byte("key"), nil)
  28. ...
  29. ```
  30. Iterate over database content:
  31. ```go
  32. iter := db.NewIterator(nil, nil)
  33. for iter.Next() {
  34. // Remember that the contents of the returned slice should not be modified, and
  35. // only valid until the next call to Next.
  36. key := iter.Key()
  37. value := iter.Value()
  38. ...
  39. }
  40. iter.Release()
  41. err = iter.Error()
  42. ...
  43. ```
  44. Seek-then-Iterate:
  45. ```go
  46. iter := db.NewIterator(nil, nil)
  47. for ok := iter.Seek(key); ok; ok = iter.Next() {
  48. // Use key/value.
  49. ...
  50. }
  51. iter.Release()
  52. err = iter.Error()
  53. ...
  54. ```
  55. Iterate over subset of database content:
  56. ```go
  57. iter := db.NewIterator(&util.Range{Start: []byte("foo"), Limit: []byte("xoo")}, nil)
  58. for iter.Next() {
  59. // Use key/value.
  60. ...
  61. }
  62. iter.Release()
  63. err = iter.Error()
  64. ...
  65. ```
  66. Iterate over subset of database content with a particular prefix:
  67. ```go
  68. iter := db.NewIterator(util.BytesPrefix([]byte("foo-")), nil)
  69. for iter.Next() {
  70. // Use key/value.
  71. ...
  72. }
  73. iter.Release()
  74. err = iter.Error()
  75. ...
  76. ```
  77. Batch writes:
  78. ```go
  79. batch := new(leveldb.Batch)
  80. batch.Put([]byte("foo"), []byte("value"))
  81. batch.Put([]byte("bar"), []byte("another value"))
  82. batch.Delete([]byte("baz"))
  83. err = db.Write(batch, nil)
  84. ...
  85. ```
  86. Use bloom filter:
  87. ```go
  88. o := &opt.Options{
  89. Filter: filter.NewBloomFilter(10),
  90. }
  91. db, err := leveldb.OpenFile("path/to/db", o)
  92. ...
  93. defer db.Close()
  94. ...
  95. ```
  96. Documentation
  97. -----------
  98. You can read package documentation [here](http:godoc.org/github.com/syndtr/goleveldb).