/de.tudarmstadt.ukp.wikipedia.revisionmachine/src/main/java/de/tudarmstadt/ukp/wikipedia/revisionmachine/index/indices/AbstractIndex.java

http://jwpl.googlecode.com/ · Java · 144 lines · 54 code · 23 blank · 67 comment · 4 complexity · 2428ffdfa47f5599495aae06564ac10e MD5 · raw file

  1. /*******************************************************************************
  2. * Copyright (c) 2011 Ubiquitous Knowledge Processing Lab
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the GNU Lesser Public License v3
  6. * which accompanies this distribution, and is available at
  7. * http://www.gnu.org/licenses/lgpl.html
  8. *
  9. * Project Website:
  10. * http://jwpl.googlecode.com
  11. *
  12. * Contributors:
  13. * Torsten Zesch
  14. * Simon Kulessa
  15. * Oliver Ferschke
  16. ******************************************************************************/
  17. package de.tudarmstadt.ukp.wikipedia.revisionmachine.index.indices;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. /**
  21. * This class represents an abstact index.
  22. *
  23. *
  24. *
  25. */
  26. public abstract class AbstractIndex
  27. {
  28. /** Current query buffer */
  29. protected StringBuilder buffer;
  30. /** List of contained queries. */
  31. private List<StringBuilder> bufferList;
  32. /** Insert Statement to use */
  33. protected final String insertStatement;
  34. /** MAX_ALLOWED_PACKET */
  35. protected long MAX_ALLOWED_PACKET;
  36. /**
  37. * (Constructor) Creates an index object.
  38. *
  39. * @param insertStatement
  40. * Insert Statement
  41. * @param MAX_ALLOWED_PACKET
  42. * MAX_ALLOWED_PACKET
  43. */
  44. public AbstractIndex()
  45. {
  46. this.bufferList = new ArrayList<StringBuilder>();
  47. this.buffer = null;
  48. //does not really matter here- should be big to speed up data file creation
  49. this.MAX_ALLOWED_PACKET = 16760832;
  50. this.insertStatement = "";
  51. storeBuffer();
  52. }
  53. /**
  54. * (Constructor) Creates an index object.
  55. *
  56. * @param insertStatement
  57. * Insert Statement
  58. * @param MAX_ALLOWED_PACKET
  59. * MAX_ALLOWED_PACKET
  60. */
  61. public AbstractIndex(final String insertStatement,
  62. final long MAX_ALLOWED_PACKET)
  63. {
  64. this.bufferList = new ArrayList<StringBuilder>();
  65. this.buffer = null;
  66. this.MAX_ALLOWED_PACKET = MAX_ALLOWED_PACKET;
  67. this.insertStatement = insertStatement;
  68. storeBuffer();
  69. }
  70. /**
  71. * Returns the size of the currently used buffer.
  72. *
  73. * @return size of current query
  74. */
  75. public int byteSize()
  76. {
  77. return this.buffer.length();
  78. }
  79. /**
  80. * Finalizes the query in the currently used buffer and creates a new one.
  81. * The finalized query will be added to the list of queries.
  82. */
  83. public void finalizeIndex()
  84. {
  85. storeBuffer();
  86. }
  87. /**
  88. * Removes a query from the list of queries.
  89. *
  90. * @return Buffer containing a finalized query
  91. */
  92. public StringBuilder remove()
  93. {
  94. return this.bufferList.remove(0);
  95. }
  96. /**
  97. * Returns the current number of buffered queries.
  98. *
  99. * @return size of the list of queries
  100. */
  101. public int size()
  102. {
  103. return bufferList.size();
  104. }
  105. /**
  106. * Finalizes the query in the currently used buffer and creates a new one.
  107. * The finalized query will be added to the list of queries.
  108. */
  109. protected void storeBuffer()
  110. {
  111. if (buffer != null && buffer.length() > insertStatement.length()) {
  112. if(!insertStatement.isEmpty()) {
  113. //only do this in SQL/DATABASE MODE
  114. this.buffer.append(";");
  115. }
  116. bufferList.add(buffer);
  117. }
  118. this.buffer = new StringBuilder();
  119. this.buffer.append(insertStatement);
  120. }
  121. }