Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
erp5
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Romain Courteaud
erp5
Commits
795e6d64
Commit
795e6d64
authored
May 30, 2012
by
Romain Courteaud
Committed by
Sebastien Robin
Aug 07, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Raw import pdata replacement structure.
Credit to Vincent Pelletier.
parent
acd98d90
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
120 additions
and
0 deletions
+120
-0
product/ERP5Type/pdata_replacement.py
product/ERP5Type/pdata_replacement.py
+120
-0
No files found.
product/ERP5Type/pdata_replacement.py
0 → 100644
View file @
795e6d64
import
transaction
from
cStringIO
import
StringIO
from
BTrees.IOBTree
import
IOBTree
class
PersistentString
(
persistent
.
Persistent
):
def
__init__
(
self
,
value
):
self
.
value
=
value
def
__str__
(
self
):
return
self
.
value
class
BTreeData2
(
persistent
.
Persistent
):
def
__init__
(
self
):
self
.
tree
=
IOBTree
()
def
write
(
self
,
buf
,
offset
):
# TODO: auto-aggregation of continuous keys when overwriting
offset
=
int
(
offset
)
assert
not
isinstance
(
offset
,
long
),
'Offset is too big for int '
\
'type: %s'
%
(
offset
,
)
tree
=
self
.
tree
buf_len
=
len
(
buf
)
try
:
lower_key
=
tree
.
maxKey
(
offset
)
except
ValueError
:
# No early-enough entry
key
=
offset
else
:
if
lower_key
<
offset
:
value
=
str
(
tree
[
lower_key
])
value_len
=
len
(
value
)
if
lower_key
+
value_len
>
offset
:
key
=
lower_key
#print 'Overwriting', key + value_len - offset, 'bytes, copying', offset - key, 'bytes'
buf
=
value
[:
offset
-
key
]
+
buf
else
:
key
=
offset
else
:
assert
lower_key
==
offset
,
(
lower_key
,
offset
)
key
=
lower_key
buf_offset
=
0
actual_buf_len
=
len
(
buf
)
to_apply
=
{}
for
next_key
in
tree
.
iterkeys
(
key
):
if
buf_offset
>=
actual_buf_len
:
break
next_buf_offset
=
buf_offset
+
(
next_key
-
key
)
#print 'Offset', offset + buf_offset, 'in key', key, 'with len', next_key - key
to_apply
[
key
]
=
PersistentString
(
buf
[
buf_offset
:
next_buf_offset
])
buf_offset
=
next_buf_offset
key
=
next_key
else
:
to_add
=
buf
[
buf_offset
:]
if
to_add
:
#print 'Offset', offset + buf_offset, 'in own key with len', len(to_add)
tree
[
offset
+
buf_offset
]
=
PersistentString
(
to_add
)
for
key
,
value
in
to_apply
.
iteritems
():
tree
[
key
]
=
value
return
buf_len
def
read
(
self
,
offset
,
size
):
#print 'read', hex(offset), hex(size)
start_offset
=
offset
=
int
(
offset
)
assert
not
isinstance
(
offset
,
long
),
'Offset is too big for int '
\
'type: %s'
%
(
offset
,
)
tree
=
self
.
tree
result
=
StringIO
()
write
=
result
.
write
try
:
key
=
tree
.
maxKey
(
offset
)
except
ValueError
:
return
''
else
:
last_key
=
key
iterator
=
tree
.
iterkeys
(
key
)
offset
-=
key
written
=
0
for
key
in
iterator
:
#print 'key', hex(key)
padding
=
min
(
size
,
key
-
start_offset
-
written
)
if
padding
:
write
(
'
\
x00
'
*
padding
)
written
+=
padding
size
-=
padding
if
size
==
0
:
break
chunk
=
tree
[
key
]
to_write
=
str
(
chunk
)[
offset
:
offset
+
size
]
chunk
.
_p_deactivate
()
to_write_len
=
len
(
to_write
)
write
(
to_write
)
size
-=
to_write_len
written
+=
to_write_len
offset
=
0
last_key
=
key
return
result
.
getvalue
()
def
truncate
(
self
,
offset
):
offset
=
int
(
offset
)
assert
not
isinstance
(
offset
,
long
),
'Offset is too big for int '
\
'type: %s'
%
(
offset
,
)
tree
=
self
.
tree
try
:
key
=
tree
.
maxKey
(
offset
)
except
ValueError
:
# No key below offset, flush everything.
tree
.
clear
()
else
:
value
=
str
(
tree
[
key
])
value_len
=
offset
-
key
if
len
(
value
)
>
value_len
:
tree
[
key
]
=
PersistentString
(
value
[:
value_len
])
minKey
=
tree
.
minKey
while
True
:
next_key
=
minKey
(
offset
)
if
next_key
is
None
:
break
del
tree
[
key
]
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment