Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HDDS-748. Use strongly typed metadata Table implementation. Contribut…
…ed by Elek Marton.
- Loading branch information
1 parent
99e201d
commit d15dc43
Showing
18 changed files
with
765 additions
and
136 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
hadoop-hdds/common/src/main/java/org/apache/hadoop/utils/db/ByteArrayKeyValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,67 @@ | |||
/* | |||
* 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.hadoop.utils.db; | |||
|
|||
import org.apache.hadoop.utils.db.Table.KeyValue; | |||
|
|||
/** | |||
* Key value for raw Table implementations. | |||
*/ | |||
public final class ByteArrayKeyValue implements KeyValue<byte[], byte[]> { | |||
private byte[] key; | |||
private byte[] value; | |||
|
|||
private ByteArrayKeyValue(byte[] key, byte[] value) { | |||
this.key = key; | |||
this.value = value; | |||
} | |||
|
|||
/** | |||
* Create a KeyValue pair. | |||
* | |||
* @param key - Key Bytes | |||
* @param value - Value bytes | |||
* @return KeyValue object. | |||
*/ | |||
public static ByteArrayKeyValue create(byte[] key, byte[] value) { | |||
return new ByteArrayKeyValue(key, value); | |||
} | |||
|
|||
/** | |||
* Return key. | |||
* | |||
* @return byte[] | |||
*/ | |||
public byte[] getKey() { | |||
byte[] result = new byte[key.length]; | |||
System.arraycopy(key, 0, result, 0, key.length); | |||
return result; | |||
} | |||
|
|||
/** | |||
* Return value. | |||
* | |||
* @return byte[] | |||
*/ | |||
public byte[] getValue() { | |||
byte[] result = new byte[value.length]; | |||
System.arraycopy(value, 0, result, 0, value.length); | |||
return result; | |||
} | |||
} |
38 changes: 38 additions & 0 deletions
38
hadoop-hdds/common/src/main/java/org/apache/hadoop/utils/db/Codec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,38 @@ | |||
/* | |||
* 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.hadoop.utils.db; | |||
|
|||
/** | |||
* Codec interface to marshall/unmarshall data to/from a byte[] based | |||
* key/value store. | |||
* | |||
* @param <T> Unserialized type | |||
*/ | |||
public interface Codec<T> { | |||
|
|||
/** | |||
* Convert object to raw persisted format. | |||
*/ | |||
byte[] toPersistedFormat(T object); | |||
|
|||
/** | |||
* Convert object from raw persisted format. | |||
*/ | |||
T fromPersistedFormat(byte[] rawData); | |||
} |
70 changes: 70 additions & 0 deletions
70
hadoop-hdds/common/src/main/java/org/apache/hadoop/utils/db/CodecRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,70 @@ | |||
/* | |||
* 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.hadoop.utils.db; | |||
|
|||
import java.util.HashMap; | |||
import java.util.Map; | |||
|
|||
/** | |||
* Collection of available codecs. | |||
*/ | |||
public class CodecRegistry { | |||
|
|||
private Map<Class, Codec<?>> valueCodecs; | |||
|
|||
public CodecRegistry() { | |||
valueCodecs = new HashMap<>(); | |||
valueCodecs.put(String.class, new StringCodec()); | |||
} | |||
|
|||
/** | |||
* Convert raw value to strongly typed value/key with the help of a codec. | |||
* | |||
* @param rawData original byte array from the db. | |||
* @param format Class of the return value | |||
* @param <T> Type of the return value. | |||
* @return the object with the parsed field data | |||
*/ | |||
public <T> T asObject(byte[] rawData, Class<T> format) { | |||
if (valueCodecs.containsKey(format)) { | |||
return (T) valueCodecs.get(format).fromPersistedFormat(rawData); | |||
} else { | |||
throw new IllegalStateException( | |||
"Codec is not registered for type: " + format); | |||
} | |||
} | |||
|
|||
/** | |||
* Convert strongly typed object to raw data to store it in the kv store. | |||
* | |||
* @param object typed object. | |||
* @param <T> Type of the typed object. | |||
* @return byte array to store it ini the kv store. | |||
*/ | |||
public <T> byte[] asRawData(T object) { | |||
Class<T> format = (Class<T>) object.getClass(); | |||
if (valueCodecs.containsKey(format)) { | |||
Codec<T> codec = (Codec<T>) valueCodecs.get(format); | |||
return codec.toPersistedFormat(object); | |||
} else { | |||
throw new IllegalStateException( | |||
"Codec is not registered for type: " + format); | |||
} | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
hadoop-hdds/common/src/main/java/org/apache/hadoop/utils/db/StringCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,45 @@ | |||
/* | |||
* 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.hadoop.utils.db; | |||
|
|||
import org.apache.hadoop.hdfs.DFSUtil; | |||
|
|||
/** | |||
* Codec to convert String to/from byte array. | |||
*/ | |||
public class StringCodec implements Codec<String> { | |||
|
|||
@Override | |||
public byte[] toPersistedFormat(String object) { | |||
if (object != null) { | |||
return DFSUtil.string2Bytes(object); | |||
} else { | |||
return null; | |||
} | |||
} | |||
|
|||
@Override | |||
public String fromPersistedFormat(byte[] rawData) { | |||
if (rawData != null) { | |||
return DFSUtil.bytes2String(rawData); | |||
} else { | |||
return null; | |||
} | |||
} | |||
} |
Oops, something went wrong.