@@ -17,130 +17,156 @@ been established.
17
17
18
18
Recommended Package imports:
19
19
20
- import org.iq80.leveldb.*;
21
- import static org.iq80.leveldb.impl.Iq80DBFactory.*;
22
- import java.io.*;
20
+ ``` java
21
+ import org.iq80.leveldb.* ;
22
+ import static org.iq80.leveldb.impl.Iq80DBFactory.* ;
23
+ import java.io.* ;
24
+ ```
23
25
24
26
Opening and closing the database.
25
27
26
- Options options = new Options();
27
- options.createIfMissing(true);
28
- DB db = factory.open(new File("example"), options);
29
- try {
30
- // Use the db in here....
31
- } finally {
32
- // Make sure you close the db to shutdown the
33
- // database and avoid resource leaks.
34
- db.close();
35
- }
28
+ ``` java
29
+ Options options = new Options ();
30
+ options. createIfMissing(true );
31
+ DB db = factory. open(new File (" example" ), options);
32
+ try {
33
+ // Use the db in here....
34
+ } finally {
35
+ // Make sure you close the db to shutdown the
36
+ // database and avoid resource leaks.
37
+ db. close();
38
+ }
39
+ ```
36
40
37
41
Putting, Getting, and Deleting key/values.
38
42
39
- db.put(bytes("Tampa"), bytes("rocks"));
40
- String value = asString(db.get(bytes("Tampa")));
41
- db.delete(bytes("Tampa"), wo);
43
+ ``` java
44
+ db. put(bytes(" Tampa" ), bytes(" rocks" ));
45
+ String value = asString(db. get(bytes(" Tampa" )));
46
+ db. delete(bytes(" Tampa" ), wo);
47
+ ```
42
48
43
49
Performing Batch/Bulk/Atomic Updates.
44
50
45
- WriteBatch batch = db.createWriteBatch();
46
- try {
47
- batch.delete(bytes("Denver"));
48
- batch.put(bytes("Tampa"), bytes("green"));
49
- batch.put(bytes("London"), bytes("red"));
51
+ ``` java
52
+ WriteBatch batch = db. createWriteBatch();
53
+ try {
54
+ batch. delete(bytes(" Denver" ));
55
+ batch. put(bytes(" Tampa" ), bytes(" green" ));
56
+ batch. put(bytes(" London" ), bytes(" red" ));
50
57
51
- db.write(batch);
52
- } finally {
53
- // Make sure you close the batch to avoid resource leaks.
54
- batch.close();
55
- }
58
+ db. write(batch);
59
+ } finally {
60
+ // Make sure you close the batch to avoid resource leaks.
61
+ batch. close();
62
+ }
63
+ ```
56
64
57
65
Iterating key/values.
58
66
59
- DBIterator iterator = db.iterator();
60
- try {
61
- for(iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
62
- String key = asString(iterator.peekNext().getKey());
63
- String value = asString(iterator.peekNext().getValue());
64
- System.out.println(key+" = "+value);
65
- }
66
- } finally {
67
- // Make sure you close the iterator to avoid resource leaks.
68
- iterator.close();
69
- }
67
+ ``` java
68
+ DBIterator iterator = db. iterator();
69
+ try {
70
+ for (iterator. seekToFirst(); iterator. hasNext(); iterator. next()) {
71
+ String key = asString(iterator. peekNext(). getKey());
72
+ String value = asString(iterator. peekNext(). getValue());
73
+ System . out. println(key+ " = " + value);
74
+ }
75
+ } finally {
76
+ // Make sure you close the iterator to avoid resource leaks.
77
+ iterator. close();
78
+ }
79
+ ```
70
80
71
81
Working against a Snapshot view of the Database.
72
82
73
- ReadOptions ro = new ReadOptions();
74
- ro.snapshot(db.getSnapshot());
75
- try {
76
-
77
- // All read operations will now use the same
78
- // consistent view of the data.
79
- ... = db.iterator(ro);
80
- ... = db.get(bytes("Tampa"), ro);
81
-
82
- } finally {
83
- // Make sure you close the snapshot to avoid resource leaks.
84
- ro.snapshot().close();
85
- }
83
+ ``` java
84
+ ReadOptions ro = new ReadOptions ();
85
+ ro. snapshot(db. getSnapshot());
86
+ try {
87
+
88
+ // All read operations will now use the same
89
+ // consistent view of the data.
90
+ ... = db. iterator(ro);
91
+ ... = db. get(bytes(" Tampa" ), ro);
92
+
93
+ } finally {
94
+ // Make sure you close the snapshot to avoid resource leaks.
95
+ ro. snapshot(). close();
96
+ }
97
+ ```
86
98
87
99
Using a custom Comparator.
88
100
89
- DBComparator comparator = new DBComparator(){
90
- public int compare(byte[] key1, byte[] key2) {
91
- return new String(key1).compareTo(new String(key2));
92
- }
93
- public String name() {
94
- return "simple";
95
- }
96
- public byte[] findShortestSeparator(byte[] start, byte[] limit) {
97
- return start;
98
- }
99
- public byte[] findShortSuccessor(byte[] key) {
100
- return key;
101
- }
102
- };
103
- Options options = new Options();
104
- options.comparator(comparator);
105
- DB db = factory.open(new File("example"), options);
101
+ ``` java
102
+ DBComparator comparator = new DBComparator (){
103
+ public int compare (byte [] key1 , byte [] key2 ) {
104
+ return new String (key1). compareTo(new String (key2));
105
+ }
106
+ public String name () {
107
+ return " simple" ;
108
+ }
109
+ public byte [] findShortestSeparator (byte [] start , byte [] limit ) {
110
+ return start;
111
+ }
112
+ public byte [] findShortSuccessor (byte [] key ) {
113
+ return key;
114
+ }
115
+ };
116
+ Options options = new Options ();
117
+ options. comparator(comparator);
118
+ DB db = factory. open(new File (" example" ), options);
119
+ ```
106
120
107
121
Disabling Compression
108
122
109
- Options options = new Options();
110
- options.compressionType(CompressionType.NONE);
111
- DB db = factory.open(new File("example"), options);
123
+ ``` java
124
+ Options options = new Options ();
125
+ options. compressionType(CompressionType . NONE );
126
+ DB db = factory. open(new File (" example" ), options);
127
+ ```
112
128
113
129
Configuring the Cache
114
-
115
- Options options = new Options();
116
- options.cacheSize(100 * 1048576); // 100MB cache
117
- DB db = factory.open(new File("example"), options);
130
+
131
+ ``` java
132
+ Options options = new Options ();
133
+ options. cacheSize(100 * 1048576 ); // 100MB cache
134
+ DB db = factory. open(new File (" example" ), options);
135
+ ```
118
136
119
137
Getting approximate sizes.
120
138
121
- long[] sizes = db.getApproximateSizes(new Range(bytes("a"), bytes("k")), new Range(bytes("k"), bytes("z")));
122
- System.out.println("Size: "+sizes[0]+", "+sizes[1]);
139
+ ``` java
140
+ long [] sizes = db. getApproximateSizes(new Range (bytes(" a" ), bytes(" k" )), new Range (bytes(" k" ), bytes(" z" )));
141
+ System . out. println(" Size: " + sizes[0 ]+ " , " + sizes[1 ]);
142
+ ```
123
143
124
144
Getting database status.
125
145
126
- String stats = db.getProperty("leveldb.stats");
127
- System.out.println(stats);
146
+ ``` java
147
+ String stats = db. getProperty(" leveldb.stats" );
148
+ System . out. println(stats);
149
+ ```
128
150
129
151
Getting informational log messages.
130
152
131
- Logger logger = new Logger() {
132
- public void log(String message) {
133
- System.out.println(message);
134
- }
135
- };
136
- Options options = new Options();
137
- options.logger(logger);
138
- DB db = factory.open(new File("example"), options);
153
+ ``` java
154
+ Logger logger = new Logger () {
155
+ public void log (String message ) {
156
+ System . out. println(message);
157
+ }
158
+ };
159
+ Options options = new Options ();
160
+ options. logger(logger);
161
+ DB db = factory. open(new File (" example" ), options);
162
+ ```
139
163
140
164
Destroying a database.
141
-
142
- Options options = new Options();
143
- factory.destroy(new File("example"), options);
165
+
166
+ ``` java
167
+ Options options = new Options ();
168
+ factory. destroy(new File (" example" ), options);
169
+ ```
144
170
145
171
# Projects using this port of LevelDB
146
172
0 commit comments