Skip to content

Commit

Permalink
sonar and faster full-scan without distinct
Browse files Browse the repository at this point in the history
  • Loading branch information
usinesoft committed Dec 1, 2023
1 parent b9cf347 commit dc4ed43
Show file tree
Hide file tree
Showing 40 changed files with 303 additions and 194 deletions.
6 changes: 5 additions & 1 deletion Cachalot/Cachalot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Company>USINESOFT</Company>
<Description>Fastest transactional database for dotnet applications. It can also be used as a very powerful distributed cache</Description>
<Copyright>USINESOFT</Copyright>
<Version>2.5.1</Version>
<Version>2.5.3</Version>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down Expand Up @@ -42,6 +42,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="protobuf-net" Version="3.2.30" />
<PackageReference Include="Remotion.Linq" Version="2.2.0" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="Teronis.MSBuild.Packaging.ProjectBuildInPackage" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
31 changes: 16 additions & 15 deletions Cachalot/Linq/Connector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace Cachalot.Linq;

public class Connector : IDisposable
public sealed class Connector : IDisposable
{
private readonly Dictionary<string, CollectionSchema> _collectionSchema = new();

Expand Down Expand Up @@ -49,11 +49,11 @@ public Connector([NotNull] ClientConfig config)
Client = new DataClient { Channel = channel };

_server = new(new()
{
IsPersistent = config.IsPersistent,
DataPath = "."
})
{ Channel = channel };
{
IsPersistent = config.IsPersistent,
DataPath = "."
})
{ Channel = channel };

_server.Start();
}
Expand Down Expand Up @@ -191,12 +191,12 @@ public CollectionSchema GetCollectionSchema(string collectionName)

// try to get schema from server
var info = Client.GetClusterInformation();
schema = info.Schema.FirstOrDefault(x =>
schema = Array.Find(info.Schema, x =>
string.Equals(x.CollectionName, collectionName, StringComparison.CurrentCultureIgnoreCase));
if (schema == null) return null;

_collectionSchema[schema.CollectionName.ToUpper()] = schema;


return schema;
}
Expand Down Expand Up @@ -250,7 +250,7 @@ public void DropCollection(string collectionName = null)

lock (_collectionSchema)
{
var key = _collectionSchema.Keys.FirstOrDefault(k=>k.Equals(collectionName, StringComparison.InvariantCultureIgnoreCase));
var key = _collectionSchema.Keys.FirstOrDefault(k => k.Equals(collectionName, StringComparison.InvariantCultureIgnoreCase));
if (key != null)
{
_collectionSchema.Remove(key);
Expand Down Expand Up @@ -291,7 +291,7 @@ public int DeleteManyWithSQL(string sql)


var schema = GetCollectionSchema(tableName);

var query = parsed.ToQuery(schema);

// ignore take clause for delete
Expand Down Expand Up @@ -351,8 +351,9 @@ private IEnumerable<PackedObject> PackJson(IEnumerable<JObject> items, Collectio
processed++;
yield return PackedObject.PackJson(item, schema, collectionName);

if (processed % 10_000 == 0)
Progress?.Invoke(this, new(ProgressEventArgs.ProgressNotification.Progress, processed));
if (processed % 10_000 != 0)
continue;
Progress?.Invoke(this, new(ProgressEventArgs.ProgressNotification.Progress, processed));
}

Progress?.Invoke(this, new(ProgressEventArgs.ProgressNotification.End, processed));
Expand Down Expand Up @@ -390,7 +391,7 @@ public void Truncate(string collectionName)

#region consistent read

//TODO check if it can work on the aggregator only

private readonly SemaphoreSlim _consistentReadSync = new(10, 10);


Expand All @@ -416,7 +417,7 @@ public void ConsistentRead(Action<ConsistentContext> action, [NotNull] params st
$"Unknown collection {collection}. Use Connector.DeclareCollection");
}

Guid sessionId = default;
Guid sessionId = Guid.Empty;
try
{
_consistentReadSync.Wait();
Expand All @@ -435,7 +436,7 @@ public void ConsistentRead(Action<ConsistentContext> action, [NotNull] params st
{
Dbg.Trace($"exit consistent read session {sessionId}");

if (sessionId != default)
if (sessionId != Guid.Empty)
{
Client.ReleaseLock(sessionId);
Client.ReleaseConnections(sessionId);
Expand Down
5 changes: 2 additions & 3 deletions Cachalot/Linq/DataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ internal DataSource([NotNull] Connector connector, [NotNull] string collectionNa
[NotNull] CollectionSchema collectionSchema, Guid sessionId = default)
: base(CreateParser(), new QueryExecutor(connector.Client, collectionSchema, sessionId, collectionName))
{
if (connector == null) throw new ArgumentNullException(nameof(connector));


_client = connector.Client;

_collectionName = collectionName ?? throw new ArgumentNullException(nameof(collectionName));
Expand Down Expand Up @@ -272,7 +271,7 @@ public OrQuery PredicateToQuery(Expression<Func<T, bool>> where, string collecti
var executor = new NullExecutor(_collectionSchema, collectionName);
var queryable = new NullQueryable<T>(executor);

var unused = queryable.Where(where).ToList();
var _ = queryable.Where(where).ToList();

var query = executor.Expression;
query.CollectionName = _collectionName;
Expand Down
1 change: 1 addition & 0 deletions CachalotMonitor/CachalotMonitor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<SpaProxyServerUrl>http://localhost:48482</SpaProxyServerUrl>
<SpaProxyLaunchCommand>npm start</SpaProxyLaunchCommand>
<ImplicitUsings>enable</ImplicitUsings>
<TrimmerSingleWarn>true</TrimmerSingleWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion CachalotMonitor/ClientApp/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<mat-toolbar color="primary" style="height: fit-content;flex-wrap: wrap;">
<mat-toolbar color="primary" style="height: fit-content;flex-wrap: wrap;padding: 0.4rem;">
<button mat-icon-button (click)="isExpanded = !isExpanded">
<mat-icon>menu</mat-icon>
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
<div class="query-container">

<!--property-->
<mat-form-field appearance="fill">
<!-- <mat-form-field appearance="fill">
<mat-label>Property</mat-label>
<mat-select [(value)]="selectedProperty">
<mat-option *ngFor="let property of properties" [value]="property">
{{property}}
</mat-option>
</mat-select>
</mat-form-field>
</mat-select>
</mat-form-field> -->
<app-smart-multi-select [(selectedValues)]="selectedProperties" [allValues]="properties" [canSelectAll]="false"
[canClearAll]="false" [isSingleValue]=true [hint]="'select property'" label="property"
style="flex-grow: 2; min-width: 20rem;">
</app-smart-multi-select>
<!--operator-->
<mat-form-field appearance="fill" style="max-width: 6rem;">
<mat-label>Operator</mat-label>
Expand All @@ -27,7 +32,7 @@
<!--search box only if more than 10 values-->
<mat-option *ngIf="values.length > 10">
<ngx-mat-select-search [(ngModel)]="searchText" placeholderLabel="Search..."
noEntriesFoundLabel="'no matching value'">
noEntriesFoundLabel="'no matching value'">
</ngx-mat-select-search>
</mat-option>
<mat-option class="clear" *ngIf="values.length > 10">
Expand All @@ -41,10 +46,10 @@
<mat-hint *ngIf="multipleValuesAllowed">multiple values allowed (use ,)</mat-hint>
</mat-form-field>
<mat-form-field appearance="fill" *ngIf="(!multipleValuesAllowed || !canSelectValues) && hasValue "
style="flex-grow: 2">
style="flex-grow: 2">
<mat-label>Value</mat-label>
<input matInput placeholder="Value" [(ngModel)]="singleValue">
<mat-hint *ngIf="multipleValuesAllowed">multiple values allowed (use ,)</mat-hint>
</mat-form-field>

</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ export class SimpleQueryComponent implements OnInit {

filteredValues: string[] = [];

// adapter for smart-multiselect component
get selectedProperties():string[]{
if(!this.selectedProperty){
return [];
}

return [this.selectedProperty];
}

set selectedProperties(value:string[]){
if(value.length > 0){
this.selectedProperty = value[0];
}
else{
this.selectedProperty = undefined;
}
}


get values(): string[] {
return this._query.possibleValues;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, EventEmitter, Input, OnInit, Output } from "@angular/core";
import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from "@angular/core";


@Component({
Expand All @@ -8,7 +8,7 @@ import { Component, EventEmitter, Input, OnInit, Output } from "@angular/core";
})
export class SmartMultiSelectComponent implements OnInit {

constructor() {}
constructor() { }

ngOnInit(): void {
}
Expand All @@ -27,6 +27,9 @@ export class SmartMultiSelectComponent implements OnInit {
@Input()
clearButton = false;

@ViewChild('multiSelect') select: any;


// all values
private _values: string[] = [];

Expand Down Expand Up @@ -87,7 +90,32 @@ export class SmartMultiSelectComponent implements OnInit {
this.selectedValuesChange.emit(this._selectedValues);
}

} else {
}
else if (this.isSingleValue) {
console.log('single value mode');

let selected = [];

if (this._selectedValues.length > 0) {
let newElements = v.filter(x => !this._selectedValues.includes(x));
console.log(newElements);
selected = newElements;
}
else{
selected = v;
}

let newv = selected[0];
let oldv = this._selectedValues[0]
if(newv != oldv && newv){
this._selectedValues = [newv];
this.selectedValuesChange.emit(this._selectedValues);
this.select.close();
}


}
else {
const filtered = v.filter(x => x); // remove empty values
if (!this.arraysAreEqual(filtered, this._selectedValues)) {
this._selectedValues = filtered;
Expand Down
2 changes: 1 addition & 1 deletion CachalotMonitor/Model/SimpleQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public bool CheckIsValid()
{
if (Values.Length == 0) return false;

if (Values.Any(string.IsNullOrWhiteSpace)) return false;
if (Array.Exists(Values, string.IsNullOrWhiteSpace)) return false;
}

return true;
Expand Down
19 changes: 9 additions & 10 deletions CachalotMonitor/Services/QueryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public QueryMetadata GetMetadata(string collection, string property)
return metadata;

if (pr.IsCollection)
MetadataForCollectionProperty(property, result!, metadata);
MetadataForCollectionProperty(result!, metadata);
else
MetadataForScalarProperty(property, result!, metadata);

Expand Down Expand Up @@ -194,14 +194,13 @@ public string ClientQueryToSql(string collection, AndQuery query)
{
var q = query.SimpleQueries[i];
SimpleQueryToSql(q, builder);
if (i < query.SimpleQueries.Length - 1)
if (query.SimpleQueries[i + 1].CheckIsValid())
{
builder.Append(" ");
builder.Append("AND");
builder.Append(Environment.NewLine);
builder.Append(" ");
}
if (i < query.SimpleQueries.Length - 1 && query.SimpleQueries[i + 1].CheckIsValid())
{
builder.Append(" ");
builder.Append("AND");
builder.Append(Environment.NewLine);
builder.Append(" ");
}
}

if (query.OrderBy != null)
Expand Down Expand Up @@ -443,7 +442,7 @@ private static void MetadataForScalarProperty(string property, List<JObject> res
}
}

private static void MetadataForCollectionProperty(string property, List<JObject> result, QueryMetadata metadata)
private static void MetadataForCollectionProperty(List<JObject> result, QueryMetadata metadata)
{
if (result == null) throw new ArgumentNullException(nameof(result));
metadata.PropertyIsCollection = true;
Expand Down
14 changes: 9 additions & 5 deletions Channel/PoolStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public abstract class PoolStrategy<T> : IDisposable where T : class

private bool _disposed;

CancellationTokenSource _tokenSource = new CancellationTokenSource();
readonly CancellationTokenSource _tokenSource = new CancellationTokenSource();


protected PoolStrategy(int poolCapacity)
Expand Down Expand Up @@ -161,7 +161,7 @@ public T Get()
// recursive call (in case a resource is not valid but the provider can produce new valid ones)
return Get();
}
catch (OperationCanceledException e)
catch (OperationCanceledException)
{
return null;
}
Expand Down Expand Up @@ -198,20 +198,24 @@ public void ClearAll()

public void Dispose()
{
if(_disposed)
return;

Dispose(true);

_disposed = true;

GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if(_disposed)
return;

_tokenSource.Cancel();
ClearAll();
_blockingQueue.Dispose();

_disposed = true;

}

#endregion
Expand Down
Loading

0 comments on commit dc4ed43

Please sign in to comment.