nfs-db 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nfs_db/__init__.py +3 -0
- nfs_db/_version.py +1 -0
- nfs_db/constants.py +7 -0
- nfs_db/database.py +15 -0
- nfs_db/table/__init__.py +0 -0
- nfs_db/table/table.py +176 -0
- nfs_db/table/table_spec.py +54 -0
- nfs_db/table/transactions.py +221 -0
- nfs_db/utils.py +14 -0
- nfs_db-1.0.0.dist-info/METADATA +82 -0
- nfs_db-1.0.0.dist-info/RECORD +14 -0
- nfs_db-1.0.0.dist-info/WHEEL +5 -0
- nfs_db-1.0.0.dist-info/licenses/LICENSE +21 -0
- nfs_db-1.0.0.dist-info/top_level.txt +1 -0
nfs_db/__init__.py
ADDED
nfs_db/_version.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "1.0.0"
|
nfs_db/constants.py
ADDED
nfs_db/database.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from .constants import NFS_DB_PATH
|
|
4
|
+
from .table.table import table
|
|
5
|
+
from .table.table_spec import table_spec
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class database:
|
|
9
|
+
|
|
10
|
+
def __init__(self, path: str):
|
|
11
|
+
self.path = Path(path) / Path(NFS_DB_PATH)
|
|
12
|
+
self.path.mkdir(parents=True, exist_ok=True)
|
|
13
|
+
|
|
14
|
+
def table(self, table_spec: table_spec):
|
|
15
|
+
return table(self.path, table_spec)
|
nfs_db/table/__init__.py
ADDED
|
File without changes
|
nfs_db/table/table.py
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
from collections.abc import Callable
|
|
2
|
+
from uuid import uuid4
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from ..constants import (
|
|
6
|
+
STAGING_PATH,
|
|
7
|
+
INDEXES_PATH,
|
|
8
|
+
ROWS_PATH,
|
|
9
|
+
ID_COLUMN_NAME,
|
|
10
|
+
CURRENT_TRANSACTION_EXTENSION,
|
|
11
|
+
FIELDS_PATH
|
|
12
|
+
)
|
|
13
|
+
from .table_spec import table_spec
|
|
14
|
+
from .transactions import insert_transaction, update_transaction, delete_transaction
|
|
15
|
+
from ..utils import exec
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class table:
|
|
19
|
+
|
|
20
|
+
def __init__(self, db_path: Path, table_spec: table_spec):
|
|
21
|
+
self.spec = table_spec
|
|
22
|
+
self.path = db_path / table_spec.identifier
|
|
23
|
+
|
|
24
|
+
self.staging_path = self.path / STAGING_PATH
|
|
25
|
+
self.staging_path.mkdir(parents=True, exist_ok=True)
|
|
26
|
+
|
|
27
|
+
self.indexes_path = self.path / INDEXES_PATH
|
|
28
|
+
self.indexes_path.mkdir(parents=True, exist_ok=True)
|
|
29
|
+
|
|
30
|
+
self.rows_path = self.path / ROWS_PATH
|
|
31
|
+
self.rows_path.mkdir(parents=True, exist_ok=True)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _validate(self, data: dict):
|
|
35
|
+
if ID_COLUMN_NAME not in data:
|
|
36
|
+
data[ID_COLUMN_NAME] = str(uuid4())
|
|
37
|
+
|
|
38
|
+
for col in self.spec.columns.values():
|
|
39
|
+
if col.name not in data:
|
|
40
|
+
raise ValueError(f"Missing value for column '{col.name}'")
|
|
41
|
+
if type(data[col.name]) is not col.data_type:
|
|
42
|
+
raise TypeError(
|
|
43
|
+
f"Incorrect type for column '{col.name}'. "
|
|
44
|
+
f"Expected {col.data_type.__name__}, got {type(data[col.name]).__name__}"
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def insert(self, data: dict) -> str:
|
|
49
|
+
self._validate(data)
|
|
50
|
+
insert_trans = insert_transaction(self.path, data)
|
|
51
|
+
try:
|
|
52
|
+
insert_trans.stage()
|
|
53
|
+
insert_trans.execute()
|
|
54
|
+
return data[ID_COLUMN_NAME]
|
|
55
|
+
except FileExistsError:
|
|
56
|
+
raise FileExistsError(
|
|
57
|
+
f"Row with ID {data[ID_COLUMN_NAME]} already exists in table {self.spec.name}"
|
|
58
|
+
) from None
|
|
59
|
+
finally:
|
|
60
|
+
insert_trans.cleanup()
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def update(self, row_id: str, edit: Callable[[dict], dict]) -> dict:
|
|
64
|
+
if not callable(edit):
|
|
65
|
+
raise TypeError("edit must be a callable that accepts and returns a row")
|
|
66
|
+
|
|
67
|
+
while True:
|
|
68
|
+
existing_transaction_id, row = self._get_row_snapshot(row_id)
|
|
69
|
+
updated_row = edit(row.copy())
|
|
70
|
+
if not isinstance(updated_row, dict):
|
|
71
|
+
raise TypeError("edit must return a row dictionary")
|
|
72
|
+
if updated_row.get(ID_COLUMN_NAME) != row_id:
|
|
73
|
+
raise ValueError(f"Row ID '{ID_COLUMN_NAME}' cannot be modified")
|
|
74
|
+
if set(updated_row) != set(self.spec.columns):
|
|
75
|
+
raise ValueError("Updated row must contain exactly the table's columns")
|
|
76
|
+
self._validate(updated_row)
|
|
77
|
+
|
|
78
|
+
update_trans = update_transaction(
|
|
79
|
+
self.path,
|
|
80
|
+
row_id,
|
|
81
|
+
existing_transaction_id,
|
|
82
|
+
updated_row,
|
|
83
|
+
)
|
|
84
|
+
try:
|
|
85
|
+
update_trans.stage()
|
|
86
|
+
try:
|
|
87
|
+
update_trans.execute()
|
|
88
|
+
except FileNotFoundError:
|
|
89
|
+
continue
|
|
90
|
+
finally:
|
|
91
|
+
update_trans.cleanup()
|
|
92
|
+
|
|
93
|
+
return updated_row
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def delete(self, row_id: str):
|
|
97
|
+
delete_trans = delete_transaction(self.path, row_id)
|
|
98
|
+
try:
|
|
99
|
+
delete_trans.execute()
|
|
100
|
+
except FileNotFoundError:
|
|
101
|
+
raise KeyError(
|
|
102
|
+
f"Row with ID {row_id} does not exist in table {self.spec.name}"
|
|
103
|
+
) from None
|
|
104
|
+
finally:
|
|
105
|
+
delete_trans.cleanup()
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _convert_value(self, column_name: str, value: str):
|
|
109
|
+
column = self.spec.columns[column_name]
|
|
110
|
+
if column.data_type is bool:
|
|
111
|
+
return value.lower() == "true"
|
|
112
|
+
return column.data_type(value)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def get(self, row_id: str) -> dict:
|
|
116
|
+
_, row = self._get_row_snapshot(row_id)
|
|
117
|
+
return row
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _get_row_snapshot(self, row_id: str) -> tuple[str, dict]:
|
|
121
|
+
row_path = self.rows_path / row_id
|
|
122
|
+
lines, _, _ = exec("find", row_path)
|
|
123
|
+
paths = lines.splitlines()
|
|
124
|
+
|
|
125
|
+
if not paths:
|
|
126
|
+
raise KeyError(f"Row with ID '{row_id}' does not exist")
|
|
127
|
+
|
|
128
|
+
data = {}
|
|
129
|
+
current_transaction_id = None
|
|
130
|
+
for path in paths:
|
|
131
|
+
if path.endswith(CURRENT_TRANSACTION_EXTENSION):
|
|
132
|
+
current_transaction_id = Path(path).name.replace(CURRENT_TRANSACTION_EXTENSION, "")
|
|
133
|
+
break
|
|
134
|
+
if current_transaction_id is None:
|
|
135
|
+
raise RuntimeError(
|
|
136
|
+
f"Row with ID '{row_id}' is missing a current transaction marker."
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
for path in paths:
|
|
140
|
+
relative_path = Path(path).relative_to(row_path)
|
|
141
|
+
parts = relative_path.parts
|
|
142
|
+
if len(parts) == 4 and parts[0] == current_transaction_id:
|
|
143
|
+
_, _, column_name, column_value = parts
|
|
144
|
+
data[column_name] = self._convert_value(column_name, column_value)
|
|
145
|
+
|
|
146
|
+
return current_transaction_id, data
|
|
147
|
+
|
|
148
|
+
def get_where(self, column_name: str, value) -> list[dict]:
|
|
149
|
+
if column_name not in self.spec.columns:
|
|
150
|
+
raise ValueError(
|
|
151
|
+
f"Column '{column_name}' does not exist in table '{self.spec.name}'"
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
index_path = self.indexes_path / column_name / str(value)
|
|
155
|
+
lines, _, _ = exec("find", "-L", str(index_path))
|
|
156
|
+
matching_transactions = set()
|
|
157
|
+
rows = {}
|
|
158
|
+
for path in lines.splitlines():
|
|
159
|
+
relative_path = Path(path).relative_to(index_path)
|
|
160
|
+
parts = relative_path.parts
|
|
161
|
+
if (
|
|
162
|
+
len(parts) == 2
|
|
163
|
+
and parts[1].endswith(CURRENT_TRANSACTION_EXTENSION)
|
|
164
|
+
and parts[0] == parts[1][:-len(CURRENT_TRANSACTION_EXTENSION)]
|
|
165
|
+
):
|
|
166
|
+
matching_transactions.add(parts[0])
|
|
167
|
+
elif len(parts) == 5 and parts[1] == parts[0] and parts[2] == FIELDS_PATH:
|
|
168
|
+
index_transaction_id, _, _, field_name, field_value = parts
|
|
169
|
+
rows.setdefault(index_transaction_id, {})[field_name] = self._convert_value(
|
|
170
|
+
field_name, field_value
|
|
171
|
+
)
|
|
172
|
+
return [
|
|
173
|
+
row
|
|
174
|
+
for index_transaction_id, row in rows.items()
|
|
175
|
+
if index_transaction_id in matching_transactions
|
|
176
|
+
]
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
import json
|
|
3
|
+
|
|
4
|
+
from ..constants import ID_COLUMN_NAME
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
_COLUMN_TYPE_NAMES = {
|
|
8
|
+
int: "INTEGER",
|
|
9
|
+
str: "STRING",
|
|
10
|
+
float: "FLOAT",
|
|
11
|
+
bool: "BOOLEAN",
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class column:
|
|
16
|
+
|
|
17
|
+
def __init__(
|
|
18
|
+
self,
|
|
19
|
+
name: str,
|
|
20
|
+
data_type: type,
|
|
21
|
+
):
|
|
22
|
+
self.name = name
|
|
23
|
+
self.data_type = data_type
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class table_spec:
|
|
27
|
+
|
|
28
|
+
def __init__(self, name: str, /, **columns: type):
|
|
29
|
+
self.name = name
|
|
30
|
+
self.columns = {}
|
|
31
|
+
self.columns[ID_COLUMN_NAME] = column(ID_COLUMN_NAME, str)
|
|
32
|
+
|
|
33
|
+
for column_name, data_type in columns.items():
|
|
34
|
+
if column_name in self.columns:
|
|
35
|
+
raise ValueError(
|
|
36
|
+
f"Column '{column_name}' already exists in table '{self.name}'"
|
|
37
|
+
)
|
|
38
|
+
if data_type not in _COLUMN_TYPE_NAMES:
|
|
39
|
+
raise TypeError("Column type must be int, str, float, or bool")
|
|
40
|
+
self.columns[column_name] = column(column_name, data_type)
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def identifier(self):
|
|
44
|
+
definition = {
|
|
45
|
+
"name": self.name,
|
|
46
|
+
"columns": [
|
|
47
|
+
(col.name, _COLUMN_TYPE_NAMES[col.data_type])
|
|
48
|
+
for col in self.columns.values()
|
|
49
|
+
],
|
|
50
|
+
}
|
|
51
|
+
serialized = json.dumps(
|
|
52
|
+
definition, ensure_ascii=False, separators=(",", ":")
|
|
53
|
+
).encode("utf-8")
|
|
54
|
+
return hashlib.sha256(serialized).hexdigest()
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
from uuid import uuid4
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
import errno
|
|
4
|
+
import os
|
|
5
|
+
from abc import ABC, abstractmethod
|
|
6
|
+
import shutil
|
|
7
|
+
from nfs_db.constants import (
|
|
8
|
+
CURRENT_TRANSACTION_EXTENSION,
|
|
9
|
+
STAGING_PATH,
|
|
10
|
+
INDEXES_PATH,
|
|
11
|
+
ROWS_PATH,
|
|
12
|
+
FIELDS_PATH,
|
|
13
|
+
ID_COLUMN_NAME,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _delete_index(index_path: Path):
|
|
18
|
+
index_path.unlink(missing_ok=True)
|
|
19
|
+
try:
|
|
20
|
+
index_path.parent.rmdir()
|
|
21
|
+
except OSError as error:
|
|
22
|
+
if error.errno not in (errno.ENOENT, errno.ENOTEMPTY, errno.EEXIST):
|
|
23
|
+
raise
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class transaction(ABC):
|
|
27
|
+
|
|
28
|
+
@abstractmethod
|
|
29
|
+
def stage(self): ...
|
|
30
|
+
|
|
31
|
+
@abstractmethod
|
|
32
|
+
def execute(self): ...
|
|
33
|
+
|
|
34
|
+
@abstractmethod
|
|
35
|
+
def cleanup(self): ...
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class insert_transaction(transaction):
|
|
39
|
+
|
|
40
|
+
def __init__(self, table_path: Path, data: dict):
|
|
41
|
+
self.id = str(uuid4())
|
|
42
|
+
self.data = data
|
|
43
|
+
self.row_id = data[ID_COLUMN_NAME]
|
|
44
|
+
self.table_path = table_path
|
|
45
|
+
self.staging_path = self.table_path / STAGING_PATH
|
|
46
|
+
self.indexes_path = self.table_path / INDEXES_PATH
|
|
47
|
+
self.rows_path = self.table_path / ROWS_PATH
|
|
48
|
+
self.staging_row_path = self.staging_path / self.id / self.row_id
|
|
49
|
+
self.future_row_path = self.rows_path / self.row_id
|
|
50
|
+
self.executed = False
|
|
51
|
+
|
|
52
|
+
def _create_indexes(self):
|
|
53
|
+
for name, value in self.data.items():
|
|
54
|
+
symlink_path = (self.indexes_path / name / str(value) / self.id)
|
|
55
|
+
symlink_path.parent.mkdir(parents=True, exist_ok=True)
|
|
56
|
+
symlink_path.symlink_to(self.future_row_path, target_is_directory=True)
|
|
57
|
+
|
|
58
|
+
def _delete_indexes(self):
|
|
59
|
+
for name, value in self.data.items():
|
|
60
|
+
_delete_index(self.indexes_path / name / str(value) / self.id)
|
|
61
|
+
|
|
62
|
+
def _create_current_transaction_marker(self):
|
|
63
|
+
(self.staging_row_path / (self.id + CURRENT_TRANSACTION_EXTENSION)).touch(exist_ok=True)
|
|
64
|
+
|
|
65
|
+
def _create_transaction_contents(self):
|
|
66
|
+
fields_path = self.staging_row_path / self.id / FIELDS_PATH
|
|
67
|
+
for name, value in self.data.items():
|
|
68
|
+
(fields_path / name / str(value)).mkdir(parents=True, exist_ok=True)
|
|
69
|
+
|
|
70
|
+
def stage(self):
|
|
71
|
+
self._create_transaction_contents()
|
|
72
|
+
self._create_current_transaction_marker()
|
|
73
|
+
self._create_indexes()
|
|
74
|
+
|
|
75
|
+
def execute(self):
|
|
76
|
+
self.executed = False
|
|
77
|
+
try:
|
|
78
|
+
os.rename(self.staging_row_path, self.future_row_path)
|
|
79
|
+
self.executed = True
|
|
80
|
+
except OSError as error:
|
|
81
|
+
if error.errno in (errno.EEXIST, errno.ENOTEMPTY):
|
|
82
|
+
raise FileExistsError() from None
|
|
83
|
+
raise
|
|
84
|
+
|
|
85
|
+
def cleanup(self):
|
|
86
|
+
if not self.executed:
|
|
87
|
+
self._delete_indexes()
|
|
88
|
+
shutil.rmtree(self.staging_row_path, ignore_errors=True)
|
|
89
|
+
self.staging_row_path.parent.rmdir()
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class update_transaction(transaction):
|
|
93
|
+
|
|
94
|
+
def __init__(
|
|
95
|
+
self,
|
|
96
|
+
table_path: Path,
|
|
97
|
+
row_id: str,
|
|
98
|
+
existing_transaction_id: str,
|
|
99
|
+
data: dict,
|
|
100
|
+
):
|
|
101
|
+
self.id = str(uuid4())
|
|
102
|
+
self.data = data
|
|
103
|
+
self.row_id = row_id
|
|
104
|
+
self.existing_transaction_id = existing_transaction_id
|
|
105
|
+
self.table_path = table_path
|
|
106
|
+
self.indexes_path = self.table_path / INDEXES_PATH
|
|
107
|
+
self.rows_path = self.table_path / ROWS_PATH
|
|
108
|
+
self.row_path = self.rows_path / self.row_id
|
|
109
|
+
self.transaction_path = self.row_path / self.id
|
|
110
|
+
self.executed = False
|
|
111
|
+
|
|
112
|
+
def _create_transaction_contents(self):
|
|
113
|
+
fields_path = self.transaction_path / FIELDS_PATH
|
|
114
|
+
for name, value in self.data.items():
|
|
115
|
+
(fields_path / name / str(value)).mkdir(parents=True, exist_ok=True)
|
|
116
|
+
|
|
117
|
+
def _delete_transaction_contents(self):
|
|
118
|
+
shutil.rmtree(self.transaction_path, ignore_errors=True)
|
|
119
|
+
|
|
120
|
+
def _create_new_indexes(self):
|
|
121
|
+
for name, value in self.data.items():
|
|
122
|
+
symlink_path = (self.indexes_path / name / str(value) / self.id)
|
|
123
|
+
symlink_path.parent.mkdir(parents=True, exist_ok=True)
|
|
124
|
+
symlink_path.symlink_to(self.row_path, target_is_directory=True)
|
|
125
|
+
|
|
126
|
+
def _delete_new_indexes(self):
|
|
127
|
+
for name, value in self.data.items():
|
|
128
|
+
_delete_index(self.indexes_path / name / str(value) / self.id)
|
|
129
|
+
|
|
130
|
+
def _delete_previous_indexes(self):
|
|
131
|
+
fields_path = (
|
|
132
|
+
self.row_path / self.existing_transaction_id / FIELDS_PATH
|
|
133
|
+
)
|
|
134
|
+
if not fields_path.is_dir():
|
|
135
|
+
return
|
|
136
|
+
|
|
137
|
+
for column_path in fields_path.iterdir():
|
|
138
|
+
for value_path in column_path.iterdir():
|
|
139
|
+
_delete_index(
|
|
140
|
+
self.indexes_path
|
|
141
|
+
/ column_path.name
|
|
142
|
+
/ value_path.name
|
|
143
|
+
/ self.existing_transaction_id
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
def stage(self):
|
|
147
|
+
self._create_transaction_contents()
|
|
148
|
+
self._create_new_indexes()
|
|
149
|
+
|
|
150
|
+
def execute(self):
|
|
151
|
+
existing_transaction_marker = (
|
|
152
|
+
self.row_path
|
|
153
|
+
/ f"{self.existing_transaction_id}{CURRENT_TRANSACTION_EXTENSION}"
|
|
154
|
+
)
|
|
155
|
+
new_transaction_marker = (
|
|
156
|
+
self.row_path / f"{self.id}{CURRENT_TRANSACTION_EXTENSION}"
|
|
157
|
+
)
|
|
158
|
+
os.rename(existing_transaction_marker, new_transaction_marker)
|
|
159
|
+
self.executed = True
|
|
160
|
+
|
|
161
|
+
def cleanup(self):
|
|
162
|
+
if self.executed:
|
|
163
|
+
self._delete_previous_indexes()
|
|
164
|
+
shutil.rmtree(
|
|
165
|
+
self.row_path / self.existing_transaction_id,
|
|
166
|
+
ignore_errors=True,
|
|
167
|
+
)
|
|
168
|
+
else:
|
|
169
|
+
self._delete_new_indexes()
|
|
170
|
+
self._delete_transaction_contents()
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
class delete_transaction(transaction):
|
|
174
|
+
|
|
175
|
+
def __init__(self, table_path: Path, row_id: str):
|
|
176
|
+
self.id = str(uuid4())
|
|
177
|
+
self.row_id = row_id
|
|
178
|
+
self.table_path = table_path
|
|
179
|
+
self.staging_path = self.table_path / STAGING_PATH
|
|
180
|
+
self.indexes_path = self.table_path / INDEXES_PATH
|
|
181
|
+
self.rows_path = self.table_path / ROWS_PATH
|
|
182
|
+
self.row_path = self.rows_path / self.row_id
|
|
183
|
+
self.deleted_path = self.staging_path / self.id
|
|
184
|
+
self.executed = False
|
|
185
|
+
|
|
186
|
+
def stage(self): ...
|
|
187
|
+
|
|
188
|
+
def execute(self):
|
|
189
|
+
try:
|
|
190
|
+
os.rename(self.row_path, self.deleted_path)
|
|
191
|
+
self.executed = True
|
|
192
|
+
except OSError as error:
|
|
193
|
+
if error.errno in (errno.ENOENT, errno.ENOTEMPTY):
|
|
194
|
+
raise FileNotFoundError() from None
|
|
195
|
+
raise
|
|
196
|
+
|
|
197
|
+
def _delete_indexes(self):
|
|
198
|
+
current_marker = next(
|
|
199
|
+
self.deleted_path.glob(f"*{CURRENT_TRANSACTION_EXTENSION}"),
|
|
200
|
+
None,
|
|
201
|
+
)
|
|
202
|
+
if current_marker is None:
|
|
203
|
+
raise FileNotFoundError(f"Row with ID {self.row_id} does not exist")
|
|
204
|
+
current_transaction = current_marker.name.removesuffix(CURRENT_TRANSACTION_EXTENSION)
|
|
205
|
+
fields_path = self.deleted_path / current_transaction / FIELDS_PATH
|
|
206
|
+
if not fields_path.is_dir():
|
|
207
|
+
raise FileNotFoundError(f"Fields for row with ID {self.row_id} do not exist")
|
|
208
|
+
for column_path in fields_path.iterdir():
|
|
209
|
+
for value_path in column_path.iterdir():
|
|
210
|
+
_delete_index(
|
|
211
|
+
self.indexes_path
|
|
212
|
+
/ column_path.name
|
|
213
|
+
/ value_path.name
|
|
214
|
+
/ current_transaction
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
def cleanup(self):
|
|
218
|
+
if not self.executed:
|
|
219
|
+
return
|
|
220
|
+
self._delete_indexes()
|
|
221
|
+
shutil.rmtree(self.deleted_path, ignore_errors=True)
|
nfs_db/utils.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import subprocess
|
|
2
|
+
|
|
3
|
+
def exec(
|
|
4
|
+
command: str,
|
|
5
|
+
*args: str,
|
|
6
|
+
) -> tuple[str, str, int]:
|
|
7
|
+
"""Executes a shell command and returns the output and error messages."""
|
|
8
|
+
process = subprocess.Popen(
|
|
9
|
+
[command, *args],
|
|
10
|
+
stdout=subprocess.PIPE,
|
|
11
|
+
stderr=subprocess.PIPE,
|
|
12
|
+
)
|
|
13
|
+
stdout, stderr = process.communicate()
|
|
14
|
+
return stdout.decode("utf-8"), stderr.decode("utf-8"), process.returncode
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nfs-db
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A database-like interface for your NFS
|
|
5
|
+
Home-page: https://github.com/karzunn/nfs-db
|
|
6
|
+
Author: Carson O'Ffill
|
|
7
|
+
Author-email: offillcarson@gmail.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Requires-Python: >=3.9
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Dynamic: author
|
|
19
|
+
Dynamic: author-email
|
|
20
|
+
Dynamic: classifier
|
|
21
|
+
Dynamic: description
|
|
22
|
+
Dynamic: description-content-type
|
|
23
|
+
Dynamic: home-page
|
|
24
|
+
Dynamic: license
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
Dynamic: requires-python
|
|
27
|
+
Dynamic: summary
|
|
28
|
+
|
|
29
|
+

|
|
30
|
+
|
|
31
|
+
# nfs-db
|
|
32
|
+
|
|
33
|
+
nfs-db is *not* a database engine. It is a lightweight Python interface that lets you interact with an existing NFS server through familiar database-style operations.
|
|
34
|
+
|
|
35
|
+
### Why nfs-db?
|
|
36
|
+
|
|
37
|
+
For small projects already utilizing a NFS server, using nfs-db avoids the infrastructure complexity and operational requirements that traditional databases bring.
|
|
38
|
+
|
|
39
|
+
### What to expect
|
|
40
|
+
|
|
41
|
+
nfs-db is not intended to compete with traditional database engines. Filesystem operations introduce overhead, and performance will not be comparable to purpose-built database systems.
|
|
42
|
+
|
|
43
|
+
nfs-db is best suited for:
|
|
44
|
+
|
|
45
|
+
- Small applications and utilities.
|
|
46
|
+
- Shared application state across multiple machines.
|
|
47
|
+
- Data processing pipelines operating on shared NFS storage.
|
|
48
|
+
- Workloads where atomic operations matter more than raw throughput.
|
|
49
|
+
|
|
50
|
+
It is not intended for large datasets, high-throughput workloads, or applications requiring the advanced capabilities of a traditional database.
|
|
51
|
+
|
|
52
|
+
## Quickstart
|
|
53
|
+
|
|
54
|
+
Create a users table:
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from nfs_db import database, table_spec
|
|
58
|
+
|
|
59
|
+
db = database("/mnt/nfs")
|
|
60
|
+
spec = table_spec("users", name=str, number=int, is_active=bool)
|
|
61
|
+
users = db.table(spec)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Insert, retrieve, update, query, and delete rows:
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
row_id = users.insert({
|
|
68
|
+
"name": "John",
|
|
69
|
+
"number": 7,
|
|
70
|
+
"is_active": True,
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
row = users.get(row_id)
|
|
74
|
+
active_users = users.get_where("is_active", True)
|
|
75
|
+
|
|
76
|
+
users.update(row_id, lambda row: {**row, "number": row["number"] + 1})
|
|
77
|
+
users.delete(row_id)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The update callback must return a row dictionary with the table's existing
|
|
81
|
+
fields, and it must not change `id`. If another update wins a race, the callback
|
|
82
|
+
may run again against the latest row, so avoid external side effects in it.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
nfs_db/__init__.py,sha256=axBafBvLly71pnCIyg-VVPNzbQSC9hiOuaVGsBtw-wA,106
|
|
2
|
+
nfs_db/_version.py,sha256=J-j-u0itpEFT6irdmWmixQqYMadNl1X91TxUmoiLHMI,22
|
|
3
|
+
nfs_db/constants.py,sha256=h9TGx6Vkb7Bxnhh4NQaFK0bif3ljL1yAdPPFbirIF6U,192
|
|
4
|
+
nfs_db/database.py,sha256=IBzo5gyLb-mITPbboAw4ZZNrZcQ8RcNewXwub6Jiw1o,381
|
|
5
|
+
nfs_db/utils.py,sha256=DZ5xfBxwU0xWaL0OiwW5CXO7UQxELceqVw_UHfzMLiA,417
|
|
6
|
+
nfs_db/table/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
nfs_db/table/table.py,sha256=eeJyKgpaKURPyimRg2uNaoktc9D3aX0IpjMW78_gJ9w,6283
|
|
8
|
+
nfs_db/table/table_spec.py,sha256=e7looBFzU7zpFQQb2fTII2N9gpZ4U653-j3iB-s8KPU,1402
|
|
9
|
+
nfs_db/table/transactions.py,sha256=oQ5sQirDGW_heuBr8sCHbfY-VvqJw-a0XoJG4I8MW24,7430
|
|
10
|
+
nfs_db-1.0.0.dist-info/licenses/LICENSE,sha256=kDTLr5tsKfRzWsKvighXodhYNK6p2vB-adM2RLfV6Xg,1071
|
|
11
|
+
nfs_db-1.0.0.dist-info/METADATA,sha256=XbMMrsvgY_wNPr1iE9jaONuKO_Te5c3YAPbGxpMEPuo,2556
|
|
12
|
+
nfs_db-1.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
13
|
+
nfs_db-1.0.0.dist-info/top_level.txt,sha256=UJK3Lfi5GX089UhVg_A_pyyxxqqFhrhcxQPweAigfV8,7
|
|
14
|
+
nfs_db-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Carson O'Ffill
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
nfs_db
|