-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/datastore-jpa
- Loading branch information
Showing
37 changed files
with
2,047 additions
and
648 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
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
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
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
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
75 changes: 75 additions & 0 deletions
75
elide-core/src/main/java/com/yahoo/elide/core/datastore/inmemory/HashMapDataStore.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,75 @@ | ||
/* | ||
* Copyright 2017, Yahoo Inc. | ||
* Licensed under the Apache License, Version 2.0 | ||
* See LICENSE file in project root for terms. | ||
*/ | ||
package com.yahoo.elide.core.datastore.inmemory; | ||
|
||
import com.yahoo.elide.core.DataStore; | ||
import com.yahoo.elide.core.DataStoreTransaction; | ||
import com.yahoo.elide.core.EntityDictionary; | ||
|
||
import org.reflections.Reflections; | ||
import org.reflections.scanners.SubTypesScanner; | ||
import org.reflections.scanners.TypeAnnotationsScanner; | ||
import org.reflections.util.ClasspathHelper; | ||
import org.reflections.util.ConfigurationBuilder; | ||
|
||
import lombok.Getter; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
import javax.persistence.Entity; | ||
|
||
/** | ||
* Simple in-memory only database. | ||
*/ | ||
public class HashMapDataStore implements DataStore { | ||
private final Map<Class<?>, Map<String, Object>> dataStore = Collections.synchronizedMap(new HashMap<>()); | ||
@Getter private EntityDictionary dictionary; | ||
@Getter private final Package beanPackage; | ||
@Getter private final ConcurrentHashMap<Class<?>, AtomicLong> typeIds = new ConcurrentHashMap<>(); | ||
|
||
public HashMapDataStore(Package beanPackage) { | ||
this.beanPackage = beanPackage; | ||
} | ||
|
||
@Override | ||
public void populateEntityDictionary(EntityDictionary dictionary) { | ||
Reflections reflections = new Reflections(new ConfigurationBuilder() | ||
.addUrls(ClasspathHelper.forPackage(beanPackage.getName())) | ||
.setScanners(new SubTypesScanner(), new TypeAnnotationsScanner())); | ||
reflections.getTypesAnnotatedWith(Entity.class).stream() | ||
.filter(entityAnnotatedClass -> entityAnnotatedClass.getPackage().getName() | ||
.startsWith(beanPackage.getName())) | ||
.forEach((cls) -> { | ||
dictionary.bindEntity(cls); | ||
dataStore.put(cls, Collections.synchronizedMap(new LinkedHashMap<>())); | ||
}); | ||
this.dictionary = dictionary; | ||
} | ||
|
||
@Override | ||
public DataStoreTransaction beginTransaction() { | ||
return new HashMapStoreTransaction(dataStore, dictionary, typeIds); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("Data store contents "); | ||
for (Class<?> cls : dataStore.keySet()) { | ||
sb.append("\n Table ").append(cls).append(" contents \n"); | ||
Map<String, Object> data = dataStore.get(cls); | ||
for (Map.Entry<String, Object> e : data.entrySet()) { | ||
sb.append(" Id: ").append(e.getKey()).append(" Value: ").append(e.getValue()); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
} |
Oops, something went wrong.