Code Monkey home page Code Monkey logo

Comments (10)

zzsoszz avatar zzsoszz commented on August 26, 2024

it will show errors below after run a few times.

5573
5574
5575
5576
5577
19-01-17 10:01:37.170 ERROR [main] io.jsondb.io.JsonWriter updateInJsonFile 575 : Failed to move temporary collection file instances6294430801854422529.tmp to collection file instances.json
java.nio.file.AccessDeniedException: C:\Users\EDZ\jsondb\instances6294430801854422529.tmp -> C:\Users\EDZ\jsondb\instances.json
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:301)
at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:287)
at java.nio.file.Files.move(Files.java:1395)
at io.jsondb.io.JsonWriter.updateInJsonFile(JsonWriter.java:573)
at io.jsondb.JsonDBTemplate.findAndModify(JsonDBTemplate.java:1333)
at io.swagger.utils.GuidUtils.getUniqueId(GuidUtils.java:44)
at io.swagger.utils.GuidUtils.main(GuidUtils.java:50)
5578
5579
5580
5581

from jsondb-core.

FarooqKhan avatar FarooqKhan commented on August 26, 2024

@zzsoszz I see what you are trying to do, I will try to check this out later today when i get some time.

from jsondb-core.

FarooqKhan avatar FarooqKhan commented on August 26, 2024

I created the following Unittest copying your code with minor modifications, seems to work fine, maybe something to do with Windows

package io.jsondb.tests;

import static org.junit.Assert.assertEquals;

import java.io.File;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import io.jsondb.JsonDBTemplate;
import io.jsondb.Util;
import io.jsondb.query.Update;
import io.jsondb.tests.model.Instance;

/**
 * @version 1.0 17-Jan-2019
 */
public class FindAndModify2Tests {
  
  private static String dbFilesLocation = "src/test/resources/dbfiles/findAndModify2Tests";
  private File dbFilesFolder = new File(dbFilesLocation);

  /**
   * @throws java.lang.Exception
   */
  @Before
  public void setUp() throws Exception {
    dbFilesFolder.mkdir();
  }

  @After
  public void tearDown() throws Exception {
    Util.delete(dbFilesFolder);
  }
  
  private static final Object lock = new Object();
  private static volatile JsonDBTemplate instance;
  private static String initValue="100000000";
  
  public static synchronized  JsonDBTemplate getJsonDBTemplate(){
    JsonDBTemplate r = instance;
    if (r == null) {
      synchronized (lock) {    // While we were waiting for the lock, another
        r = instance;        // thread may have instantiated the object.
        if (r == null) {
          r= new JsonDBTemplate(dbFilesLocation, "io.jsondb.tests.model");
          instance = r;
        }
      }
    }
    return r;
  }


  public static String getUniqueId(){
    JsonDBTemplate jsonDBTemplate=FindAndModify2Tests.getJsonDBTemplate();
    if(!jsonDBTemplate.collectionExists(Instance.class)){
        jsonDBTemplate.createCollection(Instance.class);
        Instance instance = new Instance();
        instance.setId("guid");
        instance.setHostname(initValue);
        jsonDBTemplate.insert(instance);
    }
    Instance instanceOld = jsonDBTemplate.findById("guid", Instance.class);
    Update update = Update.update("hostname", Long.parseLong(instanceOld.getHostname())+1);
    String jxQuery = String.format("/.[id='%s']", "guid");
    Instance instanceNew = jsonDBTemplate.findAndModify(jxQuery, update, "instances");
    return instanceNew.getHostname();
  }
  
  @Test
  public void test() {
    for(int i=1;i<=1000;i++){
      String id = FindAndModify2Tests.getUniqueId();
      assertEquals(Long.parseLong(initValue)+i, Long.parseLong(id));
      System.out.println("Id is " + id);
    }
  }
}

from jsondb-core.

FarooqKhan avatar FarooqKhan commented on August 26, 2024

I think this might be a windows quirk. I see a few people facing similar problems on windows. See: Stackoverflow Link
I dont have windows system to test this theory though.

Can you try this, check if the folder permissions has some SID information as mentioned in the link above,
Or maybe the folder is marked readonly
OR maybe try closing explorer and then run the code.

from jsondb-core.

FarooqKhan avatar FarooqKhan commented on August 26, 2024

You mentioned it gives this error after running a few times.

For every update operation i first create a temporary file and then replace the existing file with the temporary file as a Atomic Move. This way the update operation will be a atomic operation. Maybe a large number of file move operations are causing a problem on windows.

from jsondb-core.

zzsoszz avatar zzsoszz commented on August 26, 2024

i have tried delete directory of "jsondb" and closing explorer before running and recreated directory is not "ready only",but still get this error after running a few times. i found when you moving file to destination,but the destination still exists,does it case this probolem?

from jsondb-core.

FarooqKhan avatar FarooqKhan commented on August 26, 2024

If that was a problem it would have failed each time.

from jsondb-core.

FarooqKhan avatar FarooqKhan commented on August 26, 2024

I am going to try and get a Windows machine but that is going to take a week or two.

from jsondb-core.

FarooqKhan avatar FarooqKhan commented on August 26, 2024

Could you list here the Window OS your are using?

from jsondb-core.

zzsoszz avatar zzsoszz commented on August 26, 2024

win 10 professional

from jsondb-core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.