forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable ordinal compression for source field
- Loading branch information
Showing
2 changed files
with
135 additions
and
5 deletions.
There are no files selected for viewing
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
81 changes: 81 additions & 0 deletions
81
server/src/main/java/org/opensearch/index/mapper/Trie.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.index.mapper; | ||
|
||
import java.util.HashMap; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
public class Trie { | ||
|
||
private Node root = new Node(); | ||
public Trie() { | ||
} | ||
|
||
public void put(String key, byte val) { | ||
Node current = root; | ||
for (int i = 0; i < key.length(); i ++) { | ||
byte bx = (byte) key.charAt(i); | ||
int b = bx + 128; | ||
if (current.children[b] == null) { | ||
current.children[b] = new Node(); | ||
} | ||
current = current.children[b]; | ||
} | ||
current.val = val; | ||
current.len = key.length(); | ||
} | ||
|
||
private static class Node { | ||
byte key; | ||
private Node[] children = new Node[255]; | ||
|
||
byte val = -1; | ||
int len = -1; | ||
} | ||
|
||
public static class Matcher { | ||
|
||
Node root; | ||
Node current; | ||
|
||
Node parent = null; | ||
|
||
public Matcher(Trie trie) { | ||
root = current = trie.root; | ||
} | ||
|
||
public Byte match(int b) { | ||
b = b + 128; | ||
Node next = current.children[b]; | ||
if (next == null) { | ||
current = null; | ||
return null; | ||
} | ||
parent = current; | ||
current = next; | ||
return next.val; | ||
} | ||
|
||
public void reset() { | ||
current = root; | ||
} | ||
|
||
public boolean inTraversal() { | ||
return current != root; | ||
} | ||
|
||
public int currentLength() { | ||
return current.len; | ||
} | ||
|
||
public boolean isFirstCharacter() { | ||
return parent == root; | ||
} | ||
} | ||
} |