use of gnu.trove.map.hash.TLongIntHashMap in project overflowdb by ShiftLeftSecurity.
the class NodesList method compact.
/**
* trims down internal collections to just about the necessary size, in order to allow the remainder to be
* garbage collected
*/
synchronized void compact() {
final ArrayList<Node> newNodes = new ArrayList<>(size);
Iterator<Node> iter = iterator();
while (iter.hasNext()) {
newNodes.add(iter.next());
}
nodes = newNodes.toArray(new Node[size]);
// reindex helper collections
emptySlots.clear();
nodeIndexByNodeId = new TLongIntHashMap(this.nodes.length);
int idx = 0;
while (idx < this.nodes.length) {
Node node = this.nodes[idx];
nodeIndexByNodeId.put(node.id(), idx);
idx++;
}
}
use of gnu.trove.map.hash.TLongIntHashMap in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onMessageBulkDelete.
public void onMessageBulkDelete(MessageBulkDeleteEvent event) {
List<String> messageIds = event.getMessageIds();
TextChannel textChannel = event.getChannel();
Guild guild = event.getGuild();
LoggerEvent loggerEvent = LoggerEvent.MESSAGE_DELETE;
this.bot.getMongo().aggregateLoggers(this.getPipeline(guild.getIdLong())).whenComplete((documents, exception) -> {
if (ExceptionUtility.sendErrorMessage(exception)) {
return;
}
if (documents.isEmpty()) {
return;
}
Document data = documents.get(0);
if (guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
this.retrieveAuditLogsDelayed(guild, ActionType.MESSAGE_BULK_DELETE).whenComplete((logs, auditException) -> {
this.messageCache.putIfAbsent(guild.getIdLong(), new TLongIntHashMap());
TLongIntMap guildCache = this.messageCache.get(guild.getIdLong());
AuditLogEntry entry = logs == null ? null : logs.stream().filter(e -> Duration.between(e.getTimeCreated(), ZonedDateTime.now(ZoneOffset.UTC)).toMinutes() <= 5).filter(e -> {
int count = Integer.parseInt(e.getOptionByName("count"));
int oldCount = guildCache.get(e.getIdLong());
guildCache.put(e.getIdLong(), count);
return (count == messageIds.size() && count != oldCount) || count > oldCount;
}).findFirst().orElse(null);
this.handleBulkMessages(textChannel, messageIds, data.getList("loggers", Document.class), loggerEvent, entry == null ? null : entry.getUser());
});
return;
}
this.handleBulkMessages(textChannel, messageIds, data.getList("loggers", Document.class), loggerEvent, null);
});
}
use of gnu.trove.map.hash.TLongIntHashMap in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onGuildVoiceLeave.
public void onGuildVoiceLeave(GuildVoiceLeaveEvent event) {
Guild guild = event.getGuild();
Member member = event.getMember();
User user = member.getUser();
VoiceChannel channel = event.getChannelLeft();
LoggerContext loggerContext = new LoggerContext().setChannel(channel).setUser(user);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setDescription(String.format("`%s` just left the voice channel %s", member.getEffectiveName(), channel.getAsMention()));
embed.setColor(this.bot.getConfig().getRed());
embed.setTimestamp(Instant.now());
embed.setAuthor(new EmbedAuthor(user.getAsTag(), user.getEffectiveAvatarUrl(), null));
embed.setFooter(new EmbedFooter(String.format("User ID: %s", user.getId()), null));
this.bot.getMongo().aggregateLoggers(this.getPipeline(guild.getIdLong())).whenComplete((documents, exception) -> {
if (ExceptionUtility.sendErrorMessage(exception)) {
return;
}
if (documents.isEmpty()) {
return;
}
Document data = documents.get(0);
List<Document> loggers = data.getList("loggers", Document.class);
if (loggers.isEmpty()) {
return;
}
if (guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
this.retrieveAuditLogsDelayed(event.getGuild(), ActionType.MEMBER_VOICE_KICK).whenComplete((logs, auditException) -> {
this.disconnectCache.putIfAbsent(guild.getIdLong(), new TLongIntHashMap());
TLongIntMap guildCache = this.disconnectCache.get(guild.getIdLong());
AuditLogEntry entry = logs == null ? null : logs.stream().filter(e -> Duration.between(e.getTimeCreated(), ZonedDateTime.now(ZoneOffset.UTC)).toMinutes() <= 10).filter(e -> {
int count = Integer.parseInt(e.getOptionByName("count"));
int oldCount = guildCache.get(e.getIdLong());
guildCache.put(e.getIdLong(), count);
return (count == 1 && count != oldCount) || count > oldCount;
}).findFirst().orElse(null);
User moderator = entry == null ? null : entry.getUser();
LoggerEvent loggerEvent = moderator == null ? LoggerEvent.MEMBER_VOICE_LEAVE : LoggerEvent.MEMBER_VOICE_DISCONNECT;
if (moderator != null) {
loggerContext.setModerator(moderator);
embed.setDescription(String.format("`%s` was disconnected from the voice channel %s by **%s**", member.getEffectiveName(), channel.getAsMention(), moderator.getAsTag()));
}
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
} else {
LoggerEvent loggerEvent = LoggerEvent.MEMBER_VOICE_LEAVE;
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
}
});
}
use of gnu.trove.map.hash.TLongIntHashMap in project olca-modules by GreenDelta.
the class FlowTable method directionsOf.
/**
* Try to determine the (impact) directions of the given flows. Returns a map
* that associates an integer value $v$ to each flow ID. A value $v < 0$ means
* that the corresponding flow is an input flow and a value $v > 0$ that it is
* an output flow. The larger $|v|$ is the more certain is this classification.
* A value of $v = 0$ means that the flow direction cannot be determined from
* the information in the database. Also, product and waste flows will always
* have a value of $v = 0$ as these flows can always occur on the input or
* output side of processes.
*/
public static TLongIntHashMap directionsOf(IDatabase db, Iterable<FlowDescriptor> flows) {
// initialize the map
var map = new TLongIntHashMap();
if (db == null || flows == null)
return map;
for (var flow : flows) {
if (flow.flowType == FlowType.ELEMENTARY_FLOW) {
map.put(flow.id, 0);
}
}
// try to get the flow directions from the exchanges
var sql = "select f_flow, is_input from tbl_exchanges";
NativeSql.on(db).query(sql, r -> {
var id = r.getLong(1);
if (!map.containsKey(id))
return true;
var isInput = r.getBoolean(2);
var current = map.get(id);
map.put(id, isInput ? current - 1 : current + 1);
return true;
});
// check if there are v = 0 cases
var withZero = new TLongHashSet();
for (var mapIt = map.iterator(); mapIt.hasNext(); ) {
mapIt.advance();
if (mapIt.value() == 0) {
withZero.add(mapIt.key());
}
}
if (withZero.isEmpty())
return map;
// try to determine it from the category path
var categories = Categories.pathsOf(db);
for (var flow : flows) {
if (!withZero.contains(flow.id) || flow.category == null)
continue;
var path = categories.pathOf(flow.category);
if (path == null)
continue;
path = path.toLowerCase();
if (path.contains("resource")) {
withZero.remove(flow.id);
map.put(flow.id, -1);
} else if (path.contains("emission")) {
withZero.remove(flow.id);
map.put(flow.id, +1);
}
}
if (withZero.isEmpty())
return map;
// try to determine it from other flows that are
// used in the same impact categories
var zeroToImpacts = new TLongObjectHashMap<TLongHashSet>();
var impactTotals = new TLongIntHashMap();
sql = "select f_impact_category, f_flow from tbl_impact_factors";
NativeSql.on(db).query(sql, r -> {
var impact = r.getLong(1);
var flow = r.getLong(2);
// add zero-flow impact link
if (withZero.contains(flow)) {
var flowImpacts = zeroToImpacts.get(flow);
if (flowImpacts == null) {
flowImpacts = new TLongHashSet();
zeroToImpacts.put(flow, flowImpacts);
}
flowImpacts.add(impact);
return true;
}
// add flow direction to impact direction
var flowVal = map.get(flow);
if (flowVal != 0) {
var total = impactTotals.get(impact);
impactTotals.put(impact, total + flowVal);
}
return true;
});
for (var it = withZero.iterator(); it.hasNext(); ) {
var zeroID = it.next();
var impacts = zeroToImpacts.get(zeroID);
if (impacts == null)
continue;
var val = 0;
for (var impactIt = impacts.iterator(); impactIt.hasNext(); ) {
val += impactTotals.get(impactIt.next());
}
if (val != 0) {
map.put(zeroID, val < 0 ? -1 : 1);
}
}
return map;
}
use of gnu.trove.map.hash.TLongIntHashMap in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onGuildMessageDelete.
public void onGuildMessageDelete(GuildMessageDeleteEvent event) {
TextChannel channel = event.getChannel();
Guild guild = event.getGuild();
LoggerEvent loggerEvent = LoggerEvent.MESSAGE_DELETE;
LoggerContext loggerContext = new LoggerContext().setChannel(channel);
GuildMessage message = this.bot.getMessageCache().getMessageById(event.getMessageIdLong());
if (message != null) {
loggerContext.setUser(message.getAuthor());
}
this.bot.getMongo().aggregateLoggers(this.getPipeline(guild.getIdLong())).whenComplete((documents, exception) -> {
if (ExceptionUtility.sendErrorMessage(exception)) {
return;
}
if (documents.isEmpty()) {
return;
}
Document data = documents.get(0);
List<Document> loggers = LoggerUtility.getValidLoggers(data.getList("loggers", Document.class), loggerEvent, loggerContext);
if (loggers.isEmpty()) {
return;
}
WebhookEmbedBuilder embed = new WebhookEmbedBuilder().setColor(this.bot.getConfig().getRed()).setTimestamp(Instant.now()).setFooter(new EmbedFooter("Message ID: " + event.getMessageId(), null));
StringBuilder description = new StringBuilder();
if (message == null) {
description.append(String.format("A message sent in %s was deleted", channel.getAsMention()));
embed.setAuthor(new EmbedAuthor(guild.getName(), guild.getIconUrl(), null));
} else {
User author = message.getAuthor();
description.append(String.format("The message sent by `%s` in %s was deleted", author.getAsTag(), channel.getAsMention()));
embed.setAuthor(new EmbedAuthor(author.getAsTag(), author.getEffectiveAvatarUrl(), null));
String content = message.getContent();
if (!content.isBlank()) {
embed.addField(new EmbedField(false, "Message", StringUtility.limit(content, MessageEmbed.VALUE_MAX_LENGTH, "...")));
}
}
if (guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
this.retrieveAuditLogsDelayed(guild, ActionType.MESSAGE_DELETE).whenComplete((logs, auditException) -> {
this.messageCache.putIfAbsent(guild.getIdLong(), new TLongIntHashMap());
TLongIntMap guildCache = this.messageCache.get(guild.getIdLong());
AuditLogEntry entry = logs == null ? null : logs.stream().filter(e -> Duration.between(e.getTimeCreated(), ZonedDateTime.now(ZoneOffset.UTC)).toMinutes() <= 5).filter(e -> Long.parseLong(e.getOptionByName("channel_id")) == channel.getIdLong()).filter(e -> {
int count = Integer.parseInt(e.getOptionByName("count"));
int oldCount = guildCache.get(e.getIdLong());
guildCache.put(e.getIdLong(), count);
return (count == 1 && count != oldCount) || count > oldCount;
}).findFirst().orElse(null);
User moderator = entry == null ? null : entry.getUser();
if (moderator != null) {
loggerContext.setModerator(moderator);
description.append(" by **").append(moderator.getAsTag()).append("**");
}
embed.setDescription(description.toString());
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
return;
}
embed.setDescription(description.toString());
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
}
Aggregations