Skip to content

Commit 2d7bcb2

Browse files
committed
Store keys in a system keyspace
Added the idea of complex operations (multi-row operations) Added system keyspaces StructuredSetOperation stores each of the keys Scan can now be filtered by keyspace (inefficiently!)
1 parent 4977965 commit 2d7bcb2

32 files changed

Lines changed: 353 additions & 140 deletions

cloudata-keyvalue/src/main/java/com/cloudata/keyvalue/KeyValueStateMachine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ public Value get(long storeId, Keyspace keyspace, ByteString key) {
165165
return keyValueStore.get(keyspace.mapToKey(key).asReadOnlyByteBuffer());
166166
}
167167

168-
public BtreeQuery scan(long storeId) {
168+
public BtreeQuery scan(long storeId, Keyspace keyspace) {
169169
KeyValueStore keyValueStore = getKeyValueStore(storeId);
170-
return keyValueStore.buildQuery();
170+
return keyValueStore.buildQuery(keyspace);
171171
}
172172

173173
}

cloudata-keyvalue/src/main/java/com/cloudata/keyvalue/KeyValueStore.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import com.cloudata.btree.Btree;
1111
import com.cloudata.btree.BtreeQuery;
12+
import com.cloudata.btree.Keyspace;
1213
import com.cloudata.btree.MmapPageStore;
1314
import com.cloudata.btree.PageStore;
1415
import com.cloudata.btree.ReadOnlyTransaction;
@@ -44,8 +45,8 @@ public Value get(final ByteBuffer key) {
4445
}
4546
}
4647

47-
public BtreeQuery buildQuery() {
48-
return new BtreeQuery(btree);
48+
public BtreeQuery buildQuery(Keyspace keyspace) {
49+
return new BtreeQuery(btree, keyspace);
4950
}
5051

5152
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.cloudata.keyvalue.operation;
22

3-
import com.cloudata.btree.BtreeOperation;
3+
import com.cloudata.btree.operation.RowOperation;
44
import com.cloudata.keyvalue.KeyValueProto.KvEntry;
55

6-
public interface KeyOperation<V> extends BtreeOperation<V> {
6+
public interface KeyOperation<V> extends RowOperation<V> {
77

88
public abstract KvEntry.Builder serialize();
99
}

cloudata-keyvalue/src/main/java/com/cloudata/keyvalue/redis/RedisEndpoint.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import io.netty.bootstrap.ServerBootstrap;
44
import io.netty.channel.Channel;
5+
import io.netty.channel.ChannelFuture;
56
import io.netty.channel.ChannelInitializer;
67
import io.netty.channel.ChannelOption;
78
import io.netty.channel.ChannelPipeline;
89
import io.netty.channel.nio.NioEventLoopGroup;
910
import io.netty.channel.socket.SocketChannel;
1011
import io.netty.channel.socket.nio.NioServerSocketChannel;
1112
import io.netty.util.concurrent.DefaultEventExecutorGroup;
13+
import io.netty.util.concurrent.Future;
1214

1315
import java.net.SocketAddress;
1416

@@ -65,8 +67,11 @@ protected void initChannel(SocketChannel ch) throws Exception {
6567
}
6668

6769
public void stop() throws InterruptedException {
68-
group.shutdownGracefully();
70+
Future<?> f1 = group.shutdownGracefully();
6971

70-
serverChannel.close().sync();
72+
ChannelFuture f2 = serverChannel.close();
73+
74+
f1.sync();
75+
f2.sync();
7176
}
7277
}

cloudata-keyvalue/src/main/java/com/cloudata/keyvalue/redis/commands/SelectCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public RedisResponse execute(RedisServer server, RedisSession session, RedisRequ
2424
return new ErrorRedisReponse("invalid DB index");
2525
}
2626

27-
session.setKeyspace(Keyspace.build(Ints.checkedCast(keyspaceId)));
27+
session.setKeyspace(Keyspace.user(Ints.checkedCast(keyspaceId)));
2828
return StatusRedisResponse.OK;
2929
}
3030
}

cloudata-keyvalue/src/main/java/com/cloudata/keyvalue/web/KeyValueEndpoint.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
import com.cloudata.btree.BtreeQuery;
2727
import com.cloudata.btree.Keyspace;
2828
import com.cloudata.keyvalue.KeyValueProto.KvAction;
29+
import com.cloudata.keyvalue.KeyValueStateMachine;
2930
import com.cloudata.keyvalue.operation.DeleteOperation;
3031
import com.cloudata.keyvalue.operation.IncrementOperation;
3132
import com.cloudata.keyvalue.operation.SetOperation;
32-
import com.cloudata.keyvalue.KeyValueStateMachine;
3333
import com.cloudata.values.Value;
3434
import com.google.common.io.BaseEncoding;
3535
import com.google.common.io.ByteStreams;
@@ -63,7 +63,7 @@ public Response get(@PathParam("key") String key) throws IOException {
6363
@GET
6464
@Produces(MediaType.APPLICATION_OCTET_STREAM)
6565
public Response query() throws IOException {
66-
BtreeQuery query = stateMachine.scan(storeId);
66+
BtreeQuery query = stateMachine.scan(storeId, getKeyspace());
6767

6868
query.setFormat(MediaType.APPLICATION_OCTET_STREAM_TYPE);
6969
return Response.ok(query).build();

cloudata-server-shared/src/main/java/com/cloudata/btree/BranchPage.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.slf4j.Logger;
1010
import org.slf4j.LoggerFactory;
1111

12+
import com.cloudata.btree.operation.RowOperation;
1213
import com.cloudata.util.Hex;
1314

1415
public class BranchPage extends Page {
@@ -45,7 +46,8 @@ private int findPos(ByteBuffer find) {
4546

4647
ByteBuffer midKey = getKey(mid);
4748
int comparison = ByteBuffers.compare(midKey, find);
48-
log.info("comparison: @{} {} vs {}: {}", mid, Hex.forDebug(midKey), Hex.forDebug(find), comparison);
49+
// log.info("comparison: @{} {} vs {}: {}", mid, Hex.forDebug(midKey), Hex.forDebug(find),
50+
// comparison);
4951

5052
if (comparison < 0) {
5153
min = mid + 1;
@@ -316,7 +318,7 @@ private int findPos(ByteBuffer find) {
316318

317319
ByteBuffer midKey = getKey(mid);
318320
int comparison = ByteBuffers.compare(midKey, find);
319-
log.info("comparison: @{} {} vs {}: {}", mid, Hex.forDebug(midKey), Hex.forDebug(find), comparison);
321+
// log.info("comparison: @{} {} vs {}: {}", mid, Hex.forDebug(midKey), Hex.forDebug(find), comparison);
320322

321323
if (comparison < 0) {
322324
min = mid + 1;
@@ -411,7 +413,7 @@ Mutable getMutable() {
411413
}
412414

413415
@Override
414-
public <V> void doAction(Transaction txn, ByteBuffer key, BtreeOperation<V> operation) {
416+
public <V> void doAction(Transaction txn, ByteBuffer key, RowOperation<V> operation) {
415417
int pos = findPos(key);
416418
if (pos < 0) {
417419
pos = 0;

cloudata-server-shared/src/main/java/com/cloudata/btree/BtreeOperation.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

cloudata-server-shared/src/main/java/com/cloudata/btree/BtreeQuery.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ public class BtreeQuery {
99
private final Btree btree;
1010
private final ByteBuffer start;
1111
private MediaType format;
12+
private final Keyspace keyspace;
1213

13-
public BtreeQuery(Btree btree) {
14+
public BtreeQuery(Btree btree, Keyspace keyspace) {
1415
this.btree = btree;
16+
this.keyspace = keyspace;
1517
this.start = null;
1618
}
1719

@@ -41,6 +43,9 @@ public KeyValueResultset(ReadOnlyTransaction txn) {
4143
}
4244

4345
public void walk(EntryListener entryListener) {
46+
if (keyspace != null) {
47+
entryListener = new KeyspaceFilter(keyspace, entryListener);
48+
}
4449
txn.walk(btree, start, entryListener);
4550
}
4651

cloudata-server-shared/src/main/java/com/cloudata/btree/EntryListener.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@
66

77
public interface EntryListener {
88

9+
/*
10+
* Called for each entry.
11+
*
12+
* Return false to terminate the walk.
13+
*/
914
public boolean found(ByteBuffer key, Value value);
1015
}

0 commit comments

Comments
 (0)