nstream.persist.kv.state.MapStateInner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nstream-persist-kv Show documentation
Show all versions of nstream-persist-kv Show documentation
Adapter for persistence implementations based on key-value stores
// Copyright 2015-2024 Nstream, inc.
//
// 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 nstream.persist.kv.state;
import java.util.ArrayList;
import swim.collections.BTree;
import swim.collections.HashTrieSet;
import swim.structure.Value;
/**
* Implementation details for {@link MapState}.
*/
class MapStateInner {
private final long epoch;
private final BTree contents;
private final HashTrieSet tombstones;
private MapStateInner(long epoch, BTree contents, HashTrieSet tombstones) {
this.epoch = epoch;
this.contents = contents;
this.tombstones = tombstones;
}
MapStateInner(BTree contents) {
this.epoch = 0;
this.contents = contents;
this.tombstones = HashTrieSet.empty();
}
public MapStateInner updated(Value key, Value value) {
final var newContents = this.contents.updated(key, value);
final var newTombstones = this.tombstones.removed(key);
if (newContents == this.contents && newTombstones == this.tombstones) {
return this;
} else {
return new MapStateInner(this.epoch, newContents, newTombstones);
}
}
public MapStateInner removed(Value key) {
if (this.tombstones.contains(key)) {
return this;
} else {
final var newContents = this.contents.removed(key);
final var newTombstones = this.tombstones.added(key);
return new MapStateInner(this.epoch, newContents, newTombstones);
}
}
public MapStateInner taken(int n, ArrayList removedKeys) {
final var size = this.contents.size();
if (n >= size) {
return this;
}
final var removed = this.contents.drop(n);
final var newContents = this.contents.take(n);
final var newTombstones = this.tombstones.added(removed.keySet());
removedKeys.addAll(removed.keySet());
return new MapStateInner(this.epoch, newContents, newTombstones);
}
public MapStateInner dropped(int n, ArrayList removedKeys) {
if (n == 0) {
return this;
}
final var removed = this.contents.take(n);
final var newContents = this.contents.drop(n);
final var newTombstones = this.tombstones.added(removed.keySet());
removedKeys.addAll(removed.keySet());
return new MapStateInner(this.epoch, newContents, newTombstones);
}
public MapStateInner cleared() {
return new MapStateInner(this.epoch + 1, BTree.empty(), HashTrieSet.empty());
}
public BTree getMap() {
return this.contents;
}
public long getEpoch() {
return this.epoch;
}
}