Skip to content

Commit 6447b73

Browse files
authored
Move singleton to common directory (#10935)
### What problem does this PR solve? As title ### Type of change - [x] Refactoring Signed-off-by: Jin Hai <[email protected]>
1 parent fe4852c commit 6447b73

File tree

14 files changed

+117
-35
lines changed

14 files changed

+117
-35
lines changed

api/db/db_models.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,7 @@
3636
from api.utils.configs import deserialize_b64, serialize_b64
3737

3838
from common.time_utils import current_timestamp, timestamp_to_date, date_string_to_timestamp
39-
40-
41-
def singleton(cls, *args, **kw):
42-
instances = {}
43-
44-
def _singleton():
45-
key = str(cls) + str(os.getpid())
46-
if key not in instances:
47-
instances[key] = cls(*args, **kw)
48-
return instances[key]
49-
50-
return _singleton
39+
from common.decorator import singleton
5140

5241

5342
CONTINUOUS_FIELD_TYPE = {IntegerField, FloatField, DateTimeField}

common/decorator.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
import os
17+
18+
def singleton(cls, *args, **kw):
19+
instances = {}
20+
21+
def _singleton():
22+
key = str(cls) + str(os.getpid())
23+
if key not in instances:
24+
instances[key] = cls(*args, **kw)
25+
return instances[key]
26+
27+
return _singleton

rag/utils/__init__.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,10 @@
1515
#
1616

1717
import os
18-
1918
import tiktoken
2019

2120
from api.utils.file_utils import get_project_base_directory
2221

23-
24-
def singleton(cls, *args, **kw):
25-
instances = {}
26-
27-
def _singleton():
28-
key = str(cls) + str(os.getpid())
29-
if key not in instances:
30-
instances[key] = cls(*args, **kw)
31-
return instances[key]
32-
33-
return _singleton
34-
3522
tiktoken_cache_dir = get_project_base_directory()
3623
os.environ["TIKTOKEN_CACHE_DIR"] = tiktoken_cache_dir
3724
# encoder = tiktoken.encoding_for_model("gpt-3.5-turbo")

rag/utils/azure_sas_conn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import time
2020
from io import BytesIO
2121
from rag import settings
22-
from rag.utils import singleton
22+
from common.decorator import singleton
2323
from azure.storage.blob import ContainerClient
2424

2525

rag/utils/azure_spn_conn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import os
1919
import time
2020
from rag import settings
21-
from rag.utils import singleton
21+
from common.decorator import singleton
2222
from azure.identity import ClientSecretCredential, AzureAuthorityHosts
2323
from azure.storage.filedatalake import FileSystemClient
2424

rag/utils/es_conn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from elastic_transport import ConnectionTimeout
2727
from rag import settings
2828
from rag.settings import TAG_FLD, PAGERANK_FLD
29-
from rag.utils import singleton
29+
from common.decorator import singleton
3030
from api.utils.file_utils import get_project_base_directory
3131
from api.utils.common import convert_bytes
3232
from rag.utils.doc_store_conn import DocStoreConnection, MatchExpr, OrderByExpr, MatchTextExpr, MatchDenseExpr, \

rag/utils/infinity_conn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from infinity.errors import ErrorCode
2828
from rag import settings
2929
from rag.settings import PAGERANK_FLD, TAG_FLD
30-
from rag.utils import singleton
30+
from common.decorator import singleton
3131
import pandas as pd
3232
from api.utils.file_utils import get_project_base_directory
3333
from rag.nlp import is_english

rag/utils/minio_conn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from minio.error import S3Error
2222
from io import BytesIO
2323
from rag import settings
24-
from rag.utils import singleton
24+
from common.decorator import singleton
2525

2626

2727
@singleton

rag/utils/opendal_conn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from urllib.parse import quote_plus
55

66
from api.utils.configs import get_base_config
7-
from rag.utils import singleton
7+
from common.decorator import singleton
88

99

1010
CREATE_TABLE_SQL = """

rag/utils/opensearch_conn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from opensearchpy import ConnectionTimeout
2727
from rag import settings
2828
from rag.settings import TAG_FLD, PAGERANK_FLD
29-
from rag.utils import singleton
29+
from common.decorator import singleton
3030
from api.utils.file_utils import get_project_base_directory
3131
from rag.utils.doc_store_conn import DocStoreConnection, MatchExpr, OrderByExpr, MatchTextExpr, MatchDenseExpr, \
3232
FusionExpr

0 commit comments

Comments
 (0)