Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
osticket
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
docker
osticket
Commits
5c0ac68e
Commit
5c0ac68e
authored
9 years ago
by
Jared Hancock
Browse files
Options
Downloads
Patches
Plain Diff
orm: Use faster newInstance method, cache ModelMeta
Use APCu, if available, to cache the compiled model meta data.
parent
1ba40e35
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
include/class.orm.php
+55
-33
55 additions, 33 deletions
include/class.orm.php
with
55 additions
and
33 deletions
include/class.orm.php
+
55
−
33
View file @
5c0ac68e
...
...
@@ -51,6 +51,7 @@ class ModelMeta implements ArrayAccess {
var
$meta
=
array
();
var
$new
;
var
$subclasses
=
array
();
var
$fields
;
function
__construct
(
$model
)
{
$this
->
model
=
$model
;
...
...
@@ -68,6 +69,20 @@ class ModelMeta implements ArrayAccess {
$meta
=
$meta
+
self
::
$base
;
}
// Short circuit the meta-data processing if APCu is available.
// This is preferred as the meta-data is unlikely to change unless
// osTicket is upgraded, (then the upgrader calls the
// flushModelCache method to clear this cache). Also, GIT_VERSION is
// used in the APC key which should be changed if new code is
// deployed.
if
(
function_exists
(
'apcu_store'
))
{
$loaded
=
false
;
$apc_key
=
SECRET_SALT
.
GIT_VERSION
.
"/orm/
{
$this
->
model
}
"
;
$this
->
meta
=
apcu_fetch
(
$apc_key
,
$loaded
);
if
(
$loaded
)
return
;
}
if
(
!
$meta
[
'view'
])
{
if
(
!
$meta
[
'table'
])
throw
new
OrmConfigurationException
(
...
...
@@ -93,6 +108,10 @@ class ModelMeta implements ArrayAccess {
}
unset
(
$j
);
$this
->
meta
=
$meta
;
if
(
function_exists
(
'apcu_store'
))
{
apcu_store
(
$apc_key
,
$this
->
meta
);
}
}
function
extend
(
ModelMeta
$child
,
$meta
)
{
...
...
@@ -188,8 +207,6 @@ class ModelMeta implements ArrayAccess {
}
function
offsetGet
(
$field
)
{
if
(
!
isset
(
$this
->
meta
[
$field
]))
$this
->
setupLazy
(
$field
);
return
$this
->
meta
[
$field
];
}
function
offsetSet
(
$field
,
$what
)
{
...
...
@@ -202,31 +219,33 @@ class ModelMeta implements ArrayAccess {
throw
new
Exception
(
'Model MetaData is immutable'
);
}
function
setupLazy
(
$what
)
{
switch
(
$what
)
{
case
'fields'
:
$this
->
meta
[
'fields'
]
=
self
::
inspectFields
();
break
;
default
:
th
row
new
Exception
(
$what
.
': No such meta-data'
);
}
/**
* Fetch the column names of the table used to persist instances of this
* model in the database.
*/
function
getFieldNames
()
{
if
(
!
isset
(
$this
->
fields
))
$
th
is
->
fields
=
self
::
inspectFields
(
);
return
$this
->
fields
;
}
/**
* Create a new instance of the model, optionally hydrating it with the
* given hash table. The constructor is not called, which leaves the
* default constructor free to assume new object status.
*
* Three methods were considered, with runtime for 10000 iterations
* * unserialze('O:9:"ModelBase":0:{}') - 0.0671s
* * new ReflectionClass("ModelBase")->newInstanceWithoutConstructor()
* - 0.0478s
* * and a hybrid by cloning the reflection class instance - 0.0335s
*/
function
newInstance
(
$props
=
false
)
{
if
(
!
isset
(
$this
->
new
))
{
$this
->
new
=
sprintf
(
'O:%d:"%s":0:{}'
,
strlen
(
$this
->
model
),
$this
->
model
);
$rc
=
new
ReflectionClass
(
$this
->
model
);
$this
->
new
=
$rc
->
newInstanceWithoutConstructor
();
}
// TODO: Compare timing between unserialize() and
// ReflectionClass::newInstanceWithoutConstructor
$instance
=
unserialize
(
$this
->
new
);
$instance
=
clone
$this
->
new
;
// Hydrate if props were included
if
(
is_array
(
$props
))
{
$instance
->
ht
=
$props
;
...
...
@@ -252,7 +271,7 @@ class ModelMeta implements ArrayAccess {
static
function
flushModelCache
()
{
if
(
self
::
$model_cache
)
@
apc_clear_cache
(
'user'
);
@
apc
u
_clear_cache
(
'user'
);
}
}
...
...
@@ -346,7 +365,7 @@ class VerySimpleModel {
return
null
;
// Check to see if the column referenced is actually valid
if
(
in_array
(
$field
,
static
::
getMeta
(
'fields'
)))
if
(
in_array
(
$field
,
static
::
getMeta
(
)
->
getFieldNames
(
)))
return
null
;
throw
new
OrmException
(
sprintf
(
__
(
'%s: %s: Field not defined'
),
...
...
@@ -1300,16 +1319,17 @@ class QuerySet implements IteratorAggregate, ArrayAccess, Serializable, Countabl
// Load defaults from model
$model
=
$this
->
model
;
$meta
=
$model
::
getMeta
();
$query
=
clone
$this
;
$options
+=
$this
->
options
;
if
(
$options
[
'nosort'
])
$query
->
ordering
=
array
();
elseif
(
!
$query
->
ordering
&&
$m
odel
::
getM
eta
(
'ordering'
)
)
$query
->
ordering
=
$m
odel
::
getM
eta
(
'ordering'
)
;
if
(
false
!==
$query
->
related
&&
!
$query
->
values
&&
$m
odel
::
getM
eta
(
'select_related'
)
)
$query
->
related
=
$m
odel
::
getM
eta
(
'select_related'
)
;
if
(
!
$query
->
defer
&&
$m
odel
::
getM
eta
(
'defer'
)
)
$query
->
defer
=
$m
odel
::
getM
eta
(
'defer'
)
;
elseif
(
!
$query
->
ordering
&&
$meta
[
'ordering'
]
)
$query
->
ordering
=
$meta
[
'ordering'
]
;
if
(
false
!==
$query
->
related
&&
!
$query
->
values
&&
$meta
[
'select_related'
]
)
$query
->
related
=
$meta
[
'select_related'
]
;
if
(
!
$query
->
defer
&&
$meta
[
'defer'
]
)
$query
->
defer
=
$meta
[
'defer'
]
;
$class
=
$options
[
'compiler'
]
?:
$this
->
compiler
;
$compiler
=
new
$class
(
$options
);
...
...
@@ -1698,7 +1718,7 @@ class InstrumentedList extends ModelInstanceManager {
*/
function
window
(
$constraint
)
{
$model
=
$this
->
model
;
$fields
=
$model
::
getMeta
(
'fields'
);
$fields
=
$model
::
getMeta
(
)
->
getFieldNames
(
);
$key
=
$this
->
key
;
foreach
(
$constraint
as
$field
=>
$value
)
{
if
(
!
is_string
(
$field
)
||
false
===
in_array
(
$field
,
$fields
))
...
...
@@ -2370,10 +2390,11 @@ class MySqlCompiler extends SqlCompiler {
if
(
!
isset
(
$rmodel
))
$rmodel
=
$model
;
// Support inline views
$table
=
(
$rmodel
::
getMeta
(
'view'
))
$rmeta
=
$rmodel
::
getMeta
();
$table
=
(
$rmeta
[
'view'
])
// XXX: Support parameters from the nested query
?
$rmodel
::
getSqlAddParams
(
$this
)
:
$this
->
quote
(
$rm
odel
::
getM
eta
(
'table'
)
);
:
$this
->
quote
(
$rmeta
[
'table'
]
);
$base
=
"
{
$join
}{
$table
}
{
$alias
}
"
;
return
array
(
$base
,
$constraints
);
}
...
...
@@ -2507,14 +2528,15 @@ class MySqlCompiler extends SqlCompiler {
// Compile the field listing
$fields
=
$group_by
=
array
();
$table
=
$this
->
quote
(
$model
::
getMeta
(
'table'
))
.
' '
.
$rootAlias
;
$meta
=
$model
::
getMeta
();
$table
=
$this
->
quote
(
$meta
[
'table'
])
.
' '
.
$rootAlias
;
// Handle related tables
if
(
$queryset
->
related
)
{
$count
=
0
;
$fieldMap
=
$theseFields
=
array
();
$defer
=
$queryset
->
defer
?:
array
();
// Add local fields first
foreach
(
$m
odel
::
getMeta
(
'fields'
)
as
$f
)
{
foreach
(
$m
eta
->
getFieldNames
(
)
as
$f
)
{
// Handle deferreds
if
(
isset
(
$defer
[
$f
]))
continue
;
...
...
@@ -2536,7 +2558,7 @@ class MySqlCompiler extends SqlCompiler {
$theseFields
=
array
();
list
(
$alias
,
$fmodel
)
=
$this
->
getField
(
$full_path
,
$model
,
array
(
'table'
=>
true
,
'model'
=>
true
));
foreach
(
$fmodel
::
getMeta
(
'fields'
)
as
$f
)
{
foreach
(
$fmodel
::
getMeta
(
)
->
getFieldNames
(
)
as
$f
)
{
// Handle deferreds
if
(
isset
(
$defer
[
$sr
.
'__'
.
$f
]))
continue
;
...
...
@@ -2573,7 +2595,7 @@ class MySqlCompiler extends SqlCompiler {
// Simple selection from one table
elseif
(
!
$queryset
->
aggregated
)
{
if
(
$queryset
->
defer
)
{
foreach
(
$m
odel
::
getMeta
(
'fields'
)
as
$f
)
{
foreach
(
$m
eta
->
getFieldNames
(
)
as
$f
)
{
if
(
isset
(
$queryset
->
defer
[
$f
]))
continue
;
$fields
[
$rootAlias
.
'.'
.
$this
->
quote
(
$f
)]
=
true
;
...
...
@@ -2601,7 +2623,7 @@ class MySqlCompiler extends SqlCompiler {
}
// If no group by has been set yet, use the root model pk
if
(
!
$group_by
&&
!
$queryset
->
aggregated
&&
!
$queryset
->
distinct
)
{
foreach
(
$m
odel
::
getM
eta
(
'pk'
)
as
$pk
)
foreach
(
$meta
[
'pk'
]
as
$pk
)
$group_by
[]
=
$rootAlias
.
'.'
.
$pk
;
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment