Comments (7)
I don't know how you plan to approach a fix for this, since other data types (BigDecimal, BigInteger, etc.) may also be affected by this issue. For me it was just Long values, so in case this is helpful to you as a fix or anyone else trying to work around this issue, below is a diff (for 3.4.4 NitriteJsonImporter.java) that I've tested and resolves the issue for me. (I've minimally validated that this change fixes the data in my project: by exporting the corrupt db and then importing it with the patch applied. And, my POC - see original post - runs without throwing/logging an exception after this patch is applied.)
Patch/fix (for Long values only):
# This patch file was generated by NetBeans IDE
# It uses platform neutral UTF-8 encoding and \n newlines.
--- a/nitrite/src/main/java/org/dizitart/no2/tool/NitriteJsonImporter.java
+++ b/nitrite/src/main/java/org/dizitart/no2/tool/NitriteJsonImporter.java
@@ -24,13 +24,17 @@
import org.dizitart.no2.objects.ObjectRepository;
import java.io.IOException;
+import java.lang.reflect.Field;
import java.util.Map;
+import lombok.extern.log4j.Log4j2;
+import org.apache.commons.lang3.reflect.FieldUtils;
import static org.dizitart.no2.Constants.*;
/**
* @author Anindya Chatterjee.
*/
+@Log4j2
class NitriteJsonImporter {
private JsonParser parser;
private Nitrite db;
@@ -82,7 +86,7 @@
}
if (TAG_DATA.equals(fieldName) && repository != null) {
- readCollectionData(repository.getDocumentCollection());
+ readCollectionData(repository.getDocumentCollection(), repository);
}
}
}
@@ -112,7 +116,7 @@
}
if (TAG_DATA.equals(fieldName)) {
- readCollectionData(collection);
+ readCollectionData(collection, null);
}
}
}
@@ -144,7 +148,7 @@
}
@SuppressWarnings("unchecked")
- private void readCollectionData(NitriteCollection collection) throws IOException {
+ private void readCollectionData(NitriteCollection collection, ObjectRepository repository) throws IOException {
// move to [
parser.nextToken();
@@ -165,7 +169,10 @@
parser.nextToken();
objectMap = (Map<String, Object>) parser.readValueAs(Map.class);
objectMap.put(DOC_ID, id);
+ if (repository != null) {
+ fixLongs(objectMap, repository.getType());
}
+ }
if (objectMap != null) {
Document document = new Document(objectMap);
@@ -177,4 +184,28 @@
}
}
}
+
+ private void fixLongs(Map<String, Object> objectMap, Class repositoryType) {
+ log.debug("In fixLongs with repositoryType: " + repositoryType.getName());
+ Field[] fields = FieldUtils.getAllFields(repositoryType);
+ for (Field field : fields) {
+ log.debug("Checking field: " + field.getName());
+ log.debug("Field type: " + field.getType());
+ if (field.getType().equals(Long.class)) {
+ String fieldName = field.getName();
+ log.debug("Found Long field: " + fieldName);
+ Object importedFieldVal = objectMap.get(fieldName);
+ log.debug("Field val: " + importedFieldVal);
+ if (importedFieldVal != null) {
+ log.debug("Field val type: " + importedFieldVal.getClass());
+ log.debug("Field val instanceof Integer: " + (importedFieldVal instanceof Integer));
+ if (importedFieldVal instanceof Integer) {
+ Long longVal = new Long((Integer)importedFieldVal);
+ objectMap.put(fieldName, longVal);
+ log.debug("Field type after update: " + objectMap.get(fieldName).getClass());
}
+ }
+ }
+ }
+ }
+}
from nitrite-java.
Hi, thanks for the investigation. This bug has already been addressed in the upcoming version Nitrite 4.x. This version is not released yet, but will soon. Unfortunately, I won't be doing any further development on 3.x. For the temporary measure you can go ahead with your fix. Alternatively, you can take a look at the new import/export code in branch 4.x and change your solution for the time being.
from nitrite-java.
Ok, I searched through the Issues and couldn't find one discussing this bug. Can you point me to the issue?
from nitrite-java.
I didn't mean that there was any github issues regarding this. What I meant is this bug was identified and addressed in 4.x branch. Here is the code - https://github.com/nitrite/nitrite-java/blob/4.x/nitrite-support/src/main/java/org/dizitart/no2/support/NitriteJsonExporter.java
from nitrite-java.
Ok, so it was an undocumented bug. That sucks, because had I known there was an bug with the 3.4.4 export/import, I wouldn't have used it -- and I wouldn't have wasted a bunch of time investigating the issue and creating repro code for you.
Frankly, I'm a little confused about the current state of this project. You've said that 4.x is not "released" yet, but 3.4.4 apparently has some pretty significant undocumented bugs that cause data corruption... What branch do you recommend as the current "production" version of this project?
from nitrite-java.
You can keep using 3.4.4 except the import/export functionality. I do apologize for the confusion. In 4.x the library was rebuilt from the ground up to implement some new features which can't be built in 3.x at scale. There are breaking changes in the api and the documentation is not fully ready to be released yet.
For the import/export functionality, as I have said earlier, you can roll your own lib by looking at how it is done in 4.x. Just don't use the in-built import/export feature for the time being.
from nitrite-java.
Ok, might be a good idea to either remove the 3.x Exporter and Importer. Or put a big warning comment in the code. Or mark those classes as deprecated.
from nitrite-java.
Related Issues (20)
- nitriteMapper is null? HOT 1
- Add 4.x.x+ to maven repository
- NitriteMapper does not give enough info about the type HOT 4
- IndexingException: indexing on arrays or collections for field formula are not supported HOT 1
- Migrating from 3.x to 4.x: Database file is corrupted HOT 7
- possible memory leak HOT 1
- org.h2.mvstore.Chunk.accountForRemovedPage NPE HOT 2
- MVStore claims to have unsaved changes after calling nitriteDatabase.getCollection(...) HOT 5
- NitriteCollection.update slow when unrelated properties in document are indexed HOT 6
- EntityConverter toDocument HOT 3
- KotlinXSerializationMapper decodes lists to be size 1 always HOT 1
- Invalid repository type for entity where all fields are nullable (with JacksonMapperModule) HOT 3
- Possibiltity to index a field with both text and non-unique/unique filter HOT 3
- Incorrect result when searching indexed column HOT 1
- Support JPMS HOT 6
- Feature request: method for retrieving all document ids in a collection faster HOT 1
- Search for _id always returns an iterator with an element HOT 2
- org.dizitart.no2.common.tuples.Pair cannot be cast to class java.lang.Comparable HOT 4
- @Indices is not neccessary to use, multiple @Index annotations on class are allowed HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nitrite-java.