Skip to content

Commit

Permalink
explicitly add name to the index
Browse files Browse the repository at this point in the history
  • Loading branch information
ankush-jain-akto authored and notshivansh committed Nov 18, 2023
1 parent 8dd2dbd commit a036725
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 31 deletions.
25 changes: 25 additions & 0 deletions libs/dao/src/main/java/com/akto/dao/MCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,29 @@ public static boolean createIndexIfAbsent(String dbName, String collName, Bson i

}

public static boolean createIndexIfAbsent(String dbName, String collName, String[] fieldNames, boolean isAscending) {

Bson indexInfo = isAscending ? Indexes.ascending(fieldNames) : Indexes.descending(fieldNames);

String name = "";

int lenPerField = 30/fieldNames.length - 1;

for (String field: fieldNames) {

String[] tokens = field.split("\\.");
String lastToken = tokens[tokens.length-1];
lastToken = lastToken.substring(0, Math.min(lenPerField, lastToken.length()));
if (!name.isEmpty()) {
name += "-";
}
name += lastToken;
}

name += ("_");
name += (isAscending ? "1" : "-1");

return createIndexIfAbsent(dbName, collName, indexInfo, new IndexOptions().name(name));
}

}
37 changes: 29 additions & 8 deletions libs/dao/src/main/java/com/akto/dao/SampleDataDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.mongodb.client.model.Indexes;
import com.mongodb.client.model.Projections;
import com.mongodb.client.model.Sorts;

import org.bson.Document;
import org.bson.conversions.Bson;

import java.util.ArrayList;
Expand All @@ -32,16 +34,35 @@ public Class<SampleData> getClassT() {

public void createIndicesIfAbsent() {

String dbName = Context.accountId.get() + "";
createCollectionIfAbsent(dbName, getCollName(), new CreateCollectionOptions());
boolean exists = false;
for (String col: clients[0].getDatabase(Context.accountId.get()+"").listCollectionNames()){
if (getCollName().equalsIgnoreCase(col)){
exists = true;
break;
}
};

if (!exists) {
clients[0].getDatabase(Context.accountId.get()+"").createCollection(getCollName());
}

List<Bson> indices = new ArrayList<>(
Arrays.asList(
Indexes.ascending(new String[] { ApiInfo.ID_API_COLLECTION_ID, ApiInfo.ID_URL, ApiInfo.ID_METHOD }),
Indexes.ascending(new String[] { ApiInfo.ID_API_COLLECTION_ID }),
Indexes.ascending(new String[] { SingleTypeInfo._COLLECTION_IDS, ApiInfo.ID_URL, ApiInfo.ID_METHOD })));
MongoCursor<Document> cursor = instance.getMCollection().listIndexes().cursor();
int counter = 0;
while (cursor.hasNext()) {
counter++;
cursor.next();
}

if (counter == 1) {
String[] fieldNames = {"_id.apiCollectionId", "_id.url", "_id.method"};
MCollection.createIndexIfAbsent(getDBName(), getCollName(), fieldNames, true);
counter++;
}

if (counter == 2) {
instance.getMCollection().createIndex(Indexes.ascending("_id.apiCollectionId"));
}

createIndices(indices);
}

public List<SampleData> fetchSampleDataPaginated(int apiCollectionId, String lastFetchedUrl,
Expand Down
30 changes: 22 additions & 8 deletions libs/dao/src/main/java/com/akto/dao/SensitiveSampleDataDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.akto.dto.ApiInfo;
import com.akto.dto.SensitiveSampleData;
import com.akto.dto.type.SingleTypeInfo;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.CreateCollectionOptions;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Indexes;
Expand All @@ -12,6 +13,7 @@
import java.util.Arrays;
import java.util.List;

import org.bson.Document;
import org.bson.conversions.Bson;

public class SensitiveSampleDataDao extends AccountsContextDao<SensitiveSampleData>{
Expand Down Expand Up @@ -40,16 +42,28 @@ public static Bson getFilters(SingleTypeInfo singleTypeInfo) {
}

public void createIndicesIfAbsent() {
boolean exists = false;
for (String col: clients[0].getDatabase(Context.accountId.get()+"").listCollectionNames()){
if (getCollName().equalsIgnoreCase(col)){
exists = true;
break;
}
};

String dbName = Context.accountId.get() + "";
createCollectionIfAbsent(dbName, getCollName(), new CreateCollectionOptions());
if (!exists) {
clients[0].getDatabase(Context.accountId.get()+"").createCollection(getCollName());
}

List<Bson> indices = new ArrayList<>(Arrays.asList(
Indexes.ascending(new String[] { ApiInfo.ID_URL, ApiInfo.ID_API_COLLECTION_ID, ApiInfo.ID_METHOD }),
Indexes.ascending(new String[] { ApiInfo.ID_URL, SingleTypeInfo._COLLECTION_IDS, ApiInfo.ID_METHOD }),
Indexes.ascending(new String[] { SingleTypeInfo._COLLECTION_IDS })
));
MongoCursor<Document> cursor = instance.getMCollection().listIndexes().cursor();
int counter = 0;
while (cursor.hasNext()) {
counter++;
cursor.next();
}

createIndices(indices);
if (counter == 1) {
String[] fieldNames = {"_id.url", "_id.apiCollectionId", "_id.method"};
MCollection.createIndexIfAbsent(getDBName(), getCollName(), fieldNames, true);
}
}
}
63 changes: 49 additions & 14 deletions libs/dao/src/main/java/com/akto/dao/SingleTypeInfoDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,55 @@ public Class<SingleTypeInfo> getClassT() {

public void createIndicesIfAbsent() {

String dbName = Context.accountId.get() + "";
createCollectionIfAbsent(dbName, getCollName(), new CreateCollectionOptions());

List<Bson> indices = new ArrayList<>(Arrays.asList(
Indexes.ascending(new String[] { SingleTypeInfo._URL, SingleTypeInfo._METHOD, SingleTypeInfo._RESPONSE_CODE,SingleTypeInfo._IS_HEADER, SingleTypeInfo._PARAM, SingleTypeInfo.SUB_TYPE, SingleTypeInfo._API_COLLECTION_ID }),
Indexes.ascending(new String[] { SingleTypeInfo._API_COLLECTION_ID }),
Indexes.ascending(new String[] { SingleTypeInfo._PARAM, SingleTypeInfo._API_COLLECTION_ID }),
Indexes.ascending(new String[] { SingleTypeInfo._RESPONSE_CODE, SingleTypeInfo._IS_HEADER, SingleTypeInfo._PARAM, SingleTypeInfo.SUB_TYPE, SingleTypeInfo._API_COLLECTION_ID }),
Indexes.ascending(new String[] { SingleTypeInfo.SUB_TYPE, SingleTypeInfo._RESPONSE_CODE }),
Indexes.ascending(new String[] { SingleTypeInfo._RESPONSE_CODE, SingleTypeInfo.SUB_TYPE, SingleTypeInfo._TIMESTAMP }),
Indexes.ascending(new String[] { SingleTypeInfo._COLLECTION_IDS })
));

createIndices(indices);
boolean exists = false;
for (String col: clients[0].getDatabase(Context.accountId.get()+"").listCollectionNames()){
if (getCollName().equalsIgnoreCase(col)){
exists = true;
break;
}
};

if (!exists) {
clients[0].getDatabase(Context.accountId.get()+"").createCollection(getCollName());
}

MongoCursor<Document> cursor = instance.getMCollection().listIndexes().cursor();
int counter = 0;
while (cursor.hasNext()) {
counter++;
cursor.next();
}

if (counter == 1) {
String[] fieldNames = {"url", "method", "responseCode", "isHeader", "param", "subType", "apiCollectionId"};
MCollection.createIndexIfAbsent(getDBName(), getCollName(), fieldNames, true);
}

if (counter == 2) {
SingleTypeInfoDao.instance.getMCollection().createIndex(Indexes.ascending(new String[]{"apiCollectionId"}));
counter++;
}

if (counter == 3) {
SingleTypeInfoDao.instance.getMCollection().createIndex(Indexes.ascending(new String[]{"param", "apiCollectionId"}));
counter++;
}

if (counter == 4) {
String[] fieldNames = new String[]{SingleTypeInfo._RESPONSE_CODE, SingleTypeInfo._IS_HEADER, SingleTypeInfo._PARAM, SingleTypeInfo.SUB_TYPE, SingleTypeInfo._API_COLLECTION_ID};
MCollection.createIndexIfAbsent(getDBName(), getCollName(), fieldNames, true);
counter++;
}

if (counter == 5) {
SingleTypeInfoDao.instance.getMCollection().createIndex(Indexes.ascending(new String[]{SingleTypeInfo.SUB_TYPE, SingleTypeInfo._RESPONSE_CODE}));
counter++;
}

if (counter == 6) {
String[] fieldNames = {"responseCode", "subType", "timestamp",};
SingleTypeInfoDao.instance.getMCollection().createIndex(Indexes.ascending(fieldNames));
}
}

public static Bson filterForHostHeader(int apiCollectionId, boolean useApiCollectionId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.akto.dao.traffic_metrics;

import com.akto.dao.AccountsContextDao;
import com.akto.dao.MCollection;
import com.akto.dao.SingleTypeInfoDao;
import com.akto.dao.context.Context;
import com.akto.dto.traffic_metrics.TrafficMetrics;
Expand Down Expand Up @@ -70,7 +71,7 @@ public void createIndicesIfAbsent() {
ID+ TrafficMetrics.Key.HOST,
ID+ TrafficMetrics.Key.VXLAN_ID,
};
instance.getMCollection().createIndex(Indexes.ascending(fieldNames));
MCollection.createIndexIfAbsent(getDBName(), getCollName(), fieldNames, true);
}


Expand Down

0 comments on commit a036725

Please sign in to comment.