Skip to content

Commit d3f8f20

Browse files
author
6jkruege
committed
added reverse decoding to LIbae
1 parent ba40847 commit d3f8f20

File tree

3 files changed

+99
-5
lines changed

3 files changed

+99
-5
lines changed

src/jokrey/utilities/encoder/as_union/li/LIPosition.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import jokrey.utilities.encoder.as_union.Position;
44
import jokrey.utilities.transparent_storage.TransparentStorage;
55

6+
import java.util.Objects;
7+
68
/**
79
* @author jokrey
810
*/
@@ -15,7 +17,23 @@ public LIPosition(long pointer) {
1517
return pointer < storage.contentSize();
1618
}
1719

18-
@Override public String toString() {
19-
return "LIPosition{pointer=" + pointer + '}';
20+
@Override
21+
public boolean equals(Object o) {
22+
if (this == o) return true;
23+
if (o == null || getClass() != o.getClass()) return false;
24+
LIPosition that = (LIPosition) o;
25+
return pointer == that.pointer;
26+
}
27+
28+
@Override
29+
public int hashCode() {
30+
return Objects.hash(pointer);
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return "LIPosition{" +
36+
"pointer=" + pointer +
37+
'}';
2038
}
2139
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package jokrey.utilities.encoder.as_union.li;
2+
3+
import jokrey.utilities.encoder.as_union.Position;
4+
import jokrey.utilities.transparent_storage.TransparentStorage;
5+
6+
import java.util.Objects;
7+
8+
/**
9+
* @author jokrey
10+
*/
11+
public class ReverseLIPosition extends Position {
12+
public final long minimum;
13+
public long pointer;
14+
public ReverseLIPosition(long pointer, long minimum) {
15+
this.pointer = pointer;
16+
this.minimum = minimum;
17+
}
18+
@Override public boolean hasNext(TransparentStorage storage) {
19+
return pointer > minimum;
20+
}
21+
22+
@Override
23+
public boolean equals(Object o) {
24+
if (this == o) return true;
25+
if (o == null || getClass() != o.getClass()) return false;
26+
ReverseLIPosition that = (ReverseLIPosition) o;
27+
return minimum == that.minimum && pointer == that.pointer;
28+
}
29+
30+
@Override
31+
public int hashCode() {
32+
return Objects.hash(minimum, pointer);
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return "ReverseLIPosition{" +
38+
"minimum=" + minimum +
39+
", pointer=" + pointer +
40+
'}';
41+
}
42+
}

src/jokrey/utilities/encoder/as_union/li/bytes/LIbae.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import jokrey.utilities.encoder.EncodableAsBytes;
55
import jokrey.utilities.encoder.as_union.li.LIPosition;
66
import jokrey.utilities.encoder.as_union.li.LIe;
7+
import jokrey.utilities.encoder.as_union.li.ReverseLIPosition;
78
import jokrey.utilities.transparent_storage.StorageSystemException;
89
import jokrey.utilities.transparent_storage.TransparentStorage;
910
import jokrey.utilities.transparent_storage.bytes.TransparentBytesStorage;
@@ -263,20 +264,29 @@ public static long getIntFromByteArray(byte[] bytearr, int from, int len) {
263264
// }
264265
// }
265266

267+
protected long[] get_next_reverse_li_bounds(ReverseLIPosition start_pos, TransparentStorage<byte[]> contentBuilder) throws StorageSystemException {
268+
long i = start_pos.pointer;
269+
if(i-1 <= 0)
270+
return null;
271+
byte[] cache = contentBuilder.sub(i-9, i); //cache maximum number of required bytes. (to minimize possibly slow sub calls)
272+
273+
return get_next_reverse_li_bounds(cache, 0, i, start_pos.minimum);
274+
}
275+
266276
/**
267277
* @param partialData all or partial data
268278
* @param partialDataOffset offset in the partial data
269279
* @param li_offset latest index in the data
270-
* @param totalDataSize last index of total data(+1) of which partial data is a part of
280+
* @param minimum first index of total data of which partial data is a part of (usually 0, or header-offset)
271281
* @return Length indicated indices. The range between those indices is the encoded, connected data.
272282
*/
273-
public static long[] get_next_reverse_li_bounds(byte[] partialData, int partialDataOffset, long li_offset, long totalDataSize) {
283+
public static long[] get_next_reverse_li_bounds(byte[] partialData, int partialDataOffset, long li_offset, long minimum) {
274284
if(partialDataOffset < 0) return null;
275285

276286
byte leading_li = partialData[partialDataOffset];
277287

278288
long lengthIndicator_asInt = getIntFromByteArray(partialData, partialDataOffset-leading_li, leading_li);
279-
if(lengthIndicator_asInt==-1 || li_offset+lengthIndicator_asInt>totalDataSize)
289+
if(lengthIndicator_asInt==-1 || li_offset-lengthIndicator_asInt < minimum)
280290
return null;
281291
li_offset-=leading_li + 1; //to skip the li information.
282292
return new long[]{li_offset - lengthIndicator_asInt, li_offset};
@@ -296,4 +306,28 @@ public static byte[] generateReverseLI(long length) {
296306
li_bytes[n] = BitHelper.getByte(length, (li_bytes.length-2)-n);
297307
return li_bytes;
298308
}
309+
310+
public long reverseSkipEntry(ReverseLIPosition pos) {
311+
long[] startIndex_endIndex_ofNextLI = get_next_reverse_li_bounds(pos, getStorageSystem());
312+
if(startIndex_endIndex_ofNextLI != null) {
313+
pos.pointer = startIndex_endIndex_ofNextLI[1];
314+
return startIndex_endIndex_ofNextLI[1] - startIndex_endIndex_ofNextLI[0];
315+
}
316+
return -1;
317+
}
318+
319+
public byte[] reverseDecode(ReverseLIPosition pos, boolean delete_decoded) {
320+
long[] startIndex_endIndex_ofNextLI = get_next_reverse_li_bounds(pos, getStorageSystem());
321+
if(startIndex_endIndex_ofNextLI != null) {
322+
byte[] decoded = getStorageSystem().sub(startIndex_endIndex_ofNextLI[0], startIndex_endIndex_ofNextLI[1]);
323+
if(delete_decoded) {
324+
getStorageSystem().delete(startIndex_endIndex_ofNextLI[0], pos.pointer);
325+
} else {
326+
pos.pointer = startIndex_endIndex_ofNextLI[0];
327+
}
328+
return decoded;
329+
} else {
330+
return null;
331+
}
332+
}
299333
}

0 commit comments

Comments
 (0)