Skip to content

ConcurrentHashMap with indexing technology that improve performance.

License

Notifications You must be signed in to change notification settings

keich/IndexedHashMap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About The Project

ConcurrentHashMap with indexing technology that improve performance. Used as an in-memory key-value database for Objects.

Getting Started

  1. Clone the repository
   git clone https://github.com/keich/IndexedHashMap.git
  1. Install NPM packages
   npm install
  1. Add dependency to your project
  	<dependency>
  		<groupId>ru.keich.mon</groupId>
  		<artifactId>IndexedHashMap</artifactId>
  		<version>0.2.0</version>
  	</dependency>

Usage

  1. 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());
	    } 
	}
  1. Create instance of IndexedHashMap
IndexedHashMap<String, TestEntity> store = new IndexedHashMap<>(null,"DBName");
  1. Put and get data
		TestEntity entity = new TestEntity("Hello word", 1L);
		store.put("key1", entity);
		TestEntity result = store.get("key1");
  1. 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;
		});

Work with indexes

  1. 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());
}       
  1. Do before insert data
store.addIndexEqual(TestEntity.FIELD_NAME, TestEntity::getNameForIndex);
  1. 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));
});

Index data type support

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 types

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)

Work without indexes

  1. Do before insert data
store.addQueryField(TestEntity.FIELD_NAME, TestEntity::getSomeSetForIndex);
  1. 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));
});

About

ConcurrentHashMap with indexing technology that improve performance.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages