From 7b77851e48960716b62c0bbd0ace0d7b1038d040 Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Thu, 21 Mar 2024 11:25:12 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20FillRows?= =?UTF-8?q?Count()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/templates/configs_/settings.txt | 5 +- .../pkg/db/crud/crud_table_cache.go_ | 4 +- internal/config/config.go | 10 ++++ internal/constants/constants.go | 1 + internal/create_files/create_files.go | 21 ++++++++ .../db_crud_tables/db_crud_tables.go | 7 +++ internal/postgres/postgres.go | 53 ++++++++++++++++++- internal/types/types.go | 1 + 8 files changed, 98 insertions(+), 4 deletions(-) diff --git a/bin/templates/configs_/settings.txt b/bin/templates/configs_/settings.txt index 39d8843..9f5855c 100644 --- a/bin/templates/configs_/settings.txt +++ b/bin/templates/configs_/settings.txt @@ -227,4 +227,7 @@ NEED_CREATE_CACHE_TEST_FILES=true TEMPLATES_GRPC_CLIENT_TABLES_CACHE_FILENAME="grpc_client_table_cache.go_" #TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME - short filename of "grpc_client_table_cache_test.go_" file -TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME="grpc_client_table_cache_test.go_" \ No newline at end of file +TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME="grpc_client_table_cache_test.go_" + +#CACHE_ELEMENTS_COUNT_MAX - maximum elements counts in cache, for every table +CACHE_ELEMENTS_COUNT_MAX=1000 \ No newline at end of file diff --git a/bin/templates/pkg/db/crud/crud_table_cache.go_ b/bin/templates/pkg/db/crud/crud_table_cache.go_ index 695651d..ee5b57d 100644 --- a/bin/templates/pkg/db/crud/crud_table_cache.go_ +++ b/bin/templates/pkg/db/crud/crud_table_cache.go_ @@ -12,8 +12,8 @@ import ( // cache - кэш с данными var cache *expirable.LRU[int64, lawsuit_status_types.LawsuitStatusType] -// CACHE_SIZE - количество элементо в кэше -const CACHE_SIZE = 100 +// CACHE_SIZE - количество элементов в кэше +const CACHE_SIZE = 1000 // CACHE_EXPIRE_MINUTES - время жизни элемента в кэше const CACHE_EXPIRE_MINUTES = 86400 diff --git a/internal/config/config.go b/internal/config/config.go index 7ba2d6f..63ff6b6 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -88,6 +88,7 @@ type SettingsINI struct { TEXT_MODULE_GENERATED string READY_ALIAS_FILENAME string NEED_CREATE_UPDATE_EVERY_COLUMN bool + CACHE_ELEMENTS_COUNT_MAX int64 } func Getenv(Name string, IsRequired bool) string { @@ -480,6 +481,15 @@ func FillSettings() { s = Getenv(Name, true) Settings.TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME = s + // + Name = "CACHE_ELEMENTS_COUNT_MAX" + s = Getenv(Name, true) + i, err := micro.Int64FromString(s) + if err != nil { + log.Error("CACHE_ELEMENTS_COUNT_MAX: ", s, " Int64FromString() error: ", err) + } + Settings.CACHE_ELEMENTS_COUNT_MAX = i + } // CurrentDirectory - возвращает текущую директорию ОС diff --git a/internal/constants/constants.go b/internal/constants/constants.go index b9640d5..48ce8da 100644 --- a/internal/constants/constants.go +++ b/internal/constants/constants.go @@ -62,3 +62,4 @@ const SERVER_GRPC_TABLE_CACHE_FILENAME = "server_grpc_table_cache.go_" const SERVER_GRPC_TABLE_CACHE_TEST_FILENAME = "server_grpc_table_cache_test.go_" const TEXT_OTVET_ID_ALIAS = "Otvet.ID = ID" +const TEXT_CACHE_SIZE_1000 = "CACHE_SIZE" diff --git a/internal/create_files/create_files.go b/internal/create_files/create_files.go index 943353c..6a947a2 100644 --- a/internal/create_files/create_files.go +++ b/internal/create_files/create_files.go @@ -1374,3 +1374,24 @@ func FilenameWithoutLastUnderline(Filename string) string { return Otvet } + +// FillVariable - заменяет переменную в тексте +func FillVariable(Text, VariableName, Value string) string { + Otvet := Text + + sFind := VariableName + " = " + pos1 := strings.Index(Otvet, sFind) + if pos1 < 0 { + return Otvet + } + + s2 := Text[pos1:] + posEnd := strings.Index(s2, "\n") + if posEnd < 0 { + return Otvet + } + + Otvet = Otvet[:pos1] + VariableName + " = " + Value + Otvet[pos1+posEnd:] + + return Otvet +} diff --git a/internal/create_files/db_crud_tables/db_crud_tables.go b/internal/create_files/db_crud_tables/db_crud_tables.go index f25fe5a..a737705 100644 --- a/internal/create_files/db_crud_tables/db_crud_tables.go +++ b/internal/create_files/db_crud_tables/db_crud_tables.go @@ -828,6 +828,13 @@ func CreateFilesCache(Table1 *types.Table) error { //alias TextCache = create_files.ConvertIDToAlias_OtvetID(TextCache, Table1) + //const CACHE_SIZE = 1000 + CACHE_ELEMENTS_COUNT_MAX := config.Settings.CACHE_ELEMENTS_COUNT_MAX + Count_Now := Table1.RowsCount + CACHE_ELEMENTS_COUNT := micro.MinInt64(Count_Now, CACHE_ELEMENTS_COUNT_MAX) + sCACHE_ELEMENTS_COUNT := micro.StringFromInt64(CACHE_ELEMENTS_COUNT) + TextCache = create_files.FillVariable(TextCache, constants.TEXT_CACHE_SIZE_1000, sCACHE_ELEMENTS_COUNT) + //удаление пустого импорта TextCache = create_files.DeleteEmptyImport(TextCache) diff --git a/internal/postgres/postgres.go b/internal/postgres/postgres.go index b1888bf..6a3d0a5 100644 --- a/internal/postgres/postgres.go +++ b/internal/postgres/postgres.go @@ -30,6 +30,11 @@ type TableColumn struct { TableComment string `json:"table_comment" gorm:"column:table_comment;default:''"` } +type TableRowsStruct struct { + IDMinimum sql.NullString `json:"id_min" gorm:"column:id_min;default:0"` + RowsCount sql.NullInt64 `json:"rows_count" gorm:"column:rows_count;default:0"` +} + // FillMapTable - возвращает массив MassTable данными из БД func FillMapTable() (map[string]*types.Table, error) { var err error @@ -261,6 +266,7 @@ order by //FillTypeGo(MapTable) FillIDMinimum(MapTable) + FillRowsCount(MapTable) return MapTable, err } @@ -272,6 +278,7 @@ func CreateTable() *types.Table { return Otvet } +// FillIDMinimum - находим минимальный ID, для тестов с этим ID func FillIDMinimum(MapTable map[string]*types.Table) { var err error @@ -286,7 +293,7 @@ func FillIDMinimum(MapTable map[string]*types.Table) { continue } DefaultValueSQL := create_files.FindTextDefaultValueSQL(TypeGo) - TextSQL := "SELECT Min(" + NameID + ") from \"" + postgres_gorm.Settings.DB_SCHEMA + "\".\"" + TableName + "\" WHERE " + NameID + " <> " + DefaultValueSQL + TextSQL := "SELECT Min(" + NameID + ") as id_minimum FROM \"" + postgres_gorm.Settings.DB_SCHEMA + "\".\"" + TableName + "\" WHERE " + NameID + " <> " + DefaultValueSQL ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*60) defer ctxCancelFunc() db.WithContext(ctx) @@ -299,6 +306,7 @@ func FillIDMinimum(MapTable map[string]*types.Table) { } var IDMinimum sql.NullString + //TableRows := TableRowsStruct{} tx = tx.Scan(&IDMinimum) err = tx.Error if err != nil { @@ -315,6 +323,49 @@ func FillIDMinimum(MapTable map[string]*types.Table) { } +// FillRowsCount - находим количество строк в таблице, для кэша +func FillRowsCount(MapTable map[string]*types.Table) { + var err error + + //соединение + db := postgres_gorm.GetConnection() + ctxMain := contextmain.GetContext() + + for TableName, Table1 := range MapTable { + //текст запроса + //только Postgres SQL + TextSQL := ` +SELECT + reltuples::bigint AS rows_count +FROM + pg_class +WHERE + oid = 'public."` + TableName + `"'::regclass +` + ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*60) + defer ctxCancelFunc() + db.WithContext(ctx) + + //запрос + tx := db.Raw(TextSQL) + err = tx.Error + if err != nil { + log.Panic("Wrong SQL query: ", TextSQL, " error: ", err) + } + + var RowsCount sql.NullInt64 + //TableRows := TableRowsStruct{} + tx = tx.Scan(&RowsCount) + err = tx.Error + if err != nil { + log.Panic("Wrong SQL Scan(): ", TextSQL, " error: ", err) + } + + Table1.RowsCount = RowsCount.Int64 + } + +} + func FindNameTypeID(Table1 *types.Table) (string, string) { Otvet := "" Type := "" diff --git a/internal/types/types.go b/internal/types/types.go index cf791c9..e177fdd 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -22,6 +22,7 @@ type Table struct { NameGo string IDMinimum string Comment string `json:"table_comment" gorm:"column:table_comment;default:''"` + RowsCount int64 } type ReplaceStruct struct {