ConcurrentHashMap with indexing technology that improve performance. Used as an in-memory key-value database for Objects.
- Clone the repository
git clone https://github.com/keich/IndexedHashMap.git- Install NPM packages
npm install- Add dependency to your project
<dependency>
<groupId>ru.keich.mon</groupId>
<artifactId>IndexedHashMap</artifactId>
<version>0.2.0</version>
</dependency>- Create class extended BaseEntity
public static class TestEntity {
private final String name;
private final Long version;
public TestEntity(String name, Long version) {
this.name = name;
this.version = version;
}
public static final String FIELD_NAME = "name";
public String getName() {
return name;
}
public static Set<Object> getNameForIndex(TestEntity e) {
return Collections.singleton(e.getName());
}
}- Create instance of IndexedHashMap
IndexedHashMap<String, TestEntity> store = new IndexedHashMap<>(null,"DBName");- Put and get data
TestEntity entity = new TestEntity("Hello word", 1L);
store.put("key1", entity);
TestEntity result = store.get("key1");- Lock object for insert or update
store.compute("key1", (k, obj) -> {
// Object with key1 missing
if(obj == null) {
//Insert
return new TestEntity("Hello word", 1L);
}
// Object exists
// Replace
return new TestEntity("Hello word 2", 1L);
// Or remove
//return null;
});- Determine what data to put in the index
public static final String FIELD_NAME = "name";
public static Set<Object> getNameForIndex(TestEntity e) {
return Collections.singleton(e.getName());
} - Do before insert data
store.addIndexEqual(TestEntity.FIELD_NAME, TestEntity::getNameForIndex);- Query objects from store
QueryPredicate repdicate = QueryPredicate.notEqual(TestEntity.FIELD_NAME, "Hello world");
Set<String> result = store.keySet(repdicate);
result.forEach(key -> {
System.out.println(store.get(key));
});Any object with hash and equal or any object implements Comparable.(depends on predicate)
| Operator | Description | Long | Class String | Class Map.Entry | Class Set |
|---|---|---|---|---|---|
| EQ | Equal | ok | ok | The key is equal and the value is equal | A object in set is equal the specified object |
| NE | Not equal | ok | ok | Undefined behavior | Undefined behavior. Use NI |
| LT | Less than | ok | ok | Exception | Exception |
| GT | Gather than | ok | ok | Exception | Exception |
| GE | Gather equal | ok | ok | Exception | Exception |
| CO | Contain(Like) | Undefined behavior | The string contains the specified string | The key is equal and the value contains the specified string | A string in the set contains the specified string |
| NC | Not contain(Not Like) | Undefined behavior | Vice versa CO | The key is equal and the value not contains the specified string | Undefined behavior |
| NI | Not include(uses for Set) | Undefined behavior | Vice versa CO | Undefined behavior | Return object with set not contains object |
| Index class | Description |
|---|---|
| IndexEqual | Index from HashMap |
| IndexLongUniq | Index from TreeMap for uniq Long values |
| IndexSorted | Index from TreeMap for a objects implements Comparable |
| IndexSortedUniq | Index from TreeMap for uniq a objects implements Comparable |
| IndexSmallInt | Index for Integer with small range(Uses array) |
- Do before insert data
store.addQueryField(TestEntity.FIELD_NAME, TestEntity::getSomeSetForIndex);- Query objects from store
QueryPredicate predicate = Predicates.notEqual(TestEntity.FIELD_NAME, "Hello world");
Set<String> result = store.keySet(predicate);
result.forEach(key -> {
System.out.println(store.get(key));
});