/hazelcast/src/main/java/com/hazelcast/impl/concurrentmap/InitialState.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 75 lines · 51 code · 9 blank · 15 comment · 8 complexity · d098d6281ec8ba204a1662add9487ccf MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.impl.concurrentmap;
  17. import com.hazelcast.cluster.AbstractRemotelyProcessable;
  18. import com.hazelcast.impl.CMap;
  19. import com.hazelcast.impl.FactoryImpl;
  20. import com.hazelcast.query.Index;
  21. import java.io.DataInput;
  22. import java.io.DataOutput;
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. public class InitialState extends AbstractRemotelyProcessable {
  27. List<MapState> lsMapStates = new ArrayList<MapState>();
  28. public InitialState() {
  29. }
  30. public void createAndAddMapState(CMap cmap) {
  31. MapState mapState = new MapState(cmap.getName());
  32. Index[] indexes = cmap.getMapIndexService().getIndexesInOrder();
  33. if (indexes != null) {
  34. for (Index index : indexes) {
  35. AddMapIndex mi = new AddMapIndex(cmap.getName(), index.getExpression(), index.isOrdered(), index.getAttributeIndex());
  36. mapState.addMapIndex(mi);
  37. }
  38. }
  39. lsMapStates.add(mapState);
  40. }
  41. public void process() {
  42. FactoryImpl factory = getNode().factory;
  43. if (factory.node.isActive()) {
  44. for (MapState mapState : lsMapStates) {
  45. CMap cmap = factory.node.concurrentMapManager.getOrCreateMap(mapState.name);
  46. for (AddMapIndex mapIndex : mapState.lsMapIndexes) {
  47. cmap.addIndex(mapIndex.getExpression(), mapIndex.isOrdered(), mapIndex.getAttributeIndex());
  48. }
  49. }
  50. }
  51. }
  52. public void writeData(DataOutput out) throws IOException {
  53. out.writeInt(lsMapStates.size());
  54. for (MapState mapState : lsMapStates) {
  55. mapState.writeData(out);
  56. }
  57. }
  58. public void readData(DataInput in) throws IOException {
  59. int size = in.readInt();
  60. for (int i = 0; i < size; i++) {
  61. MapState mapState = new MapState();
  62. mapState.readData(in);
  63. lsMapStates.add(mapState);
  64. }
  65. }
  66. }