Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ARSnova Backend
Manage
Activity
Members
Labels
Plan
Issues
27
Issue boards
Milestones
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Privacy
Imprint
Contact
Snippets
Groups
Projects
Show more breadcrumbs
ARSnova
ARSnova Backend
Commits
67566602
Commit
67566602
authored
7 years ago
by
Daniel Gerhardt
Browse files
Options
Downloads
Patches
Plain Diff
Improve CouchDB view initialization
Views are now checked for existence instead of creating them on every startup.
parent
2cc374bb
1 merge request
!89
Foundation for development of version 3.0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/de/thm/arsnova/persistance/couchdb/CouchDbInitializer.java
+31
-13
31 additions, 13 deletions
...e/thm/arsnova/persistance/couchdb/CouchDbInitializer.java
with
31 additions
and
13 deletions
src/main/java/de/thm/arsnova/persistance/couchdb/CouchDbInitializer.java
+
31
−
13
View file @
67566602
...
...
@@ -29,7 +29,10 @@ import javax.script.ScriptException;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
@Component
public
class
CouchDbInitializer
implements
ResourceLoaderAware
{
...
...
@@ -41,6 +44,7 @@ public class CouchDbInitializer implements ResourceLoaderAware {
private
CouchDbConnector
connector
;
private
ObjectMapper
objectMapper
;
private
StatusService
statusService
;
private
boolean
migrationStarted
=
false
;
public
CouchDbInitializer
(
final
CouchDbConnector
couchDbConnector
,
final
ObjectMapperFactory
objectMapperFactory
,
final
StatusService
statusService
)
{
...
...
@@ -66,22 +70,30 @@ public class CouchDbInitializer implements ResourceLoaderAware {
}
protected
void
createDesignDocs
()
{
docs
.
forEach
(
doc
->
{
if
(
logger
.
isDebugEnabled
())
{
try
{
logger
.
debug
(
"Creating design doc:\n{}"
,
objectMapper
.
writeValueAsString
(
doc
));
}
catch
(
JsonProcessingException
e
)
{
logger
.
warn
(
"Failed to serialize design doc."
,
e
);
}
}
connector
.
executeBulk
(
docs
.
stream
().
filter
(
doc
->
{
try
{
final
String
rev
=
connector
.
getCurrentRevision
((
String
)
doc
.
get
(
"_id"
));
doc
.
put
(
"_rev"
,
rev
);
connector
.
update
(
doc
);
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
"Checking design doc {}:\n{}"
,
doc
.
get
(
"_id"
),
objectMapper
.
writeValueAsString
(
doc
));
}
final
Map
<
String
,
Object
>
existingDoc
=
connector
.
get
(
HashMap
.
class
,
doc
.
get
(
"_id"
).
toString
());
final
String
existingViews
=
objectMapper
.
writeValueAsString
(
existingDoc
.
get
(
"views"
));
final
String
currentViews
=
objectMapper
.
writeValueAsString
(
doc
.
get
(
"views"
));
if
(
existingViews
.
equals
(
currentViews
))
{
logger
.
debug
(
"Design doc {} already exists."
,
doc
.
get
(
"_id"
));
return
false
;
}
else
{
logger
.
debug
(
"Design doc {} will be updated."
,
doc
.
get
(
"_id"
));
doc
.
put
(
"_rev"
,
existingDoc
.
get
(
"_rev"
));
return
true
;
}
}
catch
(
final
DocumentNotFoundException
e
)
{
connector
.
create
(
doc
);
logger
.
debug
(
"Design doc {} will be created."
,
doc
.
get
(
"_id"
));
return
true
;
}
catch
(
JsonProcessingException
e
)
{
logger
.
warn
(
"Failed to serialize design doc {}."
,
doc
.
get
(
"_id"
),
e
);
return
false
;
}
});
})
.
collect
(
Collectors
.
toList
()))
;
}
protected
void
migrate
()
{
...
...
@@ -104,6 +116,12 @@ public class CouchDbInitializer implements ResourceLoaderAware {
@EventListener
private
void
onApplicationEvent
(
ContextRefreshedEvent
event
)
throws
IOException
,
ScriptException
{
/* Event is triggered more than once */
if
(
migrationStarted
)
{
return
;
}
migrationStarted
=
true
;
statusService
.
putMaintenanceReason
(
this
.
getClass
(),
"Data migration active"
);
loadDesignDocFiles
();
createDesignDocs
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
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