Skip to content

Commit

Permalink
genericify index to allow for connection data to be indexed, add trie…
Browse files Browse the repository at this point in the history
… index for partial search
  • Loading branch information
firestar committed Dec 7, 2023
1 parent 8089a99 commit e6549d9
Show file tree
Hide file tree
Showing 22 changed files with 504 additions and 647 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'com.nucleodb'
version = '1.12.3'
version = '1.13.0'

repositories {
mavenCentral()
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/com/nucleodb/library/NucleoDB.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.nucleodb.library;

import com.nucleodb.library.database.index.TreeIndex;
import com.nucleodb.library.database.modifications.Create;
import com.nucleodb.library.database.tables.annotation.Conn;
import com.nucleodb.library.database.tables.connection.ConnectionConfig;
import com.nucleodb.library.database.tables.connection.ConnectionHandler;
import com.nucleodb.library.database.tables.annotation.Index;
import com.nucleodb.library.database.index.annotation.Index;
import com.nucleodb.library.database.tables.annotation.Table;
import com.nucleodb.library.database.tables.table.DataTableConfig;
import com.nucleodb.library.database.utils.TreeSetExt;
import com.nucleodb.library.database.utils.exceptions.IncorrectDataEntryClassException;
import com.nucleodb.library.database.utils.exceptions.MissingDataEntryConstructorsException;
Expand Down Expand Up @@ -127,7 +129,7 @@ private void startTables(String bootstrap, String[] packagesToScan, DBType dbTyp
Set<Class<?>> tableTypes = tableTypesOptional.get();
CountDownLatch latch = new CountDownLatch(tableTypes.size());
Set<DataTableBuilder> tables = new TreeSetExt<>();
Map<String, Set<String>> indexes = new TreeMap<>();
Map<String, Set<DataTableConfig.IndexConfig>> indexes = new TreeMap<>();
for (Class<?> type : tableTypes) {
Table tableAnnotation = type.getAnnotation(Table.class);
String tableName = tableAnnotation.tableName();
Expand Down Expand Up @@ -181,14 +183,14 @@ public void run(DataTable table) {
}
}

private Set<String> processIndexListForClass(Class<?> clazz) {
Set<String> indexes = new TreeSet<>();
private Set<DataTableConfig.IndexConfig> processIndexListForClass(Class<?> clazz) {
Set<DataTableConfig.IndexConfig> indexes = new TreeSet<>();

getAllAnnotatedFields(clazz, Index.class, "").forEach(field->{
if (field.getAnnotation().value().isEmpty()) {
indexes.add(field.getPath());
indexes.add(new DataTableConfig.IndexConfig(field.getPath(), field.getAnnotation().type()) );
} else {
indexes.add(field.getAnnotation().value());
indexes.add(new DataTableConfig.IndexConfig(field.getAnnotation().value(), field.getAnnotation().type()));
}
});
return indexes;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nucleodb.library.database.tables.table.index;
package com.nucleodb.library.database.index;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.collect.Queues;
Expand All @@ -15,17 +15,21 @@
import java.util.Queue;
import java.util.Set;

public abstract class Index implements Serializable{
public abstract class IndexWrapper<T> implements Serializable{
private static final long serialVersionUID = 1;
String indexedKeyStr;
public Index(String indexedKey) {
public IndexWrapper(String indexedKey) {
this.indexedKeyStr = indexedKey;
}



public List<Object> getIndexValue(DataEntry dataEntry) throws JsonProcessingException {
return getValues(Queues.newConcurrentLinkedQueue(Arrays.asList(this.indexedKeyStr.split("\\."))), dataEntry.getData());
public List<Object> getIndexValue(T object) throws JsonProcessingException {
Object obj;
if(object instanceof DataEntry<?>){
obj = ((DataEntry<?>) object).getData();
}else{
obj = object;
}
return getValues(Queues.newConcurrentLinkedQueue(Arrays.asList(this.indexedKeyStr.split("\\."))), object);
/*String json = dataEntry.getReference().toString();
try (JsonReader reader = Json.createReader(new StringReader(json))) {
System.out.println(json);
Expand Down Expand Up @@ -86,28 +90,28 @@ public List<Object> getValues(Queue<String> pointer, Object start) throws JsonPr
}


public void add(DataEntry dataEntry) throws JsonProcessingException {
public void add(T dataEntry) throws JsonProcessingException {
System.out.println("Add ERROR");
}

public void delete(DataEntry dataEntry) {
public void delete(T dataEntry) {
System.out.println("Delete ERROR");
}

public void modify(DataEntry dataEntry) throws JsonProcessingException {
public void modify(T dataEntry) throws JsonProcessingException {
System.out.println("Modify ERROR");
}

public Set<DataEntry> get(Object search){
public Set<T> get(Object search){
return null;
}

public Set<DataEntry> getNotEqual(Object notEqualVal) {
public Set<T> getNotEqual(Object notEqualVal) {
return null;
}


public Set<DataEntry> search(Object searchObj) {
public Set<T> contains(Object searchObj) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.nucleodb.library.database.tables.table.index;
package com.nucleodb.library.database.index;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.nucleodb.library.database.tables.table.DataEntry;
import com.nucleodb.library.database.utils.TreeSetExt;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.TreeSet;

public class TreeIndex extends Index implements Serializable{
public class TreeIndex<T> extends IndexWrapper<T> implements Serializable{
private static final long serialVersionUID = 1;
public TreeIndex() {
super(null);
Expand All @@ -21,8 +21,8 @@ public TreeIndex() {


private boolean unique;
private Map<DataEntry, Set<Set<DataEntry>>> reverseMap = new TreeMap<>();
private Map<Object, Set<DataEntry>> index = new TreeMap<>();
private TreeMap<T, Set<Set<T>>> reverseMap = new TreeMap<>();
private TreeMap<Object, Set<T>> index = new TreeMap<>();


public TreeIndex(String indexedKey) {
Expand All @@ -33,11 +33,11 @@ public TreeIndex(String indexedKey) {


@Override
public void add(DataEntry dataEntry) throws JsonProcessingException {
public void add(T dataEntry) throws JsonProcessingException {
List<Object> values = getIndexValue(dataEntry);
//System.out.println(Serializer.getObjectMapper().getOm().writeValueAsString(values));
values.forEach(val->{
Set<DataEntry> entries;
Set<T> entries;
synchronized (index) {
entries = index.get(val);
if (entries == null) {
Expand All @@ -46,7 +46,7 @@ public void add(DataEntry dataEntry) throws JsonProcessingException {
}
}
entries.add(dataEntry);
Set<Set<DataEntry>> rMap;
Set<Set<T>> rMap;
synchronized (reverseMap) {
rMap = reverseMap.get(dataEntry);
if (rMap == null) {
Expand All @@ -61,24 +61,24 @@ public void add(DataEntry dataEntry) throws JsonProcessingException {
}

@Override
public void delete(DataEntry dataEntry) {
public void delete(T dataEntry) {
//System.out.println("Delete "+dataEntry);
Set<Set<DataEntry>> i = reverseMap.get(dataEntry);
Set<Set<T>> i = reverseMap.get(dataEntry);
if(i!=null)
i.forEach(c -> c.remove(dataEntry));
reverseMap.remove(dataEntry);
//System.out.println(reverseMap.get(dataEntry));
}

@Override
public void modify(DataEntry dataEntry) throws JsonProcessingException {
public void modify(T dataEntry) throws JsonProcessingException {
//System.out.println("Modify, "+ this.getIndexedKey() + " = " +dataEntry);
delete(dataEntry);
add(dataEntry);
}

@Override
public Set<DataEntry> get(Object search) {
public Set<T> get(Object search) {
Optional<Object> optionalO = index.keySet().stream().findFirst();
if(optionalO.isPresent()) {
Object o = optionalO.get();
Expand All @@ -102,7 +102,7 @@ public Set<DataEntry> get(Object search) {


@Override
public Set<DataEntry> search(Object searchObj) {
public Set<T> contains(Object searchObj) {
return index.keySet().stream().filter(key->{
if(key instanceof String && searchObj instanceof String){
return ((String) key).contains((String)searchObj);
Expand All @@ -114,20 +114,45 @@ public Set<DataEntry> search(Object searchObj) {
return a;
});
}
Set<T> reduce(SortedMap<Object, Set<T>> objectSetSortedMap){
return objectSetSortedMap.entrySet()
.stream()
.map(c->c.getValue()).reduce(new TreeSetExt<>(), (a,b)->{
a.addAll(b);
return a;
});
}

public Set<T> lessThan(Object searchObj) {
return reduce(index.headMap(searchObj));
}

public Set<T> lessThanEqualTo(Object searchObj) {
return reduce(index.headMap(searchObj, true));
}

public Set<T> greaterThan(Object searchObj) {
return reduce(index.tailMap(searchObj));
}

public Set<T> greaterThanEqual(Object searchObj) {
return reduce(index.tailMap(searchObj, true));
}


public Map<DataEntry, Set<Set<DataEntry>>> getReverseMap() {
public TreeMap<T, Set<Set<T>>> getReverseMap() {
return reverseMap;
}

public void setReverseMap(Map<DataEntry, Set<Set<DataEntry>>> reverseMap) {
public void setReverseMap(TreeMap<T, Set<Set<T>>> reverseMap) {
this.reverseMap = reverseMap;
}

public Map<Object, Set<DataEntry>> getIndex() {
public TreeMap<Object, Set<T>> getIndex() {
return index;
}

public void setIndex(Map<Object, Set<DataEntry>> index) {
public void setIndex(TreeMap<Object, Set<T>> index) {
this.index = index;
}

Expand Down
Loading

0 comments on commit e6549d9

Please sign in to comment.