add compressed string

This commit is contained in:
kimchy 2010-08-19 15:33:57 +03:00
parent 1a18387fab
commit 70c344b079
3 changed files with 118 additions and 0 deletions

View File

@ -32,6 +32,7 @@
<w>configurator</w>
<w>coord</w>
<w>cpus</w>
<w>cstr</w>
<w>datagram</w>
<w>datas</w>
<w>deque</w>

View File

@ -0,0 +1,77 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search 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.elasticsearch.common.compress;
import org.apache.lucene.util.UnicodeUtil;
import org.elasticsearch.common.Unicode;
import org.elasticsearch.common.compress.lzf.LZFDecoder;
import org.elasticsearch.common.compress.lzf.LZFEncoder;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import java.io.IOException;
import java.util.Arrays;
/**
* @author kimchy (shay.banon)
*/
public class CompressedString implements Streamable {
private byte[] bytes;
public CompressedString(String str) throws IOException {
UnicodeUtil.UTF8Result result = Unicode.unsafeFromStringAsUtf8(str);
this.bytes = LZFEncoder.encodeWithCache(result.result, result.length);
}
public byte[] bytes() {
return this.bytes;
}
public String string() throws IOException {
return Unicode.fromBytes(LZFDecoder.decode(bytes));
}
@Override public void readFrom(StreamInput in) throws IOException {
bytes = new byte[in.readVInt()];
in.readBytes(bytes, 0, bytes.length);
}
@Override public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(bytes.length);
out.writeBytes(bytes);
}
@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CompressedString that = (CompressedString) o;
if (!Arrays.equals(bytes, that.bytes)) return false;
return true;
}
@Override public int hashCode() {
return bytes != null ? Arrays.hashCode(bytes) : 0;
}
}

View File

@ -0,0 +1,40 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search 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.elasticsearch.common.compress;
import org.testng.annotations.Test;
import java.io.IOException;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
/**
* @author kimchy (shay.banon)
*/
public class CompressedStringTests {
@Test public void simpleTests() throws IOException {
String str = "this is a simple string";
CompressedString cstr = new CompressedString(str);
assertThat(cstr.string(), equalTo(str));
assertThat(new CompressedString(str), equalTo(cstr));
}
}