forked from MagicStack/immutables
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Delete / Insert / Select / Update operations from GeodeBacken…
…d into separate (local) classes
- Loading branch information
1 parent
8ea4236
commit c3e3780
Showing
5 changed files
with
280 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
criteria/geode/src/org/immutables/criteria/geode/SyncDelete.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2019 Immutables Authors and Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.immutables.criteria.geode; | ||
|
||
import org.apache.geode.cache.Region; | ||
import org.immutables.criteria.backend.StandardOperations; | ||
import org.immutables.criteria.backend.WriteResult; | ||
import org.immutables.criteria.expression.Expression; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.Callable; | ||
|
||
class SyncDelete implements Callable<WriteResult> { | ||
|
||
private final GeodeBackend.Session session; | ||
private final StandardOperations.Delete operation; | ||
private final Region<Object, Object> region; | ||
|
||
SyncDelete(GeodeBackend.Session session, StandardOperations.Delete operation) { | ||
this.session = session; | ||
this.operation = operation; | ||
this.region = session.region; | ||
} | ||
|
||
@Override | ||
public WriteResult call() throws Exception { | ||
if (!operation.query().filter().isPresent()) { | ||
// no filter means delete all (ie clear whole region) | ||
region.clear(); | ||
return WriteResult.unknown(); | ||
} | ||
|
||
Expression filter = operation.query().filter().orElseThrow(() -> new IllegalStateException("For " + operation)); | ||
Optional<List<?>> ids = Geodes.canDeleteByKey(filter, session.idResolver); | ||
|
||
// list of ids is present in the expression | ||
if (ids.isPresent()) { | ||
// delete by key: map.remove(key) | ||
region.removeAll(ids.get()); | ||
return WriteResult.unknown(); | ||
} | ||
|
||
GeodeQueryVisitor visitor = new GeodeQueryVisitor(true, path -> String.format("e.value.%s", path.toStringPath())); | ||
OqlWithVariables oql = filter.accept(visitor); | ||
|
||
String query = String.format("select distinct e.key from %s.entries e where %s", region.getFullPath(), oql.oql()); | ||
Collection<?> keys = (Collection<?>) session.queryService.newQuery(query).execute(oql.variables().toArray(new Object[0])); | ||
region.removeAll(keys); | ||
return GeodeWriteResult.of().withDeletedCount(keys.size()); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
criteria/geode/src/org/immutables/criteria/geode/SyncInsert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2019 Immutables Authors and Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.immutables.criteria.geode; | ||
|
||
import org.apache.geode.cache.Region; | ||
import org.immutables.criteria.backend.BackendException; | ||
import org.immutables.criteria.backend.StandardOperations; | ||
import org.immutables.criteria.backend.WriteResult; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.Callable; | ||
import java.util.stream.Collectors; | ||
|
||
class SyncInsert implements Callable<WriteResult> { | ||
|
||
private final GeodeBackend.Session session; | ||
private final StandardOperations.Insert operation; | ||
private final Region<Object, Object> region; | ||
|
||
SyncInsert(GeodeBackend.Session session, StandardOperations.Insert operation) { | ||
this.session = session; | ||
this.operation = operation; | ||
this.region = session.region; | ||
} | ||
|
||
@Override | ||
public WriteResult call() throws Exception { | ||
if (operation.values().isEmpty()) { | ||
return WriteResult.empty(); | ||
} | ||
|
||
final Map<Object, Object> toInsert = operation.values().stream().collect(Collectors.toMap(session.idExtractor::extract, x -> x)); | ||
final Region<Object, Object> region = this.region; | ||
for (Map.Entry<Object, Object> entry: toInsert.entrySet()) { | ||
Object previous = region.putIfAbsent(entry.getKey(), entry.getValue()); | ||
if (previous != null) { | ||
throw new BackendException(String.format("Duplicate id %s for %s", entry.getKey(), session.entityType())); | ||
} | ||
} | ||
|
||
return GeodeWriteResult.of().withInsertedCount(toInsert.size()); | ||
} | ||
} |
Oops, something went wrong.