Skip to content

Commit

Permalink
Add some simplified kv methods in PTable (YahooArchive#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
sijie authored and zhaijack committed Jan 24, 2018
1 parent 7464dcc commit caa97bd
Show file tree
Hide file tree
Showing 14 changed files with 764 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.distributedlog.api.exceptions;

/**
* API exception.
*/
public class ApiException extends Exception {

public ApiException(String message) {
super(message);
}

public ApiException(String message, Throwable cause) {
super(message, cause);
}

public ApiException(Throwable cause) {
super(cause);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* API related exceptions.
*/
package org.apache.distributedlog.api.exceptions;
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@

import org.apache.bookkeeper.common.annotation.InterfaceAudience;
import org.apache.bookkeeper.common.annotation.InterfaceStability;
import org.apache.distributedlog.api.kv.op.OpFactory;

/**
* Interface of kv client talking to partitioned table.
*/
@InterfaceAudience.Public
@InterfaceStability.Evolving
public interface PTable<K, V> extends PTableReadView<K, V>, PTableWriteView<K, V> {

OpFactory<K, V> opFactory();

}
72 changes: 72 additions & 0 deletions api/src/main/java/org/apache/distributedlog/api/kv/PTableBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.distributedlog.api.kv;

import org.apache.distributedlog.api.kv.op.CompareOp;
import org.apache.distributedlog.api.kv.op.CompareResult;
import org.apache.distributedlog.api.kv.op.DeleteOp;
import org.apache.distributedlog.api.kv.op.OpFactory;
import org.apache.distributedlog.api.kv.op.PutOp;
import org.apache.distributedlog.api.kv.op.RangeOp;

/**
* A base class for {@link PTable}.
*/
public interface PTableBase<K, V> extends AutoCloseable {

OpFactory<K, V> opFactory();

default CompareOp<K, V> compareCreateRevision(CompareResult result, K key, long revision) {
return opFactory().compareCreateRevision(result, key, revision);
}

default CompareOp<K, V> compareModRevision(CompareResult result, K key, long revision) {
return opFactory().compareModRevision(result, key, revision);
}

default CompareOp<K, V> compareVersion(CompareResult result, K key, long version) {
return opFactory().compareVersion(result, key, version);
}

default CompareOp<K, V> compareValue(CompareResult result, K key, V value) {
return opFactory().compareValue(result, key, value);
}

default PutOp<K, V> newPut(K key, V value) {
return opFactory().newPut(key, value, opFactory().optionFactory().newPutOption().prevKv(false).build());
}

default DeleteOp<K, V> newDelete(K key) {
return opFactory().newDelete(key, opFactory().optionFactory().newDeleteOption().prevKv(false).build());
}

default RangeOp<K, V> newGet(K key) {
return opFactory().newRange(
key,
opFactory().optionFactory().newRangeOption()
.keysOnly(false)
.countOnly(false)
.limit(1)
.endKey(null)
.build());
}

@Override
void close();

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,81 @@

package org.apache.distributedlog.api.kv;

import static io.netty.util.ReferenceCountUtil.retain;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.apache.bookkeeper.common.annotation.InterfaceAudience;
import org.apache.bookkeeper.common.annotation.InterfaceStability;
import org.apache.distributedlog.api.kv.options.RangeOption;
import org.apache.distributedlog.api.kv.result.KeyValue;
import org.apache.distributedlog.api.kv.result.RangeResult;

/**
* A review view of a key/value space.
*/
@InterfaceAudience.Public
@InterfaceStability.Evolving
public interface PTableReadView<K, V> extends AutoCloseable {
public interface PTableReadView<K, V> extends PTableBase<K, V> {

CompletableFuture<RangeResult<K, V>> get(K pKey, K lKey, RangeOption<K> option);

default CompletableFuture<V> get(K pKey, K lKey) {
RangeOption<K> option = opFactory().optionFactory().newRangeOption().build();
return get(pKey, lKey, option)
.thenApply(result -> {
try {
if (result.count() == 0) {
return null;
} else {
return retain(result.kvs().get(0).value());
}
} finally {
result.close();
}
})
.whenComplete((value, cause) -> option.close());

}

default CompletableFuture<KeyValue<K, V>> getKv(K pKey, K lKey) {
RangeOption<K> option = opFactory().optionFactory().newRangeOption()
.limit(1)
.endKey(null)
.build();
return get(pKey, lKey, option)
.thenApply(result -> {
try {
if (result.count() == 0) {
return null;
} else {
return result.getKvsAndClear().get(0);
}
} finally {
result.close();
}
})
.whenComplete((value, cause) -> option.close());
}

default CompletableFuture<List<KeyValue<K, V>>> range(K pKey, K lStartKey, K lEndKey) {
RangeOption<K> option = opFactory().optionFactory().newRangeOption()
.countOnly(false)
.keysOnly(false)
.limit(Long.MAX_VALUE)
.endKey(lEndKey)
.build();
return get(pKey, lStartKey, option)
.thenApply(result -> {
try {
return result.getKvsAndClear();
} finally {
result.close();
}
})
.whenComplete((value, cause) -> option.close());
}

CompletableFuture<RangeResult<K, V>> get(K pKey, K lKey, RangeOption<K> option);

void close();

}
Loading

0 comments on commit caa97bd

Please sign in to comment.