Skip to content

Commit 245a6f2

Browse files
johnjohndoedain
authored andcommitted
Enable syntax highlighting for code snippets.
1 parent 30f7121 commit 245a6f2

File tree

1 file changed

+115
-89
lines changed

1 file changed

+115
-89
lines changed

README.md

+115-89
Original file line numberDiff line numberDiff line change
@@ -17,130 +17,156 @@ been established.
1717

1818
Recommended Package imports:
1919

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+
```
2325

2426
Opening and closing the database.
2527

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+
```
3640

3741
Putting, Getting, and Deleting key/values.
3842

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+
```
4248

4349
Performing Batch/Bulk/Atomic Updates.
4450

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"));
5057

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+
```
5664

5765
Iterating key/values.
5866

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+
```
7080

7181
Working against a Snapshot view of the Database.
7282

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+
```
8698

8799
Using a custom Comparator.
88100

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+
```
106120

107121
Disabling Compression
108122

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+
```
112128

113129
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+
```
118136

119137
Getting approximate sizes.
120138

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+
```
123143

124144
Getting database status.
125145

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+
```
128150

129151
Getting informational log messages.
130152

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+
```
139163

140164
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+
```
144170

145171
# Projects using this port of LevelDB
146172

0 commit comments

Comments
 (0)